[pypy-svn] r15481 - in pypy/dist/pypy/translator/llvm2: . test

rxe at codespeak.net rxe at codespeak.net
Sun Jul 31 17:30:27 CEST 2005


Author: rxe
Date: Sun Jul 31 17:30:24 2005
New Revision: 15481

Added:
   pypy/dist/pypy/translator/llvm2/test/test_typed.py
Modified:
   pypy/dist/pypy/translator/llvm2/database.py
   pypy/dist/pypy/translator/llvm2/opwriter.py
   pypy/dist/pypy/translator/llvm2/test/test_genllvm.py
Log:
More tests and very little code added.  Tests were stolen
kindly from c/test/test_typed.py



Modified: pypy/dist/pypy/translator/llvm2/database.py
==============================================================================
--- pypy/dist/pypy/translator/llvm2/database.py	(original)
+++ pypy/dist/pypy/translator/llvm2/database.py	Sun Jul 31 17:30:24 2005
@@ -18,8 +18,6 @@
                       lltype.Unsigned: "uint",
                       lltype.Bool: "bool",
                       lltype.Float: "double",
-                      # XXX Preliminary support for unicode, makes sense to
-                      # make this more configurable
                       lltype.UniChar: "uint",
                       lltype.Void: "void"}
 
@@ -66,7 +64,7 @@
     elif type_ is lltype.Char:
         repr = str(ord(value))
     elif type_ is lltype.UniChar:
-        repr = "0" # XXX Dont know what to do here at all?
+        repr = str(ord(value))
     else:
         repr = str(value)
     return repr

Modified: pypy/dist/pypy/translator/llvm2/opwriter.py
==============================================================================
--- pypy/dist/pypy/translator/llvm2/opwriter.py	(original)
+++ pypy/dist/pypy/translator/llvm2/opwriter.py	Sun Jul 31 17:30:24 2005
@@ -82,13 +82,45 @@
             assert meth is not None, "operation %r not found" %(op.opname,)
             meth(op)    
 
-    def int_neg(self, op): 
+    def _generic_pow(self, op, onestr): 
+        mult_type = self.db.repr_arg_type(op.args[0])
+        mult_val = self.db.repr_arg(op.args[0])
+        last_val = mult_val
+        operand = int(op.args[1].value)
+        if operand < 1:
+            res_val = onestr
+        else:
+            res_val = mult_val
+            for ii in range(operand - 1):
+                res_val = self.db.repr_tmpvar()
+                self.codewriter.binaryop("mul", 
+                                         res_val,
+                                         mult_type,
+                                         last_val,
+                                         mult_val)
+                last_val = res_val
+        targetvar = self.db.repr_arg(op.result)
+        self.codewriter.cast(targetvar, mult_type, res_val, mult_type)        
+
+    def int_pow(self, op):
+        self._generic_pow(op, "1") 
+    uint_pow = int_pow
+    
+    def float_pow(self, op):
+        self._generic_pow(op, "1.0") 
+
+    def _generic_neg(self, op, zerostr): 
         self.codewriter.binaryop("sub", 
                                  self.db.repr_arg(op.result),
                                  self.db.repr_arg_type(op.args[0]),
-                                 "0", 
+                                 zerostr, 
                                  self.db.repr_arg(op.args[0]),
                                  )
+    def int_neg(self, op):
+        self._generic_neg(op, "0") 
+
+    def float_neg(self, op):
+        self._generic_neg(op, "0.0") 
 
     def bool_not(self, op):
         self.codewriter.binaryop("xor",
@@ -96,7 +128,6 @@
                                  self.db.repr_arg_type(op.args[0]),
                                  self.db.repr_arg(op.args[0]), 
                                  "true")
-
                     
     def binaryop(self, op):
         name = self.binary_operations[op.opname]
@@ -133,6 +164,7 @@
     cast_char_to_bool = cast_char_to_int  = cast_char_to_uint = cast_primitive
     cast_int_to_bool  = cast_int_to_char  = cast_int_to_uint  = cast_primitive
     cast_uint_to_bool = cast_uint_to_char = cast_uint_to_int  = cast_primitive
+    cast_int_to_float = cast_float_to_int = cast_primitive
     cast_pointer = cast_primitive
     same_as = cast_primitive
 

