changeset 277:1295cec4a287

Updated test_range_analysis
author Sigurd Meldgaard <stm@daimi.au.dk>
date Thu, 11 Mar 2010 11:15:51 +0100
parents ebec4ece9b58
children 8d5c7032e4cd
files pysmcl/test/unit/test_rangeanalysis.py
diffstat 1 files changed, 42 insertions(+), 75 deletions(-) [+]
line wrap: on
line diff
--- a/pysmcl/test/unit/test_rangeanalysis.py	Thu Mar 11 11:15:27 2010 +0100
+++ b/pysmcl/test/unit/test_rangeanalysis.py	Thu Mar 11 11:15:51 2010 +0100
@@ -4,40 +4,34 @@
 from pysmcl.range_analysis import RangeAnalysis, Bottom, full_range
 
 
-def do_range_analysis(program_string, p):
+def do_range_analysis(program_string):
     prog = parse(program_string)
-    range_analysis = RangeAnalysis(p)
+    range_analysis = RangeAnalysis()
     range_analysis.apply(prog.body[0])
     return prog.body[0].body[-1].out_values["range"]
 
-p = 13
-
-
 class RangeAnalysisTest(unittest.TestCase):
 
     def test_range_one_range(self):
-        r = do_range_analysis("def f():\n\ty=0\n", p)
+        r = do_range_analysis("def f():\n\ty=0\n")
         self.assertEquals(r, {'y': (0, 0)})
 
-    def test_range_too_large_constant(self):
-        r = do_range_analysis("def f():\n\ty=8\n", p)
-        self.assertEquals(r, {'y': Bottom()})
+    def test_range_bottom(self):
+        r = do_range_analysis("def f():\n\twhile(1): y=y+1\n")
+        self.assertEquals(r, {'True': (1,1), 'False': (0,0), 'y': Bottom()})
 
     def test_range_unknown_range(self):
-        r = do_range_analysis("def f(x):\n\ty=x\n", p)
-        self.assertEquals(r, {'x': full_range(p), 'y': full_range(p)})
+        r = do_range_analysis("def f(x):\n\ty=x\n")
+        self.assertEquals(r, {'x': Bottom(), 'y': Bottom()})
 
     def test_range_add(self):
-        r = do_range_analysis("def f(x):\n\tx=3\n\ty=2\n\tz=x+y\n", p)
+        r = do_range_analysis("def f(x):\n\tx=3\n\ty=2\n\tz=x+y\n")
+        print r
         self.assertEquals(r, {'y': (2, 2), 'x': (3, 3), 'z': (5, 5)})
 
-    def test_range_add_wrap(self):
-        r = do_range_analysis("def f(x):\n\tx=6\n\ty=2\n\tz=x+y\n", p)
-        self.assertEquals(r, {'y': (2, 2), 'x': (6, 6), 'z': Bottom()})
-
     def test_range_add_bottom(self):
-        r = do_range_analysis("def f(x):\n\tx=6\n\ty=8\n\tz=x+y\n", p)
-        self.assertEquals(r, {'y': Bottom(), 'x': (6, 6), 'z': Bottom()})
+        r = do_range_analysis("def f(x):\n\tx=6\n\twhile(1): y=y+1\n\tz=x+y\n")
+        self.assertEquals(r, {'True': (1,1), 'False': (0,0), 'y': Bottom(), 'x': (6, 6), 'z': Bottom()})
 
     def test_range_add_top_bottom(self):
         r = do_range_analysis("""
@@ -45,32 +39,19 @@
     x=1
     y=4
     if y:
-         y=y+2
+         while(1):
+              y=y+1
     else: y=y+0
     y=y+1
-""", p)
-        self.assertEquals(r, {'x': (1, 1), 'y': Bottom()})
+""")
+        self.assertEquals(r, {'True': (1,1), 'False': (0,0), 'x': (1, 1), 'y': Bottom()})
 
-    def test_range_add_top_bottom2(self):
-        r = do_range_analysis("""
-def f(x):
-    x=1
-    y=4
-    if x:
-         y=y+2
-    y=y+1
-""", p)
-        self.assertEquals(r, {'x': (1, 1), 'y': Bottom()})
 
     def test_range_mul(self):
-        r = do_range_analysis("def f(x):\n\tx=3\n\ty=2\n\tz=x*y\n", p)
+        r = do_range_analysis("def f(x):\n\tx=3\n\ty=2\n\tz=x*y\n")
         self.assertEquals(r, {'y': (2, 2), 'x': (3, 3), 'z': (6, 6)})
 
-    def test_range_mul_wrap(self):
-        r = do_range_analysis("def f(x):\n\tx=3\n\ty=3\n\tz=x*y\n", p)
-        self.assertEquals(r, {'y': (3, 3), 'x': (3, 3), 'z': Bottom()})
-
-    def test_range_mult_bottom(self):
+    def test_range_mult_range(self):
         r = do_range_analysis("""
 def f(x):
     x=2
@@ -78,19 +59,8 @@
     if x:
          y=y+3
     z=x*y
