aboutsummaryrefslogtreecommitdiffstats
path: root/lib/stdlib/test/binary_module_SUITE.erl
diff options
context:
space:
mode:
authorPatrik Nyblom <[email protected]>2010-04-20 17:32:48 +0200
committerBjörn Gustavsson <[email protected]>2010-05-17 15:51:49 +0200
commitf06f499690ef1f5c8659128095a82d6c9b834d68 (patch)
treea7a6430214c9e1a23f7181730425b140d28a0610 /lib/stdlib/test/binary_module_SUITE.erl
parentba8c9c7c1594b4870936814caf3520a0f4e312f7 (diff)
downloadotp-f06f499690ef1f5c8659128095a82d6c9b834d68.tar.gz
otp-f06f499690ef1f5c8659128095a82d6c9b834d68.tar.bz2
otp-f06f499690ef1f5c8659128095a82d6c9b834d68.zip
Add random compare testcase
Fix heap-hole when trapping in binary.c Fix boyer more segfaulting when searchstring is longer than haystack
Diffstat (limited to 'lib/stdlib/test/binary_module_SUITE.erl')
-rw-r--r--lib/stdlib/test/binary_module_SUITE.erl72
1 files changed, 70 insertions, 2 deletions
diff --git a/lib/stdlib/test/binary_module_SUITE.erl b/lib/stdlib/test/binary_module_SUITE.erl
index 31d0812cfc..b6d3dde1d4 100644
--- a/lib/stdlib/test/binary_module_SUITE.erl
+++ b/lib/stdlib/test/binary_module_SUITE.erl
@@ -1,6 +1,6 @@
-module(binary_module_SUITE).
--export([all/1, interesting/1]).
+-export([all/1, interesting/1,random_ref_comp/1]).
-define(STANDALONE,1).
@@ -24,7 +24,7 @@ run() ->
-endif.
-all(suite) -> [interesting].
+all(suite) -> [interesting,random_ref_comp].
interesting(doc) ->
@@ -94,3 +94,71 @@ do_interesting(Module) ->
[<<"34">>,<<"34">>,
<<"12347">>,<<"2346">>]),
ok.
+
+random_ref_comp(doc) ->
+ ["Test pseudorandomly generated cases against reference imlementation"];
+random_ref_comp(Config) when is_list(Config) ->
+ put(success_counter,0),
+ random:seed({1271,769940,559934}),
+ do_random_match_comp(5000,{1,40},{30,1000}),
+ io:format("Number of successes: ~p~n",[get(success_counter)]),
+ do_random_match_comp2(5000,{1,40},{30,1000}),
+ io:format("Number of successes: ~p~n",[get(success_counter)]),
+ ok.
+
+do_random_match_comp(0,_,_) ->
+ ok;
+do_random_match_comp(N,NeedleRange,HaystackRange) ->
+ Needle = random_string(NeedleRange),
+ Haystack = random_string(HaystackRange),
+ true = do_match_comp(Needle,Haystack),
+ do_random_match_comp(N-1,NeedleRange,HaystackRange).
+
+do_random_match_comp2(0,_,_) ->
+ ok;
+do_random_match_comp2(N,NeedleRange,HaystackRange) ->
+ Haystack = random_string(HaystackRange),
+ Needle = random_substring(NeedleRange,Haystack),
+ true = do_match_comp(Needle,Haystack),
+ do_random_match_comp2(N-1,NeedleRange,HaystackRange).
+
+do_match_comp(N,H) ->
+ A = binref:match(H,N),
+ B = binref:match(H,binref:compile_pattern([N])),
+ C = binary:match(H,N),
+ D = binary:match(H,binary:compile_pattern([N])),
+ if
+ A =/= nomatch ->
+ put(success_counter,get(success_counter)+1);
+ true ->
+ ok
+ end,
+ case {(A =:= B), (B =:= C),(C =:= D)} of
+ {true,true,true} ->
+ true;
+ _ ->
+ io:format("Failed to match ~s (needle) against ~s (haystack)~n",
+ [N,H]),
+ io:format("A:~p,~nB:~p,~n,C:~p,~n,D:~p.~n",
+ [A,B,C,D]),
+ exit(mismatch)
+ end.
+
+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,$u,$v,$w,$x,$y,$z,$�,$�,$�,$A,$B,$C,$D,$E,$F,$G,$H,$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_string({Min,Max}) ->
+ X = random:uniform(Max - Min + 1) + Min - 1,
+ list_to_binary([one_random(random:uniform(68)) || _ <- lists:seq(1,X)]).
+random_substring({Min,Max},Hay) ->
+ X = random:uniform(Max - Min + 1) + Min - 1,
+ Y = byte_size(Hay),
+ Z = if
+ X > Y -> Y;
+ true -> X
+ end,
+ PMax = Y - Z,
+ Pos = random:uniform(PMax + 1) - 1,
+ <<_:Pos/binary,Res:Z/binary,_/binary>> = Hay,
+ Res.