[pypy-svn] rev 2027 - pypy/trunk/src/pypy/translator/test

sanxiyn at codespeak.net sanxiyn at codespeak.net
Thu Oct 23 20:26:05 CEST 2003


Author: sanxiyn
Date: Thu Oct 23 20:26:04 2003
New Revision: 2027

Added:
   pypy/trunk/src/pypy/translator/test/snippet.py
Modified:
   pypy/trunk/src/pypy/translator/test/test_cltrans.py
   pypy/trunk/src/pypy/translator/test/test_pyrextrans.py
   pypy/trunk/src/pypy/translator/test/test_typedpyrex.py
Log:
moved translator test functions from each test case to:
the glorious new file: snippet.py

read snippet.py docstring for my plan

but for now, nothing is changed, and only the code
duplcation is removed.


Added: pypy/trunk/src/pypy/translator/test/snippet.py
==============================================================================
--- (empty file)
+++ pypy/trunk/src/pypy/translator/test/snippet.py	Thu Oct 23 20:26:04 2003
@@ -0,0 +1,143 @@
+"""Snippets for translation
+
+This module holds various snippets, to be used by translator
+unittests.
+
+TODO, or sanxiyn's plan:
+
+Each snippet should know about suitable arguments to test it.
+(Otherwise, there's a duplcation!) Should the result also be
+stored? It can computed by CPython if we don't store it.
+
+In case of typed translation test, we can give input_arg_types
+by actually trying type() on arguments.
+
+Each unittest should define a list of functions which it is able
+to translate correctly, and import the list for tests. When
+a translator can handle more, simply adding a function to the
+list should suffice.
+
+But for now, none of the above applies.
+"""
+
+def if_then_else(cond, x, y):
+    if cond:
+        return x
+    else:
+        return y
+
+def my_gcd(a, b):
+    r = a % b
+    while r:
+        a = b
+        b = r
+        r = a % b
+    return b
+
+def is_perfect_number(n):
+    div = 1
+    sum = 0
+    while div < n:
+        if n % div == 0:
+            sum += div
+        div += 1
+    return n == sum
+
+def my_bool(x):
+    return not not x
+
+def two_plus_two():
+    """Array test"""
+    array = [0] * 3
+    array[0] = 2
+    array[1] = 2
+    array[2] = array[0] + array[1]
+    return array[2]
+
+def sieve_of_eratosthenes():
+    """Sieve of Eratosthenes
+    
+    This one is from an infamous benchmark, "The Great Computer
+    Language Shootout".
+
+    URL is: http://www.bagley.org/~doug/shootout/
+    """
+    flags = [True] * (8192+1)
+    count = 0
+    i = 2
+    while i <= 8192:
+        if flags[i]:
+            k = i + i
+            while k <= 8192:
+                flags[k] = False
+                k = k + i
+            count = count + 1
+        i = i + 1
+    return count
+
+def simple_func(i):
+    return i + 1
+
+def while_func(i):
+    total = 0
+    while i > 0:
+        total = total + i
+        i = i - 1
+    return total
+
+def nested_whiles(i, j):
+    s = ''
+    z = 5
+    while z > 0:
+        z = z - 1
+        u = i
+        while u < j:
+            u = u + 1
+            s = s + '.'
+        s = s + '!'
+    return s
+
+def poor_man_range(i):
+    lst = []
+    while i > 0:
+        i = i - 1
+        lst.append(i)
+    lst.reverse()
+    return lst
+
+def simple_id(x):
+    return x
+
+def branch_id(cond, a, b):
+    while 1:
+        if cond:
+            return a
+        else:
+            return b
+
+def attrs():
+    def b(): pass
+    b.f = 4
+    b.g = 5
+    return b.f + b.g
+
+def builtinusage():
+    return pow(2, 2)
+
+def yast(lst):
+    total = 0
+    for z in lst:
+        total = total + z
+    return total
+
+def time_waster(n):
+    "Arbitrary test function"
+    i = 0
+    x = 1
+    while i<n:
+        j = 0
+        while j<=i:
+            j = j + 1
+            x = x + (i&j)
+        i = i + 1
+    return x

