changeset 74:25268f5d228e

Added tests for corner cases in connection to minus. Also added some debug info.
author Janus Dam Nielsen <janus.nielsen@alexandra.dk>
date Wed, 27 May 2009 14:59:22 +0200
parents 1ed5f9156fe3
children 2cad6090a114
files pysmcl/range_analysis.py pysmcl/test/unit/test_rangeanalysis.py
diffstat 2 files changed, 50 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/pysmcl/range_analysis.py	Wed May 27 14:59:22 2009 +0200
+++ b/pysmcl/range_analysis.py	Wed May 27 14:59:22 2009 +0200
@@ -221,6 +221,8 @@
 
 def combine_env(env1, env2):
     """Combine the two environments env1 and env2."""
+    if debug:
+        print "  combine_env:", env1, env2
     env = {}
     common_keys = set(env1.keys()) & set(env2.keys())
     env1_keys = set(env1.keys()) - set(env2.keys())
@@ -231,19 +233,31 @@
         env[key] = env1[key]
     for key in env2_keys:
         env[key] = env2[key]
+    if debug:
+        print "  combined env:", env
     return env
 
 def combine_range((l1, h1), (l2, h2)):
     """Combine the two ranges."""
-    (r0, r1) = (Bottom(),Bottom())
-    if l1 < l2:
-        r[0] = l1
-        r0 = l1
+    if debug:
+        print "  combine_ranges:", (l1, h1), (l2, h2)
+    if (isinstance(l1, Bottom) or
+        isinstance(l2, Bottom)):
+        r0 = Bottom()
     else:
-        r0 = l2
+        if l1 < l2:
+            r0 = l1
+        else:
+            r0 = l2
 
-    if h1 > h2:
-        r1 = h1
+    if (isinstance(h1, Bottom) or
+        isinstance(h2, Bottom)):
+        r1 = Bottom()
     else:
-        r1 = h2
-    return (r0,r1)
+        if h1 > h2:
+            r1 = h1
+        else:
+            r1 = h2
+    if debug:
+        print "  combined ranges:", (r0, r1)
+    return (r0, r1)
--- a/pysmcl/test/unit/test_rangeanalysis.py	Wed May 27 14:59:22 2009 +0200
+++ b/pysmcl/test/unit/test_rangeanalysis.py	Wed May 27 14:59:22 2009 +0200
@@ -126,6 +126,33 @@
         r = prog.body[0].body[2].out_values["range"]
         self.assertEquals(r, {'y': (4, 4), 'x': (3, 3), 'z': (Bottom(), Bottom())})
 
+    def test_range_minus_bottom(self):
+        p = 7
+        prog = parse("def f(x):\n\tx=6\n\ty=8\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': (Bottom(), Bottom()), 'x': (6, 6), 'z': (Bottom(), Bottom())})
+
+    def test_range_minus_bottom_bottom(self):
+        p = 7
+        prog = parse(
+"""
+def f(x):
+    x=1
+    y=3
+    if x:
+         y=y-3
+    y=y-2
+    z=y-x
+""")
+        init_statements(prog)
+        range_analysis = RangeAnalysis(p)
+        range_analysis.apply(prog.body[0])
+        r = prog.body[0].body[-1].out_values["range"]
+        self.assertEquals(r, {'x': (1, 1), 'y': (Bottom(), 1), 'z': (Bottom(), 0)})
+
     def test_range_equals(self):
         p = 7
         prog = parse("def f(x):\n\tx=3\n\ty=2\n\tz=x==y\n")