changeset 49:049fc91f5bef

Implementation of random and random_bit.
author Janus Dam Nielsen <janus.nielsen@alexandra.dk>
date Tue, 26 May 2009 16:34:21 +0200
parents 61c65f00b8ef
children a192b4becf2e
files pysmcl/range_analysis.py pysmcl/test/unit/test_rangeanalysis.py
diffstat 2 files changed, 24 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/pysmcl/range_analysis.py	Tue May 26 16:33:35 2009 +0200
+++ b/pysmcl/range_analysis.py	Tue May 26 16:34:21 2009 +0200
@@ -122,6 +122,14 @@
         # cmpop = Eq | NotEq | Lt | LtE | Gt | GtE | Is | IsNot | In | NotIn
         return (0, 1)
 
+    def visit_Call(self, node):
+        # TODO: Check that random and random_bit are bound to the expected functions
+        if node.func.id == "random":
+            return (0, self.prime - 1)
+        if node.func.id == "random_bit":
+            return (0, 1)
+        return (0, self.prime)
+
 class TargetVisitor(ast.NodeVisitor):
     """The following expression can appear in assignment context """                         
 
--- a/pysmcl/test/unit/test_rangeanalysis.py	Tue May 26 16:33:35 2009 +0200
+++ b/pysmcl/test/unit/test_rangeanalysis.py	Tue May 26 16:34:21 2009 +0200
@@ -119,23 +119,23 @@
         r = prog.body[0].body[2].out_values["range"]
         self.assertEquals(r, {'y': (2, 2), 'x': (3, 3), 'z': (0, 1)})
 
-#     def test_range_random(self):
-#         p = 7
-#         prog = parse("def f(x):\n\tx=random()\n")
-#         init_statements(prog)
-#         range_analysis = RangeAnalysis(p)
-#         range_analysis.visit(prog.body[0])
-#         r = range_analysis.result
-#         self.assertEquals(r, {'x': (0, p-1)})
+    def test_range_random(self):
+        p = 7
+        prog = parse("def f():\n\tx=random()\n")
+        init_statements(prog)
+        range_analysis = RangeAnalysis(p)
+        range_analysis.apply(prog.body[0])
+        r = prog.body[0].body[0].out_values["range"]
+        self.assertEquals(r, {'x': (0, p - 1)})
 
-#     def test_range_random_minus_bit(self):
-#         p = 7
-#         prog = parse("def f(x):\n\tx=random()\n\ty=x-bit()\n")
-#         init_statements(prog)
-#         range_analysis = RangeAnalysis(p)
-#         range_analysis.visit(prog.body[0])
-#         r = range_analysis.result
-#         self.assertEquals(r, {'y': (0, 1)})
+    def test_range_random_bit(self):
+        p = 7
+        prog = parse("def f(x):\n\tx=random_bit()")
+        init_statements(prog)
+        range_analysis = RangeAnalysis(p)
+        range_analysis.apply(prog.body[0])
+        r = prog.body[0].body[0].out_values["range"]
+        self.assertEquals(r, {'x': (0, 1)})
 
 #     def test_range_global_variable(self):
 #         p = 7