[svn:parrot] r40554 - in trunk: src/dynoplibs t/dynoplibs

dukeleto at svn.parrot.org dukeleto at svn.parrot.org
Sat Aug 15 05:48:10 UTC 2009


Author: dukeleto
Date: Sat Aug 15 05:48:08 2009
New Revision: 40554
URL: https://trac.parrot.org/parrot/changeset/40554

Log:
[TT #871] Add some more permutations of the rand() dynop, with tests

Modified:
   trunk/src/dynoplibs/math.ops
   trunk/t/dynoplibs/math.t

Modified: trunk/src/dynoplibs/math.ops
==============================================================================
--- trunk/src/dynoplibs/math.ops	Sat Aug 15 01:02:20 2009	(r40553)
+++ trunk/src/dynoplibs/math.ops	Sat Aug 15 05:48:08 2009	(r40554)
@@ -37,6 +37,16 @@
   $1 = Parrot_float_rand(0);
 }
 
+=item B<rand>(out INT)
+
+Set $1 to a random integer between C<[-2^31, 2^31)> .
+
+=cut
+
+inline op rand(out INT) {
+  $1 = Parrot_int_rand(0);
+}
+
 =item B<rand>(out NUM, in NUM)
 
 Set $1 to a random floating point number between 0 and and $2, inclusive.
@@ -47,6 +57,16 @@
   $1 = $2 * Parrot_float_rand(0);
 }
 
+=item B<rand>(out INT, in INT)
+
+Set $1 to a integer between 0 and and $2, inclusive.
+
+=cut
+
+inline op rand(out INT, in INT) {
+  $1 = Parrot_range_rand(0,$2,0);
+}
+
 =item B<rand>(out NUM, in NUM, in NUM)
 
 Set $1 to a random floating point number between $2 and and $3, inclusive.
@@ -59,12 +79,12 @@
 
 =item B<srand>(in NUM)
 
-Set the random number seed to $1.
+Set the random number seed to $1. $1 is casted to an INTVAL.
 
 =cut
 
 inline op srand(in NUM) {
-    Parrot_srand((FLOATVAL)$1);
+    Parrot_srand((INTVAL)$1);
 }
 
 =item B<srand>(in INT)
@@ -77,6 +97,15 @@
     Parrot_srand((INTVAL)$1);
 }
 
+=item B<rand>(out INT, in INT, in INT)
+
+Set $1 to a integer between $2 and and $3, inclusive.
+
+=cut
+
+inline op rand(out INT, in INT, in INT) {
+  $1 = Parrot_range_rand($2,$3,0);
+}
 
 =back
 

Modified: trunk/t/dynoplibs/math.t
==============================================================================
--- trunk/t/dynoplibs/math.t	Sat Aug 15 01:02:20 2009	(r40553)
+++ trunk/t/dynoplibs/math.t	Sat Aug 15 05:48:08 2009	(r40554)
@@ -20,15 +20,50 @@
 .sub main :main
     .include 'test_more.pir'
     .include 'fp_equality.pasm'
-    plan(10)
+    plan(14)
     ok(1, "load math_ops")
-    basic_test_1_arg()
-    basic_test_2_arg()
-    basic_test_3_arg()
+    rand $I0
+    test_2_arg_int()
+    test_3_arg_int()
+    test_1_arg_num()
+    test_2_arg_num()
+    test_3_arg_num()
     test_srand()
 .end
 
-.sub basic_test_1_arg
+.sub test_2_arg_int
+    rand $I0, 5
+    lt $I0, 0, fail1
+    ok(1, 'rand returns a number greater than or equal to 0')
+    goto upper
+fail1:
+    ok(0, 'rand returns a number greater than or equal to 0')
+upper:
+    gt $I0, 5, fail2
+    ok(1, 'rand returns a number less than or equal to 5')
+    goto finish
+fail2:
+    ok(0, 'rand returns a number less than or equal to 5')
+finish:
+.end
+
+.sub test_3_arg_int
+    rand $I0, 5, 25
+    lt $I0, 5, fail1
+    ok(1, 'rand returns a number greater than or equal to 5')
+    goto upper
+fail1:
+    ok(0, 'rand returns a number greater than or equal to 5')
+upper:
+    gt $I0, 25, fail2
+    ok(1, 'rand returns a number less than or equal to 25')
+    goto finish
+fail2:
+    ok(0, 'rand returns a number less than or equal to 25')
+finish:
+.end
+
+.sub test_1_arg_num
     rand $N0
     lt $N0, 0, fail1
     ok(1, 'rand returns a number greater than or equal to 0')
@@ -44,7 +79,7 @@
 finish:
 .end
 
-.sub basic_test_2_arg
+.sub test_2_arg_num
     rand $N0, 5
     lt $N0, 0, fail1
     ok(1, 'rand returns a number greater than or equal to 0')
@@ -60,7 +95,7 @@
 finish:
 .end
 
-.sub basic_test_3_arg
+.sub test_3_arg_num
     rand $N0, 5, 25
     lt $N0, 5, fail1
     ok(1, 'rand returns a number greater than or equal to 5')


More information about the parrot-commits mailing list