From ccf8728a95af72aa4847e0844d45dcda991ea435 Mon Sep 17 00:00:00 2001 From: Patrik Nyblom Date: Fri, 30 Apr 2010 18:17:17 +0200 Subject: Add binary:{encode,decode}_unsigned({1,2} Add testcases for encode/decode_unsigned/1,2. --- lib/stdlib/src/binary.erl | 2 +- lib/stdlib/test/binary_module_SUITE.erl | 84 +++++++++++++++++++++++++++++++-- lib/stdlib/test/binref.erl | 2 +- 3 files changed, 83 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/stdlib/src/binary.erl b/lib/stdlib/src/binary.erl index 61b7aacf49..0a66559602 100644 --- a/lib/stdlib/src/binary.erl +++ b/lib/stdlib/src/binary.erl @@ -31,9 +31,9 @@ %% binary:bin_to_list/{1,2,3} %% binary:list_to_bin/1 %% binary:copy/{1,2} -%% - Not yet: %% binary:referenced_byte_size/1 %% binary:decode_unsigned/{1,2} +%% - Not yet: %% %% Implemented in this module: -export([split/2,split/3,replace/3,replace/4]). diff --git a/lib/stdlib/test/binary_module_SUITE.erl b/lib/stdlib/test/binary_module_SUITE.erl index 9126d25e48..c51c70cfa8 100644 --- a/lib/stdlib/test/binary_module_SUITE.erl +++ b/lib/stdlib/test/binary_module_SUITE.erl @@ -2,9 +2,13 @@ -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, list_to_bin/1, - copy/1, referenced/1]). + copy/1, referenced/1,encode_decode/1]). --define(STANDALONE,1). +-export([random_number/1, make_unaligned/1]). + + + +%%-define(STANDALONE,1). -ifdef(STANDALONE). @@ -28,7 +32,7 @@ run() -> all(suite) -> [interesting,random_ref_fla_comp,random_ref_sr_comp, random_ref_comp,parts,bin_to_list, list_to_bin, copy, - referenced]. + referenced,encode_decode]. -define(MASK_ERROR(EXPR),mask_error((catch (EXPR)))). @@ -291,8 +295,72 @@ do_interesting(Module) -> ?line badarg = ?MASK_ERROR(Module:bin_to_list(<<1,2,3>>,3,-4)), ?line [1,2,3] = ?MASK_ERROR(Module:bin_to_list(<<1,2,3>>,3,-3)), + ?line badarg = ?MASK_ERROR(Module:decode_unsigned(<<1,2,1:2>>,big)), + ?line badarg = ?MASK_ERROR(Module:decode_unsigned(<<1,2,1:2>>,little)), + ?line badarg = ?MASK_ERROR(Module:decode_unsigned(apa)), + ?line badarg = ?MASK_ERROR(Module:decode_unsigned(125,little)), + ?line 0 = ?MASK_ERROR(Module:decode_unsigned(<<>>,little)), + ?line 0 = ?MASK_ERROR(Module:decode_unsigned(<<>>,big)), + ?line 0 = ?MASK_ERROR(Module:decode_unsigned(<<0>>,little)), + ?line 0 = ?MASK_ERROR(Module:decode_unsigned(<<0>>,big)), + ?line 0 = ?MASK_ERROR(Module:decode_unsigned(make_unaligned(<<0>>), + little)), + ?line 0 = ?MASK_ERROR(Module:decode_unsigned(make_unaligned(<<0>>),big)), + ?line badarg = ?MASK_ERROR(Module:encode_unsigned(apa)), + ?line badarg = ?MASK_ERROR(Module:encode_unsigned(125.3,little)), + ?line badarg = ?MASK_ERROR(Module:encode_unsigned({1},little)), + ?line badarg = ?MASK_ERROR(Module:encode_unsigned([1],little)), + ?line <<0>> = ?MASK_ERROR(Module:encode_unsigned(0,little)), + ?line <<0>> = ?MASK_ERROR(Module:encode_unsigned(0,big)), ok. + +encode_decode(doc) -> + ["test binary:encode_unsigned/1,2 and binary:decode_unsigned/1,2"]; +encode_decode(Config) when is_list(Config) -> + ?line random:seed({1271,769940,559934}), + ?line ok = encode_decode_loop({1,100},1000), + ok. + +encode_decode_loop(_Range,0) -> + ok; +encode_decode_loop(Range, X) -> + ?line N = random_number(Range), + ?line A = binary:encode_unsigned(N), + ?line B = binary:encode_unsigned(N,big), + ?line C = binref:encode_unsigned(N), + ?line D = binref:encode_unsigned(N,big), + ?line E = binary:encode_unsigned(N,little), + ?line F = binref:encode_unsigned(N,little), + ?line G = binary:decode_unsigned(A), + ?line H = binary:decode_unsigned(A,big), + ?line I = binref:decode_unsigned(A), + ?line J = binary:decode_unsigned(E,little), + ?line K = binref:decode_unsigned(E,little), + ?line L = binary:decode_unsigned(make_unaligned(A)), + ?line M = binary:decode_unsigned(make_unaligned(E),little), + ?line PaddedBig = <<0:48,A/binary>>, + ?line PaddedLittle = <>, + ?line O = binary:decode_unsigned(PaddedBig), + ?line P = binary:decode_unsigned(make_unaligned(PaddedBig)), + ?line Q = binary:decode_unsigned(PaddedLittle,little), + ?line R = binary:decode_unsigned(make_unaligned(PaddedLittle),little), + ?line S = binref:decode_unsigned(PaddedLittle,little), + ?line T = binref:decode_unsigned(PaddedBig), + case (((A =:= B) and (B =:= C) and (C =:= D)) and + ((E =:= F)) and + ((N =:= G) and (G =:= H) and (H =:= I) and + (I =:= J) and (J =:= K) and (K =:= L) and (L =:= M)) and + ((M =:= O) and (O =:= P) and (P =:= Q) and (Q =:= R) and + (R =:= S) and (S =:= T)))of + true -> + encode_decode_loop(Range,X-1); + _ -> + io:format("Failed to encode/decode ~w~n(Results ~p)~n", + [N,[A,B,C,D,E,F,G,H,I,J,K,L,M,x,O,P,Q,R,S,T]]), + exit(mismatch) + end. + referenced(doc) -> ["Test refernced_byte_size/1 bif."]; referenced(Config) when is_list(Config) -> @@ -837,6 +905,10 @@ do_replace_comp(N,H,R,Opts) -> exit(mismatch) end. +one_random_number(N) -> + M = ((N - 1) rem 10) + 1, + element(M,{$0,$1,$2,$3,$4,$5,$6,$7,$8,$9}). + one_random(N) -> M = ((N - 1) rem 68) + 1, element(M,{$a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k,$l,$m,$n,$o,$p,$q,$r,$s,$t, @@ -844,6 +916,12 @@ one_random(N) -> $I,$J,$K,$L,$M,$N,$O,$P,$Q,$R,$S,$T,$U,$V,$W,$X,$Y,$Z,$Å, $Ä,$Ö,$0,$1,$2,$3,$4,$5,$6,$7,$8,$9}). +random_number({Min,Max}) -> % Min and Max are *length* of number in + % decimal positions + X = random:uniform(Max - Min + 1) + Min - 1, + list_to_integer([one_random_number(random:uniform(10)) || _ <- lists:seq(1,X)]). + + random_length({Min,Max}) -> random:uniform(Max - Min + 1) + Min - 1. random_string({Min,Max}) -> diff --git a/lib/stdlib/test/binref.erl b/lib/stdlib/test/binref.erl index 2322d036a2..af79c8fa09 100644 --- a/lib/stdlib/test/binref.erl +++ b/lib/stdlib/test/binref.erl @@ -517,7 +517,7 @@ decode_unsigned(Subject) -> decode_unsigned(Subject,Endian) -> try - true = is_binary(Subject) and (byte_size(Subject) > 0), + true = is_binary(Subject), case Endian of big -> do_decode(Subject,0); -- cgit v1.2.3