Modified: pypy/trunk/src/pypy/translator/test/test_cltrans.py
==============================================================================
--- pypy/trunk/src/pypy/translator/test/test_cltrans.py	(original)
+++ pypy/trunk/src/pypy/translator/test/test_cltrans.py	Thu Oct 23 20:26:04 2003
@@ -2,7 +2,6 @@
 from pypy.tool import test
 from pypy.tool.udir import udir
 from pypy.translator.test.buildcl import _make_cl_func
-from vpath.adapter.process import exec_cmd, ExecutionFailed
 
 
 import os
@@ -34,6 +33,8 @@
     return _make_cl_func(func, global_cl, udir)
 
 
+from pypy.translator.test import snippet as t
+
 class GenCLTestCase(test.IntTestCase):
 
     def setUp(self):
@@ -41,85 +42,37 @@
             raise (test.TestSkip,
                    "Common Lisp neither configured nor detected.")
 
-    #___________________________________
-    def if_then_else(cond, x, y):
-        if cond:
-            return x
-        else:
-            return y
     def test_if_bool(self):
-        cl_if = make_cl_func(self.if_then_else)
+        cl_if = make_cl_func(t.if_then_else)
         self.assertEquals(cl_if(True, 50, 100), 50)
         self.assertEquals(cl_if(False, 50, 100), 100)
+
     def test_if_int(self):
-        cl_if = make_cl_func(self.if_then_else)
+        cl_if = make_cl_func(t.if_then_else)
         self.assertEquals(cl_if(0, 50, 100), 100)
         self.assertEquals(cl_if(1, 50, 100), 50)
 
-    #___________________________________
-    def my_gcd(a, b):
-        r = a % b
-        while r:
-            a = b
-            b = r
-            r = a % b
-        return b
     def test_gcd(self):
-        cl_gcd = make_cl_func(self.my_gcd)
+        cl_gcd = make_cl_func(t.my_gcd)
         self.assertEquals(cl_gcd(96, 64), 32)
 
-    #___________________________________
-    def is_perfect_number(n):
-        div = 1
-        sum = 0
-        while div < n:
-            if n % div == 0:
-                sum += div
-            div += 1
-        return n == sum
     def test_is_perfect(self): # pun intended
-        cl_perfect = make_cl_func(self.is_perfect_number)
+        cl_perfect = make_cl_func(t.is_perfect_number)
         self.assertEquals(cl_perfect(24), False)
         self.assertEquals(cl_perfect(28), True)
 
-    #___________________________________
-    def my_bool(x):
-        return not not x
     def test_bool(self):
-        cl_bool = make_cl_func(self.my_bool)
+        cl_bool = make_cl_func(t.my_bool)
         self.assertEquals(cl_bool(0), False)
         self.assertEquals(cl_bool(42), True)
         self.assertEquals(cl_bool(True), True)
 
-    #___________________________________
-    def two_plus_two():
-        array = [0] * 3
-        array[0] = 2
-        array[1] = 2
-        array[2] = array[0] + array[1]
-        return array[2]
     def test_array(self):
-        cl_four = make_cl_func(self.two_plus_two)
+        cl_four = make_cl_func(t.two_plus_two)
         self.assertEquals(cl_four(), 4)
 
-    #___________________________________
-    def sieve_of_eratosthenes():
-        # This one is from:
-        # The Great Computer Language Shootout
-        flags = [True] * (8192+1)
-        count = 0
-        i = 2
-        while i <= 8192:
-            if flags[i]:
-                k = i + i
-                while k <= 8192:
-                    flags[k] = False
-                    k = k + i
-                count = count + 1
-            i = i + 1
-        return count
     def test_sieve(self):
