changeset 48:61c65f00b8ef

Implemented equality tests.
author Janus Dam Nielsen <janus.nielsen@alexandra.dk>
date Tue, 26 May 2009 16:33:35 +0200
parents 961d8bf9b00c
children 049fc91f5bef
files pysmcl/range_analysis.py pysmcl/test/unit/test_rangeanalysis.py
diffstat 2 files changed, 40 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/pysmcl/range_analysis.py	Tue May 26 16:14:45 2009 +0200
+++ b/pysmcl/range_analysis.py	Tue May 26 16:33:35 2009 +0200
@@ -85,8 +85,8 @@
         return (0, self.prime)
 
     def visit_BinOp(self, node):
-#         operator = Add | Sub | Mult | Div | Mod | Pow | LShift 
-#                  | RShift | BitOr | BitXor | BitAnd | FloorDiv
+        # operator = Add | Sub | Mult | Div | Mod | Pow | LShift 
+        #          | RShift | BitOr | BitXor | BitAnd | FloorDiv
         left = self.visit(node.left)
         right = self.visit(node.right)
 
@@ -116,7 +116,11 @@
             if r1 >= self.prime:
                 r1 = Bottom()
             return (r0, r1)
-        raise Exception("Operator not implemented.") 
+        raise Exception("Operator not implemented: ", node.op) 
+
+    def visit_Compare(self, node):
+        # cmpop = Eq | NotEq | Lt | LtE | Gt | GtE | Is | IsNot | In | NotIn
+        return (0, 1)
 
 class TargetVisitor(ast.NodeVisitor):
     """The following expression can appear in assignment context """                         
--- a/pysmcl/test/unit/test_rangeanalysis.py	Tue May 26 16:14:45 2009 +0200
+++ b/pysmcl/test/unit/test_rangeanalysis.py	Tue May 26 16:33:35 2009 +0200
@@ -71,7 +71,6 @@
         init_statements(prog)
         range_analysis = RangeAnalysis(p)
         range_analysis.apply(prog.body[0])
-        r = range_analysis.result
         r = prog.body[0].body[2].out_values["range"]
         self.assertEquals(r, {'y': (2, 2), 'x': (3, 3), 'z': (1, 1)})
 
@@ -84,32 +83,41 @@
         r = prog.body[0].body[2].out_values["range"]
         self.assertEquals(r, {'y': (4, 4), 'x': (3, 3), 'z': (Bottom(), Bottom())})
 
-#     def test_range_equals(self):
-#         p = 7
-#         prog = parse("def f(x):\n\tx=3\n\ty=2\n\tz=x==y\n")
-#         init_statements(prog)
-#         range_analysis = RangeAnalysis(p)
-#         range_analysis.visit(prog.body[0])
-#         r = range_analysis.result
-#         self.assertEquals(r, {'z': (0, 1)})
+    def test_range_equals(self):
+        p = 7
+        prog = parse("def f(x):\n\tx=3\n\ty=2\n\tz=x==y\n")
+        init_statements(prog)
+        range_analysis = RangeAnalysis(p)
+        range_analysis.apply(prog.body[0])
+        r = prog.body[0].body[2].out_values["range"]
+        self.assertEquals(r, {'y': (2, 2), 'x': (3, 3), 'z': (0, 1)})
+
+    def test_range_leg(self):
+        p = 7
+        prog = parse("def f(x):\n\tx=3\n\ty=2\n\tz=x<=y\n")
+        init_statements(prog)
+        range_analysis = RangeAnalysis(p)
+        range_analysis.apply(prog.body[0])
+        r = prog.body[0].body[2].out_values["range"]
+        self.assertEquals(r, {'y': (2, 2), 'x': (3, 3), 'z': (0, 1)})
 
-#     def test_range_leg(self):
-#         p = 7
-#         prog = parse("def f(x):\n\tx=3\n\ty=2\n\tz=x<=y\n")
-#         init_statements(prog)
-#         range_analysis = RangeAnalysis(p)
-#         range_analysis.visit(prog.body[0])
-#         r = range_analysis.result
-#         self.assertEquals(r, {'z': (0, 1)})
+    def test_range_geg(self):
+        p = 7
+        prog = parse("def f(x):\n\tx=3\n\ty=2\n\tz=x>=y\n")
+        init_statements(prog)
+        range_analysis = RangeAnalysis(p)
+        range_analysis.apply(prog.body[0])
+        r = prog.body[0].body[2].out_values["range"]
+        self.assertEquals(r, {'y': (2, 2), 'x': (3, 3), 'z': (0, 1)})
 
-#     def test_range_geg(self):
-#         p = 7
-#         prog = parse("def f(x):\n\tx=3\n\ty=2\n\tz=x>=y\n")
-#         init_statements(prog)
-#         range_analysis = RangeAnalysis(p)
-#         range_analysis.visit(prog.body[0])
-#         r = range_analysis.result
-#         self.assertEquals(r, {'z': (0, 1)})
+    def test_range_multiple_compare(self):
+        p = 7
+        prog = parse("def f(x):\n\tx=3\n\ty=2\n\tz=x>y<=42\n")
+        init_statements(prog)
+        range_analysis = RangeAnalysis(p)
+        range_analysis.apply(prog.body[0])
+        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