4
%% Copyright Ericsson AB 1999-2011. All Rights Reserved.
6
%% The contents of this file are subject to the Erlang Public License,
7
%% Version 1.1, (the "License"); you may not use this file except in
8
%% compliance with the License. You should have received a copy of the
9
%% Erlang Public License along with this software. If not, it can be
10
%% retrieved online at http://www.erlang.org/.
12
%% Software distributed under the License is distributed on an "AS IS"
13
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
14
%% the License for the specific language governing rights and limitations
21
-module(exception_SUITE).
23
-export([all/0, suite/0,groups/0,init_per_group/2,end_per_group/2,
24
init_per_testcase/2,end_per_testcase/2,
25
init_per_suite/1,end_per_suite/1,
26
badmatch/1,pending_errors/1,nil_arith/1]).
30
-include_lib("test_server/include/test_server.hrl").
32
suite() -> [{ct_hooks,[ts_install_cth]}].
40
init_per_group(_GroupName, Config) ->
43
end_per_group(_GroupName, Config) ->
48
[badmatch, pending_errors, nil_arith].
52
{'EXIT', {{badmatch, nomatch}, _}} = (catch E = nomatch)).
54
init_per_testcase(_Case, Config) ->
55
test_lib:interpret(?MODULE),
56
Dog = test_server:timetrap(?t:minutes(1)),
57
[{watchdog,Dog}|Config].
59
end_per_testcase(_Case, Config) ->
60
Dog = ?config(watchdog, Config),
61
?t:timetrap_cancel(Dog),
64
init_per_suite(Config) when is_list(Config) ->
65
?line test_lib:interpret(?MODULE),
66
?line true = lists:member(?MODULE, int:interpreted()),
69
end_per_suite(Config) when is_list(Config) ->
72
badmatch(doc) -> "Test that deliberately bad matches are reported correctly.";
73
badmatch(suite) -> [];
74
badmatch(Config) when list(Config) ->
77
?line ?try_match({a, b, c}),
79
?line ?try_match(1.0),
82
pending_errors(doc) ->
83
["Test various exceptions, in the presence of a previous error suppressed ",
85
pending_errors(suite) -> [];
86
pending_errors(Config) when list(Config) ->
87
?line pending(e_badmatch, {badmatch, b}),
88
?line pending(x, function_clause),
89
?line pending(e_case, {case_clause, xxx}),
90
?line pending(e_if, if_clause),
91
?line pending(e_badarith, badarith),
92
?line pending(e_undef, undef),
93
?line pending(e_timeoutval, timeout_value),
94
?line pending(e_badarg, badarg),
95
?line pending(e_badarg_spawn, badarg),
98
bad_guy(pe_badarith, Other) when Other+1 == 0 -> % badarith (suppressed)
100
bad_guy(pe_badarg, Other) when length(Other) > 0 -> % badarg (suppressed)
102
bad_guy(_, e_case) ->
110
bad_guy(_, e_badarith) ->
112
bad_guy(_, e_undef) ->
113
non_existing_module:foo(); % undef
114
bad_guy(_, e_timeoutval) ->
116
after arne -> % timeout_value
119
bad_guy(_, e_badarg) ->
121
bad_guy(_, e_badarg_spawn) ->
122
spawn({}, {}, {}); % badarg
123
bad_guy(_, e_badmatch) ->
126
pending(Arg, Expected) ->
127
pending(pe_badarith, Arg, Expected),
128
pending(pe_badarg, Arg, Expected).
130
pending(First, Second, Expected) ->
131
pending_catched(First, Second, Expected),
132
pending_exit_message([First, Second], Expected).
134
pending_catched(First, Second, Expected) ->
135
ok = io:format("Catching bad_guy(~p, ~p)", [First, Second]),
136
case catch bad_guy(First, Second) of
138
pending(Reason, bad_guy, [First, Second], Expected);
140
test_server:fail({not_exit, Other})
143
pending_exit_message(Args, Expected) ->
144
ok = io:format("Trapping EXITs from spawn_link(~p, ~p, ~p)",
145
[?MODULE, bad_guy, Args]),
146
process_flag(trap_exit, true),
147
Pid = spawn_link(?MODULE, bad_guy, Args),
149
{'EXIT', Pid, Reason} ->
150
pending(Reason, bad_guy, Args, Expected);
152
test_server:fail({unexpected_message, Other})
154
test_server:fail(timeout)
156
process_flag(trap_exit, false).
158
pending({badarg,[{erlang,Bif,BifArgs},{?MODULE,Func,Arity}|_]}, Func, Args, _Code)
159
when atom(Bif), list(BifArgs), length(Args) == Arity -> %Threaded code.
161
pending({badarg,[{erlang,Bif,BifArgs},{?MODULE,Func,Args}|_]}, Func, Args, _Code)
162
when atom(Bif), list(BifArgs) -> %From interpreted code.
164
pending({undef,[{non_existing_module,foo,[]}|_]}, _, _, _) ->
166
pending({function_clause,[{?MODULE,Func,Args}|_]}, Func, Args, _Code) ->
168
pending({Code,[{?MODULE,Func,Arity}|_]}, Func, Args, Code) when length(Args) == Arity -> %Threaded code
170
pending({Code,[{?MODULE,Func,Args}|_]}, Func, Args, Code) -> %From interpreted code.
172
pending(Reason, Func, Args, Code) ->
173
test_server:fail({bad_exit_reason,Reason,{Func,Args,Code}}).
176
"Test that doing arithmetics on [] gives a badarith EXIT and not a crash.";
179
nil_arith(Config) when list(Config) ->
180
?line ba_plus_minus_times([], []),
182
?line ba_plus_minus_times([], 0),
183
?line ba_plus_minus_times([], 42),
184
?line ba_plus_minus_times([], 38724978123478923784),
185
?line ba_plus_minus_times([], 38.72),
187
?line ba_plus_minus_times(0, []),
188
?line ba_plus_minus_times(334, []),
189
?line ba_plus_minus_times(387249797813478923784, []),
190
?line ba_plus_minus_times(344.22, []),
192
?line ba_div_rem([], []),
194
?line ba_div_rem([], 0),
195
?line ba_div_rem([], 1),
196
?line ba_div_rem([], 42),
197
?line ba_div_rem([], 38724978123478923784),
198
?line ba_div_rem(344.22, []),
200
?line ba_div_rem(0, []),
201
?line ba_div_rem(1, []),
202
?line ba_div_rem(334, []),
203
?line ba_div_rem(387249797813478923784, []),
204
?line ba_div_rem(344.22, []),
206
?line ba_div_rem(344.22, 0.0),
207
?line ba_div_rem(1, 0.0),
208
?line ba_div_rem(392873498733971, 0.0),
210
?line ba_bop([], []),
212
?line ba_bop(42, []),
213
?line ba_bop(-42342742987343, []),
214
?line ba_bop(238.342, []),
216
?line ba_bop([], -243),
217
?line ba_bop([], 243),
218
?line ba_bop([], 2438724982478933),
219
?line ba_bop([], 3987.37),
222
?line ba_bnot(23.33),
224
?line ba_shift([], []),
225
?line ba_shift([], 0),
226
?line ba_shift([], 4),
227
?line ba_shift([], -4),
228
?line ba_shift([], 2343333333333),
229
?line ba_shift([], -333333333),
230
?line ba_shift([], 234.00),
231
?line ba_shift(23, []),
232
?line ba_shift(0, []),
233
?line ba_shift(-3433443433433323, []),
234
?line ba_shift(433443433433323, []),
235
?line ba_shift(343.93, []),
238
ba_plus_minus_times(A, B) ->
239
io:format("~p + ~p", [A, B]),
240
{'EXIT', {badarith, _}} = (catch A + B),
241
io:format("~p - ~p", [A, B]),
242
{'EXIT', {badarith, _}} = (catch A - B),
243
io:format("~p * ~p", [A, B]),
244
{'EXIT', {badarith, _}} = (catch A * B).
247
io:format("~p / ~p", [A, B]),
248
{'EXIT', {badarith, _}} = (catch A / B),
249
io:format("~p div ~p", [A, B]),
250
{'EXIT', {badarith, _}} = (catch A div B),
251
io:format("~p rem ~p", [A, B]),
252
{'EXIT', {badarith, _}} = (catch A rem B).
255
io:format("~p band ~p", [A, B]),
256
{'EXIT', {badarith, _}} = (catch A band B),
257
io:format("~p bor ~p", [A, B]),
258
{'EXIT', {badarith, _}} = (catch A bor B),
259
io:format("~p bxor ~p", [A, B]),
260
{'EXIT', {badarith, _}} = (catch A bxor B).
263
io:format("~p bsl ~p", [A, B]),
264
{'EXIT', {badarith, _}} = (catch A bsl B),
265
io:format("~p bsr ~p", [A, B]),
266
{'EXIT', {badarith, _}} = (catch A bsr B).
269
io:format("bnot ~p", [A]),
270
{'EXIT', {badarith, _}} = (catch bnot A).