4
%% Copyright Ericsson AB 1997-2009. All Rights Reserved.
4
%% Copyright Ericsson AB 1997-2011. All Rights Reserved.
6
6
%% The contents of this file are subject to the Erlang Public License,
7
7
%% Version 1.1, (the "License"); you may not use this file except in
8
8
%% compliance with the License. You should have received a copy of the
9
9
%% Erlang Public License along with this software. If not, it can be
10
10
%% retrieved online at http://www.erlang.org/.
12
12
%% Software distributed under the License is distributed on an "AS IS"
13
13
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
14
14
%% the License for the specific language governing rights and limitations
15
15
%% under the License.
20
20
-module(guard_SUITE).
22
-export([all/1, bad_arith/1, bad_tuple/1, test_heap_guards/1, guard_bifs/1,
22
-export([all/0, suite/0,groups/0,init_per_suite/1, end_per_suite/1,
23
init_per_group/2,end_per_group/2, bad_arith/1, bad_tuple/1,
24
test_heap_guards/1, guard_bifs/1,
25
type_tests/1,guard_bif_binary_part/1]).
25
-include("test_server.hrl").
27
-include_lib("test_server/include/test_server.hrl").
28
30
-import(lists, [member/2]).
30
all(suite) -> [bad_arith, bad_tuple, test_heap_guards, guard_bifs, type_tests].
32
suite() -> [{ct_hooks,[ts_install_cth]}].
35
[bad_arith, bad_tuple, test_heap_guards, guard_bifs,
36
type_tests, guard_bif_binary_part].
41
init_per_suite(Config) ->
44
end_per_suite(_Config) ->
47
init_per_group(_GroupName, Config) ->
50
end_per_group(_GroupName, Config) ->
32
54
bad_arith(doc) -> "Test that a bad arithmetic operation in a guard works correctly.";
33
55
bad_arith(Config) when is_list(Config) ->
161
-define(MASK_ERROR(EXPR),mask_error((catch (EXPR)))).
162
mask_error({'EXIT',{Err,_}}) ->
167
guard_bif_binary_part(doc) ->
168
["Test the binary_part/2,3 guard BIF's extensively"];
169
guard_bif_binary_part(Config) when is_list(Config) ->
170
%% Overflow tests that need to be unoptimized
173
binary_part(<<1,2,3>>,{16#FFFFFFFFFFFFFFFF,
174
-16#7FFFFFFFFFFFFFFF-1})),
177
binary_part(<<1,2,3>>,{16#FFFFFFFFFFFFFFFF,
178
16#7FFFFFFFFFFFFFFF})),
181
{Pid,Ref} = spawn_monitor( fun() ->
182
A = lists:duplicate(X,a),
183
B = [do_binary_part_guard() | A],
184
Master ! {self(),hd(B)},
189
erlang:demonitor(Ref,[flush]),
195
[ ok = F(N) || N <- lists:seq(1,10000) ],
199
do_binary_part_guard() ->
200
?line 1 = bptest(<<1,2,3>>),
201
?line 2 = bptest(<<2,1,3>>),
202
?line error = bptest(<<1>>),
203
?line error = bptest(<<>>),
204
?line error = bptest(apa),
205
?line 3 = bptest(<<2,3,3>>),
206
% With one variable (pos)
207
?line 1 = bptest(<<1,2,3>>,1),
208
?line 2 = bptest(<<2,1,3>>,1),
209
?line error = bptest(<<1>>,1),
210
?line error = bptest(<<>>,1),
211
?line error = bptest(apa,1),
212
?line 3 = bptest(<<2,3,3>>,1),
213
% With one variable (length)
214
?line 1 = bptesty(<<1,2,3>>,1),
215
?line 2 = bptesty(<<2,1,3>>,1),
216
?line error = bptesty(<<1>>,1),
217
?line error = bptesty(<<>>,1),
218
?line error = bptesty(apa,1),
219
?line 3 = bptesty(<<2,3,3>>,2),
220
% With one variable (whole tuple)
221
?line 1 = bptestx(<<1,2,3>>,{1,1}),
222
?line 2 = bptestx(<<2,1,3>>,{1,1}),
223
?line error = bptestx(<<1>>,{1,1}),
224
?line error = bptestx(<<>>,{1,1}),
225
?line error = bptestx(apa,{1,1}),
226
?line 3 = bptestx(<<2,3,3>>,{1,2}),
228
?line 1 = bptest(<<1,2,3>>,1,1),
229
?line 2 = bptest(<<2,1,3>>,1,1),
230
?line error = bptest(<<1>>,1,1),
231
?line error = bptest(<<>>,1,1),
232
?line error = bptest(apa,1,1),
233
?line 3 = bptest(<<2,3,3>>,1,2),
234
% Direct (autoimported) call, these will be evaluated by the compiler...
235
?line <<2>> = binary_part(<<1,2,3>>,1,1),
236
?line <<1>> = binary_part(<<2,1,3>>,1,1),
237
% Compiler warnings due to constant evaluation expected (3)
238
?line badarg = ?MASK_ERROR(binary_part(<<1>>,1,1)),
239
?line badarg = ?MASK_ERROR(binary_part(<<>>,1,1)),
240
?line badarg = ?MASK_ERROR(binary_part(apa,1,1)),
241
?line <<3,3>> = binary_part(<<2,3,3>>,1,2),
242
% Direct call through apply
243
?line <<2>> = apply(erlang,binary_part,[<<1,2,3>>,1,1]),
244
?line <<1>> = apply(erlang,binary_part,[<<2,1,3>>,1,1]),
245
% Compiler warnings due to constant evaluation expected (3)
246
?line badarg = ?MASK_ERROR(apply(erlang,binary_part,[<<1>>,1,1])),
247
?line badarg = ?MASK_ERROR(apply(erlang,binary_part,[<<>>,1,1])),
248
?line badarg = ?MASK_ERROR(apply(erlang,binary_part,[apa,1,1])),
249
?line <<3,3>> = apply(erlang,binary_part,[<<2,3,3>>,1,2]),
250
% Constant propagation
251
?line Bin = <<1,2,3>>,
253
binary_part(Bin,1,1) =:= <<2>> ->
255
%% Compiler warning, clause cannot match (expected)
260
binary_part(Bin,{1,1}) =:= <<2>> ->
262
%% Compiler warning, clause cannot match (expected)
269
bptest(B) when length(B) =:= 1337 ->
271
bptest(B) when binary_part(B,{1,1}) =:= <<2>> ->
273
bptest(B) when erlang:binary_part(B,1,1) =:= <<1>> ->
275
bptest(B) when erlang:binary_part(B,{1,2}) =:= <<3,3>> ->
280
bptest(B,A) when length(B) =:= A ->
282
bptest(B,A) when binary_part(B,{A,1}) =:= <<2>> ->
284
bptest(B,A) when erlang:binary_part(B,A,1) =:= <<1>> ->
286
bptest(B,A) when erlang:binary_part(B,{A,2}) =:= <<3,3>> ->
291
bptestx(B,A) when length(B) =:= A ->
293
bptestx(B,A) when binary_part(B,A) =:= <<2>> ->
295
bptestx(B,A) when erlang:binary_part(B,A) =:= <<1>> ->
297
bptestx(B,A) when erlang:binary_part(B,A) =:= <<3,3>> ->
302
bptesty(B,A) when length(B) =:= A ->
304
bptesty(B,A) when binary_part(B,{1,A}) =:= <<2>> ->
306
bptesty(B,A) when erlang:binary_part(B,1,A) =:= <<1>> ->
308
bptesty(B,A) when erlang:binary_part(B,{1,A}) =:= <<3,3>> ->
313
bptest(B,A,_C) when length(B) =:= A ->
315
bptest(B,A,C) when binary_part(B,{A,C}) =:= <<2>> ->
317
bptest(B,A,C) when erlang:binary_part(B,A,C) =:= <<1>> ->
319
bptest(B,A,C) when erlang:binary_part(B,{A,C}) =:= <<3,3>> ->
139
325
guard_bifs(doc) -> "Test all guard bifs with nasty (but legal arguments).";
140
326
guard_bifs(Config) when is_list(Config) ->
141
327
?line Big = -237849247829874297658726487367328971246284736473821617265433,