1%%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 2006-2018. All Rights Reserved.
5%%
6%% Licensed under the Apache License, Version 2.0 (the "License");
7%% you may not use this file except in compliance with the License.
8%% You may obtain a copy of the License at
9%%
10%%     http://www.apache.org/licenses/LICENSE-2.0
11%%
12%% Unless required by applicable law or agreed to in writing, software
13%% distributed under the License is distributed on an "AS IS" BASIS,
14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15%% See the License for the specific language governing permissions and
16%% limitations under the License.
17%%
18%% %CopyrightEnd%
19%%
20%% Originally based on Per Gustafsson's test suite.
21%%
22
23-module(bs_bincomp_SUITE).
24
25-export([all/0, suite/0,groups/0,init_per_suite/1, end_per_suite/1,
26	 init_per_group/2,end_per_group/2,
27	 byte_aligned/1,bit_aligned/1,extended_byte_aligned/1,
28	 extended_bit_aligned/1,mixed/1,filters/1,trim_coverage/1,
29	 nomatch/1,sizes/1,general_expressions/1,matched_out_size/1]).
30
31-include_lib("common_test/include/ct.hrl").
32
33suite() -> [{ct_hooks,[ts_install_cth]}].
34
35all() ->
36    [byte_aligned, bit_aligned, extended_byte_aligned,
37     extended_bit_aligned, mixed, filters, trim_coverage,
38     nomatch, sizes, general_expressions, matched_out_size].
39
40groups() ->
41    [].
42
43init_per_suite(Config) ->
44    test_lib:recompile(?MODULE),
45    Config.
46
47end_per_suite(_Config) ->
48    ok.
49
50init_per_group(_GroupName, Config) ->
51	Config.
52
53end_per_group(_GroupName, Config) ->
54	Config.
55
56byte_aligned(Config) when is_list(Config) ->
57    cs_init(),
58    <<"abcdefg">> = cs(<< <<(X+32)>> || <<X>> <= <<"ABCDEFG">> >>),
59    <<"AxyzBxyzCxyz">> = cs(<< <<X, "xyz">> || <<X>> <= <<"ABC">> >>),
60    <<1:32/little,2:32/little,3:32/little,4:32/little>> =
61	cs(<< <<X:32/little>> || <<X:32>> <= <<1:32,2:32,3:32,4:32>> >>),
62    cs(<<1:32/little,2:32/little,3:32/little,4:32/little>> =
63	   << <<X:32/little>> || <<X:16>> <= <<1:16,2:16,3:16,4:16>> >>),
64    cs_end().
65
66bit_aligned(Config) when is_list(Config) ->
67    cs_init(),
68    <<$a:7,$b:7,$c:7,$d:7,$e:7,$f:7,$g:7>> =
69	cs(<< <<(X+32):7>> || <<X>> <= <<"ABCDEFG">> >>),
70    <<"ABCDEFG">> =
71	cs(<< <<(X-32)>> || <<X:7>> <= <<$a:7,$b:7,$c:7,$d:7,$e:7,$f:7,$g:7>> >>),
72    <<1:31/little,2:31/little,3:31/little,4:31/little>> =
73	cs(<< <<X:31/little>> || <<X:31>> <= <<1:31,2:31,3:31,4:31>> >>),
74    <<1:31/little,2:31/little,3:31/little,4:31/little>> =
75	cs(<< <<X:31/little>> || <<X:15>> <= <<1:15,2:15,3:15,4:15>> >>),
76    cs_end().
77
78extended_byte_aligned(Config) when is_list(Config) ->
79    cs_init(),
80    <<"abcdefg">> = cs(<< <<(X+32)>> || X <- "ABCDEFG" >>),
81    "abcdefg" = [(X+32) || <<X>> <= <<"ABCDEFG">>],
82    <<1:32/little,2:32/little,3:32/little,4:32/little>> =
83	cs(<< <<X:32/little>> || X <- [1,2,3,4] >>),
84    [256,512,768,1024] =
85	[X || <<X:16/little>> <= <<1:16,2:16,3:16,4:16>>],
86    cs_end().
87
88extended_bit_aligned(Config) when is_list(Config) ->
89    cs_init(),
90    <<$a:7,$b:7,$c:7,$d:7,$e:7,$f:7,$g:7>> =
91	cs(<< <<(X+32):7>> || X <- "ABCDEFG" >>),
92    "ABCDEFG" = [(X-32) || <<X:7>> <= <<$a:7,$b:7,$c:7,$d:7,$e:7,$f:7,$g:7>>],
93    <<1:31/little,2:31/little,3:31/little,4:31/little>> =
94	cs(<< <<X:31/little>> || X <- [1,2,3,4] >>),
95    [256,512,768,1024] =
96	[X || <<X:15/little>> <= <<1:15,2:15,3:15,4:15>>],
97    cs_end().
98
99mixed(Config) when is_list(Config) ->
100    cs_init(),
101    <<2,3,3,4,4,5,5,6>> =
102	cs(<< <<(X+Y)>> || <<X>> <= <<1,2,3,4>>, <<Y>> <= <<1,2>> >>),
103    <<2,3,3,4,4,5,5,6>> =
104	<< <<(X+Y)>> || <<X>> <= <<1,2,3,4>>, Y <- [1,2] >>,
105    <<2,3,3,4,4,5,5,6>> =
106	cs(<< <<(X+Y)>> || X <- [1,2,3,4], Y <- [1,2] >>),
107    One = id([1,2,3,4]),
108    Two = id([1,2]),
109    <<2,3,3,4,4,5,5,6>> =
110	cs(<< <<(X+Y)>> || X <- One, Y <- Two >>),
111    [2,3,3,4,4,5,5,6] =
112	[(X+Y) || <<X>> <= <<1,2,3,4>>, <<Y>> <= <<1,2>>],
113    [2,3,3,4,4,5,5,6] =
114	[(X+Y) || <<X>> <= <<1,2,3,4>>, Y <- [1,2]],
115    <<2:3,3:3,3:3,4:3,4:3,5:3,5:3,6:3>> =
116	cs(<< <<(X+Y):3>> || <<X:3>> <= <<1:3,2:3,3:3,4:3>>,
117			     <<Y:3>> <= <<1:3,2:3>> >>),
118    <<2:3,3:3,3:3,4:3,4:3,5:3,5:3,6:3>> =
119	cs(<< <<(X+Y):3>> || <<X:3>> <= <<1:3,2:3,3:3,4:3>>, Y <- [1,2] >>),
120    <<2:3,3:3,3:3,4:3,4:3,5:3,5:3,6:3>> =
121	cs(<< <<(X+Y):3>> || X <- [1,2,3,4], Y <- [1,2] >>),
122    <<2:3,3:3,3:3,4:3,4:3,5:3,5:3,6:3>> =
123	cs_default(<< <<(X+Y):3>> || {X,Y} <- [{1,1},{1,2},{2,1},{2,2},
124					       {3,1},{3,2},{4,1},{4,2}] >>),
125    [2,3,3,4,4,5,5,6] =
126	[(X+Y) || <<X:3>> <= <<1:3,2:3,3:3,4:3>>, <<Y:3>> <= <<1:3,2:3>>],
127    [2,3,3,4,4,5,5,6] =
128	[(X+Y) || <<X:3>> <= <<1:3,2:3,3:3,4:3>>, {_,Y} <- [{a,1},{b,2}]],
129    cs_end().
130
131filters(Config) when is_list(Config) ->
132    cs_init(),
133    <<"BDF">> =
134	cs_default(<< <<(X-32)>> ||
135		       <<X:7>> <= <<$a:7,$b:7,$c:7,$d:7,$e:7,$f:7,$g:7>>,
136		       X rem 2 == 0>>),
137    <<"abc">> = cs_default(<< <<(X+32)>> ||
138			       X <- "ABCDEFG",
139			       is_less_than(X, $D)>>),
140    <<"efg">> = cs_default(<< <<(X+32)>> ||
141			       X <- "ABCDEFG",
142			       not is_less_than(X, $E)>>),
143    <<"b">> = cs_default(<< <<(X+32)>> ||
144			     X <- "ABCDEFG",
145			     is_less_than(X, $D),
146			     X rem 2 == 0>>),
147    <<"eg">> = cs_default(<< <<(X+32)>> ||
148			      X <- "ABCDEFG",
149			      not is_less_than(X, $E),
150			      X rem 2 == 1>>),
151
152    %% Filtering by a non-matching pattern.
153    <<"abd">> = cs_default(<< <<X:8>> ||
154			       <<0:1,X:7>> <= <<$a:8,$b:8,1:1,$c:7,$d:8,
155						1:1,$e:7,0:4>> >>),
156
157    <<42,42>> = cs_default(<< <<42:8>> || 42 <- [1,2,3,42,43,42] >>),
158    cs_end().
159
160is_less_than(X, C) when X < C -> true;
161is_less_than(_, _) -> false.
162
163trim_coverage(Config) when is_list(Config) ->
164    <<0,0,0,2,0,0,5,48,0,11,219,174,0,0,0,0>> = coverage_materialiv(a, b, {1328,777134}),
165    <<67,40,0,0,66,152,0,0,69,66,64,0>> = coverage_trimmer([42,19,777]),
166    <<0,0,2,43,0,0,3,9,0,0,0,3,64,8,0,0,0,0,0,0,
167	   64,68,0,0,0,0,0,0,192,171,198,0,0,0,0,0>> =
168	coverage_lightfv(555, 777, {3.0,40.0,-3555.0}),
169    ok.
170
171coverage_materialiv(A, B, Params) ->
172    A = id(A),
173    B = id(B),
174    <<(tuple_size(Params)):32,
175     (<< <<C:32>> || C <- tuple_to_list(Params)>>)/binary,
176     0:(((1+tuple_size(Params)) rem 2)*32)>>.
177
178coverage_lightfv(Light, Pname, Params) ->
179    id(<<Light:32,Pname:32,(size(Params)):32,
180	(<< <<C:64/float>> || C <- tuple_to_list(Params)>>)/binary,
181	0:(((1+size(Params)) rem 2)*32)>>).
182
183coverage_trimmer(Params) ->
184    X = id(0),
185    Y = id(1),
186    id({X,Y}),
187    << <<(begin {A,B,D} = id({C,C,C}), id(0),
188		coverage_summer(A, B, C, D) end):32/float>> ||
189	C <- Params >>.
190
191coverage_summer(A, B, C, D) -> A+B+C+D.
192
193nomatch(Config) when is_list(Config) ->
194    <<>> = << <<X:8>> || X = {_,_} = [_|_] <- [1,2,3] >>,
195    ok.
196
197sizes(Config) when is_list(Config) ->
198    cs_init(),
199    Fun0 = fun(List) ->
200		   cs(<< <<E:8>> || E <- List >>)
201	   end,
202    <<>> = Fun0([]),
203    <<1>> = Fun0([1]),
204    <<1,2>> = Fun0([1,2]),
205    <<1,2,3>> = Fun0([1,2,3]),
206
207    Fun1 = fun(List) ->
208		   cs(<< <<E:16>> || E <- List >>)
209	   end,
210    <<>> = Fun1([]),
211    <<1:16>> = Fun1([1]),
212    <<1:16,2:16>> = Fun1([1,2]),
213    <<1:16,2:16,3:16>> = Fun1([1,2,3]),
214
215    Fun2 = fun(List) ->
216		   cs(<< <<E:4>> || E <- List >>)
217	   end,
218    <<>> = Fun2([]),
219    <<1:4>> = Fun2([1]),
220    <<1:4,13:4>> = Fun2([1,13]),
221    <<1:4,13:4,7:4>> = Fun2([1,13,7]),
222    <<0:1000/unit:8>> = Fun2(lists:duplicate(2000, 0)),
223
224    Fun3 = fun(List) ->
225		   cs(<< <<E:3>> || E <- List >>)
226	   end,
227    <<>> = Fun3([]),
228    <<40,177,29:5>> = Fun3([1,2,1,3,0,7,5]),
229    <<0:512/unit:3>> = Fun3(lists:duplicate(512, 0)),
230
231    Fun4 = fun(List, Size) ->
232		   cs(<< <<E:Size>> || E <- List >>)
233	   end,
234    <<>> = Fun4([], 8),
235    <<42:6>> = Fun4([42], 6),
236    <<42:16>> = Fun4([42], 16),
237
238    Fun5 = fun(List, Sz1, Sz2, Sz3) ->
239		   cs(<< <<E:Sz1,(E+1):Sz2/unit:8,(E+2):Sz3/unit:8>> || E <- List >>)
240	   end,
241    <<>> = Fun5([], 1, 1, 1),
242    <<7:3,8:40,9:56>> = Fun5([7], 3, 5, 7),
243
244    Fun6 = fun(List, Size) ->
245		   cs(<< <<E:8,(E+1):Size>> || E <- List >>)
246	   end,
247    <<>> = Fun6([], 42),
248    <<42,43:20>> = Fun6([42], 20),
249
250    %% Binary generators.
251
252    Fun10 = fun(Bin) ->
253		    cs(<< <<E:16>> || <<E:8>> <= Bin >>)
254            end,
255    <<>> = Fun10(<<>>),
256    <<1:16>> = Fun10(<<1>>),
257    <<1:16,2:16>> = Fun10(<<1,2>>),
258
259    Fun11 = fun(Bin) ->
260		    cs(<< <<E:8>> || <<E:16>> <= Bin >>)
261            end,
262    <<>> = Fun11(<<>>),
263    <<1>> = Fun11(<<1:16>>),
264    <<1,2>> = Fun11(<<1:16,2:16>>),
265    <<1,2>> = Fun11(<<1:16,2:16,0:1>>),
266    <<1,2>> = Fun11(<<1:16,2:16,0:7>>),
267    <<1,2>> = Fun11(<<1:16,2:16,42:8>>),
268    <<1,2>> = Fun11(<<1:16,2:16,42:9>>),
269    <<1,2>> = Fun11(<<1:16,2:16,255:15>>),
270
271    Fun12 = fun(Bin, Sz1, Sz2) ->
272		    cs(<< <<E:Sz1>> || <<E:Sz2>> <= Bin >>)
273	    end,
274    <<>> = Fun12(<<>>, 1, 1),
275    Binary = list_to_binary(lists:seq(0, 255)),
276    Binary = Fun12(Binary, 1, 1),
277    Binary = Fun12(Binary, 4, 4),
278    Binary = Fun12(Binary, 8, 8),
279    <<17:9,19:9>> = Fun12(<<17:6,19:6>>, 9, 6),
280
281    Fun13 = fun(Sz) ->
282		    cs_default(<< <<C:8>> || <<C:4>> <= <<1:4,2:4,3:4,0:Sz>> >>)
283   	    end,
284    <<1,2,3>> = Fun13(0),
285    <<1,2,3,0>> = Fun13(4),
286    <<1,2,3,0>> = Fun13(5),
287    <<1,2,3,0>> = Fun13(6),
288    <<1,2,3,0>> = Fun13(7),
289    <<1,2,3,0,0>> = Fun13(8),
290
291    <<0:3>> = cs_default(<< <<0:S>> || S <- [0,1,2] >>),
292    <<0:3>> = cs_default(<< <<0:S>> || <<S>> <= <<0,1,2>> >>),
293
294    {'EXIT',_} = (catch << <<C:4>> || <<C:8>> <= {1,2,3} >>),
295
296    cs_end(),
297    ok.
298
299-define(BAD(E),   {'EXIT',{badarg,_}} = (catch << (E) || _ <- [1,2,3] >>)).
300-define(BAD_V(E), {'EXIT',{badarg,_}} = (catch << (E) || I <- [1,2,3] >>)).
301
302general_expressions(_) ->
303    <<1,2,3>> = << begin <<1,2,3>> end || _ <- [1] >>,
304    <<"abc">> = << begin <<"abc">> end || _ <- [1] >>,
305    <<1,2,3>> = << begin
306		       I = <<(I0+1)>>,
307		       id(I)
308		   end || <<I0>> <= <<0,1,2>> >>,
309    <<1,2,3>> = << I || I <- [<<1,2>>,<<3>>] >>,
310    <<1,2,3>> = << (id(<<I>>)) || I <- [1,2,3] >>,
311    <<2,4>> = << case I rem 2 of
312		     0 -> <<I>>;
313		     1 -> <<>>
314		 end || I <- [1,2,3,4,5] >>,
315    <<2,3,4,5,6,7>> = << << (id(<<J>>)) || J <- [2*I,2*I+1] >> ||
316			  I <- [1,2,3] >>,
317    <<1,2,2,3,4,4>> = << if
318			     I rem 2 =:= 0 -> <<I,I>>;
319			     true -> <<I>>
320			 end || I <- [1,2,3,4] >>,
321    self() ! <<42>>,
322    <<42>> = << receive B -> B end || _ <- [1] >>,
323    <<10,5,3>> = << try
324			<<(10 div I)>>
325		    catch _:_ ->
326			    <<>>
327		    end || I <- [0,1,2,3] >>,
328
329    %% Failing expressions.
330    ?BAD(bad_atom),
331    ?BAD(42),
332    ?BAD(42.0),
333    ?BAD_V({ok,I}),
334    ?BAD_V([I]),
335    ?BAD_V(fun() -> I end),
336
337    ok.
338
339-undef(BAD).
340
341matched_out_size(Config) when is_list(Config) ->
342    <<1, 2>> = matched_out_size_1(<<4, 1:4, 4, 2:4>>),
343    ok.
344
345matched_out_size_1(Binary) ->
346    << <<X>> || <<S, X:S>> <= Binary>>.
347
348cs_init() ->
349    erts_debug:set_internal_state(available_internal_state, true),
350    ok.
351
352cs_end() ->
353    erts_debug:set_internal_state(available_internal_state, false),
354    ok.
355
356%% Verify that the allocated size is exact (rounded up to the nearest byte).
357cs(Bin) ->
358    ByteSize = byte_size(Bin),
359    {refc_binary,ByteSize,{binary,ByteSize},_} =
360	erts_debug:get_internal_state({binary_info,Bin}),
361    Bin.
362
363%% Verify that the allocated size of the binary is the default size.
364cs_default(Bin) ->
365    ByteSize = byte_size(Bin),
366    {refc_binary,ByteSize,{binary,256},_} =
367	erts_debug:get_internal_state({binary_info,Bin}),
368    Bin.
369
370id(I) -> I.
371