-        cl_sieve = make_cl_func(self.sieve_of_eratosthenes)
+        cl_sieve = make_cl_func(t.sieve_of_eratosthenes)
         self.assertEquals(cl_sieve(), 1028)
 
 if __name__ == '__main__':

Modified: pypy/trunk/src/pypy/translator/test/test_pyrextrans.py
==============================================================================
--- pypy/trunk/src/pypy/translator/test/test_pyrextrans.py	(original)
+++ pypy/trunk/src/pypy/translator/test/test_pyrextrans.py	Thu Oct 23 20:26:04 2003
@@ -33,127 +33,52 @@
     mod = make_module_from_pyxstring(name, udir, result)
     return getattr(mod, name)
 
+
+from pypy.translator.test import snippet as t
+
 class PyrexGenTestCase(test.IntTestCase):
+
     def setUp(self):
         self.space = test.objspace('flow')
 
-    #____________________________________________________
-    def simple_func(i):
-        return i+1
-
     def test_simple_func(self):
-        cfunc = make_cfunc(self.simple_func)
+        cfunc = make_cfunc(t.simple_func)
         self.assertEquals(cfunc(1), 2)
 
-    #____________________________________________________
-    def while_func(i):
-        total = 0
-        while i > 0:
-            total = total + i
-            i = i - 1
-        return total
-
     def test_while_func(self):
-        while_func = make_cfunc(self.while_func)
+        while_func = make_cfunc(t.while_func)
         self.assertEquals(while_func(10), 55)
 
-    #____________________________________________________
-    def nested_whiles(i, j):
-        s = ''
-        z = 5
-        while z > 0:
-            z = z - 1
-            u = i
-            while u < j:
-                u = u + 1
-                s = s + '.'
-            s = s + '!'
-        return s
-
     def test_nested_whiles(self):
-        nested_whiles = make_cfunc(self.nested_whiles)
+        nested_whiles = make_cfunc(t.nested_whiles)
         self.assertEquals(nested_whiles(111, 114),
                           '...!...!...!...!...!')
 
-    #____________________________________________________
-    def poor_man_range(i):
-        lst = []
-        while i > 0:
-            i = i - 1
-            lst.append(i)
-        lst.reverse()
-        return lst
-
     def test_poor_man_range(self):
-        poor_man_range = make_cfunc(self.poor_man_range)
+        poor_man_range = make_cfunc(t.poor_man_range)
         self.assertEquals(poor_man_range(10), range(10))
 
-   #____________________________________________________
-
-    def simple_id(x):
-        return x
-
     def test_simple_id(self):
         #we just want to see, if renaming of parameter works correctly
         #if the first branch is the end branch
-        simple_id = make_cfunc(self.simple_id)
+        simple_id = make_cfunc(t.simple_id)
         self.assertEquals(simple_id(9), 9)
 
-   #____________________________________________________
-
-    def branch_id(cond, a, b):
-        while 1:
-            if cond:
-                return a
-            else:
-                return b
-
     def test_branch_id(self):
-        branch_id = make_cfunc(self.branch_id)
+        branch_id = make_cfunc(t.branch_id)
         self.assertEquals(branch_id(1, 2, 3), 2)
         self.assertEquals(branch_id(0, 2, 3), 3)
 
-    #____________________________________________________
-
-    def attrs():
-        def b(): pass
-        b.f = 4
-        b.g = 5
-
-        return b.f + b.g
-
-    def _test_attrs(self):
-        attrs = make_cfunc(self.attrs)
+    def dont_test_attrs(self):
+        attrs = make_cfunc(t.attrs)
         self.assertEquals(attrs(), 9)
 
-    #_____________________________________________________
-
-    def builtinusage():
-        return pow(2,2)
-
     def test_builtinusage(self):
-        fun = make_cfunc(self.builtinusage)
+        fun = make_cfunc(t.builtinusage)
         self.assertEquals(fun(), 4)
 
