[pypy-svn] r13401 - in pypy/dist/pypy/rpython: . test

hpk at codespeak.net hpk at codespeak.net
Tue Jun 14 20:05:47 CEST 2005


Author: hpk
Date: Tue Jun 14 20:05:46 2005
New Revision: 13401

Modified:
   pypy/dist/pypy/rpython/interp.py
   pypy/dist/pypy/rpython/test/test_interp.py
Log:
use operator module and tests ints and floats


Modified: pypy/dist/pypy/rpython/interp.py
==============================================================================
--- pypy/dist/pypy/rpython/interp.py	(original)
+++ pypy/dist/pypy/rpython/interp.py	Tue Jun 14 20:05:46 2005
@@ -71,7 +71,16 @@
         self.setvar(operation.result, retval)
 
 ##############################
-# int operations 
+# primitive operations 
+import operator
 
-def int_add(x, y): 
-    return x + y 
+for typ in (float, int): 
+    typname = typ.__name__
+    for opname in 'add', 'sub', 'mul', 'div': 
+        assert hasattr(operator, opname)
+        exec py.code.Source("""
+            def %(typname)s_%(opname)s(x,y): 
+                assert isinstance(x, %(typname)s)
+                assert isinstance(y, %(typname)s)
+                return operator.%(opname)s(x, y) 
+        """ % locals()).compile()

Modified: pypy/dist/pypy/rpython/test/test_interp.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_interp.py	(original)
+++ pypy/dist/pypy/rpython/test/test_interp.py	Tue Jun 14 20:05:46 2005
@@ -14,21 +14,28 @@
     t.checkgraphs()
     return t
     
-def test_int_adding(): 
-    t = gengraph(int_adding, [int])
+def test_int_ops(): 
+    t = gengraph(number_ops, [int])
     interp = LLInterpreter(t.flowgraphs)
-    res = interp.eval_function(int_adding, [3])
-    assert res == 6 
+    res = interp.eval_function(number_ops, [3])
+    assert res == 4 
+
+def test_float_ops(): 
+    t = gengraph(number_ops, [float])
+    interp = LLInterpreter(t.flowgraphs)
+    res = interp.eval_function(number_ops, [3.5])
+    assert res == 4.5 
+
 
 #__________________________________________________________________
 # example functions for testing the LLInterpreter 
 _snap = globals().copy()
 
-def int_adding(i): 
+def number_ops(i): 
     j = i + 2
-    return j + 1 
-
-
+    k = j * 2 
+    m = k / 2
+    return m - 1
 
 #__________________________________________________________________
 # interactive playing 
@@ -40,9 +47,9 @@
     except ImportError: 
         pass
 
-    t = gengraph(int_adding, [int])
+    t = gengraph(number_ops, [int])
     interp = LLInterpreter(t.flowgraphs)
-    res = interp.eval_function(int_adding, [3])
+    res = interp.eval_function(number_ops, [3])
     assert res == 6 
     for name, value in globals().items(): 
         if name not in _snap and name[0] != '_': 



More information about the Pypy-commit mailing list