changeset 95:f683af7aea18

Refactored rangeanalysis testcases
author Sigurd Meldgaard <stm@daimi.au.dk>
date Mon, 08 Jun 2009 13:14:17 +0200
parents 28adf540a9ba
children 2bbef4d20678
files pysmcl/test/unit/test_rangeanalysis.py
diffstat 1 files changed, 44 insertions(+), 147 deletions(-) [+]
line wrap: on
line diff
--- a/pysmcl/test/unit/test_rangeanalysis.py	Mon Jun 08 12:29:37 2009 +0200
+++ b/pysmcl/test/unit/test_rangeanalysis.py	Mon Jun 08 13:14:17 2009 +0200
@@ -4,63 +4,43 @@
 from pysmcl.range_analysis import RangeAnalysis, Bottom, full_range
 
 
+def do_range_analysis(program_string, p):
+    prog = parse(program_string)
+    range_analysis = RangeAnalysis(p)
+    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):
-        p = 13
-        prog = parse("def f():\n\ty=0\n")
-        range_analysis = RangeAnalysis(p)
-        range_analysis.apply(prog.body[0])
-#         for node in prog.body[0].body:
-#             print "Result...:", node.out_values["range"]
-
-        self.assertEquals(prog.body[0].body[0].out_values["range"],
-                          {'y': (0, 0)})
+        r = do_range_analysis("def f():\n\ty=0\n", p)
+        self.assertEquals(r, {'y': (0, 0)})
 
     def test_range_too_large_constant(self):
-        p = 13
-        prog = parse("def f():\n\ty=8\n")
-        range_analysis = RangeAnalysis(p)
-        range_analysis.apply(prog.body[0])
-        self.assertEquals(prog.body[0].body[0].out_values["range"],
-                          {'y': Bottom()})
+        r = do_range_analysis("def f():\n\ty=8\n", p)
+        self.assertEquals(r, {'y': Bottom()})
 
     def test_range_unknown_range(self):
-        p = 13
-        prog = parse("def f(x):\n\ty=x\n")
-        range_analysis = RangeAnalysis(p)
-        range_analysis.apply(prog.body[0])
-        r = prog.body[0].body[0].out_values["range"]
+        r = do_range_analysis("def f(x):\n\ty=x\n", p)
         self.assertEquals(r, {'x': full_range(p), 'y': full_range(p)})
 
     def test_range_add(self):
-        p = 13
-        prog = parse("def f(x):\n\tx=3\n\ty=2\n\tz=x+y\n")
-        range_analysis = RangeAnalysis(p)
-        range_analysis.apply(prog.body[0])
-        r = prog.body[0].body[2].out_values["range"]
+        r = do_range_analysis("def f(x):\n\tx=3\n\ty=2\n\tz=x+y\n", p)
         self.assertEquals(r, {'y': (2, 2), 'x': (3, 3), 'z': (5, 5)})
 
     def test_range_add_wrap(self):
-        p = 13
-        prog = parse("def f(x):\n\tx=6\n\ty=2\n\tz=x+y\n")
-        range_analysis = RangeAnalysis(p)
-        range_analysis.apply(prog.body[0])
-        r = prog.body[0].body[2].out_values["range"]
+        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):
-        p = 13
-        prog = parse("def f(x):\n\tx=6\n\ty=8\n\tz=x+y\n")
-        range_analysis = RangeAnalysis(p)
-        range_analysis.apply(prog.body[0])
-        r = prog.body[0].body[2].out_values["range"]
+        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()})
 
     def test_range_add_top_bottom(self):
-        p = 13
-        prog = parse(
-"""
+        r = do_range_analysis("""
 def f(x):
     x=1
     y=4
@@ -68,120 +48,74 @@
          y=y+2
     else: y=y+0
     y=y+1
-""")
-        range_analysis = RangeAnalysis(p)
-        range_analysis.apply(prog.body[0])
-        r = prog.body[0].body[-1].out_values["range"]
+""", p)
         self.assertEquals(r, {'x': (1, 1), 'y': Bottom()})
 
     def test_range_add_top_bottom2(self):