-    #_____________________________________________________
-    def sieve_of_eratosthenes():
-        # This one is from:
-        # The Great Computer Language Shootout
-        flags = [True] * (8192+1)
-        count = 0
-        i = 2
-        while i <= 8192:
-            if flags[i]:
-                k = i + i
-                while k <= 8192:
-                    flags[k] = False
-                    k = k + i
-                count = count + 1
-            i = i + 1
-        return count
-
     def test_sieve(self):
-        sieve = make_cfunc(self.sieve_of_eratosthenes)
+        sieve = make_cfunc(t.sieve_of_eratosthenes)
         self.assertEquals(sieve(), 1028)
 
 if __name__ == '__main__':

Modified: pypy/trunk/src/pypy/translator/test/test_typedpyrex.py
==============================================================================
--- pypy/trunk/src/pypy/translator/test/test_typedpyrex.py	(original)
+++ pypy/trunk/src/pypy/translator/test/test_typedpyrex.py	Thu Oct 23 20:26:04 2003
@@ -14,7 +14,10 @@
     def make_dot(*args): pass
 
 
+from pypy.translator.test import snippet as t
+
 class TypedPyrexTestCase(test.IntTestCase):
+
     def setUp(self):
         self.space = test.objspace('flow')
 
@@ -35,83 +38,29 @@
         mod = make_module_from_pyxstring(name, udir, result)
         return getattr(mod, name)
 
-    #____________________________________________________
-    def simple_func(i):
-        return i+1
-
     def test_simple_func(self):
-        cfunc = self.make_cfunc(self.simple_func, [int])
+        cfunc = self.make_cfunc(t.simple_func, [int])
         self.assertEquals(cfunc(1), 2)
 
-    #____________________________________________________
-    def while_func(i):
-        total = 0
-        while i > 0:
-            total = total + i
-            i = i - 1
-        return total
-
     def test_while_func(self):
-        while_func = self.make_cfunc(self.while_func, [int])
+        while_func = self.make_cfunc(t.while_func, [int])
         self.assertEquals(while_func(10), 55)
 
-    #____________________________________________________
-    def yast(lst):
-        total = 0
-        for z in lst:
-            total = total + z
-        return total
-
     def test_yast(self):
-        yast = self.make_cfunc(self.yast, [list])
+        yast = self.make_cfunc(t.yast, [list])
         self.assertEquals(yast(range(12)), 66)
 
-    #____________________________________________________
-    def nested_whiles(i, j):
-        s = ''
-        z = 5
-        while z > 0:
-            z = z - 1
-            u = i
-            while u < j:
-                u = u + 1
-                s = s + '.'
-            s = s + '!'
-        return s
-
     def test_nested_whiles(self):
-        nested_whiles = self.make_cfunc(self.nested_whiles, [int, int])
+        nested_whiles = self.make_cfunc(t.nested_whiles, [int, int])
         self.assertEquals(nested_whiles(111, 114),
                           '...!...!...!...!...!')
 
-    #____________________________________________________
-    def poor_man_range(i):
-        lst = []
-        while i > 0:
-            i = i - 1
-            lst.append(i)
-        lst.reverse()
-        return lst
-
     def test_poor_man_range(self):
-        poor_man_range = self.make_cfunc(self.poor_man_range, [int])
+        poor_man_range = self.make_cfunc(t.poor_man_range, [int])
         self.assertEquals(poor_man_range(10), range(10))
 
-    #____________________________________________________
-    def time_waster(n):
-        "Arbitrary test function."
-        i = 0
-        x = 1
-        while i<n:
-            j = 0
-            while j<=i:
-                j = j + 1
-                x = x + (i&j)
-            i = i + 1
-        return x
-
     def test_time_waster(self):
-        time_waster = self.make_cfunc(self.time_waster, [int])
+        time_waster = self.make_cfunc(t.time_waster, [int])
         self.assertEquals(time_waster(30), 3657)
 
 if __name__ == '__main__':


More information about the Pypy-commit mailing list