-""", p)
-        self.assertEquals(r, {'x': (2, 2), 'y': (2, 5), 'z': Bottom()})
-
-    def test_range_mult_top_bottom(self):
-        r = do_range_analysis("""
-def f(x):
-    x=2
-    y=1
-    if x:
-         y=y+6
-    z=x*y
-""", p)
-        self.assertEquals(r, {'x': (2, 2), 'y': Bottom(), 'z': Bottom()})
+""")
+        self.assertEquals(r, {'x': (2, 2), 'y': (2, 5), 'z': (4,10)})
 
     def test_range_minus(self):
         r = do_range_analysis("""
@@ -99,20 +69,16 @@
     if x:
          a=a+1
     z=a+2-a
-""", p)
-        self.assertEquals(r, {'a': (3, 4), 'x': full_range(p), 'z': (1, 3)})
+""")
+        self.assertEquals(r, {'a': (3, 4), 'x': Bottom(), 'z': (1, 3)})
 
     def test_range_minus_negative(self):
-        r = do_range_analysis("def f(x):\n\tx=3\n\ty=4\n\tz=x-y\n", p)
+        r = do_range_analysis("def f(x):\n\tx=3\n\ty=4\n\tz=x-y\n")
         self.assertEquals(r, {'y': (4, 4), 'x': (3, 3), 'z': (-1, -1)})
 
     def test_range_minus_wrap(self):
-        r = do_range_analysis("def f(x):\n\tx=-2\n\ty=-5\n\tz=x+y\n", p)
-        self.assertEquals(r, {'x': (-2, -2), 'y': (-5, -5), 'z': Bottom()})
-
-    def test_range_minus_bottom(self):
-        r = do_range_analysis("def f(x):\n\tx=6\n\ty=8\n\tz=x-y\n", p)
-        self.assertEquals(r, {'y': Bottom(), 'x': (6, 6), 'z': Bottom()})
+        r = do_range_analysis("def f(x):\n\tx=-2\n\ty=-5\n\tz=x+y\n")
+        self.assertEquals(r, {'x': (-2, -2), 'y': (-5, -5), 'z': (-7,-7)})
 
     def test_range_minus_bottom_bottom(self):
         r = do_range_analysis("""
@@ -120,43 +86,44 @@
     x=1
     y=0
     if x:
-         y=y-3
+         while(1):
+            y=y-3
     y=y-4
     z=y-x
-""", p)
-        self.assertEquals(r, {'x': (1, 1), 'y': Bottom(), 'z': Bottom()})
+""")
+        self.assertEquals(r, {'True': (1,1), 'False': (0,0), 'x': (1, 1), 'y': Bottom(), 'z': Bottom()})
 
     def test_range_equals(self):
-        r = do_range_analysis("def f(x):\n\tx=3\n\ty=2\n\tz=x==y\n", p)
+        r = do_range_analysis("def f(x):\n\tx=3\n\ty=2\n\tz=x==y\n")
         self.assertEquals(r, {'y': (2, 2), 'x': (3, 3), 'z': (0, 1)})
 
     def test_range_leg(self):
-        r = do_range_analysis("def f(x):\n\tx=3\n\ty=2\n\tz=x<=y\n", p)
+        r = do_range_analysis("def f(x):\n\tx=3\n\ty=2\n\tz=x<=y\n")
         self.assertEquals(r, {'y': (2, 2), 'x': (3, 3), 'z': (0, 1)})
 
     def test_range_geg(self):
-        r = do_range_analysis("def f(x):\n\tx=3\n\ty=2\n\tz=x>=y\n", p)
+        r = do_range_analysis("def f(x):\n\tx=3\n\ty=2\n\tz=x>=y\n")
         self.assertEquals(r, {'y': (2, 2), 'x': (3, 3), 'z': (0, 1)})
 
     def test_range_multiple_compare(self):
-        r = do_range_analysis("def f(x):\n\tx=3\n\ty=2\n\tz=x>y<=42\n", p)
+        r = do_range_analysis("def f(x):\n\tx=3\n\ty=2\n\tz=x>y<=42\n")
         self.assertEquals(r, {'y': (2, 2), 'x': (3, 3), 'z': (0, 1)})
 
     def test_range_random(self):
-        r = do_range_analysis("def f():\n\tx=random()\n", p)
-        self.assertEquals(r, {'x': full_range(p)})
+        r = do_range_analysis("def f():\n\tx=random()\n")
+        self.assertEquals(r, {'x': Bottom()})
 
     def test_range_random_bit(self):
-        r = do_range_analysis("def f(x):\n\tx=random_bit()", p)
+        r = do_range_analysis("def f(x):\n\tx=random_bit()")
         self.assertEquals(r, {'x': (0, 1)})
 
     def test_range_global_variable(self):
         p = 13
         prog = parse("def f():\n\ty = x")
-        range_analysis = RangeAnalysis(p)
+        range_analysis = RangeAnalysis()
         range_analysis.apply(prog.body[0])
         r = prog.body[0].body[0].out_values["range"]
-        self.assertEquals(r, {'y': full_range(p)})
+        self.assertEquals(r, {'y': Bottom()})
 
     def test_range_if(self):
         r = do_range_analysis("""
@@ -167,8 +134,8 @@
     else:
         y=3
     return y
-""", p)
-        self.assertEquals(r, {'x': full_range(p), 'y': (2, 3)})
+""")
+        self.assertEquals(r, {'x': Bottom(), 'y': (2, 3)})
 
 #    def test_range_while(self):
 #        p = 13
@@ -192,8 +159,8 @@
         r = do_range_analysis("""
 def f(x):
     return x
-""", p)
-        self.assertEquals(r, {'x': full_range(p)})
+""")
+        self.assertEquals(r, {'x': Bottom()})
 
 
 #     def test_range_multiple_assign_and_tuples(self):