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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
|
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2015-2016. 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(beam_utils_SUITE).
-export([all/0,suite/0,groups/0,init_per_suite/1,end_per_suite/1,
init_per_group/2,end_per_group/2,
apply_fun/1,apply_mf/1,bs_init/1,bs_save/1,
is_not_killed/1,is_not_used_at/1,
select/1,y_catch/1,otp_8949_b/1,liveopt/1,coverage/1,
y_registers/1,user_predef/1,scan_f/1,cafu/1,
receive_label/1,read_size_file_version/1]).
-export([id/1]).
suite() -> [{ct_hooks,[ts_install_cth]}].
all() ->
test_lib:recompile(?MODULE),
[{group,p}].
groups() ->
[{p,[parallel],
[apply_fun,
apply_mf,
bs_init,
bs_save,
is_not_killed,
is_not_used_at,
select,
y_catch,
otp_8949_b,
liveopt,
coverage,
y_registers,
user_predef,
scan_f,
cafu,
read_size_file_version
]}].
init_per_suite(Config) ->
Config.
end_per_suite(_Config) ->
ok.
init_per_group(_GroupName, Config) ->
Config.
end_per_group(_GroupName, Config) ->
Config.
apply_fun(_Config) ->
3 = do_apply_fun(false, false),
3 = do_apply_fun(false, true),
3 = do_apply_fun(true, false),
2 = do_apply_fun(true, true),
ok.
do_apply_fun(X, Y) ->
F = fun(I) -> I+1 end,
Arg = case X andalso id(Y) of
true -> 1;
false -> 2
end,
F(Arg).
apply_mf(_Config) ->
ok = do_apply_mf_used({a,b}, ?MODULE, id),
error = do_apply_mf_used([a], ?MODULE, id),
{'EXIT',{{case_clause,{[],b}},_}} = (catch do_apply_mf_used({[],b}, ?MODULE, id)),
error = do_apply_mf_killed({error,[a]}, ?MODULE, id),
ok = do_apply_mf_killed([b], ?MODULE, id),
{'EXIT',{{case_clause,{a,[b]}},_}} = (catch do_apply_mf_killed({a,[b]}, ?MODULE, id)),
{'EXIT',{{case_clause,{error,[]}},_}} = (catch do_apply_mf_killed({error,[]}, ?MODULE, id)),
ok.
do_apply_mf_used(Arg, Mod, Func) ->
Res = case id(Arg) of
{Decoded,_} when Decoded =/= [] ->
ok;
List when is_list(List) ->
error
end,
Mod:Func(Res).
do_apply_mf_killed(Arg, Mod, Func) ->
Res = case id(Arg) of
{Tag,Decoded} when Decoded =/= [], Tag =:= error ->
error;
List when is_list(List) ->
ok
end,
Mod:Func(Res).
bs_init(_Config) ->
<<7>> = do_bs_init_1([?MODULE], 7),
error = do_bs_init_1([?MODULE], 0.0),
error = do_bs_init_1([?MODULE], -43),
error = do_bs_init_1([?MODULE], 42),
<<>> = do_bs_init_2([]),
<<0:32,((1 bsl 32)-1):32>> = do_bs_init_2([0,(1 bsl 32)-1]),
{'EXIT',{badarg,_}} = (catch do_bs_init_2([0.5])),
{'EXIT',{badarg,_}} = (catch do_bs_init_2([-1])),
{'EXIT',{badarg,_}} = (catch do_bs_init_2([1 bsl 32])),
ok.
do_bs_init_1([?MODULE], Sz) ->
if
is_integer(Sz), Sz >= -42, Sz < 42 ->
id(<<Sz:8>>);
true ->
error
end.
do_bs_init_2(SigNos) ->
<< <<SigNo:32>> ||
SigNo <- SigNos,
(is_integer(SigNo) andalso SigNo >= 0 andalso SigNo < (1 bsl 32)) orelse
erlang:error(badarg)
>>.
bs_save(_Config) ->
{a,30,<<>>} = do_bs_save(<<1:1,30:5>>),
{b,127,<<>>} = do_bs_save(<<1:1,31:5,0:1,127:7>>),
{c,127,<<>>} = do_bs_save(<<1:1,31:5,1:1,127:7>>),
{c,127,<<>>} = do_bs_save(<<0:1,31:5,1:1,127:7>>),
{d,1024,<<>>} = do_bs_save(<<0:1,31:5>>),
ok.
do_bs_save(<<_:1, Tag:5, T/binary>>) when Tag < 31 ->
{a,Tag,T};
do_bs_save(<<1:1, 31:5, 0:1, Tag:7, T/binary>>) ->
{b,Tag,T};
do_bs_save(<<_:1, 31:5, 1:1, Tag:7, T/binary>>) ->
{c,Tag,T};
do_bs_save(<<_:1, 31:5, T/binary>>) ->
{d,1024,T}.
is_not_killed(_Config) ->
{Pid,Ref} = spawn_monitor(fun() -> exit(banan) end),
receive
{'DOWN', Ref, process, Pid, banan} ->
ok
end,
receive after 0 -> ok end.
is_not_used_at(_Config) ->
{a,b} = do_is_not_used_at(a, [{a,b}]),
{a,b} = do_is_not_used_at(a, [x,{a,b}]),
{a,b} = do_is_not_used_at(a, [{x,y},{a,b}]),
none = do_is_not_used_at(z, [{a,b}]),
none = do_is_not_used_at(a, [x]),
none = do_is_not_used_at(a, [{x,y}]),
ok.
do_is_not_used_at(Key, [P|Ps]) ->
if
tuple_size(P) >= 1, element(1, P) =:= Key ->
P;
true ->
do_is_not_used_at(Key, Ps)
end;
do_is_not_used_at(_Key, []) -> none.
-record(select, {fixed=false}).
select(_Config) ->
a = do_select(#select{}, 0, 0),
b = do_select(#select{}, 0, 1),
c = do_select(#select{fixed=true}, 0, 0),
c = do_select(#select{fixed=true}, 0, 1),
ok.
do_select(Head, OldSize, BSize) ->
Overwrite0 =
if
OldSize =:= BSize -> same;
true -> true
end,
Overwrite =
if
Head#select.fixed =/= false ->
false;
true ->
Overwrite0
end,
if
Overwrite =:= same ->
a;
Overwrite ->
b;
true ->
c
end.
y_catch(_Config) ->
ok = try
do_y_catch(<<"<?xmlX">>, {state}),
failed
catch
throw:{<<"<?xmlX">>,{state}} ->
ok
end.
do_y_catch(<<"<?xml",Rest0/binary>> = Bytes, State0) ->
{Rest1,State1} =
case do_y_catch_1(Rest0, State0) of
false ->
{Bytes,State0};
true ->
{_XmlAttributes, R, S} = do_y_catch_2(Rest0),
{R,S}
end,
case catch id({Rest1,State1}) of
Other ->
throw(Other)
end.
do_y_catch_1(<<_,_/binary>>, _) ->
false.
do_y_catch_2(_) -> {a,b,c}.
otp_8949_b(_Config) ->
self() ! something,
value = otp_8949_b([], false),
{'EXIT',_} = (catch otp_8949_b([], true)),
ok.
%% Would cause an endless loop in beam_utils.
otp_8949_b(A, B) ->
Var = id(value),
if
A == [], B == false ->
ok
end,
receive
something ->
id(Var)
end.
-record(alarmInfo, {type,cause,origin}).
liveopt(_Config) ->
F = liveopt_fun(42, pebkac, user),
void = F(42, #alarmInfo{type=sctp,cause=pebkac,origin=user}),
A = {#alarmInfo{cause = {abc, def}}, ghi},
A = liveopt_guard_bif(A),
B = {#alarmInfo{cause = {abc}}, def},
{#alarmInfo{cause = {{abc}}}, def} = liveopt_guard_bif(B),
ok.
liveopt_fun(Peer, Cause, Origin) ->
fun(PeerNo, AlarmInfo)
when PeerNo == Peer andalso
AlarmInfo == #alarmInfo{type=sctp,
cause=Cause,
origin=Origin} ->
void
end.
liveopt_guard_bif({#alarmInfo{cause=F}=R, X}=A) ->
%% ERIERL-48
if
is_tuple(F), tuple_size(F) == 2 -> A;
true ->
R2 = R#alarmInfo{cause={F}},
{R2,X}
end.
%% Thanks to QuickCheck.
coverage(_Config) ->
42+7 = merchant([[],7,false]),
{'EXIT',{{try_clause,0},_}} = (catch resulting([0], stone)),
0.0 = resulting([true], stone),
{'EXIT',{if_clause,_}} = (catch clinic(false)),
{'EXIT',{{try_clause,"trials"},_}} = (catch clinic(true)),
{'EXIT',{function_clause,_}} = (catch town(overall, {{abc},alcohol})),
self() ! junk_message,
{"url",#{true:="url"}} = appointment(#{"resolution" => "url"}),
ok.
%% Cover check_liveness/3.
merchant([Merchant, Laws, Electric]) ->
id(42),
oklahoma([[] || 0 <- Merchant],
if true; Electric -> Laws end) + 42.
oklahoma([], Int) -> Int.
town(overall, {{If}, Healing = alcohol})
when Healing#{[] => Healing}; include ->
[If || Healing <- awareness].
%% Cover is_reg_used_at/3.
resulting([Conservation], stone) ->
try 0 of
Conservation when Conservation -> Conservation;
_ when Conservation; 0 -> 0.0
after
Conservation
end.
%% Cover is_reg_used_at_1/3.
clinic(Damage) ->
if
Damage ->
try "trials" of Damage when Damage -> Damage catch true -> [] end
end,
carefully.
y_registers(_Config) ->
{'EXIT',{{badfun,0},_}} = (catch economic(0.0, jim)),
{'EXIT',{{badmatch,apartments},_}} = (catch louisiana()),
{a,b} = (boxes(true))({a,b}),
{'EXIT',{{case_clause,webmaster},_}} = (catch yellow(true)),
ok.
economic(0.0 = Serves, Existence) ->
case Serves of
Serves -> 0
end,
Existence = jim,
0(),
Serves,
Existence.
louisiana() ->
{catch necessarily,
try
[] == reg,
true = apartments
catch [] -> barbara
end}.
boxes(Call) ->
case Call of
Call -> approval
end,
Call,
fun id/1.
yellow(Hill) ->
case webmaster of
station -> eyes; Hill ->
"under"
end,
Hill,
id(42).
do(A, B) -> {A,B}.
appointment(#{"resolution" := Url}) ->
do(receive _ -> Url end, #{true => Url}).
%% From epp.erl.
user_predef(_Config) ->
#{key:="value"} = user_predef({key,"value"}, #{}),
#{key:="value"} = user_predef({key,"value"}, #{key=>defined}),
error = user_predef({key,"value"}, #{key=>[defined]}),
ok.
user_predef({M,Val}, Ms) ->
case Ms of
#{M:=Defs} when is_list(Defs) ->
error;
_ ->
Ms#{M=>Val}
end.
%% From disk_log_1.erl.
scan_f(_Config) ->
{1,<<>>,[]} = scan_f(<<1:32>>, 1, []),
{1,<<>>,[<<156>>]} = scan_f(<<1:32,156,1:32>>, 1, []),
ok.
scan_f(<<Size:32,Tail/binary>>, FSz, Acc) when Size =< FSz ->
case Tail of
<<BinTerm:Size/binary,Tail2/binary>> ->
scan_f(Tail2, FSz, [BinTerm | Acc]);
_ ->
{Size,Tail,Acc}
end.
%% From file_io_server.erl.
cafu(_Config) ->
error = cafu(<<42:32>>, -1, 0, {utf32,big}),
error = cafu(<<42:32>>, 10, 0, {utf32,big}),
error = cafu(<<42:32>>, -1, 0, {utf32,little}),
ok.
cafu(<<_/big-utf32,Rest/binary>>, N, Count, {utf32,big}) when N < 0 ->
cafu(Rest, -1, Count+1, {utf32,big});
cafu(<<_/big-utf32,Rest/binary>>, N, Count, {utf32,big}) ->
cafu(Rest, N-1, Count+1, {utf32,big});
cafu(<<_/little-utf32,Rest/binary>>, N, Count, {utf32,little}) when N < 0 ->
cafu(Rest, -1, Count+1, {utf32,little});
cafu(_, _, _, _) ->
error.
-record(rec_label, {bool}).
receive_label(_Config) ->
Pid = spawn_link(fun() -> do_receive_label(#rec_label{bool=true}) end),
Msg = {a,b,c},
Pid ! {self(),Msg},
receive
{ok,Msg} ->
unlink(Pid),
exit(Pid, die),
ok
end.
do_receive_label(Rec) ->
receive
{From,Message} when Rec#rec_label.bool ->
From ! {ok,Message},
do_receive_label(Rec)
end.
read_size_file_version(_Config) ->
ok = do_read_size_file_version({ok,<<42>>}),
{ok,7777} = do_read_size_file_version({ok,<<7777:32>>}),
ok.
do_read_size_file_version(E) ->
case E of
{ok,<<Version>>} when Version =:= 42 ->
ok;
{ok,<<MaxFiles:32>>} ->
{ok,MaxFiles}
end.
%% The identity function.
id(I) -> I.
|