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_match_misc_SUITE).
22
-author('bjorn@erix.ericsson.se').
23
-export([all/0, suite/0,groups/0,init_per_group/2,end_per_group/2,
24
init_per_testcase/2,end_per_testcase/2,
25
init_per_suite/1,end_per_suite/1,
26
bound_var/1,bound_tail/1,t_float/1,little_float/1,sean/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
[bound_var, bound_tail, t_float, little_float, sean].
48
init_per_testcase(_Case, Config) ->
49
test_lib:interpret(?MODULE),
50
Dog = test_server:timetrap(?t:minutes(1)),
51
[{watchdog,Dog}|Config].
53
end_per_testcase(_Case, Config) ->
54
Dog = ?config(watchdog, Config),
55
?t:timetrap_cancel(Dog),
58
init_per_suite(Config) when is_list(Config) ->
59
?line test_lib:interpret(?MODULE),
60
?line true = lists:member(?MODULE, int:interpreted()),
63
end_per_suite(Config) when is_list(Config) ->
66
bound_var(doc) -> "Test matching of bound variables.";
67
bound_var(Config) when list(Config) ->
68
?line ok = bound_var(42, 13, <<42,13>>),
69
?line nope = bound_var(42, 13, <<42,255>>),
70
?line nope = bound_var(42, 13, <<154,255>>),
73
bound_var(A, B, <<A:8,B:8>>) -> ok;
74
bound_var(_, _, _) -> nope.
76
bound_tail(doc) -> "Test matching of a bound tail.";
77
bound_tail(Config) when list(Config) ->
78
?line ok = bound_tail(<<>>, <<13,14>>),
79
?line ok = bound_tail(<<2,3>>, <<1,1,2,3>>),
80
?line nope = bound_tail(<<2,3>>, <<1,1,2,7>>),
81
?line nope = bound_tail(<<2,3>>, <<1,1,2,3,4>>),
82
?line nope = bound_tail(<<2,3>>, <<>>),
85
bound_tail(T, <<_:16,T/binary>>) -> ok;
86
bound_tail(_, _) -> nope.
88
t_float(Config) when list(Config) ->
92
?line G = match_float(<<63,128,0,0>>, 32, 0),
93
?line G = match_float(<<63,240,0,0,0,0,0,0>>, 64, 0),
95
?line fcmp(F, match_float(<<F:32/float>>, 32, 0)),
96
?line fcmp(F, match_float(<<F:64/float>>, 64, 0)),
97
?line fcmp(F, match_float(<<1:1,F:32/float,127:7>>, 32, 1)),
98
?line fcmp(F, match_float(<<1:1,F:64/float,127:7>>, 64, 1)),
99
?line fcmp(F, match_float(<<1:13,F:32/float,127:3>>, 32, 13)),
100
?line fcmp(F, match_float(<<1:13,F:64/float,127:3>>, 64, 13)),
104
fcmp(F1, F2) when (F1 - F2) / F2 < 0.0000001 -> ok.
106
match_float(Bin0, Fsz, I) ->
107
Bin = make_sub_bin(Bin0),
110
<<_:I,F:Fsz/float,_:Tsz>> = Bin,
113
little_float(Config) when list(Config) ->
117
?line G = match_float_little(<<0,0,0,0,0,0,240,63>>, 64, 0),
118
?line G = match_float_little(<<0,0,128,63>>, 32, 0),
120
?line fcmp(F, match_float_little(<<F:32/float-little>>, 32, 0)),
121
?line fcmp(F, match_float_little(<<F:64/float-little>>, 64, 0)),
122
?line fcmp(F, match_float_little(<<1:1,F:32/float-little,127:7>>, 32, 1)),
123
?line fcmp(F, match_float_little(<<1:1,F:64/float-little,127:7>>, 64, 1)),
124
?line fcmp(F, match_float_little(<<1:13,F:32/float-little,127:3>>, 32, 13)),
125
?line fcmp(F, match_float_little(<<1:13,F:64/float-little,127:3>>, 64, 13)),
129
match_float_little(Bin0, Fsz, I) ->
130
Bin = make_sub_bin(Bin0),
133
<<_:I,F:Fsz/float-little,_:Tsz>> = Bin,
137
make_sub_bin(Bin0) ->
139
Bin1 = <<37,Bin0/binary,38,39>>,
140
<<_:8,Bin:Sz/binary,_:8,_:8>> = Bin1,
152
sean(Config) when list(Config) ->
153
?line small = sean1(<<>>),
154
?line small = sean1(<<1>>),
155
?line small = sean1(<<1,2>>),
156
?line small = sean1(<<1,2,3>>),
157
?line large = sean1(<<1,2,3,4>>),
159
?line small = sean1(<<4>>),
160
?line small = sean1(<<4,5>>),
161
?line small = sean1(<<4,5,6>>),
162
?line {'EXIT',{function_clause,_}} = (catch sean1(<<4,5,6,7>>)),
165
sean1(<<B/binary>>) when size(B) < 4 -> small;
166
sean1(<<1, _B/binary>>) -> large.