changeset 25:a83e83de91ff

Added minimal tests for range analysis.
author Janus Dam Nielsen <janus.nielsen@alexandra.dk>
date Mon, 25 May 2009 11:57:04 +0200
parents 7184b6fb9d30
children 72602b2d107a
files pysmcl/test/unit/test_intervalanalysis.py pysmcl/test/unit/test_rangeanalysis.py
diffstat 2 files changed, 130 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/pysmcl/test/unit/test_intervalanalysis.py	Mon May 25 09:48:54 2009 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,8 +0,0 @@
-
-
-from unittest import TestCase
-
-from ast import *
-
-class IntervalAnalasisTest(TestCase):
-    pass
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pysmcl/test/unit/test_rangeanalysis.py	Mon May 25 11:57:04 2009 +0200
@@ -0,0 +1,130 @@
+
+
+import unittest
+
+from ast import *
+from pysmcl.ideal_functionality import init_statements
+from pysmcl.range_analysis import RangeAnalysis, Buttom
+
+class RangeAnalasisTest(unittest.TestCase):
+
+
+    def test_range_one_range(self):
+        p = 7
+        prog = parse("def f():\n\ty=0\n")
+        init_statements(prog)
+        range_analysis = RangeAnalysis(p)
+        range_analysis.visit(prog.body[0])
+        r = range_analysis.result
+        self.assertEquals(r, {'y': (0, 0)})
+
+    def test_range_unknown_range(self):
+        p = 7
+        prog = parse("def f(x):\n\tx\n")
+        init_statements(prog)
+        range_analysis = RangeAnalysis(p)
+        range_analysis.visit(prog.body[0])
+        r = range_analysis.result
+        self.assertEquals(r, {'x': (0, p)})
+        
+    def test_range_add(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': (5, 5)})
+
+    def test_range_add_wrap(self):
+        p = 7
+        prog = parse("def f(x):\n\tx=6\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': (Buttom(), Buttom())})
+
+    def test_range_mul(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': (6, 6)})
+
+    def test_range_mul_wrap(self):
+        p = 7
+        prog = parse("def f(x):\n\tx=3\n\ty=3\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': (Buttom(), Buttom())})
+
+    def test_range_minus(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': (1, 1)})
+
+    def test_range_minus_wrap(self):
+        p = 7
+        prog = parse("def f(x):\n\tx=3\n\ty=4\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': (Buttom(), Buttom())})
+
+    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_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.visit(prog.body[0])
+        r = range_analysis.result
+        self.assertEquals(r, {'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_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)})
+
+if __name__ == '__main__':
+    unittest.main()