-        p = 13
-        prog = parse(
-"""
+        r = do_range_analysis("""
 def f(x):
     x=1
     y=4
     if x:
          y=y+2
     y=y+1
-""")
-        range_analysis = RangeAnalysis(p)
-        range_analysis.apply(prog.body[0])
-        r = prog.body[0].body[-1].out_values["range"]
-        print prog.body[0].body[-1]
+""", p)
         self.assertEquals(r, {'x': (1, 1), 'y': Bottom()})
 
     def test_range_mul(self):
-        p = 13
-        prog = parse("def f(x):\n\tx=3\n\ty=2\n\tz=x*y\n")
-        range_analysis = RangeAnalysis(p)
-        range_analysis.apply(prog.body[0])
-        r = prog.body[0].body[2].out_values["range"]
+        r = do_range_analysis("def f(x):\n\tx=3\n\ty=2\n\tz=x*y\n", p)
         self.assertEquals(r, {'y': (2, 2), 'x': (3, 3), 'z': (6, 6)})
 
     def test_range_mul_wrap(self):
-        p = 13
-        prog = parse("def f(x):\n\tx=3\n\ty=3\n\tz=x*y\n")
-        range_analysis = RangeAnalysis(p)
-        range_analysis.apply(prog.body[0])
-        r = prog.body[0].body[2].out_values["range"]
+        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):
-        p = 13
-        prog = parse(
-"""
+        r = do_range_analysis("""
 def f(x):
     x=2
     y=2
     if x:
          y=y+3
     z=x*y
-""")
-        range_analysis = RangeAnalysis(p)
-        range_analysis.apply(prog.body[0])
-        r = prog.body[0].body[-1].out_values["range"]
+""", p)
         self.assertEquals(r, {'x': (2, 2), 'y': (2, 5), 'z': Bottom()})
 
     def test_range_mult_top_bottom(self):
-        p = 13
-        prog = parse(
-"""
+        r = do_range_analysis("""
 def f(x):
     x=2
     y=1
     if x:
          y=y+6
     z=x*y
-""")
-        range_analysis = RangeAnalysis(p)
-        range_analysis.apply(prog.body[0])
-        r = prog.body[0].body[-1].out_values["range"]
+""", p)
         self.assertEquals(r, {'x': (2, 2), 'y': Bottom(), 'z': Bottom()})
 
     def test_range_minus(self):
-        p = 13
-        prog = parse(
-"""
+        r = do_range_analysis("""
 def f(x):
     a=3
     if x:
          a=a+1
     z=a+2-a
-""")
-        range_analysis = RangeAnalysis(p)
-        range_analysis.apply(prog.body[0])
-        r = prog.body[0].body[-1].out_values["range"]
+""", p)
         self.assertEquals(r, {'a': (3, 4), 'x': full_range(p), 'z': (1, 3)})
 
     def test_range_minus_negative(self):
-        p = 13
-        prog = parse("def f(x):\n\tx=3\n\ty=4\n\tz=x-y\n")
-        range_analysis = RangeAnalysis(p)
-        range_analysis.apply(prog.body[0])
-        r = prog.body[0].body[2].out_values["range"]
+        r = do_range_analysis("def f(x):\n\tx=3\n\ty=4\n\tz=x-y\n", p)
         self.assertEquals(r, {'y': (4, 4), 'x': (3, 3), 'z': (-1, -1)})
 
     def test_range_minus_wrap(self):
-        p = 13
-        prog = parse("def f(x):\n\tx=-2\n\ty=-5\n\tz=x+y\n")
-        range_analysis = RangeAnalysis(p)
-        range_analysis.apply(prog.body[0])
-        r = prog.body[0].body[2].out_values["range"]
+        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):
-        p = 13
-        prog = parse("def f(x):\n\tx=6\n\ty=8\n\tz=x-y\n")
-        range_analysis = RangeAnalysis(p)
-        range_analysis.apply(prog.body[0])
-        r = prog.body[0].body[2].out_values["range"]
+        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()})
 
     def test_range_minus_bottom_bottom(self):
-        p = 13
-        prog = parse(
-"""
+        r = do_range_analysis("""
 def f(x):
     x=1
     y=0
@@ -189,58 +123,31 @@
          y=y-3
     y=y-4
     z=y-x
-""")
-        range_analysis = RangeAnalysis(p)
-        range_analysis.apply(prog.body[0])
-        r = prog.body[0].body[-1].out_values["range"]
+""", p)
         self.assertEquals(r, {'x': (1, 1), 'y': Bottom(), 'z': Bottom()})
 
     def test_range_equals(self):
