[pypy-svn] r25810 - pypy/dist/pypy/rpython/rctypes/test

arigo at codespeak.net arigo at codespeak.net
Thu Apr 13 18:52:12 CEST 2006


Author: arigo
Date: Thu Apr 13 18:52:11 2006
New Revision: 25810

Modified:
   pypy/dist/pypy/rpython/rctypes/test/test_rarray.py
   pypy/dist/pypy/rpython/rctypes/test/test_rstruct.py
Log:
Some more tests.


Modified: pypy/dist/pypy/rpython/rctypes/test/test_rarray.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/test/test_rarray.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/test/test_rarray.py	Thu Apr 13 18:52:11 2006
@@ -20,6 +20,24 @@
 
 c_int_10 = ARRAY(c_int,10)
 
+def maketest():
+    A1 = c_int * 10
+    A2 = POINTER(c_int) * 10
+    A3 = A1 * 10
+    A4 = POINTER(A1) * 10
+    A5 = c_char_p * 10
+    def func():
+        a1 = A1(); a1[4] = 1000
+        a2 = A2(); a2[5] = pointer(c_int(200))
+        a3 = A3(); a3[2][9] = 30
+        a4 = A4(); a4[3] = pointer(a1); a1[1] = 4
+        a5 = A5(); a5[7] = "hello"
+        res = a1[4] + a2[5].contents.value + a3[2][9] + a4[3].contents[1]
+        res *= ord(a5[7][1])
+        return res
+    return func, 1234 * ord('e')
+
+
 class Test_annotation:
     def test_annotate_array(self):
         def create_array():
@@ -119,22 +137,8 @@
         assert s.knowntype == int
 
     def test_annotate_variants(self):
-        A1 = c_int * 10
-        A2 = POINTER(c_int) * 10
-        A3 = A1 * 10
-        A4 = POINTER(A1) * 10
-        A5 = c_char_p * 10
-        def func():
-            a1 = A1(); a1[4] = 1000
-            a2 = A2(); a2[5] = pointer(c_int(200))
-            a3 = A3(); a3[2][9] = 30
-            a4 = A4(); a4[3] = pointer(a1); a1[1] = 4
-            a5 = A5(); a5[7] = "hello"
-            res = a1[4] + a2[5].contents.value + a3[2][9] + a4[3].contents[1]
-            res *= ord(a5[7][1])
-            return res
-        assert func() == 1234 * ord('e')
-
+        func, expected = maketest()
+        assert func() == expected
         t = TranslationContext()
         a = t.buildannotator()
         s = a.build_types(func, [])
@@ -181,6 +185,11 @@
         res = interpret(func, [3, 7])
         assert res == 343
 
+    def test_specialize_variants(self):
+        func, expected = maketest()
+        res = interpret(func, [])
+        assert res == expected
+
 class Test_compilation:
     def test_compile_array_access(self):
         def access_array():
@@ -207,3 +216,8 @@
         fn = compile(func, [int, int])
         assert fn(2, 7) == 49
         assert fn(3, 6) == 216
+
+    def test_compile_variants(self):
+        func, expected = maketest()
+        fn = compile(func, [])
+        assert fn() == expected

Modified: pypy/dist/pypy/rpython/rctypes/test/test_rstruct.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/test/test_rstruct.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/test/test_rstruct.py	Thu Apr 13 18:52:11 2006
@@ -16,12 +16,30 @@
 except ImportError:
     py.test.skip("this test needs ctypes installed")
 
-from ctypes import c_int, c_short, Structure, POINTER, pointer
+from ctypes import c_int, c_short, Structure, POINTER, pointer, c_char_p
 
 class tagpoint(Structure):
     _fields_ = [("x", c_int),
                 ("y", c_int)]
 
+def maketest():
+    class S1(Structure): _fields_ = [('x', c_int)]
+    class S2(Structure): _fields_ = [('x', POINTER(c_int))]
+    class S3(Structure): _fields_ = [('x', S1)]
+    class S4(Structure): _fields_ = [('x', POINTER(S1))]
+    class S5(Structure): _fields_ = [('x', c_char_p)]
+    def func():
+        s1 = S1(); s1.x = 500
+        s2 = S2(); s2.x = pointer(c_int(200))
+        s3 = S3(); s3.x.x = 30
+        s4 = S4(); s4.x = pointer(s1)
+        s5 = S5(); s5.x = "hello"
+        res = s1.x + s2.x.contents.value + s3.x.x + s4.x.contents.x
+        res *= ord(s5.x[4])
+        return res
+    return func, 1230 * ord('o')
+
+
 class Test_annotation:
     def test_annotate_struct(self):
         def create_struct():
@@ -68,6 +86,16 @@
             a.translator.view()
         assert s.knowntype == int
 
+    def test_annotate_variants(self):
+        func, expected = maketest()
+        assert func() == expected
+        t = TranslationContext()
+        a = t.buildannotator()
+        s = a.build_types(func, [])
+        if conftest.option.view:
+            a.translator.view()
+        assert s.knowntype == int
+
 class Test_specialization:
     def test_specialize_struct(self):
         def create_struct():
@@ -105,6 +133,11 @@
         res = interpret(func, [3])
         assert res == 11
 
+    def test_specialize_variants(self):
+        func, expected = maketest()
+        res = interpret(func, [])
+        assert res == expected
+
 class Test_compilation:
     def test_compile_struct_access(self):
         def access_struct(n):
@@ -131,3 +164,8 @@
         fn = compile(func, [int])
         assert fn(2) == 7
         assert fn(3) == 11
+
+    def test_compile_variants(self):
+        func, expected = maketest()
+        fn = compile(func, [])
+        assert fn() == expected



More information about the Pypy-commit mailing list