From 49059dbd211987987a7e9d9ef40cc0b2484f829a Mon Sep 17 00:00:00 2001 From: Juan Jose Comellas Date: Mon, 9 Sep 2013 14:19:41 -0300 Subject: Fix incorrect values returned by integer_to_binary/2 When integer_to_binary/2 receives 0 or a negative number as an argument with a base that is different from 10, it will return incorrect values (<<>> in the case of 0) or it will crash (with negative numbers). This commit fixes these problems and adds tests to cover these cases. --- erts/emulator/test/num_bif_SUITE.erl | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'erts/emulator') diff --git a/erts/emulator/test/num_bif_SUITE.erl b/erts/emulator/test/num_bif_SUITE.erl index b92a0e2059..ff8d18eef8 100644 --- a/erts/emulator/test/num_bif_SUITE.erl +++ b/erts/emulator/test/num_bif_SUITE.erl @@ -350,12 +350,34 @@ t_integer_to_string(Config) when is_list(Config) -> (catch erlang:integer_to_list(Value)) end,[atom,1.2,0.0,[$1,[$2]]]), + %% Base-2 integers + test_its("0", 0, 2), + test_its("1", 1, 2), + test_its("110110", 54, 2), + test_its("-1000000", -64, 2), + %% Base-16 integers + test_its("0", 0, 16), + test_its("A", 10, 16), + test_its("D4BE", 54462, 16), + test_its("-D4BE", -54462, 16), + + lists:foreach(fun(Value) -> + {'EXIT', {badarg, _}} = + (catch erlang:integer_to_binary(Value, 8)), + {'EXIT', {badarg, _}} = + (catch erlang:integer_to_list(Value, 8)) + end,[atom,1.2,0.0,[$1,[$2]]]), + ok. test_its(List,Int) -> Int = list_to_integer(List), Int = binary_to_integer(list_to_binary(List)). +test_its(List,Int,Base) -> + Int = list_to_integer(List, Base), + Int = binary_to_integer(list_to_binary(List), Base). + %% Tests binary_to_integer/1. t_string_to_integer(Config) when is_list(Config) -> -- cgit v1.2.3