Modified: pypy/dist/pypy/translator/llvm2/test/test_genllvm.py
==============================================================================
--- pypy/dist/pypy/translator/llvm2/test/test_genllvm.py	(original)
+++ pypy/dist/pypy/translator/llvm2/test/test_genllvm.py	Sun Jul 31 17:30:24 2005
@@ -52,13 +52,58 @@
         x += i >= i
         x += i > i
         x += x % i
+        x += x ** 0
+        x += x ** 1
+        x += x ** 2
+        x += i + 1 * i // i - 1
         #x += i is not None
         #x += i is None
-        return i + 1 * i // i - 1
+        return x
     f = compile_function(ops, [int])
-    assert f(1) == 1
-    assert f(2) == 2
+    assert f(1) == ops(1)
+    assert f(2) == ops(2)
     
+def test_uint_ops():
+    def ops(i):
+        x = 0
+        x += i < i
+        x += i <= i
+        x += i == i
+        x += i != i
+        x += i >= i
+        x += i > i
+        x += x % i
+        x += x ** 0
+        x += x ** 1
+        x += x ** 2
+        x += i + 1 * i // i - 1
+        #x += i is not None
+        #x += i is None
+        return x
+    f = compile_function(ops, [r_uint])
+    assert f(1) == ops(1)
+    assert f(2) == ops(2)
+
+def test_float_ops():
+    def ops(flt):
+        x = 0
+        x += flt < flt
+        x += flt <= flt
+        x += flt == flt
+        x += flt != flt
+        x += flt >= flt
+        x += flt > flt
+        x += x ** 0
+        x += x ** 1
+        x += x ** 2
+        x += int(flt + 1 * flt / flt - 1)
+        #x += flt fs not None
+        #x += flt is None
+        return x 
+    f = compile_function(ops, [float])
+    assert f(1) == ops(1)
+    assert f(2) == ops(2)
+
 def test_while_loop():
     def factorial(i):
         r = 1
@@ -108,39 +153,6 @@
     assert f(256.0)
     assert not f(0.0)
 
-def test_uint_ops():
-    def ops(i):
-        x = r_uint(0)
-        x += i < i
-        x += i <= i
-        x += i == i
-        x += i != i
-        x += i >= i
-        x += i > i
-        x += x % i
-        #x += i is not None
-        #x += i is None
-        return i + 1 * i // i - 1
-    f = compile_function(ops, [r_uint])
-    assert f(1) == 1
-    assert f(2) == 2
-
-def test_float_ops():
-    def ops(flt):
-        x = 0
-        x += flt < flt
-        x += flt <= flt
-        x += flt == flt
-        x += flt != flt
-        x += flt >= flt
-        x += flt > flt
-        #x += flt fs not None
-        #x += flt is None
-        return flt + 1 * flt / flt - 1
-    f = compile_function(ops, [float])
-    assert f(1) == 1
-    assert f(2) == 2
-
 def test_function_call():
     def callee():
         return 1

