1: %% -*- coding: utf-8 -*-
    2: %%
    3: %% %CopyrightBegin%
    4: %%
    5: %% Copyright Ericsson AB 1997-2012. All Rights Reserved.
    6: %%
    7: %% The contents of this file are subject to the Erlang Public License,
    8: %% Version 1.1, (the "License"); you may not use this file except in
    9: %% compliance with the License. You should have received a copy of the
   10: %% Erlang Public License along with this software. If not, it can be
   11: %% retrieved online at http://www.erlang.org/.
   12: %%
   13: %% Software distributed under the License is distributed on an "AS IS"
   14: %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
   15: %% the License for the specific language governing rights and limitations
   16: %% under the License.
   17: %%
   18: %% %CopyrightEnd%
   19: %%
   20: -module(binary_module_SUITE).
   21: 
   22: -export([all/0, suite/0,groups/0,init_per_suite/1, end_per_suite/1, 
   23: 	 init_per_group/2,end_per_group/2, 
   24: 	 interesting/1,scope_return/1,random_ref_comp/1,random_ref_sr_comp/1,
   25: 	 random_ref_fla_comp/1,parts/1, bin_to_list/1, list_to_bin/1,
   26: 	 copy/1, referenced/1,guard/1,encode_decode/1,badargs/1,longest_common_trap/1]).
   27: 
   28: -export([random_number/1, make_unaligned/1]).
   29: 
   30: 
   31: 
   32: %%-define(STANDALONE,1).
   33: 
   34: -ifdef(STANDALONE).
   35: 
   36: -define(line,erlang:display({?MODULE,?LINE}),).
   37: 
   38: -else.
   39: 
   40: -include_lib("test_server/include/test_server.hrl").
   41: -export([init_per_testcase/2, end_per_testcase/2]).
   42: % Default timetrap timeout (set in init_per_testcase).
   43: % Some of these testcases are really heavy...
   44: -define(default_timeout, ?t:minutes(20)).
   45: 
   46: -endif.
   47: 
   48: 
   49: 
   50: -ifdef(STANDALONE).
   51: -export([run/0]).
   52: 
   53: run() ->
   54:     [ apply(?MODULE,X,[[]]) || X <- all(suite) ].
   55: 
   56: -else.
   57: 
   58: init_per_testcase(_Case, Config) ->
   59:     ?line Dog = ?t:timetrap(?default_timeout),
   60:     [{watchdog, Dog} | Config].
   61: 
   62: end_per_testcase(_Case, Config) ->
   63:     ?line Dog = ?config(watchdog, Config),
   64:     ?line test_server:timetrap_cancel(Dog),
   65:     ok.
   66: -endif.
   67: 
   68: suite() -> [{ct_hooks,[ts_install_cth]}].
   69: 
   70: all() -> 
   71:     [scope_return,interesting, random_ref_fla_comp, random_ref_sr_comp,
   72:      random_ref_comp, parts, bin_to_list, list_to_bin, copy,
   73:      referenced, guard, encode_decode, badargs,
   74:      longest_common_trap].
   75: 
   76: groups() -> 
   77:     [].
   78: 
   79: init_per_suite(Config) ->
   80:     Config.
   81: 
   82: end_per_suite(_Config) ->
   83:     ok.
   84: 
   85: init_per_group(_GroupName, Config) ->
   86:     Config.
   87: 
   88: end_per_group(_GroupName, Config) ->
   89:     Config.
   90: 
   91: 
   92: -define(MASK_ERROR(EXPR),mask_error((catch (EXPR)))).
   93: 
   94: 
   95: badargs(doc) ->
   96:     ["Tests various badarg exceptions in the module"];
   97: badargs(Config) when is_list(Config) ->
   98:     ?line badarg = ?MASK_ERROR(binary:compile_pattern([<<1,2,3:3>>])),
   99:     ?line badarg = ?MASK_ERROR(binary:compile_pattern([<<1,2,3>>|<<1,2>>])),
  100:     ?line badarg = ?MASK_ERROR(binary:compile_pattern(<<1,2,3:3>>)),
  101:     ?line badarg = ?MASK_ERROR(binary:compile_pattern(<<>>)),
  102:     ?line badarg = ?MASK_ERROR(binary:match(<<1,2,3:3>>,<<1>>)),
  103:     ?line badarg = ?MASK_ERROR(binary:matches(<<1,2,3:3>>,<<1>>)),
  104:     ?line badarg = ?MASK_ERROR(binary:match(<<1,2,3>>,<<1>>,
  105: 					    [{scope,{0,1},1}])),
  106:     ?line badarg = ?MASK_ERROR(binary:match(<<1,2,3>>,<<1>>,
  107: 					    [{scape,{0,1}}])),
  108:     ?line badarg = ?MASK_ERROR(binary:match(<<1,2,3>>,<<1>>,
  109: 					    [{scope,{0,1,1}}])),
  110:     ?line badarg = ?MASK_ERROR(binary:match(<<1,2,3>>,<<1>>,[{scope,0,1}])),
  111:     ?line badarg = ?MASK_ERROR(binary:match(<<1,2,3>>,<<1>>,[{scope,[0,1]}])),
  112:     ?line badarg = ?MASK_ERROR(binary:match(<<1,2,3>>,<<1>>,
  113: 					    [{scope,{0.1,1}}])),
  114:     ?line badarg = ?MASK_ERROR(binary:match(<<1,2,3>>,<<1>>,
  115: 					    [{scope,{1,1.1}}])),
  116:     ?line badarg =
  117: 	?MASK_ERROR(
  118: 	   binary:match(<<1,2,3>>,<<1>>,
  119: 			[{scope,{16#FF,
  120: 				 16#FFFFFFFFFFFFFFFF}}])),
  121:     ?line badarg =
  122: 	?MASK_ERROR(
  123: 	   binary:match(<<1,2,3>>,<<1>>,
  124: 			[{scope,{16#FFFFFFFFFFFFFFFF,
  125: 				 -16#7FFFFFFFFFFFFFFF-1}}])),
  126:     ?line badarg =
  127: 	?MASK_ERROR(
  128: 	   binary:match(<<1,2,3>>,<<1>>,
  129: 			[{scope,{16#FFFFFFFFFFFFFFFF,
  130: 				 16#7FFFFFFFFFFFFFFF}}])),
  131:     ?line badarg =
  132: 	?MASK_ERROR(
  133: 	   binary:part(<<1,2,3>>,{16#FF,
  134: 				 16#FFFFFFFFFFFFFFFF})),
  135:     ?line badarg =
  136: 	?MASK_ERROR(
  137: 	   binary:part(<<1,2,3>>,{16#FFFFFFFFFFFFFFFF,
  138: 				 -16#7FFFFFFFFFFFFFFF-1})),
  139:     ?line badarg =
  140: 	?MASK_ERROR(
  141: 	   binary:part(<<1,2,3>>,{16#FFFFFFFFFFFFFFFF,
  142: 				 16#7FFFFFFFFFFFFFFF})),
  143:     ?line badarg =
  144: 	?MASK_ERROR(
  145: 	   binary:part(make_unaligned(<<1,2,3>>),{1,1,1})),
  146:     ?line badarg =
  147: 	?MASK_ERROR(
  148: 	   binary_part(make_unaligned(<<1,2,3>>),{1,1,1})),
  149:     ?line badarg =
  150: 	?MASK_ERROR(
  151: 	   binary_part(make_unaligned(<<1,2,3>>),{16#FFFFFFFFFFFFFFFF,
  152: 				 -16#7FFFFFFFFFFFFFFF-1})),
  153:     ?line badarg =
  154: 	?MASK_ERROR(
  155: 	   binary_part(make_unaligned(<<1,2,3>>),{16#FF,
  156: 				 16#FFFFFFFFFFFFFFFF})),
  157:     ?line badarg =
  158: 	?MASK_ERROR(
  159: 	   binary_part(make_unaligned(<<1,2,3>>),{16#FFFFFFFFFFFFFFFF,
  160: 				 16#7FFFFFFFFFFFFFFF})),
  161:     ?line badarg =
  162: 	?MASK_ERROR(
  163: 	   binary_part(make_unaligned(<<1,2,3>>),{16#FFFFFFFFFFFFFFFFFF,
  164: 				 -16#7FFF})),
  165:     ?line badarg =
  166: 	?MASK_ERROR(
  167: 	   binary_part(make_unaligned(<<1,2,3>>),{16#FF,
  168: 				 -16#7FFF})),
  169:     ?line badarg =
  170: 	?MASK_ERROR(
  171: 	   binary:bin_to_list(<<1,2,3>>,{16#FF,
  172: 				 16#FFFFFFFFFFFFFFFF})),
  173:     ?line badarg =
  174: 	?MASK_ERROR(
  175: 	   binary:bin_to_list(<<1,2,3>>,{16#FFFFFFFFFFFFFFFF,
  176: 				 -16#7FFFFFFFFFFFFFFF-1})),
  177:     ?line badarg =
  178: 	?MASK_ERROR(
  179: 	   binary:bin_to_list(<<1,2,3>>,{16#FFFFFFFFFFFFFFFF,
  180: 				 16#7FFFFFFFFFFFFFFF})),
  181:     ?line [1,2,3] =
  182: 	?MASK_ERROR(
  183: 	   binary:bin_to_list(<<1,2,3>>)),
  184:     ?line badarg =
  185: 	?MASK_ERROR(
  186: 	   binary:bin_to_list(<<1,2,3>>,[])),
  187:     ?line badarg =
  188: 	?MASK_ERROR(
  189: 	   binary:bin_to_list(<<1,2,3>>,{1,2,3})),
  190:     ?line badarg =
  191: 	?MASK_ERROR(
  192: 	   binary:bin_to_list(<<1,2,3>>,{1.0,1})),
  193:     ?line badarg =
  194: 	?MASK_ERROR(
  195: 	   binary:bin_to_list(<<1,2,3>>,{1,1.0})),
  196:     ?line badarg =
  197: 	?MASK_ERROR(
  198: 	   binary:bin_to_list(<<1,2,3:3>>,{1,1})),
  199:     ?line badarg =
  200: 	?MASK_ERROR(
  201: 	   binary:bin_to_list(<<1,2,3:3>>)),
  202:     ?line badarg =
  203: 	?MASK_ERROR(
  204: 	   binary:bin_to_list([1,2,3])),
  205: 
  206:     ?line nomatch =
  207: 	?MASK_ERROR(binary:match(<<1,2,3>>,<<1>>,[{scope,{0,0}}])),
  208:     ?line badarg =
  209: 	?MASK_ERROR(binary:match(<<1,2,3>>,{bm,<<>>},[{scope,{0,1}}])),
  210:     ?line badarg =
  211: 	?MASK_ERROR(binary:match(<<1,2,3>>,[],[{scope,{0,1}}])),
  212:     ?line badarg =
  213: 	?MASK_ERROR(binary:match(<<1,2,3>>,{ac,<<>>},[{scope,{0,1}}])),
  214:     ?line {bm,BMMagic} = binary:compile_pattern([<<1,2,3>>]),
  215:     ?line {ac,ACMagic} = binary:compile_pattern([<<1,2,3>>,<<4,5>>]),
  216:     ?line badarg =
  217: 	?MASK_ERROR(binary:match(<<1,2,3>>,{bm,ACMagic},[{scope,{0,1}}])),
  218:     ?line badarg =
  219: 	?MASK_ERROR(binary:match(<<1,2,3>>,{ac,BMMagic},[{scope,{0,1}}])),
  220:     ?line badarg =
  221: 	?MASK_ERROR(
  222: 	   binary:match(<<1,2,3>>,
  223: 			{bm,ets:match_spec_compile([{'_',[],['$_']}])},
  224: 			[{scope,{0,1}}])),
  225:     ?line badarg =
  226: 	?MASK_ERROR(
  227: 	   binary:match(<<1,2,3>>,
  228: 			{ac,ets:match_spec_compile([{'_',[],['$_']}])},
  229: 			[{scope,{0,1}}])),
  230:      ?line [] =
  231: 	?MASK_ERROR(binary:matches(<<1,2,3>>,<<1>>,[{scope,{0,0}}])),
  232:     ?line badarg =
  233: 	?MASK_ERROR(binary:matches(<<1,2,3>>,{bm,<<>>},[{scope,{0,1}}])),
  234:     ?line badarg =
  235: 	?MASK_ERROR(binary:matches(<<1,2,3>>,[],[{scope,{0,1}}])),
  236:     ?line badarg =
  237: 	?MASK_ERROR(binary:matches(<<1,2,3>>,{ac,<<>>},[{scope,{0,1}}])),
  238:     ?line badarg =
  239: 	?MASK_ERROR(binary:matches(<<1,2,3>>,{bm,ACMagic},[{scope,{0,1}}])),
  240:     ?line badarg =
  241: 	?MASK_ERROR(binary:matches(<<1,2,3>>,{ac,BMMagic},[{scope,{0,1}}])),
  242:     ?line badarg =
  243: 	?MASK_ERROR(
  244: 	   binary:matches(<<1,2,3>>,
  245: 			{bm,ets:match_spec_compile([{'_',[],['$_']}])},
  246: 			[{scope,{0,1}}])),
  247:     ?line badarg =
  248: 	?MASK_ERROR(
  249: 	   binary:matches(<<1,2,3>>,
  250: 			{ac,ets:match_spec_compile([{'_',[],['$_']}])},
  251: 			[{scope,{0,1}}])),
  252:     %% OTP-11350
  253:     badarg = ?MASK_ERROR(
  254: 		binary:matches(<<"foo">>, 
  255: 			       [<<>>, <<"f">>])),
  256:     ?line badarg =
  257: 	?MASK_ERROR(binary:longest_common_prefix(
  258: 		      [<<0:10000,1,2,4,1:3>>,
  259: 		       <<0:10000,1,2,3>>])),
  260:     ?line badarg =
  261: 	?MASK_ERROR(binary:longest_common_suffix(
  262: 		      [<<0:10000,1,2,4,1:3>>,
  263: 		       <<0:10000,1,2,3>>])),
  264:     ?line badarg =
  265: 	?MASK_ERROR(binary:encode_unsigned(-1)),
  266:     ?line badarg =
  267: 	?MASK_ERROR(
  268: 	   binary:encode_unsigned(-16#FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)),
  269:     ?line badarg =
  270: 	?MASK_ERROR(
  271: 	   binary:first(<<1,2,4,1:3>>)),
  272:     ?line badarg =
  273: 	?MASK_ERROR(
  274: 	   binary:first([1,2,4])),
  275:     ?line badarg =
  276: 	?MASK_ERROR(
  277: 	   binary:last(<<1,2,4,1:3>>)),
  278:     ?line badarg =
  279: 	?MASK_ERROR(
  280: 	   binary:last([1,2,4])),
  281:     ?line badarg =
  282: 	?MASK_ERROR(
  283: 	   binary:at(<<1,2,4,1:3>>,2)),
  284:     ?line badarg =
  285: 	?MASK_ERROR(
  286: 	   binary:at(<<>>,2)),
  287:     ?line badarg =
  288: 	?MASK_ERROR(
  289: 	   binary:at([1,2,4],2)),
  290:     ok.
  291: 
  292: longest_common_trap(doc) ->
  293:     ["Whitebox test to force special trap conditions in longest_common_{prefix,suffix}"];
  294: longest_common_trap(Config) when is_list(Config) ->
  295:     ?line erts_debug:set_internal_state(available_internal_state,true),
  296:     ?line io:format("oldlimit: ~p~n",
  297: 		    [erts_debug:set_internal_state(binary_loop_limit,10)]),
  298:     erlang:bump_reductions(10000000),
  299:     ?line _ = binary:longest_common_prefix(
  300: 		[<<0:10000,1,2,4>>,
  301: 		 <<0:10000,1,2,3>>,
  302: 		 <<0:10000,1,3,3>>,
  303: 		 <<0:10000,1,2,4>>,
  304: 		 <<0:10000,1,2,4>>,
  305: 		 <<0:10000,1,2,3>>,
  306: 		 <<0:10000,1,3,3>>,
  307: 		 <<0:10000,1,2,3>>,
  308: 		 <<0:10000,1,3,3>>,
  309: 		 <<0:10000,1,2,4>>,
  310: 		 <<0:10000,1,2,4>>,
  311: 		 <<0:10000,1,2,3>>,
  312: 		 <<0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0:10000,1,3,3>>,
  313: 		 <<0:10000,1,2,4>>]),
  314:     ?line _ = binary:longest_common_prefix(
  315: 		[<<0:10000,1,2,4>>,
  316: 		 <<0:10000,1,2,3>>,
  317: 		 <<0:10000,1,3,3>>,
  318: 		 <<0:10000,1,2,4>>,
  319: 		 <<0:10000,1,2,4>>,
  320: 		 <<0:10000,1,2,3>>,
  321: 		 <<0:10000,1,3,3>>,
  322: 		 <<0:10000,1,2,3>>,
  323: 		 <<0:10000,1,3,3>>,
  324: 		 <<0:10000,1,2,4>>,
  325: 		 <<0:10000,1,2,4>>,
  326: 		 <<0:10000,1,2,3>>,
  327: 		 <<0,0,0,0,0,0,0,0,0,0,0,0,0,0>>,
  328: 		 <<0:10000,1,2,4>>]),
  329:     erlang:bump_reductions(10000000),
  330:     ?line _ = binary:longest_common_suffix(
  331: 		[<<1,2,4,0:10000>>,
  332: 		 <<1,2,4,0:10000>>,
  333: 		 <<1,2,4,0:10000>>,
  334: 		 <<1,2,4,0:10000>>,
  335: 		 <<1,2,4,0:10000>>,
  336: 		 <<1,2,4,0:10000>>,
  337: 		 <<1,2,4,0:10000>>,
  338: 		 <<1,2,4,0:10000>>,
  339: 		 <<1,2,4,0:10000>>,
  340: 		 <<1,2,4,0:10000>>,
  341: 		 <<1,2,4,0:10000>>,
  342: 		 <<1,2,4,0:10000>>,
  343: 		 <<1,3,3,0:10000,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0>>,
  344: 		 <<1,2,4,0:10000>>]),
  345:     ?line _ = binary:longest_common_suffix(
  346: 		[<<1,2,4,0:10000>>,
  347: 		 <<1,2,4,0:10000>>,
  348: 		 <<1,2,4,0:10000>>,
  349: 		 <<1,2,4,0:10000>>,
  350: 		 <<1,2,4,0:10000>>,
  351: 		 <<1,2,4,0:10000>>,
  352: 		 <<1,2,4,0:10000>>,
  353: 		 <<1,2,4,0:10000>>,
  354: 		 <<1,2,4,0:10000>>,
  355: 		 <<1,2,4,0:10000>>,
  356: 		 <<1,2,4,0:10000>>,
  357: 		 <<1,2,4,0:10000>>,
  358: 		 <<0,0,0,0,0,0,0,0,0,0,0,0,0,0>>,
  359: 		 <<1,2,4,0:10000>>]),
  360:     Subj = subj(),
  361:     Len = byte_size(Subj),
  362:     ?line Len = binary:longest_common_suffix(
  363: 		  [Subj,Subj,Subj]),
  364:     ?line io:format("limit was: ~p~n",
  365: 		    [erts_debug:set_internal_state(binary_loop_limit,
  366: 						   default)]),
  367:     ?line erts_debug:set_internal_state(available_internal_state,false),
  368:     ok.
  369: 
  370: subj() ->
  371:   Me = self(),
  372:   spawn(fun() ->
  373:     X0 = iolist_to_binary([
  374:       "1234567890",
  375:       %lists:seq(16#21, 16#7e),
  376:       lists:duplicate(100, $x)
  377:     ]),
  378:     Me ! X0,
  379:     receive X -> X end
  380:   end),
  381:   X0 = receive A -> A end,
  382:   <<X1:32/binary,_/binary>> = X0,
  383:   Subject= <<X1/binary>>,
  384:   Subject.
  385: 
  386: 
  387: scope_return(doc) ->
  388:     ["Test correct return values for scopes (OTP-9701)."];
  389: scope_return(Config) when is_list(Config) ->
  390:     N=10000,
  391:     Bin=binary:copy(<<"a">>,N),
  392:     scope_loop(Bin,0,N).
  393: 
  394: scope_loop(_,N,N) ->
  395:     ok;
  396: scope_loop(Bin,N,M) ->
  397:     ?line {N,1} = binary:match(Bin,<<"a">>,[{scope,{N,1}}]),
  398:     ?line {N,1} = binary:match(Bin,[<<"a">>,<<"b">>],[{scope,{N,1}}]),
  399:     scope_loop(Bin,N+1,M).
  400: 
  401: interesting(doc) ->
  402:     ["Try some interesting patterns"];
  403: interesting(Config) when is_list(Config) ->
  404:     X = do_interesting(binary),
  405:     X = do_interesting(binref).
  406: 
  407: do_interesting(Module) ->
  408:     ?line {0,4} = Module:match(<<"123456">>,
  409: 			 Module:compile_pattern([<<"12">>,<<"1234">>,
  410: 						 <<"23">>,<<"3">>,
  411: 						 <<"34">>,<<"456">>,
  412: 						 <<"45">>,<<"6">>])),
  413:     ?line [{0,4},{5,1}] = Module:matches(<<"123456">>,
  414: 				   Module:compile_pattern([<<"12">>,<<"1234">>,
  415: 							   <<"23">>,<<"3">>,
  416: 							   <<"34">>,<<"456">>,
  417: 							   <<"45">>,<<"6">>])),
  418:     ?line [{0,4}] = Module:matches(<<"123456">>,
  419: 			     Module:compile_pattern([<<"12">>,<<"1234">>,
  420: 						     <<"23">>,<<"3">>,
  421: 						     <<"34">>,<<"456">>,
  422: 						     <<"45">>])),
  423:     ?line [{0,2},{2,2}] = Module:matches(<<"123456">>,
  424: 			     Module:compile_pattern([<<"12">>,
  425: 						     <<"23">>,<<"3">>,
  426: 						     <<"34">>,<<"456">>,
  427: 						     <<"45">>])),
  428:     ?line {1,4} = Module:match(<<"123456">>,
  429: 			 Module:compile_pattern([<<"34">>,<<"34">>,
  430: 						 <<"12347">>,<<"2345">>])),
  431:     ?line [{1,4}] = Module:matches(<<"123456">>,
  432: 			     Module:compile_pattern([<<"34">>,<<"34">>,
  433: 						     <<"12347">>,<<"2345">>])),
  434:     ?line [{2,2}] = Module:matches(<<"123456">>,
  435: 			     Module:compile_pattern([<<"34">>,<<"34">>,
  436: 						     <<"12347">>,<<"2346">>])),
  437: 
  438:     ?line {0,4} = Module:match(<<"123456">>,
  439: 			 [<<"12">>,<<"1234">>,
  440: 			  <<"23">>,<<"3">>,
  441: 			  <<"34">>,<<"456">>,
  442: 			  <<"45">>,<<"6">>]),
  443:     ?line [{0,4},{5,1}] = Module:matches(<<"123456">>,
  444: 				   [<<"12">>,<<"1234">>,
  445: 				    <<"23">>,<<"3">>,
  446: 				    <<"34">>,<<"456">>,
  447: 				    <<"45">>,<<"6">>]),
  448:     ?line [{0,4}] = Module:matches(<<"123456">>,
  449: 			     [<<"12">>,<<"1234">>,
  450: 			      <<"23">>,<<"3">>,
  451: 			      <<"34">>,<<"456">>,
  452: 			      <<"45">>]),
  453:     ?line [{0,2},{2,2}] = Module:matches(<<"123456">>,
  454: 					 [<<"12">>,
  455: 					  <<"23">>,<<"3">>,
  456: 					  <<"34">>,<<"456">>,
  457: 					  <<"45">>]),
  458:     ?line {1,4} = Module:match(<<"123456">>,
  459: 			       [<<"34">>,<<"34">>,
  460: 				<<"12347">>,<<"2345">>]),
  461:     ?line [{1,4}] = Module:matches(<<"123456">>,
  462: 				   [<<"34">>,<<"34">>,
  463: 				    <<"12347">>,<<"2345">>]),
  464:     ?line [{2,2}] = Module:matches(<<"123456">>,
  465: 				   [<<"34">>,<<"34">>,
  466: 				    <<"12347">>,<<"2346">>]),
  467:     ?line nomatch = Module:match(<<1,2,3,4>>,<<2>>,[{scope,{0,1}}]),
  468:     ?line {1,1} = Module:match(<<1,2,3,4>>,<<2>>,[{scope,{0,2}}]),
  469:     ?line nomatch = Module:match(<<1,2,3,4>>,<<2,3>>,[{scope,{0,2}}]),
  470:     ?line {1,2} = Module:match(<<1,2,3,4>>,<<2,3>>,[{scope,{0,3}}]),
  471:     ?line {1,2} = Module:match(<<1,2,3,4>>,<<2,3>>,[{scope,{0,4}}]),
  472:     ?line badarg = ?MASK_ERROR(Module:match(<<1,2,3,4>>,<<2,3>>,
  473: 					    [{scope,{0,5}}])),
  474:     ?line {1,2} = Module:match(<<1,2,3,4>>,<<2,3>>,[{scope,{4,-4}}]),
  475:     ?line {0,3} = Module:match(<<1,2,3,4>>,<<1,2,3>>,[{scope,{4,-4}}]),
  476:     ?line {0,4} = Module:match(<<1,2,3,4>>,<<1,2,3,4>>,[{scope,{4,-4}}]),
  477:     ?line badarg = ?MASK_ERROR(Module:match(<<1,2,3,4>>,<<1,2,3,4>>,
  478: 					    [{scope,{3,-4}}])),
  479:     ?line [] = Module:matches(<<1,2,3,4>>,<<2>>,[{scope,{0,1}}]),
  480:     ?line [{1,1}] = Module:matches(<<1,2,3,4>>,[<<2>>,<<3>>],[{scope,{0,2}}]),
  481:     ?line [] = Module:matches(<<1,2,3,4>>,<<2,3>>,[{scope,{0,2}}]),
  482:     ?line [{1,2}] = Module:matches(<<1,2,3,4>>,<<2,3>>,[{scope,{0,3}}]),
  483:     ?line [{1,2}] = Module:matches(<<1,2,3,4>>,<<2,3>>,[{scope,{0,4}}]),
  484:     ?line [{1,2}] = Module:matches(<<1,2,3,4>>,[<<2,3>>,<<4>>],
  485: 				   [{scope,{0,3}}]),
  486:     ?line [{1,2},{3,1}] = Module:matches(<<1,2,3,4>>,[<<2,3>>,<<4>>],
  487: 					 [{scope,{0,4}}]),
  488:     ?line badarg = ?MASK_ERROR(Module:matches(<<1,2,3,4>>,<<2,3>>,
  489: 					      [{scope,{0,5}}])),
  490:     ?line [{1,2}] = Module:matches(<<1,2,3,4>>,<<2,3>>,[{scope,{4,-4}}]),
  491:     ?line [{1,2},{3,1}] = Module:matches(<<1,2,3,4>>,[<<2,3>>,<<4>>],
  492: 					 [{scope,{4,-4}}]),
  493:     ?line [{0,3}] = Module:matches(<<1,2,3,4>>,<<1,2,3>>,[{scope,{4,-4}}]),
  494:     ?line [{0,4}] = Module:matches(<<1,2,3,4>>,<<1,2,3,4>>,[{scope,{4,-4}}]),
  495:     ?line badarg = ?MASK_ERROR(Module:matches(<<1,2,3,4>>,<<1,2,3,4>>,
  496: 					      [{scope,{3,-4}}])),
  497:     ?line badarg = ?MASK_ERROR(Module:matches(<<1,2,3,4>>,[<<1,2,3,4>>],
  498: 					      [{scope,{3,-4}}])),
  499:     ?line [<<1,2,3>>,<<6,7,8>>] = Module:split(<<1,2,3,4,5,6,7,8>>,<<4,5>>),
  500:     ?line [<<1,2,3>>,<<6,7,8>>] = Module:split(<<1,2,3,4,5,6,7,8>>,
  501: 					       [<<4,5>>,<<7>>]),
  502:     ?line [<<1,2,3>>,<<6>>,<<8>>] = Module:split(<<1,2,3,4,5,6,7,8>>,
  503: 						 [<<4,5>>,<<7>>],[global]),
  504:     ?line [<<1,2,3>>,<<6>>,<<>>,<<>>] = Module:split(<<1,2,3,4,5,6,7,8>>,
  505: 						     [<<4,5>>,<<7>>,<<8>>],
  506: 						     [global]),
  507:     ?line [<<1,2,3>>,<<6>>] = Module:split(<<1,2,3,4,5,6,7,8>>,
  508: 					   [<<4,5>>,<<7>>,<<8>>],
  509: 					   [global,trim]),
  510:     ?line [<<1,2,3,4,5,6,7,8>>] = Module:split(<<1,2,3,4,5,6,7,8>>,
  511: 					       [<<4,5>>,<<7>>,<<8>>],
  512: 					       [global,trim,{scope,{0,4}}]),
  513:     ?line [<<1,2,3>>,<<6,7,8>>] = Module:split(<<1,2,3,4,5,6,7,8>>,
  514: 					       [<<4,5>>,<<7>>,<<8>>],
  515: 					       [global,trim,{scope,{0,5}}]),
  516:     ?line badarg = ?MASK_ERROR(
  517: 		      Module:replace(<<1,2,3,4,5,6,7,8>>,
  518: 				     [<<4,5>>,<<7>>,<<8>>],<<99>>,
  519: 				     [global,trim,{scope,{0,5}}])),
  520:     ?line <<1,2,3,99,6,7,8>> = Module:replace(<<1,2,3,4,5,6,7,8>>,
  521: 					      [<<4,5>>,<<7>>,<<8>>],<<99>>,[]),
  522:     ?line <<1,2,3,99,6,99,99>> = Module:replace(<<1,2,3,4,5,6,7,8>>,
  523: 						[<<4,5>>,<<7>>,<<8>>],<<99>>,
  524: 						[global]),
  525:     ?line <<1,2,3,99,6,7,8>> = Module:replace(<<1,2,3,4,5,6,7,8>>,
  526: 					      [<<4,5>>,<<7>>,<<8>>],<<99>>,
  527: 					      [global,{scope,{0,5}}]),
  528:     ?line <<1,2,3,99,6,7,8>> = Module:replace(<<1,2,3,4,5,6,7,8>>,
  529: 					      [<<4,5>>,<<7>>,<<8>>],<<99>>,
  530: 					      [global,{scope,{0,5}}]),
  531:     ?line <<1,2,3,99,6,7,8>> = Module:replace(<<1,2,3,4,5,6,7,8>>,
  532: 					      [<<4,5>>,<<7>>,<<8>>],<<99>>,
  533: 					      [global,{scope,{0,5}}]),
  534:     ?line badarg = ?MASK_ERROR(Module:replace(<<1,2,3,4,5,6,7,8>>,
  535: 					      [<<4,5>>,<<7>>,<<8>>],<<99>>,
  536: 					      [global,{scope,{0,5}},
  537: 					       {insert,1}])),
  538:     ?line <<1,2,3,99,4,5,6,7,8>> = Module:replace(<<1,2,3,4,5,6,7,8>>,
  539: 						  [<<4,5>>,<<7>>,<<8>>],<<99>>,
  540: 						  [global,{scope,{0,5}},
  541: 						   {insert_replaced,1}]),
  542:     ?line <<1,2,3,9,4,5,9,6,7,8>> = Module:replace(<<1,2,3,4,5,6,7,8>>,
  543: 						   [<<4,5>>,<<7>>,<<8>>],
  544: 						   <<9,9>>,
  545: 						   [global,{scope,{0,5}},
  546: 						    {insert_replaced,1}]),
  547:     ?line badarg = ?MASK_ERROR(Module:replace(<<1,2,3,4,5,6,7,8>>,
  548: 					      [<<4,5>>,<<7>>,<<8>>],<<>>,
  549: 					      [global,{scope,{0,5}},
  550: 					       {insert_replaced,1}])),
  551:     ?line 2 = Module:longest_common_prefix([<<1,2,4>>,<<1,2,3>>]),
  552:     ?line 2 = Module:longest_common_prefix([<<1,2,4>>,<<1,2>>]),
  553:     ?line 1 = Module:longest_common_prefix([<<1,2,4>>,<<1>>]),
  554:     ?line 0 = Module:longest_common_prefix([<<1,2,4>>,<<>>]),
  555:     ?line 1 = Module:longest_common_prefix([<<1,2,4>>,<<1,2,3>>,<<1,3,3>>]),
  556:     ?line 1 = Module:longest_common_prefix([<<1,2,4>>,<<1,2,3>>,<<1,3,3>>,<<1,2,4>>]),
  557:     ?line 1251 = Module:longest_common_prefix([<<0:10000,1,2,4>>,
  558: 					       <<0:10000,1,2,3>>,
  559: 					       <<0:10000,1,3,3>>,
  560: 					       <<0:10000,1,2,4>>]),
  561:     ?line 12501 = Module:longest_common_prefix([<<0:100000,1,2,4>>,
  562: 						<<0:100000,1,2,3>>,
  563: 						<<0:100000,1,3,3>>,
  564: 						<<0:100000,1,2,4>>]),
  565:     ?line 1251 = Module:longest_common_prefix(
  566: 		   [make_unaligned(<<0:10000,1,2,4>>),
  567: 		    <<0:10000,1,2,3>>,
  568: 		    make_unaligned(<<0:10000,1,3,3>>),
  569: 		    <<0:10000,1,2,4>>]),
  570:     ?line 12501 = Module:longest_common_prefix(
  571: 		    [<<0:100000,1,2,4>>,
  572: 		     make_unaligned(<<0:100000,1,2,3>>),
  573: 		     <<0:100000,1,3,3>>,
  574: 		     make_unaligned(<<0:100000,1,2,4>>)]),
  575:     ?line 1250001 = Module:longest_common_prefix([<<0:10000000,1,2,4>>,
  576: 						  <<0:10000000,1,2,3>>,
  577: 						  <<0:10000000,1,3,3>>,
  578: 						  <<0:10000000,1,2,4>>]),
  579:     if % Too cruel for the reference implementation
  580: 	Module =:= binary ->
  581: 	    ?line erts_debug:set_internal_state(available_internal_state,true),
  582: 	    ?line io:format("oldlimit: ~p~n",
  583: 			    [erts_debug:set_internal_state(
  584: 			       binary_loop_limit,100)]),
  585: 	    ?line 1250001 = Module:longest_common_prefix(
  586: 				[<<0:10000000,1,2,4>>,
  587: 				 <<0:10000000,1,2,3>>,
  588: 				 <<0:10000000,1,3,3>>,
  589: 				 <<0:10000000,1,2,4>>]),
  590: 	    ?line io:format("limit was: ~p~n",
  591: 			    [erts_debug:set_internal_state(binary_loop_limit,
  592: 							   default)]),
  593: 	    ?line erts_debug:set_internal_state(available_internal_state,
  594: 						false);
  595: 	true ->
  596: 	    ok
  597:     end,
  598:     ?line 1 = Module:longest_common_suffix([<<0:100000000,1,2,4,5>>,
  599: 					    <<0:100000000,1,2,3,5>>,
  600: 					    <<0:100000000,1,3,3,5>>,
  601: 					    <<0:100000000,1,2,4,5>>]),
  602:     ?line 1 = Module:longest_common_suffix([<<1,2,4,5>>,
  603: 					    <<0:100000000,1,2,3,5>>,
  604: 					    <<0:100000000,1,3,3,5>>,
  605: 					    <<0:100000000,1,2,4,5>>]),
  606:     ?line 1 = Module:longest_common_suffix([<<1,2,4,5,5>>,<<5,5>>,
  607: 					    <<0:100000000,1,3,3,5,5>>,
  608: 					    <<0:100000000,1,2,4,5>>]),
  609:     ?line 0 = Module:longest_common_suffix([<<1,2,4,5,5>>,<<5,5>>,
  610: 					    <<0:100000000,1,3,3,5,5>>,
  611: 					    <<0:100000000,1,2,4>>]),
  612:     ?line 2 = Module:longest_common_suffix([<<1,2,4,5,5>>,<<5,5>>,
  613: 					    <<0:100000000,1,3,3,5,5>>,
  614: 					    <<0:100000000,1,2,4,5,5>>]),
  615:     ?line 1 = Module:longest_common_suffix([<<1,2,4,5,5>>,<<5>>,
  616: 					    <<0:100000000,1,3,3,5,5>>,
  617: 					    <<0:100000000,1,2,4,5,5>>]),
  618:     ?line 0 = Module:longest_common_suffix([<<1,2,4,5,5>>,<<>>,
  619: 					    <<0:100000000,1,3,3,5,5>>,
  620: 					    <<0:100000000,1,2,4,5,5>>]),
  621:     ?line 0 = Module:longest_common_suffix([<<>>,<<0:100000000,1,3,3,5,5>>,
  622: 					    <<0:100000000,1,2,4,5,5>>]),
  623:     ?line 0 = Module:longest_common_suffix([<<>>,<<0:100000000,1,3,3,5,5>>,
  624: 					    <<0:100000000,1,2,4,5,5>>]),
  625:     ?line 2 = Module:longest_common_suffix([<<5,5>>,<<0:100000000,1,3,3,5,5>>,
  626: 					    <<0:100000000,1,2,4,5,5>>]),
  627:     ?line 2 = Module:longest_common_suffix([<<5,5>>,<<5,5>>,<<4,5,5>>]),
  628:     ?line 2 = Module:longest_common_suffix([<<5,5>>,<<5,5>>,<<5,5>>]),
  629:     ?line 3 = Module:longest_common_suffix([<<4,5,5>>,<<4,5,5>>,<<4,5,5>>]),
  630:     ?line 0 = Module:longest_common_suffix([<<>>]),
  631:     ?line badarg = ?MASK_ERROR(Module:longest_common_suffix([])),
  632:     ?line badarg = ?MASK_ERROR(Module:longest_common_suffix([apa])),
  633:     ?line badarg = ?MASK_ERROR(Module:longest_common_suffix([[<<>>]])),
  634:     ?line badarg = ?MASK_ERROR(Module:longest_common_suffix([[<<0>>,
  635: 							      <<1:9>>]])),
  636:     ?line 0 = Module:longest_common_prefix([<<>>]),
  637:     ?line badarg = ?MASK_ERROR(Module:longest_common_prefix([])),
  638:     ?line badarg = ?MASK_ERROR(Module:longest_common_prefix([apa])),
  639:     ?line badarg = ?MASK_ERROR(Module:longest_common_prefix([[<<>>]])),
  640:     ?line badarg = ?MASK_ERROR(Module:longest_common_prefix([[<<0>>,
  641: 							      <<1:9>>]])),
  642: 
  643:     ?line <<1:6,Bin:3/binary,_:2>> = <<1:6,1,2,3,1:2>>,
  644:     ?line <<1,2,3>> = Bin,
  645:     ?line 1 = Module:first(Bin),
  646:     ?line 1 = Module:first(<<1>>),
  647:     ?line 1 = Module:first(<<1,2,3>>),
  648:     ?line badarg = ?MASK_ERROR(Module:first(<<>>)),
  649:     ?line badarg = ?MASK_ERROR(Module:first(apa)),
  650:     ?line 3 = Module:last(Bin),
  651:     ?line 1 = Module:last(<<1>>),
  652:     ?line 3 = Module:last(<<1,2,3>>),
  653:     ?line badarg = ?MASK_ERROR(Module:last(<<>>)),
  654:     ?line badarg = ?MASK_ERROR(Module:last(apa)),
  655:     ?line 1 = Module:at(Bin,0),
  656:     ?line 1 = Module:at(<<1>>,0),
  657:     ?line 1 = Module:at(<<1,2,3>>,0),
  658:     ?line 2 = Module:at(<<1,2,3>>,1),
  659:     ?line 3 = Module:at(<<1,2,3>>,2),
  660:     ?line badarg = ?MASK_ERROR(Module:at(<<1,2,3>>,3)),
  661:     ?line badarg = ?MASK_ERROR(Module:at(<<1,2,3>>,-1)),
  662:     ?line badarg = ?MASK_ERROR(Module:at(<<1,2,3>>,apa)),
  663:     ?line "hejsan" = [ Module:at(<<"hejsan">>,I) || I <- lists:seq(0,5) ],
  664: 
  665:     ?line badarg = ?MASK_ERROR(Module:bin_to_list(<<1,2,3>>,3,-4)),
  666:     ?line [1,2,3] = ?MASK_ERROR(Module:bin_to_list(<<1,2,3>>,3,-3)),
  667: 
  668:     ?line badarg = ?MASK_ERROR(Module:decode_unsigned(<<1,2,1:2>>,big)),
  669:     ?line badarg = ?MASK_ERROR(Module:decode_unsigned(<<1,2,1:2>>,little)),
  670:     ?line badarg = ?MASK_ERROR(Module:decode_unsigned(apa)),
  671:     ?line badarg = ?MASK_ERROR(Module:decode_unsigned(125,little)),
  672:     ?line 0 = ?MASK_ERROR(Module:decode_unsigned(<<>>,little)),
  673:     ?line 0 = ?MASK_ERROR(Module:decode_unsigned(<<>>,big)),
  674:     ?line 0 = ?MASK_ERROR(Module:decode_unsigned(<<0>>,little)),
  675:     ?line 0 = ?MASK_ERROR(Module:decode_unsigned(<<0>>,big)),
  676:     ?line 0 = ?MASK_ERROR(Module:decode_unsigned(make_unaligned(<<0>>),
  677: 						 little)),
  678:     ?line 0 = ?MASK_ERROR(Module:decode_unsigned(make_unaligned(<<0>>),big)),
  679:     ?line badarg = ?MASK_ERROR(Module:encode_unsigned(apa)),
  680:     ?line badarg = ?MASK_ERROR(Module:encode_unsigned(125.3,little)),
  681:     ?line badarg = ?MASK_ERROR(Module:encode_unsigned({1},little)),
  682:     ?line badarg = ?MASK_ERROR(Module:encode_unsigned([1],little)),
  683:     ?line <<0>> = ?MASK_ERROR(Module:encode_unsigned(0,little)),
  684:     ?line <<0>> = ?MASK_ERROR(Module:encode_unsigned(0,big)),
  685:     ok.
  686: 
  687: encode_decode(doc) ->
  688:     ["test binary:encode_unsigned/1,2 and binary:decode_unsigned/1,2"];
  689: encode_decode(Config) when is_list(Config) ->
  690:     ?line random:seed({1271,769940,559934}),
  691:     ?line ok = encode_decode_loop({1,200},1000), % Need to be long enough
  692: 						 % to create offheap binaries
  693:     ok.
  694: 
  695: encode_decode_loop(_Range,0) ->
  696:     ok;
  697: encode_decode_loop(Range, X) ->
  698:     ?line N = random_number(Range),
  699:     ?line A = binary:encode_unsigned(N),
  700:     ?line B = binary:encode_unsigned(N,big),
  701:     ?line C = binref:encode_unsigned(N),
  702:     ?line D = binref:encode_unsigned(N,big),
  703:     ?line E = binary:encode_unsigned(N,little),
  704:     ?line F = binref:encode_unsigned(N,little),
  705:     ?line G = binary:decode_unsigned(A),
  706:     ?line H = binary:decode_unsigned(A,big),
  707:     ?line I = binref:decode_unsigned(A),
  708:     ?line J = binary:decode_unsigned(E,little),
  709:     ?line K = binref:decode_unsigned(E,little),
  710:     ?line L = binary:decode_unsigned(make_unaligned(A)),
  711:     ?line M = binary:decode_unsigned(make_unaligned(E),little),
  712:     ?line PaddedBig = <<0:48,A/binary>>,
  713:     ?line PaddedLittle = <<E/binary,0:48>>,
  714:     ?line O = binary:decode_unsigned(PaddedBig),
  715:     ?line P = binary:decode_unsigned(make_unaligned(PaddedBig)),
  716:     ?line Q = binary:decode_unsigned(PaddedLittle,little),
  717:     ?line R = binary:decode_unsigned(make_unaligned(PaddedLittle),little),
  718:     ?line S = binref:decode_unsigned(PaddedLittle,little),
  719:     ?line T = binref:decode_unsigned(PaddedBig),
  720:     case (((A =:= B) and (B =:= C) and (C =:= D)) and
  721: 	  ((E =:= F)) and
  722: 	  ((N =:= G) and (G =:= H) and (H =:= I) and
  723: 	   (I =:= J) and (J =:= K) and (K =:= L) and (L =:= M)) and
  724: 	  ((M =:= O) and (O =:= P) and (P =:= Q) and (Q =:= R) and
  725: 	   (R =:= S) and (S =:= T)))of
  726: 	true ->
  727: 	    encode_decode_loop(Range,X-1);
  728: 	_ ->
  729: 	    io:format("Failed to encode/decode ~w~n(Results ~p)~n",
  730: 		      [N,[A,B,C,D,E,F,G,H,I,J,K,L,M,x,O,P,Q,R,S,T]]),
  731: 	    exit(mismatch)
  732:     end.
  733: 
  734: guard(doc) ->
  735:     ["Smoke test of the guard BIFs binary_part/2,3"];
  736: guard(Config) when is_list(Config) ->
  737:     {comment, "Guard tests are run in emulator test suite"}.
  738: 
  739: referenced(doc) ->
  740:     ["Test refernced_byte_size/1 bif."];
  741: referenced(Config) when is_list(Config) ->
  742:     ?line badarg = ?MASK_ERROR(binary:referenced_byte_size([])),
  743:     ?line badarg = ?MASK_ERROR(binary:referenced_byte_size(apa)),
  744:     ?line badarg = ?MASK_ERROR(binary:referenced_byte_size({})),
  745:     ?line badarg = ?MASK_ERROR(binary:referenced_byte_size(1)),
  746:     ?line A = <<1,2,3>>,
  747:     ?line B = binary:copy(A,1000),
  748:     ?line 3 = binary:referenced_byte_size(A),
  749:     ?line 3000 = binary:referenced_byte_size(B),
  750:     ?line <<_:8,C:2/binary>> = A,
  751:     ?line 3 = binary:referenced_byte_size(C),
  752:     ?line 2 = binary:referenced_byte_size(binary:copy(C)),
  753:     ?line <<_:7,D:2/binary,_:1>> = A,
  754:     ?line 2 = binary:referenced_byte_size(binary:copy(D)),
  755:     ?line 3 = binary:referenced_byte_size(D),
  756:     ?line <<_:8,E:2/binary,_/binary>> = B,
  757:     ?line 3000 = binary:referenced_byte_size(E),
  758:     ?line 2 = binary:referenced_byte_size(binary:copy(E)),
  759:     ?line <<_:7,F:2/binary,_:1,_/binary>> = B,
  760:     ?line 2 = binary:referenced_byte_size(binary:copy(F)),
  761:     ?line 3000 = binary:referenced_byte_size(F),
  762:     ok.
  763: 
  764: 
  765: 
  766: list_to_bin(doc) ->
  767:     ["Test list_to_bin/1 bif"];
  768: list_to_bin(Config) when is_list(Config) ->
  769:     %% Just some smoke_tests first, then go nuts with random cases
  770:     ?line badarg = ?MASK_ERROR(binary:list_to_bin({})),
  771:     ?line badarg = ?MASK_ERROR(binary:list_to_bin(apa)),
  772:     ?line badarg = ?MASK_ERROR(binary:list_to_bin(<<"apa">>)),
  773:     F1 = fun(L) ->
  774: 		 ?MASK_ERROR(binref:list_to_bin(L))
  775: 	 end,
  776:     F2 = fun(L) ->
  777: 		 ?MASK_ERROR(binary:list_to_bin(L))
  778: 	 end,
  779:     ?line random_iolist:run(1000,F1,F2),
  780:     ok.
  781: 
  782: copy(doc) ->
  783:     ["Test copy/1,2 bif's"];
  784: copy(Config) when is_list(Config) ->
  785:     ?line <<1,2,3>> = binary:copy(<<1,2,3>>),
  786:     ?line RS = random_string({1,10000}),
  787:     ?line RS = RS2 = binary:copy(RS),
  788:     ?line false = erts_debug:same(RS,RS2),
  789:     ?line <<>> = ?MASK_ERROR(binary:copy(<<1,2,3>>,0)),
  790:     ?line badarg = ?MASK_ERROR(binary:copy(<<1,2,3:3>>,2)),
  791:     ?line badarg = ?MASK_ERROR(binary:copy([],0)),
  792:     ?line <<>> = ?MASK_ERROR(binary:copy(<<>>,0)),
  793:     ?line badarg = ?MASK_ERROR(binary:copy(<<1,2,3>>,1.0)),
  794:     ?line badarg = ?MASK_ERROR(binary:copy(<<1,2,3>>,
  795: 					   16#FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)),
  796:     ?line <<>> = binary:copy(<<>>,10000),
  797:     ?line random:seed({1271,769940,559934}),
  798:     ?line ok = random_copy(3000),
  799:     ?line erts_debug:set_internal_state(available_internal_state,true),
  800:     ?line io:format("oldlimit: ~p~n",
  801: 		    [erts_debug:set_internal_state(binary_loop_limit,10)]),
  802:     ?line Subj = subj(),
  803:     ?line XX = binary:copy(Subj,1000),
  804:     ?line XX = binref:copy(Subj,1000),
  805:     ?line ok = random_copy(1000),
  806:     ?line kill_copy_loop(1000),
  807:     ?line io:format("limit was: ~p~n",
  808: 		    [erts_debug:set_internal_state(binary_loop_limit,
  809: 						   default)]),
  810:     ?line erts_debug:set_internal_state(available_internal_state,false),
  811:     ok.
  812: 
  813: kill_copy_loop(0) ->
  814:     ok;
  815: kill_copy_loop(N) ->
  816:     {Pid,Ref} = spawn_monitor(fun() ->
  817: 				    ok = random_copy(1000)
  818: 			      end),
  819:     receive
  820:     after 10 ->
  821: 	    ok
  822:     end,
  823:     exit(Pid,kill),
  824:     receive
  825: 	{'DOWN',Ref,process,Pid,_} ->
  826: 	    kill_copy_loop(N-1)
  827:     after 1000 ->
  828: 	    exit(did_not_die)
  829:     end.
  830: 
  831: random_copy(0) ->
  832:     ok;
  833: random_copy(N) ->
  834:     Str = random_string({0,N}),
  835:     Num = random:uniform(N div 10+1),
  836:     A = ?MASK_ERROR(binary:copy(Str,Num)),
  837:     B = ?MASK_ERROR(binref:copy(Str,Num)),
  838:     C = ?MASK_ERROR(binary:copy(make_unaligned(Str),Num)),
  839:     case {(A =:= B), (B =:= C)} of
  840: 	{true,true} ->
  841: 	    random_copy(N-1);
  842: 	_ ->
  843: 	    io:format("Failed to pick copy ~s ~p times~n",
  844: 		      [Str,Num]),
  845: 	    io:format("A:~p,~nB:~p,~n,C:~p.~n",
  846: 		      [A,B,C]),
  847: 	    exit(mismatch)
  848:     end.
  849: 
  850: bin_to_list(doc) ->
  851:     ["Test bin_to_list/1,2,3 bif's"];
  852: bin_to_list(Config) when is_list(Config) ->
  853:     %% Just some smoke_tests first, then go nuts with random cases
  854:     ?line X = <<1,2,3,4,0:1000000,5>>,
  855:     ?line Y = make_unaligned(X),
  856:     ?line LX = binary:bin_to_list(X),
  857:     ?line LX = binary:bin_to_list(X,0,byte_size(X)),
  858:     ?line LX = binary:bin_to_list(X,byte_size(X),-byte_size(X)),
  859:     ?line LX = binary:bin_to_list(X,{0,byte_size(X)}),
  860:     ?line LX = binary:bin_to_list(X,{byte_size(X),-byte_size(X)}),
  861:     ?line LY = binary:bin_to_list(Y),
  862:     ?line LY = binary:bin_to_list(Y,0,byte_size(Y)),
  863:     ?line LY = binary:bin_to_list(Y,byte_size(Y),-byte_size(Y)),
  864:     ?line LY = binary:bin_to_list(Y,{0,byte_size(Y)}),
  865:     ?line LY = binary:bin_to_list(Y,{byte_size(Y),-byte_size(Y)}),
  866:     ?line 1 = hd(LX),
  867:     ?line 5 = lists:last(LX),
  868:     ?line 1 = hd(LY),
  869:     ?line 5 = lists:last(LY),
  870:     ?line X = list_to_binary(LY),
  871:     ?line Y = list_to_binary(LY),
  872:     ?line X = list_to_binary(LY),
  873:     ?line [5] = lists:nthtail(byte_size(X)-1,LX),
  874:     ?line [0,5] = lists:nthtail(byte_size(X)-2,LX),
  875:     ?line [0,5] = lists:nthtail(byte_size(Y)-2,LY),
  876:     ?line random:seed({1271,769940,559934}),
  877:     ?line ok = random_bin_to_list(5000),
  878:     ok.
  879: 
  880: random_bin_to_list(0) ->
  881:     ok;
  882: random_bin_to_list(N) ->
  883:     Str = random_string({1,N}),
  884:     Parts0 = random_parts(10,N),
  885:     Parts1 = Parts0 ++ [ {X+Y,-Y} || {X,Y} <- Parts0 ],
  886:     [ begin
  887: 	  try
  888: 	  true = ?MASK_ERROR(binary:bin_to_list(Str,Z)) =:=
  889: 	      ?MASK_ERROR(binref:bin_to_list(Str,Z)),
  890: 	  true = ?MASK_ERROR(binary:bin_to_list(Str,Z)) =:=
  891: 	      ?MASK_ERROR(binary:bin_to_list(make_unaligned(Str),Z))
  892: 	  catch
  893: 	      _:_ ->
  894: 		  io:format("Error, Str = <<\"~s\">>.~nZ = ~p.~n",
  895: 			    [Str,Z]),
  896: 		  exit(badresult)
  897: 	  end
  898:       end || Z <- Parts1 ],
  899:     [ begin
  900: 	  try
  901: 	  true = ?MASK_ERROR(binary:bin_to_list(Str,A,B)) =:=
  902: 	      ?MASK_ERROR(binref:bin_to_list(Str,A,B)),
  903: 	  true = ?MASK_ERROR(binary:bin_to_list(Str,A,B)) =:=
  904: 	      ?MASK_ERROR(binary:bin_to_list(make_unaligned(Str),A,B))
  905: 	  catch
  906: 	      _:_ ->
  907: 		  io:format("Error, Str = <<\"~s\">>.~nA = ~p.~nB = ~p.~n",
  908: 			    [Str,A,B]),
  909: 		  exit(badresult)
  910: 	  end
  911:       end || {A,B} <- Parts1 ],
  912:     random_bin_to_list(N-1).
  913: 
  914: parts(doc) ->
  915:     ["Test the part/2,3 bif's"];
  916: parts(Config) when is_list(Config) ->
  917:     %% Some simple smoke tests to begin with
  918:     ?line Simple = <<1,2,3,4,5,6,7,8>>,
  919:     ?line <<1,2>> = binary:part(Simple,0,2),
  920:     ?line <<1,2>> = binary:part(Simple,{0,2}),
  921:     ?line Simple = binary:part(Simple,0,8),
  922:     ?line Simple = binary:part(Simple,{0,8}),
  923:     ?line badarg = ?MASK_ERROR(binary:part(Simple,0,9)),
  924:     ?line badarg = ?MASK_ERROR(binary:part(Simple,{0,9})),
  925:     ?line badarg = ?MASK_ERROR(binary:part(Simple,1,8)),
  926:     ?line badarg = ?MASK_ERROR(binary:part(Simple,{1,8})),
  927:     ?line badarg = ?MASK_ERROR(binary:part(Simple,{3,-4})),
  928:     ?line badarg = ?MASK_ERROR(binary:part(Simple,{3.0,1})),
  929:     ?line badarg = ?MASK_ERROR(
  930: 		      binary:part(Simple,{16#FFFFFFFFFFFFFFFFFFFFFFFFFFFFF
  931: 					  ,1})),
  932:     ?line <<2,3,4,5,6,7,8>> = binary:part(Simple,{1,7}),
  933:     ?line <<2,3,4,5,6,7,8>> = binary:part(Simple,{8,-7}),
  934:     ?line Simple = binary:part(Simple,{8,-8}),
  935:     ?line badarg = ?MASK_ERROR(binary:part(Simple,{1,-8})),
  936:     ?line badarg = ?MASK_ERROR(binary:part(Simple,{8,-9})),
  937:     ?line badarg = ?MASK_ERROR(binary:part(Simple,{0,-1})),
  938:     ?line <<>> = binary:part(Simple,{8,0}),
  939:     ?line badarg = ?MASK_ERROR(binary:part(Simple,{9,0})),
  940:     ?line badarg = ?MASK_ERROR(binary:part(Simple,{-1,0})),
  941:     ?line badarg = ?MASK_ERROR(binary:part(Simple,{7,2})),
  942:     ?line <<8>> = binary:part(Simple,{7,1}),
  943:     ?line random:seed({1271,769940,559934}),
  944:     ?line random_parts(5000),
  945:     ok.
  946: 
  947: 
  948: random_parts(0) ->
  949:     ok;
  950: random_parts(N) ->
  951:     Str = random_string({1,N}),
  952:     Parts0 = random_parts(10,N),
  953:     Parts1 = Parts0 ++ [ {X+Y,-Y} || {X,Y} <- Parts0 ],
  954:     [ begin
  955: 	  true = ?MASK_ERROR(binary:part(Str,Z)) =:=
  956: 	      ?MASK_ERROR(binref:part(Str,Z)),
  957: 	  true = ?MASK_ERROR(binary:part(Str,Z)) =:=
  958: 	      ?MASK_ERROR(erlang:binary_part(Str,Z)),
  959: 	  true = ?MASK_ERROR(binary:part(Str,Z)) =:=
  960: 	      ?MASK_ERROR(binary:part(make_unaligned(Str),Z))
  961:       end || Z <- Parts1 ],
  962:     random_parts(N-1).
  963: 
  964: random_parts(0,_) ->
  965:     [];
  966: random_parts(X,N) ->
  967:     Pos = random:uniform(N),
  968:     Len = random:uniform((Pos * 12) div 10),
  969:     [{Pos,Len} | random_parts(X-1,N)].
  970: 
  971: random_ref_comp(doc) ->
  972:     ["Test pseudorandomly generated cases against reference imlementation"];
  973: random_ref_comp(Config) when is_list(Config) ->
  974:     ?line put(success_counter,0),
  975:     ?line random:seed({1271,769940,559934}),
  976:     ?line do_random_match_comp(5000,{1,40},{30,1000}),
  977:     io:format("Number of successes: ~p~n",[get(success_counter)]),
  978:     ?line do_random_match_comp2(5000,{1,40},{30,1000}),
  979:     io:format("Number of successes: ~p~n",[get(success_counter)]),
  980:     ?line do_random_match_comp3(5000,{1,40},{30,1000}),
  981:     io:format("Number of successes: ~p~n",[get(success_counter)]),
  982:     ?line do_random_match_comp4(5000,{1,40},{30,1000}),
  983:     io:format("Number of successes: ~p~n",[get(success_counter)]),
  984:     ?line do_random_matches_comp(5000,{1,40},{30,1000}),
  985:     io:format("Number of successes: ~p~n",[get(success_counter)]),
  986:     ?line do_random_matches_comp2(5000,{1,40},{30,1000}),
  987:     io:format("Number of successes: ~p~n",[get(success_counter)]),
  988:     ?line do_random_matches_comp3(5,{1,40},{30,1000}),
  989:     ?line erts_debug:set_internal_state(available_internal_state,true),
  990:     ?line io:format("oldlimit: ~p~n",[ erts_debug:set_internal_state(binary_loop_limit,100)]),
  991:     ?line do_random_match_comp(5000,{1,40},{30,1000}),
  992:     ?line do_random_matches_comp3(5,{1,40},{30,1000}),
  993:     ?line io:format("limit was: ~p~n",[ erts_debug:set_internal_state(binary_loop_limit,default)]),
  994:     ?line erts_debug:set_internal_state(available_internal_state,false),
  995:     ok.
  996: 
  997: random_ref_sr_comp(doc) ->
  998:     ["Test pseudorandomly generated cases against reference imlementation of split and replace"];
  999: random_ref_sr_comp(Config) when is_list(Config) ->
 1000:     ?line put(success_counter,0),
 1001:     ?line random:seed({1271,769940,559934}),
 1002:     ?line do_random_split_comp(5000,{1,40},{30,1000}),
 1003:     io:format("Number of successes: ~p~n",[get(success_counter)]),
 1004:     ?line do_random_replace_comp(5000,{1,40},{30,1000}),
 1005:     io:format("Number of successes: ~p~n",[get(success_counter)]),
 1006:     ?line do_random_split_comp2(5000,{1,40},{30,1000}),
 1007:     io:format("Number of successes: ~p~n",[get(success_counter)]),
 1008:     ?line do_random_replace_comp2(5000,{1,40},{30,1000}),
 1009:     io:format("Number of successes: ~p~n",[get(success_counter)]),
 1010:     ok.
 1011: random_ref_fla_comp(doc) ->
 1012:     ["Test pseudorandomly generated cases against reference imlementation of split and replace"];
 1013: random_ref_fla_comp(Config) when is_list(Config) ->
 1014:     ?line put(success_counter,0),
 1015:     ?line random:seed({1271,769940,559934}),
 1016:     ?line do_random_first_comp(5000,{1,1000}),
 1017:     ?line do_random_last_comp(5000,{1,1000}),
 1018:     ?line do_random_at_comp(5000,{1,1000}),
 1019:     io:format("Number of successes: ~p~n",[get(success_counter)]),
 1020:     ok.
 1021: 
 1022: do_random_first_comp(0,_) ->
 1023:     ok;
 1024: do_random_first_comp(N,Range) ->
 1025:     S = random_string(Range),
 1026:     A = ?MASK_ERROR(binref:first(S)),
 1027:     B = ?MASK_ERROR(binary:first(S)),
 1028:     C = ?MASK_ERROR(binary:first(make_unaligned(S))),
 1029:     case {(A =:= B), (B =:= C)} of
 1030: 	{true,true} ->
 1031: 	    do_random_first_comp(N-1,Range);
 1032: 	_ ->
 1033: 	    io:format("Failed to pick first of ~s~n",
 1034: 		      [S]),
 1035: 	    io:format("A:~p,~nB:~p,~n,C:~p.~n",
 1036: 		      [A,B,C]),
 1037: 	    exit(mismatch)
 1038:     end.
 1039: 
 1040: do_random_last_comp(0,_) ->
 1041:     ok;
 1042: do_random_last_comp(N,Range) ->
 1043:     S = random_string(Range),
 1044:     A = ?MASK_ERROR(binref:last(S)),
 1045:     B = ?MASK_ERROR(binary:last(S)),
 1046:     C = ?MASK_ERROR(binary:last(make_unaligned(S))),
 1047:     case {(A =:= B), (B =:= C)} of
 1048: 	{true,true} ->
 1049: 	    do_random_last_comp(N-1,Range);
 1050: 	_ ->
 1051: 	    io:format("Failed to pick last of ~s~n",
 1052: 		      [S]),
 1053: 	    io:format("A:~p,~nB:~p,~n,C:~p.~n",
 1054: 		      [A,B,C]),
 1055: 	    exit(mismatch)
 1056:     end.
 1057: do_random_at_comp(0,_) ->
 1058:     ok;
 1059: do_random_at_comp(N,{Min,Max}=Range) ->
 1060:     S = random_string(Range),
 1061:     XMax = Min + ((Max - Min) * 3) div 4,
 1062:     Pos = random_length({Min,XMax}), %% some out of range
 1063:     A = ?MASK_ERROR(binref:at(S,Pos)),
 1064:     B = ?MASK_ERROR(binary:at(S,Pos)),
 1065:     C = ?MASK_ERROR(binary:at(make_unaligned(S),Pos)),
 1066:     if
 1067: 	A =/= badarg ->
 1068: 	    put(success_counter,get(success_counter)+1);
 1069: 	true ->
 1070: 	    ok
 1071:     end,
 1072:     case {(A =:= B), (B =:= C)} of
 1073: 	{true,true} ->
 1074: 	    do_random_at_comp(N-1,Range);
 1075: 	_ ->
 1076: 	    io:format("Failed to pick last of ~s~n",
 1077: 		      [S]),
 1078: 	    io:format("A:~p,~nB:~p,~n,C:~p.~n",
 1079: 		      [A,B,C]),
 1080: 	    exit(mismatch)
 1081:     end.
 1082: 
 1083: do_random_matches_comp(0,_,_) ->
 1084:     ok;
 1085: do_random_matches_comp(N,NeedleRange,HaystackRange) ->
 1086:     NumNeedles = element(2,HaystackRange) div element(2,NeedleRange),
 1087:     Needles = [random_string(NeedleRange) ||
 1088: 		  _ <- lists:duplicate(NumNeedles,a)],
 1089:     Haystack = random_string(HaystackRange),
 1090:     true = do_matches_comp(Needles,Haystack),
 1091:     do_random_matches_comp(N-1,NeedleRange,HaystackRange).
 1092: 
 1093: do_random_matches_comp2(0,_,_) ->
 1094:     ok;
 1095: do_random_matches_comp2(N,NeedleRange,HaystackRange) ->
 1096:     NumNeedles = element(2,HaystackRange) div element(2,NeedleRange),
 1097:     Haystack = random_string(HaystackRange),
 1098:     Needles = [random_substring(NeedleRange,Haystack) ||
 1099: 		  _ <- lists:duplicate(NumNeedles,a)],
 1100:     true = do_matches_comp(Needles,Haystack),
 1101:     do_random_matches_comp2(N-1,NeedleRange,HaystackRange).
 1102: 
 1103: do_random_matches_comp3(0,_,_) ->
 1104:     ok;
 1105: do_random_matches_comp3(N,NeedleRange,HaystackRange) ->
 1106:     NumNeedles = element(2,HaystackRange) div element(2,NeedleRange),
 1107:     Haystack = random_string(HaystackRange),
 1108:     Needles = [random_substring(NeedleRange,Haystack) ||
 1109: 		  _ <- lists:duplicate(NumNeedles,a)],
 1110:     RefRes = binref:matches(Haystack,Needles),
 1111:     true = do_matches_comp_loop(10000,Needles,Haystack, RefRes),
 1112:     do_random_matches_comp3(N-1,NeedleRange,HaystackRange).
 1113: 
 1114: do_matches_comp_loop(0,_,_,_) ->
 1115:     true;
 1116: do_matches_comp_loop(N, Needles, Haystack0,RR) ->
 1117:     DummySize=N*8,
 1118:     Haystack1 = <<0:DummySize,Haystack0/binary>>,
 1119:     RR1=[{X+N,Y} || {X,Y} <- RR],
 1120:     true = do_matches_comp2(Needles,Haystack1,RR1),
 1121:     Haystack2 = <<Haystack0/binary,Haystack1/binary>>,
 1122:     RR2 = RR ++ [{X2+N+byte_size(Haystack0),Y2} || {X2,Y2} <- RR],
 1123:     true = do_matches_comp2(Needles,Haystack2,RR2),
 1124:     do_matches_comp_loop(N-1, Needles, Haystack0,RR).
 1125: 
 1126: 
 1127: do_matches_comp2(N,H,A) ->
 1128:     C = ?MASK_ERROR(binary:matches(H,N)),
 1129:     case (A =:= C) of
 1130: 	true ->
 1131: 	    true;
 1132: 	_ ->
 1133: 	    io:format("Failed to match ~p (needle) against ~s (haystack)~n",
 1134: 		      [N,H]),
 1135: 	    io:format("A:~p,~n,C:~p.~n",
 1136: 		      [A,C]),
 1137: 	    exit(mismatch)
 1138:     end.
 1139: do_matches_comp(N,H) ->
 1140:     A = ?MASK_ERROR(binref:matches(H,N)),
 1141:     B = ?MASK_ERROR(binref:matches(H,binref:compile_pattern(N))),
 1142:     C = ?MASK_ERROR(binary:matches(H,N)),
 1143:     D = ?MASK_ERROR(binary:matches(make_unaligned(H),
 1144: 				   binary:compile_pattern([make_unaligned2(X) || X <- N]))),
 1145:     if
 1146: 	A =/= nomatch ->
 1147: 	    put(success_counter,get(success_counter)+1);
 1148: 	true ->
 1149: 	    ok
 1150:     end,
 1151:     case {(A =:= B), (B =:= C),(C =:= D)} of
 1152: 	{true,true,true} ->
 1153: 	    true;
 1154: 	_ ->
 1155: 	    io:format("Failed to match ~p (needle) against ~s (haystack)~n",
 1156: 		      [N,H]),
 1157: 	    io:format("A:~p,~nB:~p,~n,C:~p,~n,D:~p.~n",
 1158: 		      [A,B,C,D]),
 1159: 	    exit(mismatch)
 1160:     end.
 1161: 
 1162: do_random_match_comp(0,_,_) ->
 1163:     ok;
 1164: do_random_match_comp(N,NeedleRange,HaystackRange) ->
 1165:     Needle = random_string(NeedleRange),
 1166:     Haystack = random_string(HaystackRange),
 1167:     true = do_match_comp(Needle,Haystack),
 1168:     do_random_match_comp(N-1,NeedleRange,HaystackRange).
 1169: 
 1170: do_random_match_comp2(0,_,_) ->
 1171:     ok;
 1172: do_random_match_comp2(N,NeedleRange,HaystackRange) ->
 1173:     Haystack = random_string(HaystackRange),
 1174:     Needle = random_substring(NeedleRange,Haystack),
 1175:     true = do_match_comp(Needle,Haystack),
 1176:     do_random_match_comp2(N-1,NeedleRange,HaystackRange).
 1177: 
 1178: do_random_match_comp3(0,_,_) ->
 1179:     ok;
 1180: do_random_match_comp3(N,NeedleRange,HaystackRange) ->
 1181:     NumNeedles = element(2,HaystackRange) div element(2,NeedleRange),
 1182:     Haystack = random_string(HaystackRange),
 1183:     Needles = [random_substring(NeedleRange,Haystack) ||
 1184: 		  _ <- lists:duplicate(NumNeedles,a)],
 1185:     true = do_match_comp3(Needles,Haystack),
 1186:     do_random_match_comp3(N-1,NeedleRange,HaystackRange).
 1187: 
 1188: do_random_match_comp4(0,_,_) ->
 1189:     ok;
 1190: do_random_match_comp4(N,NeedleRange,HaystackRange) ->
 1191:     NumNeedles = element(2,HaystackRange) div element(2,NeedleRange),
 1192:     Haystack = random_string(HaystackRange),
 1193:     Needles = [random_string(NeedleRange) ||
 1194: 		  _ <- lists:duplicate(NumNeedles,a)],
 1195:     true = do_match_comp3(Needles,Haystack),
 1196:     do_random_match_comp4(N-1,NeedleRange,HaystackRange).
 1197: 
 1198: do_match_comp(N,H) ->
 1199:     A = ?MASK_ERROR(binref:match(H,N)),
 1200:     B = ?MASK_ERROR(binref:match(H,binref:compile_pattern([N]))),
 1201:     C = ?MASK_ERROR(binary:match(make_unaligned(H),N)),
 1202:     D = ?MASK_ERROR(binary:match(H,binary:compile_pattern([N]))),
 1203:     E = ?MASK_ERROR(binary:match(H,binary:compile_pattern(make_unaligned(N)))),
 1204:     if
 1205: 	A =/= nomatch ->
 1206: 	    put(success_counter,get(success_counter)+1);
 1207: 	true ->
 1208: 	    ok
 1209:     end,
 1210:     case {(A =:= B), (B =:= C),(C =:= D),(D =:= E)} of
 1211: 	{true,true,true,true} ->
 1212: 	    true;
 1213: 	_ ->
 1214: 	    io:format("Failed to match ~s (needle) against ~s (haystack)~n",
 1215: 		      [N,H]),
 1216: 	    io:format("A:~p,~nB:~p,~n,C:~p,~n,D:~p,E:~p.~n",
 1217: 		      [A,B,C,D,E]),
 1218: 	    exit(mismatch)
 1219:     end.
 1220: 
 1221: do_match_comp3(N,H) ->
 1222:     A = ?MASK_ERROR(binref:match(H,N)),
 1223:     B = ?MASK_ERROR(binref:match(H,binref:compile_pattern(N))),
 1224:     C = ?MASK_ERROR(binary:match(H,N)),
 1225:     D = ?MASK_ERROR(binary:match(H,binary:compile_pattern(N))),
 1226:     if
 1227: 	A =/= nomatch ->
 1228: 	    put(success_counter,get(success_counter)+1);
 1229: 	true ->
 1230: 	    ok
 1231:     end,
 1232:     case {(A =:= B), (B =:= C),(C =:= D)} of
 1233: 	{true,true,true} ->
 1234: 	    true;
 1235: 	_ ->
 1236: 	    io:format("Failed to match ~s (needle) against ~s (haystack)~n",
 1237: 		      [N,H]),
 1238: 	    io:format("A:~p,~nB:~p,~n,C:~p,~n,D:~p.~n",
 1239: 		      [A,B,C,D]),
 1240: 	    exit(mismatch)
 1241:     end.
 1242: 
 1243: do_random_split_comp(0,_,_) ->
 1244:     ok;
 1245: do_random_split_comp(N,NeedleRange,HaystackRange) ->
 1246:     Haystack = random_string(HaystackRange),
 1247:     Needle = random_substring(NeedleRange,Haystack),
 1248:     true = do_split_comp(Needle,Haystack,[]),
 1249:     true = do_split_comp(Needle,Haystack,[global]),
 1250:     true = do_split_comp(Needle,Haystack,[global,trim]),
 1251:     do_random_split_comp(N-1,NeedleRange,HaystackRange).
 1252: do_random_split_comp2(0,_,_) ->
 1253:     ok;
 1254: do_random_split_comp2(N,NeedleRange,HaystackRange) ->
 1255:     NumNeedles = element(2,HaystackRange) div element(2,NeedleRange),
 1256:     Haystack = random_string(HaystackRange),
 1257:     Needles = [random_substring(NeedleRange,Haystack) ||
 1258: 		  _ <- lists:duplicate(NumNeedles,a)],
 1259:     true = do_split_comp(Needles,Haystack,[]),
 1260:     true = do_split_comp(Needles,Haystack,[global]),
 1261:     do_random_split_comp2(N-1,NeedleRange,HaystackRange).
 1262: 
 1263: do_split_comp(N,H,Opts) ->
 1264:     A = ?MASK_ERROR(binref:split(H,N,Opts)),
 1265:     D = ?MASK_ERROR(binary:split(H,binary:compile_pattern(N),Opts)),
 1266:     if
 1267: 	(A =/= [N]) and is_list(A) ->
 1268: 	    put(success_counter,get(success_counter)+1);
 1269: 	true ->
 1270: 	    ok
 1271:     end,
 1272:     case (A =:= D) of
 1273: 	true ->
 1274: 	    true;
 1275: 	_ ->
 1276: 	    io:format("Failed to split ~n~p ~n(haystack) with ~n~p ~n(needle) "
 1277: 		      "~nand options ~p~n",
 1278: 		      [H,N,Opts]),
 1279: 	    io:format("A:~p,D:~p.~n",
 1280: 		      [A,D]),
 1281: 	    exit(mismatch)
 1282:     end.
 1283: 
 1284: do_random_replace_comp(0,_,_) ->
 1285:     ok;
 1286: do_random_replace_comp(N,NeedleRange,HaystackRange) ->
 1287:     Haystack = random_string(HaystackRange),
 1288:     Needle = random_substring(NeedleRange,Haystack),
 1289:     Repl = random_string(NeedleRange),
 1290:     Insertat = random_length(NeedleRange), %Sometimes larger than Repl
 1291:     true = do_replace_comp(Needle,Haystack,Repl,[]),
 1292:     true = do_replace_comp(Needle,Haystack,Repl,[global]),
 1293:     true = do_replace_comp(Needle,Haystack,Repl,
 1294: 			 [global,{insert_replaced,Insertat}]),
 1295:     do_random_replace_comp(N-1,NeedleRange,HaystackRange).
 1296: do_random_replace_comp2(0,_,_) ->
 1297:     ok;
 1298: do_random_replace_comp2(N,NeedleRange,HaystackRange) ->
 1299:     NumNeedles = element(2,HaystackRange) div element(2,NeedleRange),
 1300:     Haystack = random_string(HaystackRange),
 1301:     Needles = [random_substring(NeedleRange,Haystack) ||
 1302: 		  _ <- lists:duplicate(NumNeedles,a)],
 1303:     Repl = random_string(NeedleRange),
 1304:     Insertat = random_length(NeedleRange), %Sometimes larger than Repl
 1305:     true = do_replace_comp(Needles,Haystack,Repl,[]),
 1306:     true = do_replace_comp(Needles,Haystack,Repl,[global]),
 1307:     true = do_replace_comp(Needles,Haystack,Repl,
 1308: 			 [global,{insert_replaced,Insertat}]),
 1309:     do_random_replace_comp2(N-1,NeedleRange,HaystackRange).
 1310: 
 1311: do_replace_comp(N,H,R,Opts) ->
 1312:     A = ?MASK_ERROR(binref:replace(H,N,R,Opts)),
 1313:     D = ?MASK_ERROR(binary:replace(H,binary:compile_pattern(N),R,Opts)),
 1314:     if
 1315: 	(A =/= N) and is_binary(A) ->
 1316: 	    put(success_counter,get(success_counter)+1);
 1317: 	true ->
 1318: 	    ok
 1319:     end,
 1320:     case (A =:= D) of
 1321: 	true ->
 1322: 	    true;
 1323: 	_ ->
 1324: 	    io:format("Failed to replace ~s (haystack) by ~s (needle) "
 1325: 		      "inserting ~s (replacement) and options ~p~n",
 1326: 		      [H,N,R,Opts]),
 1327: 	    io:format("A:~p,D:~p.~n",
 1328: 		      [A,D]),
 1329: 	    exit(mismatch)
 1330:     end.
 1331: 
 1332: one_random_number(N) ->
 1333:     M = ((N - 1) rem 10) + 1,
 1334:     element(M,{$0,$1,$2,$3,$4,$5,$6,$7,$8,$9}).
 1335: 
 1336: one_random(N) ->
 1337:     M = ((N - 1) rem 68) + 1,
 1338:     element(M,{$a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k,$l,$m,$n,$o,$p,$q,$r,$s,$t,
 1339: 	       $u,$v,$w,$x,$y,$z,$å,$ä,$ö,$A,$B,$C,$D,$E,$F,$G,$H,
 1340: 	       $I,$J,$K,$L,$M,$N,$O,$P,$Q,$R,$S,$T,$U,$V,$W,$X,$Y,$Z,$Å,
 1341: 	       $Ä,$Ö,$0,$1,$2,$3,$4,$5,$6,$7,$8,$9}).
 1342: 
 1343: random_number({Min,Max}) -> % Min and Max are *length* of number in
 1344:                             % decimal positions
 1345:     X = random:uniform(Max - Min + 1) + Min - 1,
 1346:     list_to_integer([one_random_number(random:uniform(10)) || _ <- lists:seq(1,X)]).
 1347: 
 1348: 
 1349: random_length({Min,Max}) ->
 1350:     random:uniform(Max - Min + 1) + Min - 1.
 1351: random_string({Min,Max}) ->
 1352:     X = random:uniform(Max - Min + 1) + Min - 1,
 1353:     list_to_binary([one_random(random:uniform(68)) || _ <- lists:seq(1,X)]).
 1354: random_substring({Min,Max},Hay) ->
 1355:     X = random:uniform(Max - Min + 1) + Min - 1,
 1356:     Y = byte_size(Hay),
 1357:     Z = if
 1358: 	    X > Y -> Y;
 1359: 	    true -> X
 1360: 	end,
 1361:     PMax = Y - Z,
 1362:     Pos = random:uniform(PMax + 1) - 1,
 1363:     <<_:Pos/binary,Res:Z/binary,_/binary>> = Hay,
 1364:     Res.
 1365: 
 1366: mask_error({'EXIT',{Err,_}}) ->
 1367:     Err;
 1368: mask_error(Else) ->
 1369:     Else.
 1370: 
 1371: make_unaligned(Bin0) when is_binary(Bin0) ->
 1372:     Bin1 = <<0:3,Bin0/binary,31:5>>,
 1373:     Sz = byte_size(Bin0),
 1374:     <<0:3,Bin:Sz/binary,31:5>> = id(Bin1),
 1375:     Bin.
 1376: make_unaligned2(Bin0) when is_binary(Bin0) ->
 1377:     Bin1 = <<31:5,Bin0/binary,0:3>>,
 1378:     Sz = byte_size(Bin0),
 1379:     <<31:5,Bin:Sz/binary,0:3>> = id(Bin1),
 1380:     Bin.
 1381: 
 1382: id(I) -> I.