Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorng committed Nov 4, 2024
1 parent 5e527b8 commit 7ee650e
Showing 1 changed file with 36 additions and 4 deletions.
40 changes: 36 additions & 4 deletions lib/compiler/test/zlc_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
strict_list/1,strict_binary/1]).

-include_lib("common_test/include/ct.hrl").
-include_lib("stdlib/include/assert.hrl").

suite() ->
[{ct_hooks,[ts_install_cth]},
Expand Down Expand Up @@ -203,6 +204,8 @@ cartesian(Config) when is_list(Config) ->
ok.

strict_list(Config) when is_list(Config) ->
Seq100 = lists:seq(1, 100),

[2,3,4] = [X+Y || X <:- [1,2,3] && Y <- [1,1,1]],
[3,4] = [X+Y || X <:- [1,2,3] && Y <:- [1,1,1], X > 1],

Expand All @@ -218,13 +221,14 @@ strict_list(Config) when is_list(Config) ->
[15] = strict_list_mixed_2([{i,3}], #{{k,4} => {v,3}}),
[15] = strict_list_mixed_2([{i,0},{i,3}], #{{a,0} => {a,0},
{k,4} => {v,3}}),
SimpleMap = #{{k,1} => {v,2}},

%% How should a bad match of map generator be presented?
%% Maybe like this: {badmatches,{{a,3},{{k,1},{v,2}}}}
?assertEqual([I * 3*I + 7*I || I <- Seq100],
strict_list_mixed_2([{i,I} || I <- Seq100],
#{{k,3*I} => {v,7*I} || I <- Seq100})),

SimpleMap = #{{k,1} => {v,2}},
{'EXIT',{{bad_generators,{[{a,3}],{{k,1},{v,2},none}}},_}} =
catch strict_list_mixed_2([{a,3}], SimpleMap),

{'EXIT',{{bad_generators,{[],{{k,1},{v,2},none}}},_}} =
catch strict_list_mixed_2([], SimpleMap),

Expand Down Expand Up @@ -279,20 +283,48 @@ strict_list_strict_4(List, Bin) ->
[A || {i,A} <:- List && <<42:8>> <:= Bin].

strict_binary(Config) when is_list(Config) ->
Seq100 = lists:seq(1, 100),

<<2,4,6>> = << <<(X+Y)>> || X <:- [1,2,3] && <<Y>> <= <<1,2,3>>>>,
<<2,4>> = << <<(X+Y)>> || <<X>> <:= <<1,2,3>> && {X, Y} <- [{1,1},{2,2},{2,3}]>>,
<<2,24>> = << <<(X*Y*Z)>> || X := Y <:- #{1 => 2, 3 => 4} && <<Z>> <:= <<1,2>> >>,

<<>> = strict_binary_1(#{}, <<>>),
<<24:64>> = strict_binary_1(#{2 => {val,3}}, <<4:8>>),
?assertEqual(<< <<(5*I * 3*I * I):64>> || I <- Seq100 >>,
strict_binary_1(maps:iterator(#{5*I => {val,3*I} || I <- Seq100}, ordered),
list_to_binary(Seq100))),
{'EXIT',{{bad_generators,{none,<<42:8>>}},_}} = catch strict_binary_1(#{}, <<42:8>>),
{'EXIT',{{bad_generators,{none,<<42:7>>}},_}} = catch strict_binary_1(#{}, <<42:7>>),
{'EXIT',{{bad_generators,{none,<<0:4>>}},_}} = catch strict_binary_1(#{2 => {val,3}}, <<0,0:4>>),

<<>> = strict_binary_mixed_1(<<>>, #{}, #{}),
<<>> = strict_binary_mixed_1(<<1:2>>, #{}, #{}),
<<999:64>> = strict_binary_mixed_1(<<1:1>>, #{0 => {v,0}}, #{1 => {v,999}}),
?assertEqual(<< <<I:64>> || I <- Seq100>>,
strict_binary_mixed_1(<<0:100>>,
#{I => {v,I} || I <- Seq100},
#{I => {v,-I} || I <- Seq100})),
?assertEqual(<< <<-I:64>> || I <- Seq100>>,
strict_binary_mixed_1(<<-1:100>>,
#{I => {v,I} || I <- Seq100},
#{I => {v,-I} || I <- Seq100})),
{'EXIT',{{bad_generators,{<<0:1>>,{0,0,none},{0,{v,7},none}}},_}} =
catch strict_binary_mixed_1(<<0:1>>, #{0 => 0}, #{0 => {v,7}}),

ok.

strict_binary_1(Map, Bin) ->
<< <<(X*Y*Z):64>> || X := {val,Y} <:- Map && <<Z:8>> <:= Bin >>.

strict_binary_mixed_1(Bin, MapA0, MapB0) ->
MapA = maps:iterator(MapA0, ordered),
MapB = maps:iterator(MapB0, ordered),
<<case N of
0 -> <<V1:64>>;
1 -> <<V2:64>>
end || <<N:1>> <= Bin && _ := {v,V1} <:- MapA && _ := {v,V2} <- MapB>>.

nomatch(Config) when is_list(Config) ->
[] = do_nomatch_1([], []),
[] = do_nomatch_1([1], [a]),
Expand Down

0 comments on commit 7ee650e

Please sign in to comment.