Added: pypy/dist/pypy/translator/llvm2/test/test_typed.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/llvm2/test/test_typed.py	Sun Jul 31 17:30:24 2005
@@ -0,0 +1,285 @@
+import sys
+import py
+from py.test import raises
+from pypy.translator.test import snippet 
+from pypy.rpython.rarithmetic import r_uint
+
+from pypy.translator.llvm2.genllvm import compile_function
+
+def test_call_five():
+    # --  the result of call_five() isn't a real list, but an rlist
+    #     that can't be converted to a PyListObject
+    def wrapper():
+        lst = snippet.call_five()
+        res = list((len(lst), lst[0]))
+        expected = [1, 5]
+        return res == expected
+    fn = compile_function(wrapper, [])
+    result = fn()
+    assert result
+
+def test_get_set_del_slice():
+    def get_set_del_nonneg_slice(): # no neg slices for now!
+        l = [ord('a'), ord('b'), ord('c'), ord('d'), ord('e'), ord('f'), ord('g'), ord('h'), ord('i'), ord('j')]
+        del l[:1]
+        bound = len(l)-1
+        if bound >= 0:
+            del l[bound:]
+        del l[2:4]
+        #l[:1] = [3]
+        #bound = len(l)-1
+        #assert bound >= 0
+        #l[bound:] = [9]    no setting slice into lists for now
+        #l[2:4] = [8,11]
+        l[0], l[-1], l[2], l[3] = 3, 9, 8, 11
+
+        list_3_c = l[:2]
+        list_9 = l[5:]
+        list_11_h = l[3:5]
+        return list((len(l), l[0], l[1], l[2], l[3], l[4], l[5],
+                     len(list_3_c),  list_3_c[0],  list_3_c[1],
+                     len(list_9),    list_9[0],
+                     len(list_11_h), list_11_h[0], list_11_h[1]))
+    
+    def wrapper():
+        res = get_set_del_nonneg_slice()
+        expected = [6, 3, ord('c'), 8, 11, ord('h'), 9,
+                    2, 3, ord('c'),
+                    1, 9,
+                    2, 11, ord('h')]
+    
+        return res == expected
+
+    fn = compile_function(wrapper, [])
+    result = fn()
+    assert result 
+
+def test_is():
+    py.test.skip("array type of void")
+    def testfn():
+        l1 = []
+        return l1 is l1
+    fn = compile_function(testfn, [])
+    result = fn()
+    assert result is True
+    def testfn():
+        l1 = []
+        return l1 is None
+    fn = compile_function(testfn, [])
+    result = fn()
+    assert result is False
+
+def test_str_compare():
+    def testfn(i, j):
+        s1 = ['one', 'two']
+        s2 = ['one', 'two', 'o', 'on', 'twos', 'foobar']
+        return s1[i] == s2[j]
+    fn = compile_function(testfn, [int, int])
+    for i in range(2):
+        for j in range(6):
+            res = fn(i, j)
+            assert res == testfn(i, j)
+
+    def testfn(i, j):
+        s1 = ['one', 'two']
+        s2 = ['one', 'two', 'o', 'on', 'twos', 'foobar']
+        return s1[i] != s2[j]
+    fn = compile_function(testfn, [int, int])
+    for i in range(2):
+        for j in range(6):
+            res = fn(i, j)
+            assert res == testfn(i, j)
+
+    def testfn(i, j):
+        s1 = ['one', 'two']
+        s2 = ['one', 'two', 'o', 'on', 'twos', 'foobar']
+        return s1[i] < s2[j]
+    fn = compile_function(testfn, [int, int])
+    for i in range(2):
+        for j in range(6):
+            res = fn(i, j)
+            assert res == testfn(i, j)
+
+    def testfn(i, j):
+        s1 = ['one', 'two']
+        s2 = ['one', 'two', 'o', 'on', 'twos', 'foobar']
+        return s1[i] <= s2[j]
+    fn = compile_function(testfn, [int, int])
+    for i in range(2):
+        for j in range(6):
+            res = fn(i, j)
+            assert res == testfn(i, j)
+
+    def testfn(i, j):
+        s1 = ['one', 'two']
+        s2 = ['one', 'two', 'o', 'on', 'twos', 'foobar']
+        return s1[i] > s2[j]
+    fn = compile_function(testfn, [int, int])
+    for i in range(2):
+        for j in range(6):
+            res = fn(i, j)
+            assert res == testfn(i, j)
+
+    def testfn(i, j):
+        s1 = ['one', 'two']
+        s2 = ['one', 'two', 'o', 'on', 'twos', 'foobar']
+        return s1[i] >= s2[j]
+    fn = compile_function(testfn, [int, int])
+    for i in range(2):
+        for j in range(6):
+            res = fn(i, j)
+            assert res == testfn(i, j)
+
+def test_str_methods():
+    def testfn(i, j):
+        s1 = ['one', 'two']
+        s2 = ['one', 'two', 'o', 'on', 'ne', 'e', 'twos', 'foobar', 'fortytwo']
+        return s1[i].startswith(s2[j])
+    fn = compile_function(testfn, [int, int])
+    for i in range(2):
+        for j in range(9):
+            res = fn(i, j)
+            assert res == testfn(i, j)
+    def testfn(i, j):
+        s1 = ['one', 'two']
+        s2 = ['one', 'two', 'o', 'on', 'ne', 'e', 'twos', 'foobar', 'fortytwo']
+        return s1[i].endswith(s2[j])
+    fn = compile_function(testfn, [int, int])
+    for i in range(2):
+        for j in range(9):
+            res = fn(i, j)
+            assert res == testfn(i, j)
+
+def test_str_join():
+    def testfn(i, j):
+        s1 = [ '', ',', ' and ']
+        s2 = [ [], ['foo'], ['bar', 'baz', 'bazz']]
+        return len(s1[i].join(s2[j]))
+
+    fn = compile_function(testfn, [int, int])
+    for i in range(3):
+        for j in range(3):
+            res = fn(i, j)
+            assert res == testfn(i, j)
+
+def test_unichr_eq():
+    py.test.skip("unichar_eq operation missing")
+    l = list(u'Hello world')
+    def f(i, j):
+        return l[i] == l[j]
+    fn = compile_function(f, [int, int])
+    for i in range(len(l)):
+        for j in range(len(l)):
+            res = fn(i, j)
+            assert res == f(i,j) 
+
+def test_unichr_ne():
+    py.test.skip("unichar_ne operation missing")
+    l = list(u'Hello world')
+    def f(i, j):
+        return l[i] != l[j]
+    fn = compile_function(f, [int, int])
+    for i in range(len(l)):
+        for j in range(len(l)):
+            res = fn(i, j)
+            assert res == f(i, j)
+
+def test_unichr_ord():
+    py.test.skip("cast_unichar_to_int operation missing")
+    l = list(u'Hello world')
+    def f(i):
+        return ord(l[i]) 
+    fn = compile_function(f, [int])
+    for i in range(len(l)):
+        res = fn(i)
+        assert res == f(i)
+
+def test_unichr_unichr():
+    py.test.skip("cast_int_to_unichar operation missing")
+    l = list(u'Hello world')
+    def f(i, j):
+        return l[i] == unichr(j)
+    fn = compile_function(f, [int, int])
+    for i in range(len(l)):
+        for j in range(len(l)):
+            res = fn(i, ord(l[j]))
+            assert res == f(i, ord(l[j]))
+
+# floats 
+def test_float_operations(): 
+    py.test.skip("strangness with llvm rem operation...")    
+    def func(x, y): 
+        z = x + y / 2.1 * x 
+        z = z % 60.0
+        z = pow(z, 2)
+        z = -z
+        return int(z)
+
+    fn = compile_function(func, [float, float])
+    r1 = fn(5.0, 6.0)
+    r2 = func(5.0, 6.0)
+    assert r1 == r2 
+
+def test_rpbc_bound_method_static_call():
+    class R:
+        def meth(self):
+            return 0
+    r = R()
+    m = r.meth
+    def fn():
+        return m()
+    res = compile_function(fn, [])()
+    assert res == 0
+
+def test_constant_return_disagreement():
+    class R:
+        def meth(self):
+            return 0
+    r = R()
+    def fn():
+        return r.meth()
+    res = compile_function(fn, [])()
+    assert res == 0
+
+def test_stringformatting():
+    def fn(i):
+        return "you said %d, you did" % i
+    def wrapper(i):
+        res = fn(i)
+        return res == "you said 42, you did"
+
+    f = compile_function(wrapper, [int])
+    assert f(42)
+
+def test_str2int():
+    def fn(i):
+        return str(i)
+    def wrapper(i):
+        res = fn(i)
+        return res == "42"
+
+    f = compile_function(wrapper, [int])
+    assert f(42)
+
+def test_float2int():
+    py.test.skip("XXX dont understand")
+    def fn(f):
+        return str(f)
+    def wrapper():
+        res = fn(1.0)
+        return res == "1.0"
+
+    f = compile_function(wrapper, [], view=True)
+    assert f()
+
+def test_uint_arith():
+    py.test.skip("uint_invert operation missing")
+    def fn(i):
+        try:
+            return ~(i*(i+1))/(i-1)
+        except ZeroDivisionError:
+            return r_uint(91872331)
+    f = compile_function(fn, [r_uint])
+    for value in range(15):
+        i = r_uint(value)
+        assert f(i) == fn(i)



More information about the Pypy-commit mailing list