[svn:parrot] r40518 - in trunk: . config/gen/makefiles src/dynoplibs t/dynoplibs

dukeleto at svn.parrot.org dukeleto at svn.parrot.org
Thu Aug 13 10:00:11 UTC 2009


Author: dukeleto
Date: Thu Aug 13 10:00:07 2009
New Revision: 40518
URL: https://trac.parrot.org/parrot/changeset/40518

Log:
[TT #871] Add rand as a dynop, with tests

Added:
   trunk/src/dynoplibs/math.ops
   trunk/t/dynoplibs/math.t
Modified:
   trunk/MANIFEST
   trunk/config/gen/makefiles/dynoplibs.in

Modified: trunk/MANIFEST
==============================================================================
--- trunk/MANIFEST	Thu Aug 13 09:32:18 2009	(r40517)
+++ trunk/MANIFEST	Thu Aug 13 10:00:07 2009	(r40518)
@@ -1248,6 +1248,7 @@
 src/dynext.c                                                []
 src/dynoplibs/README                                        []doc
 src/dynoplibs/obscure.ops                                   []
+src/dynoplibs/math.ops                                      []
 src/dynpmc/README.pod                                       []doc
 src/dynpmc/dynlexpad.pmc                                    [devel]src
 src/dynpmc/ext.pir                                          []
@@ -1674,6 +1675,7 @@
 t/distro/manifest.t                                         [test]
 t/distro/meta_yml.t                                         [test]
 t/dynoplibs/obscure.t                                       [test]
+t/dynoplibs/math.t                                          [test]
 t/dynpmc/dynlexpad.t                                        [test]
 t/dynpmc/foo.t                                              [test]
 t/dynpmc/foo2.t                                             [test]

Modified: trunk/config/gen/makefiles/dynoplibs.in
==============================================================================
--- trunk/config/gen/makefiles/dynoplibs.in	Thu Aug 13 09:32:18 2009	(r40517)
+++ trunk/config/gen/makefiles/dynoplibs.in	Thu Aug 13 10:00:07 2009	(r40518)
@@ -26,7 +26,11 @@
 #IF(cg_flag):  obscure_ops_cg$(LOAD_EXT) \
 #IF(cg_flag):  obscure_ops_cgp$(LOAD_EXT) \
   obscure_ops$(LOAD_EXT) \
-  obscure_ops_switch$(LOAD_EXT)
+  obscure_ops_switch$(LOAD_EXT) \
+#IF(cg_flag):  math_ops_cg$(LOAD_EXT) \
+#IF(cg_flag):  math_ops_cgp$(LOAD_EXT) \
+  math_ops$(LOAD_EXT) \
+  math_ops_switch$(LOAD_EXT)
 
 CLEANUPS := \
   "*.c" \
@@ -85,6 +89,42 @@
 obscure_ops_cgp.c: obscure.ops
 	$(OPS2C) CGP --dynamic obscure.ops
 
+math_ops$(LOAD_EXT): math_ops$(O)
+	$(LD) @ld_out at math_ops$(LOAD_EXT) math_ops$(O) $(LINKARGS)
+
+math_ops$(O): math_ops.c
+	$(CC) -c @cc_o_out at math_ops$(O) $(INCLUDES) $(CFLAGS) math_ops.c
+
+math_ops.c: math.ops
+	$(OPS2C) C --dynamic math.ops
+
+math_ops_switch$(LOAD_EXT): math_ops_switch$(O)
+	$(LD) @ld_out at math_ops_switch$(LOAD_EXT) math_ops_switch$(O) $(LINKARGS)
+
+math_ops_switch$(O): math_ops_switch.c
+	$(CC) -c @cc_o_out at math_ops_switch$(O) $(INCLUDES) $(CFLAGS) math_ops_switch.c
+
+math_ops_switch.c: math.ops
+	$(OPS2C) CSwitch --dynamic math.ops
+
+math_ops_cg$(LOAD_EXT): math_ops_cg$(O)
+	$(LD) @ld_out at math_ops_cg$(LOAD_EXT) math_ops_cg$(O) $(LINKARGS)
+
+math_ops_cg$(O): math_ops_cg.c
+	$(CC) -c @cc_o_out at math_ops_cg$(O) $(INCLUDES) $(CFLAGS) math_ops_cg.c
+
+math_ops_cg.c: math.ops
+	$(OPS2C) CGoto --dynamic math.ops
+
+math_ops_cgp$(LOAD_EXT): math_ops_cgp$(O)
+	$(LD) @ld_out at math_ops_cgp$(LOAD_EXT) math_ops_cgp$(O) $(LINKARGS)
+
+math_ops_cgp$(O): math_ops_cgp.c
+	$(CC) -c @cc_o_out at math_ops_cgp$(O) $(INCLUDES) $(CFLAGS) math_ops_cgp.c
+
+math_ops_cgp.c: math.ops
+	$(OPS2C) CGP --dynamic math.ops
+
 test : all
 	cd ../.. && $(PERL) -Ilib t/harness t/dynoplibs/*.t
 

Added: trunk/src/dynoplibs/math.ops
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ trunk/src/dynoplibs/math.ops	Thu Aug 13 10:00:07 2009	(r40518)
@@ -0,0 +1,79 @@
+/*
+ * $Id$
+** math.ops
+*/
+
+BEGIN_OPS_PREAMBLE
+
+#include <math.h>
+
+END_OPS_PREAMBLE
+
+=head1 NAME
+
+math.ops - Mathematical Opcodes
+
+=cut
+
+=head1 DESCRIPTION
+
+Parrot's library of mathematical ops.
+
+To use this library of ops, add this directive to your PIR:
+
+ .loadlib 'math_ops'
+
+=cut
+
+=over
+
+=item B<rand>(out NUM)
+
+Set $1 to a random floating point number between 0 and 1, inclusive.
+
+=cut
+
+inline op rand(out NUM) {
+  $1 = Parrot_float_rand(0);
+}
+
+=item B<rand>(out NUM, in NUM)
+
+Set $1 to a random floating point number between 0 and and $2, inclusive.
+
+=cut
+
+inline op rand(out NUM, in NUM) {
+  $1 = $2 * Parrot_float_rand(0);
+}
+
+=item B<rand>(out NUM, in NUM, in NUM)
+
+Set $1 to a random floating point number between $2 and and $3, inclusive.
+
+=cut
+
+inline op rand(out NUM, in NUM, in NUM) {
+  $1 = $2 + ($3 - $2) * Parrot_float_rand(0);
+}
+
+=back
+
+=head1 COPYRIGHT
+
+Copyright (C) 2001-2009, Parrot Foundation.
+
+=head1 LICENSE
+
+This program is free software. It is subject to the same license
+as the Parrot interpreter itself.
+
+=cut
+
+
+/*
+ * Local variables:
+ *   c-file-style: "parrot"
+ * End:
+ * vim: expandtab shiftwidth=4:
+ */

Added: trunk/t/dynoplibs/math.t
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ trunk/t/dynoplibs/math.t	Thu Aug 13 10:00:07 2009	(r40518)
@@ -0,0 +1,82 @@
+#! parrot
+# Copyright (C) 2009, Parrot Foundation.
+# $Id$
+
+=head1 NAME
+
+t/dynoplibs/math.t - Tests for mathematical ops
+
+=head1 SYNOPSIS
+
+        % prove t/dynoblibs/math.t
+
+=head1 DESCRIPTION
+
+Tests math.ops
+
+=cut
+
+.loadlib 'math_ops'
+.sub main :main
+    .include 'test_more.pir'
+    plan(7)
+    ok(1, "load math_ops")
+    basic_test_1_arg()
+    basic_test_2_arg()
+    basic_test_3_arg()
+.end
+
+.sub basic_test_1_arg
+    rand $N0
+    lt $N0, 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 $N0, 1, fail2
+    ok(1, 'rand returns a number less than or equal to 1')
+    goto finish
+fail2:
+    ok(0, 'rand returns a number less than or equal to 1')
+finish:
+.end
+
+.sub basic_test_2_arg
+    rand $N0, 5
+    lt $N0, 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 $N0, 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 basic_test_3_arg
+    rand $N0, 5, 25
+    lt $N0, 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 $N0, 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
+
+# Local Variables:
+#   mode: cperl
+#   cperl-indent-level: 4
+#   fill-column: 100
+# End:
+# vim: expandtab shiftwidth=4 filetype=pir:


More information about the parrot-commits mailing list