diff options
Diffstat (limited to 'lib/compiler/test')
-rw-r--r-- | lib/compiler/test/Makefile | 7 | ||||
-rw-r--r-- | lib/compiler/test/beam_block_SUITE.erl | 27 | ||||
-rw-r--r-- | lib/compiler/test/beam_jump_SUITE.erl | 19 | ||||
-rw-r--r-- | lib/compiler/test/beam_validator_SUITE.erl | 25 | ||||
-rw-r--r-- | lib/compiler/test/bif_SUITE.erl | 45 | ||||
-rw-r--r-- | lib/compiler/test/bs_bincomp_SUITE.erl | 1 | ||||
-rw-r--r-- | lib/compiler/test/bs_utf_SUITE.erl | 1 | ||||
-rw-r--r-- | lib/compiler/test/compile_SUITE.erl | 9 | ||||
-rw-r--r-- | lib/compiler/test/compiler.cover | 2 | ||||
-rw-r--r-- | lib/compiler/test/core_fold_SUITE.erl | 16 | ||||
-rw-r--r-- | lib/compiler/test/float_SUITE.erl | 5 | ||||
-rw-r--r-- | lib/compiler/test/match_SUITE.erl | 10 | ||||
-rw-r--r-- | lib/compiler/test/num_bif_SUITE.erl | 285 | ||||
-rw-r--r-- | lib/compiler/test/overridden_bif_SUITE.erl | 101 | ||||
-rw-r--r-- | lib/compiler/test/test_lib.erl | 9 |
15 files changed, 256 insertions, 306 deletions
diff --git a/lib/compiler/test/Makefile b/lib/compiler/test/Makefile index f0185acbc7..de06e8760f 100644 --- a/lib/compiler/test/Makefile +++ b/lib/compiler/test/Makefile @@ -36,7 +36,7 @@ MODULES= \ map_SUITE \ match_SUITE \ misc_SUITE \ - num_bif_SUITE \ + overridden_bif_SUITE \ receive_SUITE \ record_SUITE \ regressions_SUITE \ @@ -67,7 +67,7 @@ NO_OPT= \ map \ match \ misc \ - num_bif \ + overridden_bif \ receive \ record \ trycatch @@ -78,6 +78,7 @@ INLINE= \ beam_block \ beam_bool \ beam_utils \ + bif \ bs_bincomp \ bs_bit_binaries \ bs_construct \ @@ -91,7 +92,7 @@ INLINE= \ map \ match \ misc \ - num_bif \ + overridden_bif \ receive \ record diff --git a/lib/compiler/test/beam_block_SUITE.erl b/lib/compiler/test/beam_block_SUITE.erl index 4bcb252833..9fcb6e497d 100644 --- a/lib/compiler/test/beam_block_SUITE.erl +++ b/lib/compiler/test/beam_block_SUITE.erl @@ -21,7 +21,8 @@ -export([all/0,suite/0,groups/0,init_per_suite/1,end_per_suite/1, init_per_group/2,end_per_group/2, - get_map_elements/1,otp_7345/1,move_opt_across_gc_bif/1]). + get_map_elements/1,otp_7345/1,move_opt_across_gc_bif/1, + erl_202/1]). %% The only test for the following functions is that %% the code compiles and is accepted by beam_validator. @@ -37,7 +38,8 @@ groups() -> [{p,[parallel], [get_map_elements, otp_7345, - move_opt_across_gc_bif + move_opt_across_gc_bif, + erl_202 ]}]. init_per_suite(Config) -> @@ -135,6 +137,27 @@ positive(speaking) -> paris([], P) -> P + 1. + +%% See https://bugs.erlang.org/browse/ERL-202. +%% Test that move_allocates/1 in beam_block doesn't move allocate +%% when it would not be safe. + +-record(erl_202_r1, {y}). +-record(erl_202_r2, {x}). + +erl_202(_Config) -> + Ref = make_ref(), + Ref = erl_202({{1,2},Ref}, 42), + + {Ref} = erl_202({7,8}, #erl_202_r1{y=#erl_202_r2{x=Ref}}), + + ok. + +erl_202({{_, _},X}, _) -> + X; +erl_202({_, _}, #erl_202_r1{y=R2}) -> + {R2#erl_202_r2.x}. + %%% %%% The only test of the following code is that it compiles. %%% diff --git a/lib/compiler/test/beam_jump_SUITE.erl b/lib/compiler/test/beam_jump_SUITE.erl index 0b13adaff2..088f63606c 100644 --- a/lib/compiler/test/beam_jump_SUITE.erl +++ b/lib/compiler/test/beam_jump_SUITE.erl @@ -21,7 +21,7 @@ -export([all/0,suite/0,groups/0,init_per_suite/1,end_per_suite/1, init_per_group/2,end_per_group/2, - undefined_label/1]). + undefined_label/1,ambiguous_catch_try_state/1]). suite() -> [{ct_hooks,[ts_install_cth]}]. @@ -32,7 +32,8 @@ all() -> groups() -> [{p,[parallel], - [undefined_label + [undefined_label, + ambiguous_catch_try_state ]}]. init_per_suite(Config) -> @@ -57,3 +58,17 @@ flights(0, [], []) when [], 0; 0.0, [], false -> clark; flights(_, Reproduction, introduction) when false, Reproduction -> responsible. + +%% [ERL-209] beam_jump would share 'catch' blocks, causing an +%% ambiguous_catch_try_state error in beam_validator. + +ambiguous_catch_try_state(_Config) -> + {{'EXIT',{{case_clause,song},_}},{'EXIT',{{case_clause,song},_}}} = + checks(42), + ok. + +river() -> song. + +checks(Wanted) -> + %% Must be one line to cause the unsafe optimization. + {catch case river() of sheet -> begin +Wanted, if "da" -> Wanted end end end, catch case river() of sheet -> begin + Wanted, if "da" -> Wanted end end end}. diff --git a/lib/compiler/test/beam_validator_SUITE.erl b/lib/compiler/test/beam_validator_SUITE.erl index 263fd2ca7e..ca85eef688 100644 --- a/lib/compiler/test/beam_validator_SUITE.erl +++ b/lib/compiler/test/beam_validator_SUITE.erl @@ -32,7 +32,8 @@ bad_bin_match/1,bad_dsetel/1, state_after_fault_in_catch/1,no_exception_in_catch/1, undef_label/1,illegal_instruction/1,failing_gc_guard_bif/1, - map_field_lists/1,cover_bin_opt/1]). + map_field_lists/1,cover_bin_opt/1, + val_dsetel/1]). -include_lib("common_test/include/ct.hrl"). @@ -60,7 +61,7 @@ groups() -> freg_state,bad_bin_match,bad_dsetel, state_after_fault_in_catch,no_exception_in_catch, undef_label,illegal_instruction,failing_gc_guard_bif, - map_field_lists,cover_bin_opt]}]. + map_field_lists,cover_bin_opt,val_dsetel]}]. init_per_suite(Config) -> Config. @@ -546,3 +547,23 @@ beam_val(M) -> _ = [io:put_chars(beam_validator:format_error(E)) || E <- Errors], Errors. + +%%%------------------------------------------------------------------------- + +val_dsetel(_Config) -> + self() ! 13, + {'EXIT',{{try_clause,participating},_}} = (catch night(0)), + ok. + +night(Turned) -> + receive + 13 -> + try participating of engine -> 16 after false end + end, + %% The setelement/3 call is unreachable. + Turned(setelement(#{true => Turned}, + participating(Turned, "suit", 40, []), + Turned < Turned)), + ok. + +participating(_, _, _, _) -> ok. diff --git a/lib/compiler/test/bif_SUITE.erl b/lib/compiler/test/bif_SUITE.erl index 51bc71da81..6bde2f1da9 100644 --- a/lib/compiler/test/bif_SUITE.erl +++ b/lib/compiler/test/bif_SUITE.erl @@ -19,9 +19,11 @@ %% -module(bif_SUITE). +-include_lib("syntax_tools/include/merl.hrl"). + -export([all/0,suite/0,groups/0,init_per_suite/1,end_per_suite/1, init_per_group/2,end_per_group/2, - beam_validator/1]). + beam_validator/1,trunc_and_friends/1]). suite() -> [{ct_hooks,[ts_install_cth]}]. @@ -32,7 +34,8 @@ all() -> groups() -> [{p,[parallel], - [beam_validator + [beam_validator, + trunc_and_friends ]}]. init_per_suite(Config) -> @@ -63,3 +66,41 @@ food(Curriculum) -> catch _ -> 0 end, Curriculum]. + +%% Test trunc/1, round/1, floor/1, ceil/1. +trunc_and_friends(_Config) -> + Bifs = [trunc,round,floor,ceil], + Fs = trunc_and_friends_1(Bifs), + Mod = ?FUNCTION_NAME, + Calls = [begin + Atom = erl_syntax:function_name(N), + ?Q("'@Atom'()") + end || N <- Fs], + Tree = ?Q(["-module('@Mod@').", + "-export([test/0]).", + "test() -> _@Calls, ok.", + "id(I) -> I."]) ++ Fs, + merl:print(Tree), + Opts = test_lib:opt_opts(?MODULE), + {ok,_Bin} = merl:compile_and_load(Tree, Opts), + Mod:test(), + ok. + +trunc_and_friends_1([F|Fs]) -> + Func = list_to_atom("f"++integer_to_list(length(Fs))), + [trunc_template(Func, F)|trunc_and_friends_1(Fs)]; +trunc_and_friends_1([]) -> []. + +trunc_template(Func, Bif) -> + Val = 42.77, + Res = erlang:Bif(Val), + FloatRes = float(Res), + ?Q("'@Func@'() -> + Var = id(_@Val@), + if _@Bif@(Var) =:= _@Res@ -> ok end, + if _@Bif@(Var) == _@FloatRes@ -> ok end, + if _@Bif@(Var) == _@Res@ -> ok end, + _@Res@ = _@Bif@(Var), + try begin _@Bif@(a), ok end + catch error:badarg -> ok end, + ok."). diff --git a/lib/compiler/test/bs_bincomp_SUITE.erl b/lib/compiler/test/bs_bincomp_SUITE.erl index 4743821337..dd1d245f88 100644 --- a/lib/compiler/test/bs_bincomp_SUITE.erl +++ b/lib/compiler/test/bs_bincomp_SUITE.erl @@ -56,6 +56,7 @@ end_per_group(_GroupName, Config) -> byte_aligned(Config) when is_list(Config) -> cs_init(), <<"abcdefg">> = cs(<< <<(X+32)>> || <<X>> <= <<"ABCDEFG">> >>), + <<"AxyzBxyzCxyz">> = cs(<< <<X, "xyz">> || <<X>> <= <<"ABC">> >>), <<1:32/little,2:32/little,3:32/little,4:32/little>> = cs(<< <<X:32/little>> || <<X:32>> <= <<1:32,2:32,3:32,4:32>> >>), cs(<<1:32/little,2:32/little,3:32/little,4:32/little>> = diff --git a/lib/compiler/test/bs_utf_SUITE.erl b/lib/compiler/test/bs_utf_SUITE.erl index c894041f72..ef3fc54b37 100644 --- a/lib/compiler/test/bs_utf_SUITE.erl +++ b/lib/compiler/test/bs_utf_SUITE.erl @@ -235,6 +235,7 @@ utf32_to_unicode(<<>>) -> []. literals(Config) when is_list(Config) -> abc_utf8 = match_literal(<<"abc"/utf8>>), abc_utf8 = match_literal(<<$a,$b,$c>>), + abc_utf8 = match_literal(<<$a/utf8,$b/utf8,$c/utf8>>), abc_utf16be = match_literal(<<"abc"/utf16>>), abc_utf16be = match_literal(<<$a:16,$b:16,$c:16>>), diff --git a/lib/compiler/test/compile_SUITE.erl b/lib/compiler/test/compile_SUITE.erl index b0148f7103..28a353d27b 100644 --- a/lib/compiler/test/compile_SUITE.erl +++ b/lib/compiler/test/compile_SUITE.erl @@ -403,12 +403,11 @@ other_output(Config) when is_list(Config) -> end], io:put_chars("to_exp (file)"), - {ok,simple,Expand} = compile:file(Simple, [to_exp,binary,time]), - case Expand of - {simple,Exports,Forms} when is_list(Exports), is_list(Forms) -> ok - end, + {ok,[],Expand} = compile:file(Simple, [to_exp,binary,time]), + true = is_list(Expand), + {attribute,_,module,simple} = lists:keyfind(module, 3, Expand), io:put_chars("to_exp (forms)"), - {ok,simple,Expand} = compile:forms(PP, [to_exp,binary,time]), + {ok,[],Expand} = compile:forms(PP, [to_exp,binary,time]), io:put_chars("to_core (file)"), {ok,simple,Core} = compile:file(Simple, [to_core,binary,time]), diff --git a/lib/compiler/test/compiler.cover b/lib/compiler/test/compiler.cover index 3fd7fc1937..2be079944f 100644 --- a/lib/compiler/test/compiler.cover +++ b/lib/compiler/test/compiler.cover @@ -1,5 +1,5 @@ {incl_app,compiler,details}. %% -*- erlang -*- -{excl_mods,compiler,[core_scan,core_parse]}. +{excl_mods,compiler,[core_scan,core_parse,sys_pre_expand]}. diff --git a/lib/compiler/test/core_fold_SUITE.erl b/lib/compiler/test/core_fold_SUITE.erl index 376d2c8e9a..ced0e39d06 100644 --- a/lib/compiler/test/core_fold_SUITE.erl +++ b/lib/compiler/test/core_fold_SUITE.erl @@ -26,7 +26,7 @@ unused_multiple_values_error/1,unused_multiple_values/1, multiple_aliases/1,redundant_boolean_clauses/1, mixed_matching_clauses/1,unnecessary_building/1, - no_no_file/1]). + no_no_file/1,configuration/1]). -export([foo/0,foo/1,foo/2,foo/3]). @@ -45,7 +45,7 @@ groups() -> unused_multiple_values_error,unused_multiple_values, multiple_aliases,redundant_boolean_clauses, mixed_matching_clauses,unnecessary_building, - no_no_file]}]. + no_no_file,configuration]}]. init_per_suite(Config) -> @@ -499,4 +499,16 @@ experiment() -> end, ok. + +%% Make sure we don't try to move a fun into a guard. +configuration(_Config) -> + {'EXIT',_} = (catch configuration()), + ok. + +configuration() -> + [forgotten || Components <- enemy, is_tuple(fun art/0)]. + +art() -> + creating. + id(I) -> I. diff --git a/lib/compiler/test/float_SUITE.erl b/lib/compiler/test/float_SUITE.erl index f6095947ca..0ebc71eb9b 100644 --- a/lib/compiler/test/float_SUITE.erl +++ b/lib/compiler/test/float_SUITE.erl @@ -150,6 +150,11 @@ math_functions(Config) when is_list(Config) -> ?OPTIONAL(0.0, math:erf(id(0))), ?OPTIONAL(1.0, math:erfc(id(0))), + 5.0 = math:floor(5.6), + 6.0 = math:ceil(5.6), + 5.0 = math:floor(id(5.4)), + 6.0 = math:ceil(id(5.4)), + %% Only for coverage (of beam_type.erl). {'EXIT',{undef,_}} = (catch math:fnurfla(0)), {'EXIT',{undef,_}} = (catch math:fnurfla(0, 0)), diff --git a/lib/compiler/test/match_SUITE.erl b/lib/compiler/test/match_SUITE.erl index 127679ba69..52b2da05f7 100644 --- a/lib/compiler/test/match_SUITE.erl +++ b/lib/compiler/test/match_SUITE.erl @@ -576,7 +576,15 @@ grab_bag_remove_failure([{stretch,_,Mi}=Stretch | Specs], Unit, _MaxFailure) -> %% Regression in 19.0, reported by Alexei Sholik literal_binary(_Config) -> - 3 = literal_binary_match(bar,<<"y">>), + 3 = literal_binary_match(bar, <<"y">>), + + %% While we are at it, also test the remaining code paths + %% in literal_binary_match/2. + 1 = literal_binary_match(bar, <<"x">>), + 2 = literal_binary_match(foo, <<"x">>), + 3 = literal_binary_match(foo, <<"y">>), + fail = literal_binary_match(bar, <<"z">>), + fail = literal_binary_match(foo, <<"z">>), ok. literal_binary_match(bar, <<"x">>) -> 1; diff --git a/lib/compiler/test/num_bif_SUITE.erl b/lib/compiler/test/num_bif_SUITE.erl deleted file mode 100644 index 7eac90bac3..0000000000 --- a/lib/compiler/test/num_bif_SUITE.erl +++ /dev/null @@ -1,285 +0,0 @@ -%% -%% %CopyrightBegin% -%% -%% Copyright Ericsson AB 2004-2016. All Rights Reserved. -%% -%% Licensed under the Apache License, Version 2.0 (the "License"); -%% you may not use this file except in compliance with the License. -%% You may obtain a copy of the License at -%% -%% http://www.apache.org/licenses/LICENSE-2.0 -%% -%% Unless required by applicable law or agreed to in writing, software -%% distributed under the License is distributed on an "AS IS" BASIS, -%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -%% See the License for the specific language governing permissions and -%% limitations under the License. -%% -%% %CopyrightEnd% -%% --module(num_bif_SUITE). - --include_lib("common_test/include/ct.hrl"). - -%% Tests optimization of the BIFs: -%% abs/1 -%% float/1 -%% float_to_list/1 -%% integer_to_list/1 -%% list_to_float/1 -%% list_to_integer/1 -%% round/1 -%% trunc/1 - --export([all/0, suite/0,groups/0,init_per_suite/1, end_per_suite/1, - init_per_group/2,end_per_group/2, t_abs/1, t_float/1, - t_float_to_list/1, t_integer_to_list/1, - t_list_to_integer/1, - t_list_to_float_safe/1, t_list_to_float_risky/1, - t_round/1, t_trunc/1]). - -suite() -> [{ct_hooks,[ts_install_cth]}]. - -all() -> - test_lib:recompile(?MODULE), - [t_abs, t_float, t_float_to_list, t_integer_to_list, - {group, t_list_to_float}, t_list_to_integer, t_round, - t_trunc]. - -groups() -> - [{t_list_to_float, [], - [t_list_to_float_safe, t_list_to_float_risky]}]. - -init_per_suite(Config) -> - Config. - -end_per_suite(_Config) -> - ok. - -init_per_group(_GroupName, Config) -> - Config. - -end_per_group(_GroupName, Config) -> - Config. - - -t_abs(Config) when is_list(Config) -> - %% Floats. - 5.5 = abs(5.5), - 0.0 = abs(0.0), - 100.0 = abs(-100.0), - - %% Integers. - 5 = abs(5), - 0 = abs(0), - 100 = abs(-100), - - %% The largest smallnum. OTP-3190. - X = (1 bsl 27) - 1, - X = abs(X), - X = abs(X-1)+1, - X = abs(X+1)-1, - X = abs(-X), - X = abs(-X-1)-1, - X = abs(-X+1)+1, - - %% Bignums. - BigNum = 13984792374983749, - BigNum = abs(BigNum), - BigNum = abs(-BigNum), - ok. - -t_float(Config) when is_list(Config) -> - 0.0 = float(0), - 2.5 = float(2.5), - 0.0 = float(0.0), - -100.55 = float(-100.55), - 42.0 = float(42), - -100.0 = float(-100), - - %% Bignums. - 4294967305.0 = float(4294967305), - -4294967305.0 = float(-4294967305), - - %% Extremly big bignums. - Big = list_to_integer(lists:duplicate(2000, $1)), - {'EXIT', {badarg, _}} = (catch float(Big)), - - %% Invalid types and lists. - {'EXIT', {badarg, _}} = (catch list_to_integer(atom)), - {'EXIT', {badarg, _}} = (catch list_to_integer(123)), - {'EXIT', {badarg, _}} = (catch list_to_integer([$1, [$2]])), - {'EXIT', {badarg, _}} = (catch list_to_integer("1.2")), - {'EXIT', {badarg, _}} = (catch list_to_integer("a")), - {'EXIT', {badarg, _}} = (catch list_to_integer("")), - ok. - - -%% Tests float_to_list/1. - -t_float_to_list(Config) when is_list(Config) -> - test_ftl("0.0e+0", 0.0), - test_ftl("2.5e+1", 25.0), - test_ftl("2.5e+0", 2.5), - test_ftl("2.5e-1", 0.25), - test_ftl("-3.5e+17", -350.0e15), - ok. - -test_ftl(Expect, Float) -> - %% No on the next line -- we want the line number from t_float_to_list. - Expect = remove_zeros(lists:reverse(float_to_list(Float)), []). - -%% Removes any non-significant zeros in a floating point number. -%% Example: 2.500000e+01 -> 2.5e+1 - -remove_zeros([$+, $e|Rest], [$0, X|Result]) -> - remove_zeros([$+, $e|Rest], [X|Result]); -remove_zeros([$-, $e|Rest], [$0, X|Result]) -> - remove_zeros([$-, $e|Rest], [X|Result]); -remove_zeros([$0, $.|Rest], [$e|Result]) -> - remove_zeros(Rest, [$., $0, $e|Result]); -remove_zeros([$0|Rest], [$e|Result]) -> - remove_zeros(Rest, [$e|Result]); -remove_zeros([Char|Rest], Result) -> - remove_zeros(Rest, [Char|Result]); -remove_zeros([], Result) -> - Result. - -%% Tests integer_to_list/1. - -t_integer_to_list(Config) when is_list(Config) -> - "0" = integer_to_list(0), - "42" = integer_to_list(42), - "-42" = integer_to_list(-42), - "-42" = integer_to_list(-42), - "32768" = integer_to_list(32768), - "268435455" = integer_to_list(268435455), - "-268435455" = integer_to_list(-268435455), - "123456932798748738738" = integer_to_list(123456932798748738738), - Big_List = lists:duplicate(2000, $1), - Big = list_to_integer(Big_List), - Big_List = integer_to_list(Big), - ok. - -%% Tests list_to_float/1. - - -t_list_to_float_safe(Config) when is_list(Config) -> - 0.0 = list_to_float("0.0"), - 0.0 = list_to_float("-0.0"), - 0.5 = list_to_float("0.5"), - -0.5 = list_to_float("-0.5"), - 100.0 = list_to_float("1.0e2"), - 127.5 = list_to_float("127.5"), - -199.5 = list_to_float("-199.5"), - - {'EXIT', {badarg, _}} = (catch list_to_float("0")), - {'EXIT', {badarg, _}} = (catch list_to_float("0..0")), - {'EXIT', {badarg, _}} = (catch list_to_float("0e12")), - {'EXIT', {badarg, _}} = (catch list_to_float("--0.0")), -%% {'EXIT', {badarg, _}} = (catch list_to_float("0.0e+99999999")), - - ok. - -%% This might crash the emulator... -%% (Known to crash the Unix version of Erlang 4.4.1) - -t_list_to_float_risky(Config) when is_list(Config) -> - Many_Ones = lists:duplicate(25000, $1), - _ = list_to_float("2."++Many_Ones), - {'EXIT', {badarg, _}} = (catch list_to_float("2"++Many_Ones)), - ok. - -%% Tests list_to_integer/1. - -t_list_to_integer(Config) when is_list(Config) -> - 0 = list_to_integer("0"), - 0 = list_to_integer("00"), - 0 = list_to_integer("-0"), - 1 = list_to_integer("1"), - -1 = list_to_integer("-1"), - 42 = list_to_integer("42"), - -12 = list_to_integer("-12"), - 32768 = list_to_integer("32768"), - 268435455 = list_to_integer("268435455"), - -268435455 = list_to_integer("-268435455"), - - %% Bignums. - 123456932798748738738 = list_to_integer("123456932798748738738"), - _ = list_to_integer(lists:duplicate(2000, $1)), - ok. - -%% Tests round/1. - -t_round(Config) when is_list(Config) -> - 0 = round(0.0), - 0 = round(0.4), - 1 = round(0.5), - 0 = round(-0.4), - -1 = round(-0.5), - 255 = round(255.3), - 256 = round(255.6), - -1033 = round(-1033.3), - -1034 = round(-1033.6), - - % OTP-3722: - X = (1 bsl 27) - 1, - MX = -X, - MXm1 = -X-1, - MXp1 = -X+1, - F = X + 0.0, - X = round(F), - X = round(F+1)-1, - X = round(F-1)+1, - MX = round(-F), - MXm1 = round(-F-1), - MXp1 = round(-F+1), - - X = round(F+0.1), - X = round(F+1+0.1)-1, - X = round(F-1+0.1)+1, - MX = round(-F+0.1), - MXm1 = round(-F-1+0.1), - MXp1 = round(-F+1+0.1), - - X = round(F-0.1), - X = round(F+1-0.1)-1, - X = round(F-1-0.1)+1, - MX = round(-F-0.1), - MXm1 = round(-F-1-0.1), - MXp1 = round(-F+1-0.1), - - 0.5 = abs(round(F+0.5)-(F+0.5)), - 0.5 = abs(round(F-0.5)-(F-0.5)), - 0.5 = abs(round(-F-0.5)-(-F-0.5)), - 0.5 = abs(round(-F+0.5)-(-F+0.5)), - - %% Bignums. - 4294967296 = round(4294967296.1), - 4294967297 = round(4294967296.9), - -4294967296 = -round(4294967296.1), - -4294967297 = -round(4294967296.9), - ok. - -t_trunc(Config) when is_list(Config) -> - 0 = trunc(0.0), - 5 = trunc(5.3333), - -10 = trunc(-10.978987), - % The largest smallnum, converted to float (OTP-3722): - X = (1 bsl 27) - 1, - F = X + 0.0, - io:format("X = ~p/~w/~w, F = ~p/~w/~w, trunc(F) = ~p/~w/~w~n", - [X, X, binary_to_list(term_to_binary(X)), - F, F, binary_to_list(term_to_binary(F)), - trunc(F), trunc(F), binary_to_list(term_to_binary(trunc(F)))]), - X = trunc(F), - X = trunc(F+1)-1, - X = trunc(F-1)+1, - X = -trunc(-F), - X = -trunc(-F-1)-1, - X = -trunc(-F+1)+1, - - %% Bignums. - 4294967305 = trunc(4294967305.7), - -4294967305 = trunc(-4294967305.7), - ok. diff --git a/lib/compiler/test/overridden_bif_SUITE.erl b/lib/compiler/test/overridden_bif_SUITE.erl new file mode 100644 index 0000000000..ce18916515 --- /dev/null +++ b/lib/compiler/test/overridden_bif_SUITE.erl @@ -0,0 +1,101 @@ +%% +%% %CopyrightBegin% +%% +%% Copyright Ericsson AB 2001-2016. All Rights Reserved. +%% +%% Licensed under the Apache License, Version 2.0 (the "License"); +%% you may not use this file except in compliance with the License. +%% You may obtain a copy of the License at +%% +%% http://www.apache.org/licenses/LICENSE-2.0 +%% +%% Unless required by applicable law or agreed to in writing, software +%% distributed under the License is distributed on an "AS IS" BASIS, +%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +%% See the License for the specific language governing permissions and +%% limitations under the License. +%% +%% %CopyrightEnd% +%% +-module(overridden_bif_SUITE). +-compile({no_auto_import,[is_reference/1,size/1]}). + +-export([all/0, suite/0,groups/0,init_per_suite/1, end_per_suite/1, + init_per_group/2,end_per_group/2, + init_per_testcase/2,end_per_testcase/2, + overridden_bif/1]). + +-include_lib("common_test/include/ct.hrl"). + +%% Used by overridden_bif/1. +-import(gb_sets, [size/1]). +-import(test_lib, [binary/1]). + +suite() -> + [{ct_hooks,[ts_install_cth]}, + {timetrap,{minutes,1}}]. + +all() -> + test_lib:recompile(?MODULE), + [{group,p}]. + +groups() -> + [{p,test_lib:parallel(), + [overridden_bif + ]}]. + +init_per_suite(Config) -> + Config. + +end_per_suite(_Config) -> + ok. + +init_per_group(_GroupName, Config) -> + Config. + +end_per_group(_GroupName, Config) -> + Config. + + +init_per_testcase(Case, Config) when is_atom(Case), is_list(Config) -> + Config. + +end_per_testcase(Case, Config) when is_atom(Case), is_list(Config) -> + ok. + +overridden_bif(_Config) -> + L = [-3,-2,-1,0,1,2,3,4], + [-3,0,3] = do_overridden_bif_1(L), + [-2,0,2,4] = do_overridden_bif_2(L), + [3] = do_overridden_bif_3(L), + [2,4] = do_overridden_bif_4(L), + + Set = gb_sets:from_list(L), + [Set] = do_overridden_bif_5([gb_sets:singleton(42),Set]), + + [100,0] = do_overridden_bif_6([100|L]), + ok. + +do_overridden_bif_1(L) -> + [E || E <- L, is_reference(E)]. + +do_overridden_bif_2(L) -> + [E || E <- L, port(E)]. + +do_overridden_bif_3(L) -> + [E || E <- L, (is_reference(E) andalso E > 0)]. + +do_overridden_bif_4(L) -> + [E || E <- L, (port(E) andalso E > 0)]. + +do_overridden_bif_5(L) -> + [E || E <- L, size(E) > 1]. + +do_overridden_bif_6(L) -> + [E || E <- L, binary(E)]. + +is_reference(N) -> + N rem 3 =:= 0. + +port(N) -> + N rem 2 =:= 0. diff --git a/lib/compiler/test/test_lib.erl b/lib/compiler/test/test_lib.erl index d5b79e2357..8954a9f5fb 100644 --- a/lib/compiler/test/test_lib.erl +++ b/lib/compiler/test/test_lib.erl @@ -22,7 +22,10 @@ -include_lib("common_test/include/ct.hrl"). -compile({no_auto_import,[binary_part/2]}). -export([id/1,recompile/1,parallel/0,uniq/0,opt_opts/1,get_data_dir/1, - is_cloned_mod/1,smoke_disasm/1,p_run/2,binary_part/2]). + is_cloned_mod/1,smoke_disasm/1,p_run/2]). + +%% Used by test case that override BIFs. +-export([binary_part/2,binary/1]). id(I) -> I. @@ -151,3 +154,7 @@ p_run_loop(Test, List, N, Refs0, Errors0, Ws0) -> %% This is for the misc_SUITE:override_bif testcase binary_part(_A,_B) -> dummy. + +%% This is for overridden_bif_SUITE. +binary(N) -> + N rem 10 =:= 0. |