f798582016-01-18Martin Nilsson START_MARKER
3f895d2016-03-17Martin Nilsson test_do( add_constant("real_random", random) )
c9c99f2016-01-18Martin Nilsson dnl int random(int) test_eq(random(-1), 0) test_eq(random(0), 0) test_eq(random(1), 0)
3f895d2016-03-17Martin Nilsson test_do( add_constant("random", Random.Deterministic(42)->random) ) test_eq( random(0x7fffffff), 1351601066) test_eq( random(10), 1) test_eq( random(10), 4) test_eq( random(10), 5)
92b3772016-03-31Henrik Grubbström (Grubba) cond(Pike.get_runtime_info()->int_size == 64, [[
e754912016-04-23Henrik Grubbström (Grubba) test_eq( random(0x7ffffffff), 27692873120) test_eq( random(0x7fffffffffffffff), 5484257398290798648) test_eq( random(0x7ffffffffffffffff), 120525567616026645578)
92b3772016-03-31Henrik Grubbström (Grubba) ]]) cond(Pike.get_runtime_info()->int_size == 32, [[ test_eq( random(0x7ffffffff), 3047125750) test_eq( random(0x7fffffffffffffff), 4060256752354925388) test_eq( random(0x7ffffffffffffffff), 120525567616026645578) ]])
3f895d2016-03-17Martin Nilsson test_do( add_constant("random", real_random) )
c9c99f2016-01-18Martin Nilsson  dnl float random(float) test_eq(random(-1.0), 0.0) test_eq(random(0.0), 0.0)
3f895d2016-03-17Martin Nilsson test_do( add_constant("random", Random.Deterministic(42)->random) ) test_eq(sprintf("%.4f", random(10.0)), "7.7220") test_eq(sprintf("%.4f", random(10.0)), "3.9002") test_eq(sprintf("%.4f", random(10.0)), "2.9730") test_do( add_constant("random", real_random) )
c9c99f2016-01-18Martin Nilsson  dnl mixed random(array) test_eval_error(random(({}))) test_eq( random(({7})), 7 )
3f895d2016-03-17Martin Nilsson test_do( add_constant("random", Random.Deterministic(20)->random) ) test_any([[ string res = ""; for(int i; i<10; i++) res += random( ({1,2,3,4}) ); return res; ]], "1341312312") test_do( add_constant("random", real_random) )
c9c99f2016-01-18Martin Nilsson  dnl mixed random(multiset) test_eval_error(random((<>)))
3f895d2016-03-17Martin Nilsson test_eq( random((<8>)), 8 ) test_do( add_constant("random", Random.Deterministic(11)->random) ) test_any([[ mapping lex=([]); string res = ""; multiset m = (< "a","b","c","d" >); for(int i; i<10; i++) { string s = random(m); if( !lex[s] ) lex[s] = sizeof(lex); res += lex[s]; } return res; ]], "0121234231") test_do( add_constant("random", real_random) )
c9c99f2016-01-18Martin Nilsson  dnl array random(mapping) test_eval_error(random(([]))) test_equal( random(([5:6])), ({5,6}) )
3f895d2016-03-17Martin Nilsson test_do( add_constant("random", Random.Deterministic(11)->random) ) test_any([[ mapping lex=([]); string res = ""; mapping m = ([ "1":1, "2":2, "3":3, "4":4 ]); for(int i; i<10; i++) { string s = random(m)[0]; if( !lex[s] ) lex[s] = sizeof(lex); res += lex[s]; } return res; ]], "0121234231") test_do( add_constant("random", real_random) )
c9c99f2016-01-18Martin Nilsson  dnl mixed random(object)
d7fdf72016-03-09Martin Nilsson test_eq([[ random( class() { function _random(function a, function b) { return a; }}() ) ]], random_string) test_eq([[ random( class() { function _random(function a, function b) { return b; }}() ) ]], random)
c9c99f2016-01-18Martin Nilsson  dnl string random_string(int) test_eval_error( return random_string(-1); ) test_eq( random_string(0), "" ) test_eq( sizeof(random_string(47)), 47 )
f798582016-01-18Martin Nilsson test_any([[ string x = "\377"*16; for(int i; i<100; i++) x &= random_string(16); return x; ]], "\0"*16) test_any([[ string x = "\377"*16; for(int i; i<100; i++) x &= ~random_string(16); return x; ]], "\0"*16)
c9c99f2016-01-18Martin Nilsson test_do([[ class R { inherit Random.Interface; int counter; string random_string(int len) { String.Buffer b = String.Buffer(len); for(int i; i<len; i++) b->putchar( (counter + i)&0xff ); counter += len; return (string)b; } }; add_constant("RND",R()); ]]) test_eq( RND->random_string(5), "\0\1\2\3\4" )
a7315e2016-03-17Martin Nilsson test_eq( RND->counter, 5)
c9c99f2016-01-18Martin Nilsson test_eq( RND->random_string(5), "\5\6\7\10\11" )
a7315e2016-03-17Martin Nilsson test_eq( RND->counter, 10)
c9c99f2016-01-18Martin Nilsson test_eq( RND->random(10), 0 ) test_eq( RND->counter, 18)
a7315e2016-03-17Martin Nilsson test_eq( sprintf("%.4f", RND->random(10.0)), "0.9802" ) test_eq( RND->counter, 26) test_eq( RND->random( ({1,2,3}) ), 3 ) test_eq( RND->counter, 34) test_eq( RND->random( (<1,2,3>) ), 3 ) test_equal( RND->random( ([1:2,3:4]) ), ({ 3,4 })) test_eq( RND->counter, 34)
c9c99f2016-01-18Martin Nilsson test_do( add_constant("RND") ) test_eval_error([[ class R { inherit Random.Interface; string random_string(int len) { return 0; } }()->random(10); ]]) test_eval_error([[ class R { inherit Random.Interface; string random_string(int len) { return "12345"; } }()->random(10); ]]) dnl test_do([[ dnl class R { dnl inherit Random.Interface; dnl string random_string(int len) { return 0; } dnl variant int random(int limit) { return ::random(limit); } dnl }()->random( ({1,2,3}) ); dnl ]]) test_any([[ function r1 = Random.System()->random_string; function r2 = Random.System()->random_string; return sizeof(r1(1)+r2(1)+r1(1)+r2(1)); ]], 4) test_do( add_constant("RND",Random.Deterministic(0)) ) test_eq( String.string2hex(RND->random_string(10)), "983e6a98702d56a70531") test_eq( RND->random(10), 5 ) test_do( add_constant("RND") )
f798582016-01-18Martin Nilsson 
1ffbde2016-01-26Martin Nilsson cond_resolv( Random.Hardware, [[ test_any([[ for(int i=1; i<18; i++) { string x = "\377"*i; for(int j; j<100; j++) x &= random_string(i); if( x != "\0"*i ) return 0; } return 1; ]], 1) test_any([[ for(int i=1; i<18; i++) { string x = "\377"*i; for(int j; j<100; j++) x &= ~random_string(i); if( x != "\0"*i ) return 0; } return 1; ]], 1) ]])
6f0f362016-01-26Martin Nilsson test_any([[ int sz; Random.Interface r = Random.Fast(); for(int i; i<128; i++) sz += sizeof(r->random_string(9001)); return sz; ]], 1152128)
3f895d2016-03-17Martin Nilsson test_do( add_constant("real_random") )
f798582016-01-18Martin Nilsson END_MARKER