aboutsummaryrefslogtreecommitdiffstats
path: root/lib/compiler/test/float_SUITE.erl
blob: ec64ef6901b7738eac5eb24235d69caaf6c28720 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2002-2011. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%     http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%%
-module(float_SUITE).
-export([all/0, suite/0,groups/0,init_per_suite/1, end_per_suite/1, 
	 init_per_group/2,end_per_group/2,
	 pending/1,bif_calls/1,math_functions/1,mixed_float_and_int/1]).

-include_lib("common_test/include/ct.hrl").

suite() -> [{ct_hooks,[ts_install_cth]}].

all() -> 
    test_lib:recompile(?MODULE),
    [pending, bif_calls, math_functions,
     mixed_float_and_int].

groups() -> 
    [].

init_per_suite(Config) ->
    Config.

end_per_suite(_Config) ->
    ok.

init_per_group(_GroupName, Config) ->
    Config.

end_per_group(_GroupName, Config) ->
    Config.


%% Thanks to Tobias Lindahl <[email protected]>
%% Shows the effect of pending exceptions on the x86.

pending(Config) when is_list(Config) ->
    ?line case catch float_mul(1, 1.1e300, 3.14e300) of
	      {'EXIT',{badarith,_}} -> ok;
	      Other -> ct:fail({expected_exception,Other})
	  end,
    ?line 0.0 = float_sub(2.0).

float_sub(A)->
    catch A - 2.0.

float_mul(0, _, _)->
    ok;
float_mul(Iter, A, B) when is_float(A), is_float(B) ->
    _ = A*B,
    float_mul(Iter-1, A, B).

%% Thanks to Mikael Pettersson and Tobias Lindahl (HiPE).

bif_calls(Config) when is_list(Config) ->
    ?line {'EXIT',{badarith,_}} = (catch bad_arith(2.0, 1.7)),
    ?line {'EXIT',{badarith,_}} = (catch bad_arith_again(2.0, [])),
    ?line {'EXIT',{badarith,_}} = (catch bad_arith_xor(2.0, [])),
    ?line {'EXIT',{badarith,_}} = (catch bad_arith_hd(2.0, [])),
    ?line {'EXIT',{badarith,_}} = (catch bad_negate(2.0, 1.7)),
    ok.

bad_arith(X, Y) when is_float(X) ->
    X1 = X * 1.7e+308,
    X2 = X1 + 1.0,
    Y1 = Y * 2,					%Calls erts_mixed_times/2.
						%(A BIF call.)
    {X2, Y1}.

bad_arith_xor(X, Y) when is_float(X) ->
    X1 = X * 1.7e+308,
    Y1 = Y xor true,				%A failing BIF call.
    {X1 + 1.0, Y1}.

bad_arith_hd(X, Y) when is_float(X) ->
    X1 = X * 1.7e+308,
    Y1 = hd(Y),					%A failing BIF call.
    {X1 + 1.0, Y1}.

bad_arith_again(X, Y) when is_float(X) ->
    X1 = X * 1.7e+308,
    Y1 = element(1, Y),				%A failing BIF call.
    {X1 + 1.0, Y1}.

bad_negate(X, Y) when is_float(X) ->
    X1 = X * 1.7e+308,
    X2 = X1 + 1.0,
    Y1 = -Y,					%BIF call.
    {X2, Y1}.

%% Some math functions are not implemented on all platforms.
-define(OPTIONAL(Expected, Expr),
	try
	    Expected = Expr
	catch
	    error:undef -> ok
	end).

math_functions(Config) when is_list(Config) ->
    %% Mostly silly coverage.
    ?line 0.0 = math:tan(0),
    ?line 0.0 = math:atan2(0, 1),
    ?line 0.0 = math:sinh(0),
    ?line 1.0 = math:cosh(0),
    ?line 0.0 = math:tanh(0),
    1.0 = math:log2(2),
    ?line 1.0 = math:log10(10),
    ?line -1.0 = math:cos(math:pi()),
    ?line 1.0 = math:exp(0),
    ?line 1.0 = math:pow(math:pi(), 0),
    ?line 0.0 = math:log(1),
    ?line 0.0 = math:asin(0),
    ?line 0.0 = math:acos(1),
    ?line ?OPTIONAL(0.0, math:asinh(0)),
    ?line ?OPTIONAL(0.0, math:acosh(1)),
    ?line ?OPTIONAL(0.0, math:atanh(0)),
    ?line ?OPTIONAL(0.0, math:erf(0)),
    ?line ?OPTIONAL(1.0, math:erfc(0)),

    ?line 0.0 = math:tan(id(0)),
    ?line 0.0 = math:atan2(id(0), 1),
    ?line 0.0 = math:sinh(id(0)),
    ?line 1.0 = math:cosh(id(0)),
    ?line 0.0 = math:tanh(id(0)),
    1.0 = math:log2(id(2)),
    ?line 1.0 = math:log10(id(10)),
    ?line 1.0 = math:exp(id(0)),
    ?line 0.0 = math:log(id(1)),
    ?line 0.0 = math:asin(id(0)),
    ?line 0.0 = math:acos(id(1)),
    ?line ?OPTIONAL(0.0, math:asinh(id(0))),
    ?line ?OPTIONAL(0.0, math:acosh(id(1))),
    ?line ?OPTIONAL(0.0, math:atanh(id(0))),
    ?line ?OPTIONAL(0.0, math:erf(id(0))),
    ?line ?OPTIONAL(1.0, math:erfc(id(0))),

    %% Only for coverage (of beam_type.erl).
    ?line {'EXIT',{undef,_}} = (catch math:fnurfla(0)),
    ?line {'EXIT',{undef,_}} = (catch math:fnurfla(0, 0)),
    ?line {'EXIT',{badarg,_}} = (catch float(kalle)),
    ?line {'EXIT',{badarith,_}} = (catch name/1),
    ok.

mixed_float_and_int(Config) when is_list(Config) ->
    ?line 129.0 = pc(77, 23, 5),
    ok.

pc(Cov, NotCov, X) ->
    round(Cov/(Cov+NotCov)*100) + 42 + 2.0*X.

id(I) -> I.