summaryrefslogtreecommitdiff
path: root/t
diff options
context:
space:
mode:
authorArtur Bergman <sky@nanisky.com>2001-09-03 11:04:15 +0000
committerArtur Bergman <sky@nanisky.com>2001-09-03 11:04:15 +0000
commitefec44ca9b235a3617685a4d7c0d5b1e8d8d2fbd (patch)
tree6684c6cc88277311e694b6340039fbff49d3a56e /t
parent9be67dbc13b2c63ded42048245f871f99aa3fa6c (diff)
downloadperl-efec44ca9b235a3617685a4d7c0d5b1e8d8d2fbd.tar.gz
Forgot to add a file with Change #11840
p4raw-id: //depot/perl@11841
Diffstat (limited to 't')
-rw-r--r--t/op/srand.t63
1 files changed, 63 insertions, 0 deletions
diff --git a/t/op/srand.t b/t/op/srand.t
new file mode 100644
index 0000000000..b9b8ebce06
--- /dev/null
+++ b/t/op/srand.t
@@ -0,0 +1,63 @@
+#!./perl -w
+
+# Test srand.
+
+use strict;
+use Test::More tests => 5;
+
+# Generate a load of random numbers.
+# int() avoids possible floating point error.
+sub mk_rand { map int rand 10000, 1..100; }
+
+
+# Check that rand() is deterministic.
+srand(1138);
+my @first_run = mk_rand;
+
+srand(1138);
+my @second_run = mk_rand;
+
+ok( eq_array(\@first_run, \@second_run), 'srand(), same arg, same rands' );
+
+
+# Check that different seeds provide different random numbers
+srand(31337);
+@first_run = mk_rand;
+
+srand(1138);
+@second_run = mk_rand;
+
+ok( !eq_array(\@first_run, \@second_run),
+ 'srand(), different arg, different rands' );
+
+
+# Check that srand() with no args provides different seeds.
+srand();
+@first_run = mk_rand;
+
+srand();
+@second_run = mk_rand;
+
+ok( !eq_array(\@first_run, \@second_run), 'srand(), no arg, different rands');
+
+
+# Check that srand() isn't effected by $_
+{
+ local $_ = 42;
+ srand();
+ @first_run = mk_rand;
+
+ srand();
+ @second_run = mk_rand;
+
+ ok( !eq_array(\@first_run, \@second_run),
+ 'srand(), no arg, not effected by $_');
+}
+
+
+
+# This test checks whether Perl called srand for you.
+@first_run = `$^X -le "print int rand 100 for 1..100"`;
+@second_run = `$^X -le "print int rand 100 for 1..100"`;
+
+ok( !eq_array(\@first_run, \@second_run), 'srand() called automatically');