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
|
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2004-2013. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%%
%% %CopyrightEnd%
%%
-module(match_SUITE).
-export([all/0, suite/0,groups/0,init_per_suite/1, end_per_suite/1,
init_per_group/2,end_per_group/2,
pmatch/1,mixed/1,aliases/1,match_in_call/1,
untuplify/1,shortcut_boolean/1,letify_guard/1,
selectify/1,underscore/1,match_map/1,map_vars_used/1,
coverage/1]).
-include_lib("test_server/include/test_server.hrl").
suite() -> [{ct_hooks,[ts_install_cth]}].
all() ->
test_lib:recompile(?MODULE),
[{group,p}].
groups() ->
[{p,test_lib:parallel(),
[pmatch,mixed,aliases,match_in_call,untuplify,
shortcut_boolean,letify_guard,selectify,
underscore,match_map,map_vars_used,coverage]}].
init_per_suite(Config) ->
Config.
end_per_suite(_Config) ->
ok.
init_per_group(_GroupName, Config) ->
Config.
end_per_group(_GroupName, Config) ->
Config.
pmatch(Config) when is_list(Config) ->
?line ok = doit(1),
?line ok = doit(2),
?line {error,baz} = doit(3),
?line {error,foobar} = doit(4),
ok.
%% Thanks to Tobias Lindahl (HiPE).
-define(FOO(X),
case X of
1 -> foo;
2 -> bar;
3 -> baz;
4 -> foobar
end).
doit(X) ->
case ?FOO(X) of
foo -> ok;
bar -> ok;
Other -> {error, Other}
end.
mixed(Config) when is_list(Config) ->
?line glufs = mixit(1),
?line klafs = mixit(2),
?line fnurra = mixit(3),
?line usch = mixit(4),
?line {error,blurf} = mixit(5),
?line {error,87987987} = mixit(6),
?line {error,{a,b,c}} = mixit(7),
ok.
mixit(X) ->
case case X of
1 -> a;
2 -> b;
3 -> 42;
4 -> 77;
5 -> blurf;
6 -> 87987987;
7 -> {a,b,c}
end of
a -> glufs;
b -> klafs;
42 -> fnurra;
77 -> usch;
Other -> {error,Other}
end.
aliases(Config) when is_list(Config) ->
%% Lists/strings.
?line ok = str_alias("abc"),
?line ok = str_alias("def"),
?line ok = str_alias("ghi"),
?line ok = str_alias("klm"),
?line ok = str_alias("qrs"),
?line ok = str_alias("xy"),
?line ok = str_alias(""),
?line ok = str_alias([]),
?line error = str_alias("blurf"),
%% Characters/integers.
?line ok = char_alias($v),
?line ok = char_alias(118),
?line ok = char_alias($w),
?line ok = char_alias(119),
?line ok = char_alias(42),
?line ok = char_alias(3.0),
?line error = char_alias($_),
?line error = char_alias(0),
?line {42,42,42} = three(42),
?line {1,42,99,1,42,99} = tuple_alias({1,42,99}),
?line {-10,20,-10,20,-10,20} = tuple_alias({-10,20}),
?line 6 = tup_lit_alias({1,2,3}),
?line 6 = tup_lit_alias_rev({1,2,3}),
?line {42,42,42,42} = multiple_aliases_1(42),
?line {7,7,7} = multiple_aliases_2(7),
?line {{a,b},{a,b},{a,b}} = multiple_aliases_3({a,b}),
%% Lists/literals.
?line {a,b} = list_alias1([a,b]),
?line {a,b} = list_alias2([a,b]),
?line {a,b} = list_alias3([a,b]),
ok.
str_alias(V) ->
Res = str_alias_1(V),
Res = str_alias_2(V).
str_alias_1([$a,$b,$c]="abc"="a"++[$b,$c]=[97,98,99]) -> ok;
str_alias_1([$d|"ef"]="def") -> ok;
str_alias_1([$g|"hi"]="g"++"hi"="gh"++"i"="ghi"++"") -> ok;
str_alias_1("k"++"lm"=[$k|"lm"]) -> ok;
str_alias_1([113,114,115]="qrs"=[$q,$r,$s]="q"++"r"++"s") -> ok;
str_alias_1([$x,$y]="xy") -> ok;
str_alias_1(""=[]) -> ok;
str_alias_1(_) -> error.
%% Make sure that different line numbers do not matter.
str_alias_2([$a,$b,$c]=
"abc"=
"a"++[$b,$c
]=
[97,98,99
]) -> ok;
str_alias_2([$d|"ef"]=
"def") -> ok;
str_alias_2([$g|"hi"]=
"g"++"hi"=
"gh"++"i"=
"ghi"++"") -> ok;
str_alias_2("k"++"lm"=
[$k|"lm"
]) -> ok;
str_alias_2([113,114,115]=
"qrs"=[$q,$r,$s
]=
"q"++"r"++"s") -> ok;
str_alias_2([$x,$y]=
"xy") -> ok;
str_alias_2(""=
[]) -> ok;
str_alias_2(_) -> error.
char_alias(V) ->
Res = char_alias_1(V),
Res = char_alias_2(V).
char_alias_1(118=$v) -> ok;
char_alias_1($w=119) -> ok;
char_alias_1(42=42) -> ok;
char_alias_1(3.0=3.0) -> ok;
char_alias_1(_) -> error.
char_alias_2(118=
$v) -> ok;
char_alias_2($w=
119) -> ok;
char_alias_2(42=
42) -> ok;
char_alias_2(3.0=
3.0) -> ok;
char_alias_2(_) -> error.
three(V) ->
Res = three_1(V),
Res = three_2(V).
three_1(A=B=C) ->
{A,B,C}.
three_2(A=
B=
C) ->
{A,B,C}.
tuple_alias({A,B,C}={X,Y,Z}) ->
{A,B,C,X,Y,Z};
tuple_alias({A,B}={C,D}={E,F}) ->
{A,B,C,D,E,F}.
tup_lit_alias({A,B,C}={1,2,3}) ->
A+B+C.
tup_lit_alias_rev({1,2,3}={A,B,C}) ->
A+B+C.
multiple_aliases_1((A=B)=(C=D)) ->
{A,B,C,D}.
multiple_aliases_2((A=B)=(A=C)) ->
{A,B,C}.
multiple_aliases_3((A={_,_}=B)={_,_}=C) ->
{A,B,C}.
list_alias1([a,b]=[X,Y]) ->
{X,Y}.
list_alias2([X,Y]=[a,b]) ->
{X,Y}.
list_alias3([X,b]=[a,Y]) ->
{X,Y}.
%% OTP-7018.
match_in_call(Config) when is_list(Config) ->
?line mac_a(0),
?line mac_b(1),
?line mac_c(42),
?line mac_d(42),
?line mac_e({gurka,42}),
?line [{2,2},{2,2}] = mac_lc([{2,any},{2,2}]),
?line {'EXIT',_} = (catch mac_lc([{1,1}])),
ok.
mac_a(X) ->
id(_Gurka = {gurka,X}),
ok.
mac_b(X) ->
id(Gurka = {gurka,X}),
gurka(Gurka, X),
ok.
mac_c(X) ->
id(Gurka = Yxa = {gurka,X}),
id({Gurka,Yxa}),
ok.
mac_d(X) ->
id({gurka,42} = {gurka,X}),
ok.
mac_e(X) ->
id({gurka,42} = X),
ok.
mac_lc(E) ->
Res = mac_lc1(E),
Res = mac_lc2(E).
mac_lc1(E) ->
[{X,Y} ||
{X,_} <- E,
(Y = X) =:= (Y = 1 + 1)].
mac_lc2(E) ->
[{X,Y} ||
{X,_} <- E,
(Y = X) =:= (Y = 2)].
gurka({gurka,X}, X) -> ok.
untuplify(Config) when is_list(Config) ->
%% We do this to cover sys_core_fold:unalias_pat/1.
?line {1,2,3,4,alias,{[1,2],{3,4},alias}} = untuplify_1([1,2], {3,4}, alias),
?line error = untuplify_1([1,2], {3,4}, 42),
ok.
untuplify_1(A, B, C) ->
case {A,B,C} of
{[X,Y],{Z,W},alias=Alias}=Top ->
{X,Y,Z,W,Alias,Top};
[_,_]=CantMatch ->
CantMatch;
_ ->
error
end.
%% Coverage of beam_dead:shortcut_boolean_label/4.
shortcut_boolean(Config) when is_list(Config) ->
?line false = shortcut_boolean_1([0]),
?line true = shortcut_boolean_1({42}),
?line maybe = shortcut_boolean_1(self()),
?line {'EXIT',_} = (catch shortcut_boolean_1([a,b])),
?line {'EXIT',_} = (catch shortcut_boolean_1({a,b})),
ok.
shortcut_boolean_1(X) ->
Outer = case not is_pid(X) of
true ->
V = case X of
[_] -> true;
{_} -> false
end,
not V;
false ->
maybe
end,
id(Outer).
%% Test sys_core_fold:letify_guard/3.
letify_guard(Config) when is_list(Config) ->
?line {-15,a} = letify_guard(-15, a),
?line 5 = letify_guard(2, 3),
ok.
letify_guard(A, B) ->
case {A,B} of
%% The tuple will be built in the guard...
Z when tuple_size(Z) =:= 2, element(1, Z) < 0 ->
%% ... and again here.
Z;
{X,Y} -> X+Y
end.
%% Test combining of is_eq_exact instructions to select_val
%% instructions in beam_dead and beam_peep.
selectify(Config) when is_list(Config) ->
?line integer = sel_different_types({r,42}),
?line atom = sel_different_types({r,forty_two}),
?line none = sel_different_types({r,18}),
?line {'EXIT',_} = (catch sel_different_types([a,b,c])),
?line integer = sel_same_value({r,42}),
?line error = sel_same_value({r,100}),
?line error = sel_same_value(a),
?line integer42 = sel_same_value2(42),
?line integer43 = sel_same_value2(43),
?line error = sel_same_value2(44),
ok.
sel_different_types({r,_}=T) when element(2, T) =:= forty_two ->
atom;
sel_different_types({r,_}=T) when element(2, T) =:= 42 ->
integer;
sel_different_types({r,_}) ->
none.
sel_same_value({r,V}) when V =:= 42 ->
integer;
sel_same_value({r,V}) when V =:= 42 ->
integer42;
sel_same_value(_) ->
error.
sel_same_value2(V) when V =:= 42 ->
integer42;
sel_same_value2(V) when V =:= 42; V =:= 43 ->
integer43;
sel_same_value2(_) ->
error.
underscore(Config) when is_list(Config) ->
case Config of
[] ->
%% Assignment to _ at the end of a construct.
_ = length(Config);
[_|_] ->
%% Assignment to _ at the end of a construct.
_ = list_to_tuple(Config)
end,
_ = is_list(Config),
ok.
-record(s, {map,t}).
match_map(Config) when is_list(Config) ->
Map = #{key=>{x,y},ignore=>anything},
#s{map=Map,t={x,y}} = do_match_map(#s{map=Map}),
ok.
do_match_map(#s{map=#{key:=Val}}=S) ->
%% Would crash with a 'badarg' exception.
S#s{t=Val}.
map_vars_used(Config) when is_list(Config) ->
{some,value} = do_map_vars_used(a, b, #{{a,b}=>42,v=>{some,value}}),
ok.
do_map_vars_used(X, Y, Map) ->
case {X,Y} of
T ->
%% core_lib:is_var_used/2 would not consider T used.
#{T:=42,v:=Val} = Map,
Val
end.
coverage(Config) when is_list(Config) ->
%% Cover beam_dead.
ok = coverage_1(x, a),
ok = coverage_1(x, b).
coverage_1(B, Tag) ->
case Tag of
a -> coverage_2(1, a, B);
b -> coverage_2(2, b, B)
end.
coverage_2(1, a, x) -> ok;
coverage_2(2, b, x) -> ok.
id(I) -> I.
|