aboutsummaryrefslogtreecommitdiffstats
path: root/lib/stdlib
diff options
context:
space:
mode:
authorPatrik Nyblom <[email protected]>2010-04-29 20:06:55 +0200
committerBjörn Gustavsson <[email protected]>2010-05-17 15:51:50 +0200
commit1dad48ee9f2e1aba6a0ec69d9cf688705d6f187c (patch)
treed846c1e74473c6ee9504cb563cd8a1b3a06ce3fb /lib/stdlib
parent02af6209ed16b8588b76e8c1972000eee12ba225 (diff)
downloadotp-1dad48ee9f2e1aba6a0ec69d9cf688705d6f187c.tar.gz
otp-1dad48ee9f2e1aba6a0ec69d9cf688705d6f187c.tar.bz2
otp-1dad48ee9f2e1aba6a0ec69d9cf688705d6f187c.zip
Add binary:list_to_bin/1 and binary:copy/1,2
Add testcases for binary:list_to_bin/1 and binary:copy/1,2. Add reference implementation of list_to_bin/1.
Diffstat (limited to 'lib/stdlib')
-rw-r--r--lib/stdlib/src/binary.erl2
-rw-r--r--lib/stdlib/test/binary_module_SUITE.erl63
-rw-r--r--lib/stdlib/test/binref.erl13
3 files changed, 73 insertions, 5 deletions
diff --git a/lib/stdlib/src/binary.erl b/lib/stdlib/src/binary.erl
index ff10a24f66..61b7aacf49 100644
--- a/lib/stdlib/src/binary.erl
+++ b/lib/stdlib/src/binary.erl
@@ -29,9 +29,9 @@
%% binary:at/2
%% binary:part/{2,3}
%% binary:bin_to_list/{1,2,3}
-%% - Not yet:
%% binary:list_to_bin/1
%% binary:copy/{1,2}
+%% - Not yet:
%% binary:referenced_byte_size/1
%% binary:decode_unsigned/{1,2}
%%
diff --git a/lib/stdlib/test/binary_module_SUITE.erl b/lib/stdlib/test/binary_module_SUITE.erl
index 00fb87b76f..1c386b049c 100644
--- a/lib/stdlib/test/binary_module_SUITE.erl
+++ b/lib/stdlib/test/binary_module_SUITE.erl
@@ -1,7 +1,9 @@
-module(binary_module_SUITE).
-export([all/1, interesting/1,random_ref_comp/1,random_ref_sr_comp/1,
- random_ref_fla_comp/1,parts/1, bin_to_list/1]).
+ random_ref_fla_comp/1,parts/1, bin_to_list/1, list_to_bin/1, copy/1]).
+
+-export([random_copy/1]).
-define(STANDALONE,1).
@@ -26,7 +28,7 @@ run() ->
-endif.
all(suite) -> [interesting,random_ref_fla_comp,random_ref_sr_comp,
- random_ref_comp,parts,bin_to_list].
+ random_ref_comp,parts,bin_to_list, list_to_bin, copy].
-define(MASK_ERROR(EXPR),mask_error((catch (EXPR)))).
@@ -290,6 +292,62 @@ do_interesting(Module) ->
?line [1,2,3] = ?MASK_ERROR(Module:bin_to_list(<<1,2,3>>,3,-3)),
ok.
+list_to_bin(doc) ->
+ ["Test list_to_bin/1 bif"];
+list_to_bin(Config) when is_list(Config) ->
+ %% Just some smoke_tests first, then go nuts with random cases
+ ?line badarg = ?MASK_ERROR(binary:list_to_bin({})),
+ ?line badarg = ?MASK_ERROR(binary:list_to_bin(apa)),
+ ?line badarg = ?MASK_ERROR(binary:list_to_bin(<<"apa">>)),
+ F1 = fun(L) ->
+ ?MASK_ERROR(binref:list_to_bin(L))
+ end,
+ F2 = fun(L) ->
+ ?MASK_ERROR(binary:list_to_bin(L))
+ end,
+ ?line random_iolist:run(1000,F1,F2),
+ ok.
+
+copy(doc) ->
+ ["Test copy/1,2 bif's"];
+copy(Config) when is_list(Config) ->
+ ?line <<1,2,3>> = binary:copy(<<1,2,3>>),
+ ?line RS = random_string({1,10000}),
+ ?line RS = RS2 = binary:copy(RS),
+ ?line false = erts_debug:same(RS,RS2),
+ ?line badarg = ?MASK_ERROR(binary:copy(<<1,2,3>>,0)),
+ ?line badarg = ?MASK_ERROR(binary:copy(<<>>,0)),
+ ?line <<>> = binary:copy(<<>>,10000),
+ ?line random:seed({1271,769940,559934}),
+ ?line ok = random_copy(3000),
+ ?line erts_debug:set_internal_state(available_internal_state,true),
+ ?line io:format("oldlimit: ~p~n",
+ [erts_debug:set_internal_state(binary_loop_limit,10)]),
+ ?line ok = random_copy(1000),
+ ?line io:format("limit was: ~p~n",
+ [erts_debug:set_internal_state(binary_loop_limit,
+ default)]),
+ ?line erts_debug:set_internal_state(available_internal_state,false),
+ ok.
+
+random_copy(0) ->
+ ok;
+random_copy(N) ->
+ Str = random_string({0,N}),
+ Num = random:uniform(N div 10+1),
+ A = ?MASK_ERROR(binary:copy(Str,Num)),
+ B = ?MASK_ERROR(binref:copy(Str,Num)),
+ C = ?MASK_ERROR(binary:copy(make_unaligned(Str),Num)),
+ case {(A =:= B), (B =:= C)} of
+ {true,true} ->
+ random_copy(N-1);
+ _ ->
+ io:format("Failed to pick copy ~s ~p times~n",
+ [Str,Num]),
+ io:format("A:~p,~nB:~p,~n,C:~p.~n",
+ [A,B,C]),
+ exit(mismatch)
+ end.
bin_to_list(doc) ->
["Test bin_to_list/1,2,3 bif's"];
@@ -426,7 +484,6 @@ random_ref_comp(Config) when is_list(Config) ->
?line do_random_matches_comp3(5,{1,40},{30,1000}),
?line io:format("limit was: ~p~n",[ erts_debug:set_internal_state(binary_loop_limit,default)]),
?line erts_debug:set_internal_state(available_internal_state,false),
- ?line put(success_counter,0),
ok.
random_ref_sr_comp(doc) ->
diff --git a/lib/stdlib/test/binref.erl b/lib/stdlib/test/binref.erl
index e09a0f2743..2322d036a2 100644
--- a/lib/stdlib/test/binref.erl
+++ b/lib/stdlib/test/binref.erl
@@ -4,7 +4,8 @@
split/2,split/3,replace/3,replace/4,first/1,last/1,at/2,
part/2,part/3,copy/1,copy/2,encode_unsigned/1,encode_unsigned/2,
decode_unsigned/1,decode_unsigned/2,referenced_byte_size/1,
- longest_common_prefix/1,longest_common_suffix/1,bin_to_list/1, bin_to_list/2, bin_to_list/3 ]).
+ longest_common_prefix/1,longest_common_suffix/1,bin_to_list/1,
+ bin_to_list/2,bin_to_list/3,list_to_bin/1]).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -351,6 +352,16 @@ bin_to_list(Subject,A,B) ->
_:_ ->
erlang:error(badarg)
end.
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% list_to_bin
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+list_to_bin(List) ->
+ try
+ erlang:list_to_binary(List)
+ catch
+ _:_ ->
+ erlang:error(badarg)
+ end.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% longest_common_prefix