-        p = 13
-        prog = parse("def f(x):\n\tx=3\n\ty=2\n\tz=x==y\n")
-        range_analysis = RangeAnalysis(p)
-        range_analysis.apply(prog.body[0])
-        r = prog.body[0].body[2].out_values["range"]
+        r = do_range_analysis("def f(x):\n\tx=3\n\ty=2\n\tz=x==y\n", p)
         self.assertEquals(r, {'y': (2, 2), 'x': (3, 3), 'z': (0, 1)})
 
     def test_range_leg(self):
-        p = 13
-        prog = parse("def f(x):\n\tx=3\n\ty=2\n\tz=x<=y\n")
-        range_analysis = RangeAnalysis(p)
-        range_analysis.apply(prog.body[0])
-        r = prog.body[0].body[2].out_values["range"]
+        r = do_range_analysis("def f(x):\n\tx=3\n\ty=2\n\tz=x<=y\n", p)
         self.assertEquals(r, {'y': (2, 2), 'x': (3, 3), 'z': (0, 1)})
 
     def test_range_geg(self):
-        p = 13
-        prog = parse("def f(x):\n\tx=3\n\ty=2\n\tz=x>=y\n")
-        range_analysis = RangeAnalysis(p)
-        range_analysis.apply(prog.body[0])
-        r = prog.body[0].body[2].out_values["range"]
+        r = do_range_analysis("def f(x):\n\tx=3\n\ty=2\n\tz=x>=y\n", p)
         self.assertEquals(r, {'y': (2, 2), 'x': (3, 3), 'z': (0, 1)})
 
     def test_range_multiple_compare(self):
-        p = 13
-        prog = parse("def f(x):\n\tx=3\n\ty=2\n\tz=x>y<=42\n")
-        range_analysis = RangeAnalysis(p)
-        range_analysis.apply(prog.body[0])
-        r = prog.body[0].body[2].out_values["range"]
+        r = do_range_analysis("def f(x):\n\tx=3\n\ty=2\n\tz=x>y<=42\n", p)
         self.assertEquals(r, {'y': (2, 2), 'x': (3, 3), 'z': (0, 1)})
 
     def test_range_random(self):
-        p = 13
-        prog = parse("def f():\n\tx=random()\n")
-        range_analysis = RangeAnalysis(p)
-        range_analysis.apply(prog.body[0])
-        r = prog.body[0].body[0].out_values["range"]
+        r = do_range_analysis("def f():\n\tx=random()\n", p)
         self.assertEquals(r, {'x': full_range(p)})
 
     def test_range_random_bit(self):
-        p = 13
-        prog = parse("def f(x):\n\tx=random_bit()")
-        range_analysis = RangeAnalysis(p)
-        range_analysis.apply(prog.body[0])
-        r = prog.body[0].body[0].out_values["range"]
+        r = do_range_analysis("def f(x):\n\tx=random_bit()", p)
         self.assertEquals(r, {'x': (0, 1)})
 
     def test_range_global_variable(self):
@@ -252,9 +159,7 @@
         self.assertEquals(r, {'y': full_range(p)})
 
     def test_range_if(self):
-        p = 13
-        prog = parse(
-"""
+        r = do_range_analysis("""
 def f(x):
     y=1
     if(x>1):
@@ -262,10 +167,7 @@
     else:
         y=3
     return y
-""")
-        range_analysis = RangeAnalysis(p)
-        range_analysis.apply(prog.body[0])
-        r = prog.body[0].body[-1].out_values["range"]
+""", p)
         self.assertEquals(r, {'x': full_range(p), 'y': (2, 3)})
 
 #    def test_range_while(self):
@@ -287,15 +189,10 @@
 #           (1, Bottom())})
 #
     def test_range_return(self):
-        p = 13
-        prog = parse(
-"""
+        r = do_range_analysis("""
 def f(x):
     return x
-""")
-        range_analysis = RangeAnalysis(p)
-        range_analysis.apply(prog.body[0])
-        r = prog.body[0].body[-1].out_values["range"]
+""", p)
         self.assertEquals(r, {'x': full_range(p)})