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
|
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 1997-2011. 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(erl_match_SUITE).
-include_lib("test_server/include/test_server.hrl").
-include("erl_match_SUITE_data/match_test_cases.hrl").
-export([all/0, suite/0,groups/0,init_per_suite/1, end_per_suite/1,
init_per_group/2,end_per_group/2,
atoms/1, lists/1, tuples/1, references/1, pids/1, ports/1,
bind/1, integers/1, floats/1, binaries/1, strings/1]).
%% For interactive running of matcher.
-export([start_matcher/0, erl_match/3]).
%% This test suite tests the erl_match() function.
suite() -> [{ct_hooks,[ts_install_cth]}].
all() ->
[atoms, lists, tuples, references, pids, ports, bind,
integers, floats, binaries, strings].
groups() ->
[].
init_per_suite(Config) ->
Config.
end_per_suite(_Config) ->
ok.
init_per_group(_GroupName, Config) ->
Config.
end_per_group(_GroupName, Config) ->
Config.
atoms(suite) -> [];
atoms(Config) when is_list(Config) ->
?line P = start_matcher(),
?line eq(P, '', ''),
?line eq(P, a, a),
?line ne(P, a, b),
?line ne(P, a, aa),
?line eq(P, kalle, kalle),
?line ne(P, kalle, arne),
?line ne(P, kalle, 42),
?line ne(P, 42, kalle),
?line runner:finish(P),
ok.
lists(suite) -> [];
lists(Config) when is_list(Config) ->
?line P = start_matcher(),
?line eq(P, [], []),
?line ne(P, [], [a]),
?line ne(P, [a], []),
?line eq(P, [a], [a]),
?line ne(P, [a], [b]),
?line eq(P, [a|b], [a|b]),
?line ne(P, [a|b], [a|x]),
?line eq(P, [a, b], [a, b]),
?line ne(P, [a, b], [a, x]),
?line eq(P, [a, b, c], [a, b, c]),
?line ne(P, [a, b|c], [a, b|x]),
?line ne(P, [a, b, c], [a, b, x]),
?line ne(P, [a, b|c], [a, b|x]),
?line ne(P, [a, x|c], [a, b|c]),
?line ne(P, [a, b, c], [a, x, c]),
?line runner:finish(P),
ok.
tuples(suite) -> [];
tuples(Config) when is_list(Config) ->
?line P = start_matcher(),
?line ne(P, {}, {a, b}),
?line ne(P, {a, b}, {}),
?line ne(P, {a}, {a, b}),
?line ne(P, {a, b}, {a}),
?line eq(P, {}, {}),
?line eq(P, {a}, {a}),
?line ne(P, {a}, {b}),
?line eq(P, {1}, {1}),
?line ne(P, {1}, {2}),
?line eq(P, {a, b}, {a, b}),
?line ne(P, {x, b}, {a, b}),
?line ne(P, {error, x}, {error, y}),
?line ne(P, {error, {undefined, {subscriber, last}}},
{error, {undefined, {subscriber, name}}}),
?line runner:finish(P),
ok.
references(suite) -> [];
references(Config) when is_list(Config) ->
?line P = start_matcher(),
?line Ref1 = make_ref(),
?line Ref2 = make_ref(),
?line eq(P, Ref1, Ref1),
?line eq(P, Ref2, Ref2),
?line ne(P, Ref1, Ref2),
?line ne(P, Ref2, Ref1),
?line runner:finish(P),
ok.
pids(suite) -> [];
pids(Config) when is_list(Config) ->
?line P = start_matcher(),
?line Pid1 = c:pid(0,1,2),
?line Pid2 = c:pid(0,1,3),
?line eq(P, self(), self()),
?line eq(P, Pid1, Pid1),
?line ne(P, Pid1, self()),
?line ne(P, Pid2, Pid1),
?line runner:finish(P),
ok.
ports(suite) -> [];
ports(Config) when is_list(Config) ->
case os:type() of
vxworks ->
{skipped,"not on vxworks, pucko"};
_ ->
?line P = start_matcher(),
?line P2 = start_matcher(),
?line eq(P, P, P),
?line ne(P, P, P2),
?line runner:finish(P),
?line runner:finish(P2),
ok
end.
integers(suite) -> [];
integers(Config) when is_list(Config) ->
?line P = start_matcher(),
?line I1 = 123,
?line I2 = 12345,
?line I3 = -123,
?line I4 = 2234,
?line eq(P, I1, I1),
?line eq(P, I2, I2),
?line ne(P, I1, I2),
?line ne(P, I1, I3),
?line eq(P, I4, I4),
?line runner:finish(P),
ok.
floats(suite) -> [];
floats(Config) when is_list(Config) ->
?line P = start_matcher(),
?line F1 = 3.1414,
?line F2 = 3.1415,
?line F3 = 3.1416,
?line S1 = "string",
?line S2 = "string2",
?line eq(P, F1, F1),
?line eq(P, F2, F2),
?line ne(P, F1, F2),
?line ne(P, F3, F2),
?line eq(P, S2, S2),
?line ne(P, S1, S2),
?line runner:finish(P),
ok.
binaries(suite) -> [];
binaries(Config) when is_list(Config) ->
?line P = start_matcher(),
?line Bin1 = term_to_binary({kalle, 146015, {kungsgatan, 23}}),
?line Bin2 = term_to_binary(sune),
?line Bin3 = list_to_binary("sune"),
?line eq(P, Bin1, Bin1),
?line eq(P, Bin2, Bin2),
?line eq(P, Bin3, Bin3),
?line ne(P, Bin1, Bin2),
?line ne(P, Bin1, Bin3),
?line ne(P, Bin2, Bin3),
?line runner:finish(P),
ok.
strings(suite) -> [];
strings(Config) when is_list(Config) ->
?line P = start_matcher(),
?line S1 = "string",
?line S2 = "streng",
?line S3 = "String",
?line eq(P, S1, S1),
?line ne(P, S1, S2),
?line ne(P, S1, S3),
?line runner:finish(P),
ok.
bind(suite) -> [];
bind(Config) when is_list(Config) ->
?line P = start_bind(),
?line S = "[X,Y,Z]",
?line L1 = [301,302,302],
?line L2 = [65,66,67],
?line bind_ok(P, S, L1),
?line bind_ok(P, S, L2),
?line runner:finish(P),
ok.
start_bind() ->
runner:start(?erl_match_bind).
bind_ok(Port, Bind, Term) ->
true = erl_bind(Port, Bind, Term).
%bind_nok(Port, Bind, Term) ->
% false = erl_bind(Port, Bind, Term).
erl_bind(Port, Pattern, Term) ->
Port ! {self(), {command, [$b, Pattern, 0]}},
runner:send_term(Port, Term),
case runner:get_term(Port) of
{term, 0} -> false;
{term, 1} -> true
end.
start_matcher() ->
runner:start(?erl_match_server).
eq(Port, Pattern, Term) ->
true = erl_match(Port, Pattern, Term).
ne(Port, Pattern, Term) ->
false = erl_match(Port, Pattern, Term).
erl_match(Port, Pattern, Term) ->
runner:send_term(Port, Pattern),
runner:send_term(Port, Term),
case runner:get_term(Port) of
{term, 0} -> false;
{term, 1} -> true
end.
|