4
%% Copyright Ericsson AB 2000-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
20
-module(bs_construct_SUITE).
22
-export([all/0, suite/0,groups/0,init_per_group/2,end_per_group/2,
23
init_per_testcase/2,end_per_testcase/2,
24
init_per_suite/1,end_per_suite/1,
25
test1/1, test2/1, test3/1, test4/1, test5/1, testf/1, not_used/1, in_guard/1,
28
-include_lib("test_server/include/test_server.hrl").
30
suite() -> [{ct_hooks,[ts_install_cth]}].
38
init_per_group(_GroupName, Config) ->
41
end_per_group(_GroupName, Config) ->
46
[test1, test2, test3, test4, test5, testf, not_used,
47
in_guard, coerce_to_float].
49
init_per_testcase(_Case, Config) ->
50
test_lib:interpret(?MODULE),
51
Dog = test_server:timetrap(?t:minutes(1)),
52
[{watchdog,Dog}|Config].
54
end_per_testcase(_Case, Config) ->
55
Dog = ?config(watchdog, Config),
56
?t:timetrap_cancel(Dog),
59
init_per_suite(Config) when is_list(Config) ->
60
?line test_lib:interpret(?MODULE),
61
?line true = lists:member(?MODULE, int:interpreted()),
64
end_per_suite(Config) when is_list(Config) ->
68
57285702734876389752897683.
75
-define(T(B, L), {B, ??B, L}).
76
-define(N(B), {B, ??B, unknown}).
78
-define(FAIL(Expr), ?line {'EXIT',{badarg,_}} = (catch Expr)).
96
?T(<<0.125:32/float>>,
98
?T(<<0.125:32/little-float>>,
102
?T(<<57285702734876389752897684:32>>,
104
?T(<<I_big1:32/little>>,
105
r([138, 99, 0, 147])),
107
lists:duplicate(17, 255)),
112
?T(<<4:8/unit:2,5:2/unit:8>>,
115
?T(<<1:1, 0:6, 1:1>>,
117
?T(<<1:1/little, 0:6/little, 1:1/little>>,
120
?T(<<<<1,2>>/binary>>,
122
?T(<<<<1,2>>:1/binary>>,
124
?T(<<4,3,<<1,2>>:1/binary>>,
136
?T(<<1:3,"string",9:5>>,
137
[46,110,142,77,45,204,233]),
142
?T(<<37.98:64/native-float>>,
145
?T(<<32978297842987249827298387697777669766334937:128/native-integer>>,
151
case <<1:16/native>> of
152
<<0,1>> -> [64,66,253,112,163,215,10,61];
153
<<1,0>> -> [61,10,215,163,112,253,66,64]
157
case <<1:16/native>> of
158
<<0,1>> -> [129,205,18,177,1,213,170,101,39,231,109,128,176,11,73,217];
159
<<1,0>> -> [217,73,11,176,128,109,231,39,101,170,213,1,177,18,205,129]
162
evaluate(Str, Vars) ->
164
erl_scan:string(Str ++ " . "),
165
{ok, [Expr]} = erl_parse:parse_exprs(Tokens),
166
case erl_eval:expr(Expr, Vars) of
167
{value, Result, _} ->
171
eval_list([], _Vars) ->
173
eval_list([{C_bin, Str, Bytes} | Rest], Vars) ->
174
case catch evaluate(Str, Vars) of
176
io:format("Evaluation error: ~p, ~p, ~p~n", [Str, Vars, Error]),
179
[{C_bin, E_bin, Str, Bytes} | eval_list(Rest, Vars)]
182
one_test({C_bin, E_bin, Str, Bytes}) when list(Bytes) ->
183
io:format(" ~s, ~p~n", [Str, Bytes]),
184
Bin = list_to_binary(Bytes),
189
io:format("ERROR: Compiled: ~p. Expected ~p. Got ~p.~n",
190
[Str, Bytes, binary_to_list(C_bin)]),
191
test_server:fail(comp)
197
io:format("ERROR: Interpreted: ~p. Expected ~p. Got ~p.~n",
198
[Str, Bytes, binary_to_list(E_bin)]),
199
test_server:fail(comp)
201
one_test({C_bin, E_bin, Str, Result}) ->
202
io:format(" ~s ~p~n", [Str, C_bin]),
207
Arbitrary = case Result of
213
case equal_lists(binary_to_list(C_bin),
214
binary_to_list(E_bin),
217
io:format("ERROR: Compiled not equal to interpreted:"
219
[binary_to_list(C_bin), binary_to_list(E_bin)]),
220
test_server:fail(comp);
223
%% For situations where the final bits may not matter, like
226
io:format("Info: compiled and interpreted differ in the"
227
" last bytes:~n ~p, ~p.~n",
228
[binary_to_list(C_bin), binary_to_list(E_bin)]),
233
equal_lists([], [], _) ->
235
equal_lists([], _, _) ->
237
equal_lists(_, [], _) ->
239
equal_lists([A|AR], [A|BR], R) ->
240
equal_lists(AR, BR, R);
241
equal_lists(A, B, R) ->
243
length(A) /= length(B) ->
251
%%% Simple working cases
253
test1(Config) when list(Config) ->
255
?line I_big1 = big(1),
256
?line Vars = [{'I_13', I_13},
258
?line lists:foreach(fun one_test/1, eval_list(l(I_13, I_big1), Vars)).
265
M2 = (1 bsl (N-S)) - 1,
266
[((A band M1) bsl (N-S)) bor (A band M2)].
269
[?T(<<A:S, A:(N-S)>>, comp(N, A, S))].
272
[?T(<<A:S/little, A:(N-S)/little>>, comp(N, A, S))].
275
test2(Config) when list(Config) ->
276
?line test2(0, 8, 2#10101010101010101),
277
?line test2(0, 8, 2#1111111111).
279
test2(End, End, _) ->
287
Vars = [{'A',A}, {'N',N}, {'S',S}],
288
io:format("Vars: ~p\n", [Vars]),
289
lists:foreach(fun one_test/1, eval_list(gen(N, S, A), Vars)),
290
lists:foreach(fun one_test/1, eval_list(gen_l(N, S, A), Vars)).
292
%%% Tests without facit
295
[?N(<<4711:13, 9876:13, 3:6>>),
296
?N(<<4.57:64/float>>),
297
?N(<<4.57:32/float>>),
303
test3(Config) when list(Config) ->
305
?line lists:foreach(fun one_test/1, eval_list(t3(), Vars)).
308
[?N(<<A:S, A:(N-S)>>)].
311
[?N(<<A:S/little, A:(N-S)/little>>)].
314
test4(Config) when list(Config) ->
315
?line test4(0, 16, 2#10101010101010101),
316
?line test4(0, 16, 2#1111111111).
318
test4(End, End, _) ->
326
Vars = [{'A', A}, {'N', 16}, {'S', S}],
327
lists:foreach(fun one_test/1, eval_list(gen_u(N, S, A), Vars)),
328
lists:foreach(fun one_test/1, eval_list(gen_u_l(N, S, A), Vars)).
331
[?T(<<A:S/binary-unit:1, A:(N-S)/binary-unit:1>>,
332
binary_to_list(<<A:S/binary-unit:1, A:(N-S)/binary-unit:1>>))].
335
test5(doc) -> ["OTP-3995"];
336
test5(Config) when list(Config) ->
337
?line test5(0, 8, <<73>>),
338
?line test5(0, 8, <<68>>).
340
test5(End, End, _) ->
348
Vars = [{'A', A}, {'N', 8}, {'S', S}],
349
lists:foreach(fun one_test/1, eval_list(gen_b(N, S, A), Vars)).
353
testf(Config) when list(Config) ->
357
?FAIL(<<2.71/binary>>),
358
?FAIL(<<24334/binary>>),
359
?FAIL(<<24334344294788947129487129487219847/binary>>),
361
?FAIL(<<<<1,2,3>>/float>>),
363
%% Negative field widths.
364
testf_1(-8, <<1,2,3,4,5>>),
367
?FAIL(<<3.14:(-8)/float>>),
368
?FAIL(<<<<23,56,0,2>>:(-16)/binary>>),
369
?FAIL(<<<<23,56,0,2>>:(2.5)/binary>>),
370
?FAIL(<<<<23,56,0,2>>:(anka)>>),
376
?FAIL(<<3.14:W/float>>),
377
?FAIL(<<B:W/binary>>).
380
"Test that constructed binaries that are not used will still give an exception.";
381
not_used(Config) when is_list(Config) ->
382
?line ok = not_used1(3, <<"dum">>),
383
?line ?FAIL(not_used1(3, "dum")),
384
?line ?FAIL(not_used2(444, -2)),
385
?line ?FAIL(not_used2(444, anka)),
386
?line ?FAIL(not_used3(444)),
389
not_used1(I, BinString) ->
390
<<I:32,BinString/binary>>,
401
in_guard(Config) when list(Config) ->
402
?line 1 = in_guard(<<16#74ad:16>>, 16#e95, 5),
403
?line 2 = in_guard(<<16#3A,16#F7,"hello">>, 16#3AF7, <<"hello">>),
404
?line 3 = in_guard(<<16#FBCD:14,3.1415/float,3:2>>, 16#FBCD, 3.1415),
405
nope = in_guard(<<1>>, 42, b),
406
nope = in_guard(<<1>>, a, b),
407
nope = in_guard(<<1,2>>, 1, 1),
408
nope = in_guard(<<4,5>>, 1, 2.71),
409
nope = in_guard(<<4,5>>, 1, <<12,13>>),
412
in_guard(Bin, A, B) when <<A:13,B:3>> == Bin -> 1;
413
in_guard(Bin, A, B) when <<A:16,B/binary>> == Bin -> 2;
414
in_guard(Bin, A, B) when <<A:14,B/float,3:2>> == Bin -> 3;
415
in_guard(Bin, A, B) when {a,b,<<A:14,B/float,3:2>>} == Bin -> cant_happen;
416
in_guard(_, _, _) -> nope.
420
true = <<Int:32/float>> =:= <<(float(Int)):32/float>>,
421
true = <<Int:64/float>> =:= <<(float(Int)):64/float>>
422
end)(nonliteral(Int0)),
423
?line true = <<Int0:32/float>> =:= <<(float(Int0)):32/float>>,
424
?line true = <<Int0:64/float>> =:= <<(float(Int0)):64/float>>).
428
true = <<Int:64/float>> =:= <<(float(Int)):64/float>>
429
end)(nonliteral(Int0)),
430
?line true = <<Int0:64/float>> =:= <<(float(Int0)):64/float>>).
434
coerce_to_float(Config) when list(Config) ->
442
?COF(387498738948729893849444444443),
443
?COF(-37489378937773899999999999999993),
444
?COF64(298748888888888888888888888883478264866528467367364766666666666666663),
445
?COF64(-367546729879999999999947826486652846736736476555566666663),