[pypy-svn] r4948 - pypy/trunk/src/pypy/translator/test

hpk at codespeak.net hpk at codespeak.net
Sat Jun 5 13:04:10 CEST 2004


Author: hpk
Date: Sat Jun  5 13:04:10 2004
New Revision: 4948

Modified:
   pypy/trunk/src/pypy/translator/test/snippet.py
   pypy/trunk/src/pypy/translator/test/test_annrpython.py
   pypy/trunk/src/pypy/translator/test/test_pyrextrans.py
   pypy/trunk/src/pypy/translator/test/test_translator.py
Log:
simplified the specification of translator/genpyrex tests 
a bit.  The snippets have default-values for arguments
specifying starting-types for the annotation ... 



Modified: pypy/trunk/src/pypy/translator/test/snippet.py
==============================================================================
--- pypy/trunk/src/pypy/translator/test/snippet.py	(original)
+++ pypy/trunk/src/pypy/translator/test/snippet.py	Sat Jun  5 13:04:10 2004
@@ -19,99 +19,23 @@
 
 But for now, none of the above applies.
 """
-function_info={
- 'append_five':{'arg_names': ['lst'],'arg_types':[list],
-               'name': 'append_five'},
- 'branch_id': {'arg_names': ['cond', 'a', 'b'],'arg_types':[int,int,int],
-               'name': 'branch_id'},
- 'break_continue': {'arg_names': ['x'],'arg_types':[int],
-                    'name': 'break_continue'},
- 'build_instance': {'arg_names': [],'arg_types':[],
-                    'name': 'build_instance'},
- 'builtinusage': {'arg_names': [],'arg_types':[],
-                  'name': 'builtinusage'},
- 'call_five': {'arg_names': [],'arg_types':[],
-                    'name': 'call_five'},
- 'choose_last': {'arg_names': [],'arg_types':[],
-                 'name': 'choose_last'},
- 'factorial': {'arg_names': ['n'],'arg_types':[int],
-               'name': 'factorial'},
- 'factorial2': {'arg_names': ['n'],'arg_types':[int],
-                'name': 'factorial2'},
- 'finallys': {'arg_names': ['lst'],'arg_types':[list],
-              'name': 'finallys'},
- 'greet': {'arg_names': ['target'],'arg_types':[str],
-           'name': 'greet'},
- 'half_of_n': {'arg_names': ['n'],'arg_types':[int],
-               'name': 'half_of_n'},
- 'if_then_else': {'arg_names': ['cond', 'x', 'y'],
-                     'arg_types':[object,object,object],
-                  'name': 'if_then_else'},
- 'inheritance1': {'arg_names': [],'arg_types':[],
-                  'name': 'inheritance1'},
- 'inheritance2': {'arg_names': [],'arg_types':[],
-                  'name': 'inheritance2'},
- #'inheritance_nonrunnable': {'arg_names': [],'arg_types':[],
- #                            'name': 'inheritance_nonrunnable'},
- 'int_id': {'arg_names': ['x'],'arg_types':[int],
-            'name': 'int_id'},
- 'is_perfect_number': {'arg_names': ['i'],'arg_types':[int],
-                    'name': 'is_perfect_number'},
- 'knownkeysdict': {'arg_names': ['b'],'arg_types':[object],
-                   'name': 'knownkeysdict'},
- 'merge_setattr': {'arg_names': ['x'],'arg_types':[object],
-                   'name': 'merge_setattr'},
-# 'methodcall1': {'arg_names': ['cond'],'arg_types':[int],
-#                   'name': 'methodcall1'},
- 'my_bool': {'arg_names': ['x'],'arg_types':[object],
-             'name': 'my_bool'},
- 'my_gcd': {'arg_names': ['a', 'b'],'arg_types':[int,int],
-            'name': 'my_gcd'},
- 'nested_whiles': {'arg_names': ['i', 'j'],'arg_types':[int,int],
-                   'name': 'nested_whiles'},
- 'poly_branch': {'arg_names': ['x'],'arg_types':[object],
-                 'name': 'poly_branch'},
- 'poor_man_range': {'arg_names': ['i'],'arg_types':[int],
-                    'name': 'poor_man_range'},
- 'poor_man_rev_range': {'arg_names': ['i'],'arg_types':[int],
-                    'name': 'poor_man__rev_range'},
- 'powerset': {'arg_names': ['x'],'arg_types':[int],
-                 'name': 'powerset'},
- 'prime': {'arg_names': ['n'],'arg_types':[int],
-           'name': 'prime'},
- 'reverse_3': {'arg_names': ['lst'],'arg_types':[list],
-               'name': 'reverse_3'},
- 's_and': {'arg_names': ['x', 'y'],'arg_types':[object,object],
-           'name': 's_and'},
- 'set_attr': {'arg_names': [],'arg_types':[],
-              'name': 'set_attr'},
- 'sieve_of_eratosthenes': {'arg_names': [],'arg_types':[],
-                           'name': 'sieve_of_eratosthenes'},
- 'simple_func': {'arg_names': ['i'],'arg_types':[int],
-                 'name': 'simple_func'},
- 'simple_id': {'arg_names': ['x'],'arg_types':[object],
-               'name': 'simple_id'},
- 'simple_method': {'arg_names': ['v'],'arg_types':[object],
-                   'name': 'simple_method'},
-# 'somebug1': {'arg_names': ['n'],'arg_types':[int],
-#              'name': 'somebug1'},
- 'time_waster': {'arg_names': ['n'],'arg_types':[int],
-                 'name': 'time_waster'},
- 'two_plus_two': {'arg_names': [],'arg_types':[],
-                  'name': 'two_plus_two'},
- 'while_func': {'arg_names': ['i'],'arg_types':[int],
-                'name': 'while_func'},
- 'yast': {'arg_names': ['lst'],'arg_types':[list],
-          'name': 'yast'}
- }
 
-def if_then_else(cond, x, y):
+# we define the starting types in the snippet 
+# function's default arguments.  the following
+# definitions denote to the "test-generator"
+# the possible types that can be passed to
+# the specific snippet. 
+numtype = (int, float, ) 
+anytype = (int, float, str, )
+seqtype = (list, tuple) 
+
+def if_then_else(cond=anytype, x=anytype, y=anytype):
     if cond:
         return x
     else:
         return y
 
-def my_gcd(a, b):
+def my_gcd(a=numtype, b=numtype):
     r = a % b
     while r:
         a = b
@@ -119,7 +43,7 @@
         r = a % b
     return b
 
-def is_perfect_number(n):
+def is_perfect_number(n=int):
     div = 1
     sum = 0
     while div < n:
@@ -128,7 +52,7 @@
         div += 1
     return n == sum
 
-def my_bool(x):
+def my_bool(x=int):
     return not not x
 
 def two_plus_two():
@@ -160,17 +84,17 @@
         i = i + 1
     return count
 
-def simple_func(i):
+def simple_func(i=numtype):
     return i + 1
 
-def while_func(i):
+def while_func(i=numtype):
     total = 0
     while i > 0:
         total = total + i
         i = i - 1
     return total
 
-def nested_whiles(i, j):
+def nested_whiles(i=int, j=int):
     s = ''
     z = 5
     while z > 0:
@@ -182,7 +106,7 @@
         s = s + '!'
     return s
 
-def poor_man_range(i):
+def poor_man_range(i=int): 
     lst = []
     while i > 0:
         i = i - 1
@@ -190,17 +114,17 @@
     lst.reverse()
     return lst
 
-def poor_man_rev_range(i):
+def poor_man_rev_range(i=int): 
     lst = []
     while i > 0:
         i = i - 1
         lst += [i]
     return lst
 
-def simple_id(x):
+def simple_id(x=anytype): 
     return x
 
-def branch_id(cond, a, b):
+def branch_id(cond=anytype, a=anytype, b=anytype):
     while 1:
         if cond:
             return a
@@ -210,13 +134,13 @@
 def builtinusage():
     return pow(2, 2)
 
-def yast(lst):
+def yast(lst=seqtype):
     total = 0
     for z in lst:
         total = total + z
     return total
 
-def time_waster(n):
+def time_waster(n=int):
     """Arbitrary test function"""
     i = 0
     x = 1
@@ -228,7 +152,7 @@
         i = i + 1
     return x
 
-def half_of_n(n):
+def half_of_n(n=int):
     """Slice test"""
     i = 0
     lst = range(n)
@@ -237,13 +161,13 @@
         i = i + 1
     return i
 
-def int_id(x):
+def int_id(x=int):
     i = 0
     while i < x:
         i = i + 1
     return i
 
-def greet(target):
+def greet(target=str):
     """String test"""
     hello = "hello"
     return hello + target
@@ -255,7 +179,7 @@
         pass
     return choice
 
-def poly_branch(x):
+def poly_branch(x=int):
     if x:
         y = [1,2,3]
     else:
@@ -264,13 +188,13 @@
     z = y
     return z*2
 
-def s_and(x, y):
+def s_and(x=anytype, y=anytype):
     if x and y:
         return 'yes'
     else:
         return 'no'
 
-def break_continue(x):
+def break_continue(x=numtype):
     result = []
     i = 0
     while 1:
@@ -285,14 +209,14 @@
         i = i + 1
     return result
 
-def reverse_3(lst):
+def reverse_3(lst=seqtype):
     try:
         a, b, c = lst
     except:
         return 0, 0, 0
     return c, b, a
 
-def finallys(lst):
+def finallys(lst=seqtype):
     x = 1
     try:
         x = 2
@@ -310,28 +234,27 @@
         x = 8
     return x
 
-def factorial(n):
+def factorial(n=int):
     if n <= 1:
         return 1
     else:
         return n * factorial(n-1)
 
-def factorial2(n):   # analysed in a different order
+def factorial2(n=int):   # analysed in a different order
     if n > 1:
         return n * factorial(n-1)
     else:
         return 1
 
-def append_five(lst):
+def _append_five(lst): 
     lst += [5]
-    
 
 def call_five():
     a = []
-    append_five(a)
+    _append_five(a)
     return a
 
-
+# INHERITANCE / CLASS TESTS  
 class C(object): pass
 
 def build_instance():
@@ -370,7 +293,7 @@
     d.stuff = (-12, -12)
     e = E()
     e.stuff = (3, "world")
-    return getstuff(d), getstuff(e)
+    return _getstuff(d), _getstuff(e)
 
 class F:
     pass
@@ -384,7 +307,7 @@
         self.attr = 1
         return E(), y
 
-def knownkeysdict(b):
+def knownkeysdict(b=anytype):
     if b:
         d = {'a': 0}
         d['b'] = b
@@ -393,7 +316,7 @@
         d = {'b': -123}
     return d['b']
 
-def prime(n):
+def prime(n=int):
     return len([i for i in range(1,n+1) if n%i==0]) == 2
 
 
@@ -401,13 +324,13 @@
     def my_method(self):
         return self.my_attribute
 
-def simple_method(v):
+def simple_method(v=anytype):
     z = Z()
     z.my_attribute = v
     return z.my_method()
 
 
-def powerset(setsize):
+def powerset(setsize=int):
     """Powerset
 
     This one is from a Philippine Pythonista Hangout, an modified
@@ -439,14 +362,15 @@
 
 # --------------------(Currently) Non runnable Functions ---------------------
 
-def somebug1(n):
+def _somebug1(n=int):
     l = []
     v = l.append
     while n:
-        l[7] = 5
+        l[7] = 5 # raises an exception 
+        break 
     return v
 
-def inheritance_nonrunnable():
+def _inheritance_nonrunnable():
     d = D()
     d.stuff = (-12, -12)
     e = E()
@@ -455,16 +379,16 @@
 
 # --------------------(Currently) Non compillable Functions ---------------------
 
-def attrs():
+def _attrs():
     def b(): pass
     b.f = 4
     b.g = 5
     return b.f + b.g
 
-def getstuff(x):
+def _getstuff(x):
     return x.stuff
 
-def methodcall1(cond):
+def _methodcall1(cond):
     if cond:
         x = G()
     else:

Modified: pypy/trunk/src/pypy/translator/test/test_annrpython.py
==============================================================================
--- pypy/trunk/src/pypy/translator/test/test_annrpython.py	(original)
+++ pypy/trunk/src/pypy/translator/test/test_annrpython.py	Sat Jun  5 13:04:10 2004
@@ -165,7 +165,7 @@
 
     def test_inheritance2(self):
         a = RPythonAnnotator()
-        s = a.build_types(snippet.inheritance_nonrunnable, [])
+        s = a.build_types(snippet._inheritance_nonrunnable, [])
         # result should be exactly:
         self.assertEquals(s, annmodel.SomeTuple([
                                 annmodel.SomeInteger(),
@@ -181,7 +181,7 @@
 
     def test_methodcall1(self):
         a = RPythonAnnotator()
-        s = a.build_types(snippet.methodcall1, [int])
+        s = a.build_types(snippet._methodcall1, [int])
         # result should be a tuple of (C, positive_int)
         self.assertEquals(s.knowntype, tuple)
         self.assertEquals(len(s.items), 2)
@@ -191,7 +191,7 @@
 
     def test_classes_methodcall1(self):
         a = RPythonAnnotator()
-        a.build_types(snippet.methodcall1, [int])
+        a.build_types(snippet._methodcall1, [int])
         # the user classes should have the following attributes:
         classes = a.bookkeeper.userclasses
         self.assertEquals(classes[snippet.F].attrs.keys(), ['m'])
@@ -207,7 +207,7 @@
 
     def test_somebug1(self):
         a = RPythonAnnotator()
-        s = a.build_types(snippet.somebug1, [int])
+        s = a.build_types(snippet._somebug1, [int])
         # result should be a built-in method
         self.assert_(isinstance(s, annmodel.SomeBuiltin))
 

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	Sat Jun  5 13:04:10 2004
@@ -4,8 +4,9 @@
 from pypy.translator.genpyrex import GenPyrex
 from pypy.objspace.flow.model import *
 from pypy.translator.tool.buildpyxmodule import build_cfunc
+from pypy.translator.translator import Translator
 
-from pypy.translator.test import snippet as t
+from pypy.translator.test import snippet 
 
 class NoTypePyrexGenTestCase(testit.IntTestCase):
 
@@ -24,64 +25,111 @@
         return build_cfunc(func, **options)
 
     def test_simple_func(self):
-        cfunc = self.build_cfunc(t.simple_func)
+        cfunc = self.build_cfunc(snippet.simple_func)
         self.assertEquals(cfunc(1), 2)
 
     def test_while_func(self):
-        while_func = self.build_cfunc(t.while_func)
+        while_func = self.build_cfunc(snippet.while_func)
         self.assertEquals(while_func(10), 55)
 
     def test_nested_whiles(self):
-        nested_whiles = self.build_cfunc(t.nested_whiles)
+        nested_whiles = self.build_cfunc(snippet.nested_whiles)
         self.assertEquals(nested_whiles(111, 114),
                           '...!...!...!...!...!')
 
     def test_poor_man_range(self):
-        poor_man_range = self.build_cfunc(t.poor_man_range)
+        poor_man_range = self.build_cfunc(snippet.poor_man_range)
         self.assertEquals(poor_man_range(10), range(10))
 
     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 = self.build_cfunc(t.simple_id)
+        simple_id = self.build_cfunc(snippet.simple_id)
         self.assertEquals(simple_id(9), 9)
 
     def test_branch_id(self):
-        branch_id = self.build_cfunc(t.branch_id)
+        branch_id = self.build_cfunc(snippet.branch_id)
         self.assertEquals(branch_id(1, 2, 3), 2)
         self.assertEquals(branch_id(0, 2, 3), 3)
 
     def test_int_id(self):
-        int_id = self.build_cfunc(t.int_id)
+        int_id = self.build_cfunc(snippet.int_id)
         self.assertEquals(int_id(3), 3)
 
     def dont_test_attrs(self):
-        attrs = self.build_cfunc(t.attrs)
+        attrs = self.build_cfunc(snippet.attrs)
         self.assertEquals(attrs(), 9)
 
     def test_builtinusage(self):
-        fun = self.build_cfunc(t.builtinusage)
+        fun = self.build_cfunc(snippet.builtinusage)
         self.assertEquals(fun(), 4)
 
     def test_sieve(self):
-        sieve = self.build_cfunc(t.sieve_of_eratosthenes)
+        sieve = self.build_cfunc(snippet.sieve_of_eratosthenes)
         self.assertEquals(sieve(), 1028)
 
     def test_slice(self):
-        half = self.build_cfunc(t.half_of_n)
+        half = self.build_cfunc(snippet.half_of_n)
         self.assertEquals(half(10), 5)
 
     def test_poly_branch(self):
-        poly_branch = self.build_cfunc(t.poly_branch)
+        poly_branch = self.build_cfunc(snippet.poly_branch)
         self.assertEquals(poly_branch(10), [1,2,3]*2)
         self.assertEquals(poly_branch(0), ['a','b','c']*2)
 
     def test_and(self):
-        sand = self.build_cfunc(t.s_and)
+        sand = self.build_cfunc(snippet.s_and)
         self.assertEquals(sand(5, 6), "yes")
         self.assertEquals(sand(5, 0), "no")
         self.assertEquals(sand(0, 6), "no")
         self.assertEquals(sand(0, 0), "no")
 
+class TypedTestCase(testit.IntTestCase):
+
+    def getcompiled(self, func):
+        t = Translator(func) 
+        t.simplify()
+        # builds starting-types from func_defs 
+        argstypelist = []
+        if func.func_defaults:
+            for spec in func.func_defaults:
+                if isinstance(spec, tuple):
+                    spec = spec[0] # use the first type only for the tests
+                argstypelist.append(spec)
+        t.annotate(argstypelist) 
+        return t.compile()
+
+    def test_set_attr(self):
+        set_attr = self.getcompiled(snippet.set_attr)
+        self.assertEquals(set_attr(), 2)
+
+    def test_inheritance2(self):
+        inheritance2 = self.getcompiled(snippet.inheritance2)
+        self.assertEquals(inheritance2(), ((-12, -12), (3, "world")))
+
+    def test_factorial2(self):
+        factorial2 = self.getcompiled(snippet.factorial2)
+        self.assertEquals(factorial2(5), 120)
+
+    def test_factorial(self):
+        factorial = self.getcompiled(snippet.factorial)
+        self.assertEquals(factorial(5), 120)
+
+    def test_simple_method(self):
+        simple_method = self.getcompiled(snippet.simple_method)
+        self.assertEquals(simple_method(55), 55)
+
+    def test_sieve_of_eratosthenes(self):
+        sieve_of_eratosthenes = self.getcompiled(snippet.sieve_of_eratosthenes)
+        self.assertEquals(sieve_of_eratosthenes(), 1028)
+
+    def test_nested_whiles(self):
+        nested_whiles = self.getcompiled(snippet.nested_whiles)
+        self.assertEquals(nested_whiles(5,3), '!!!!!')
+
+    def xxxtest_call_five(self):
+        call_five = self.getcompiled(snippet.call_five)
+        self.assertEquals(call_five(), [5])
+
 if __name__ == '__main__':
     testit.main()

Modified: pypy/trunk/src/pypy/translator/test/test_translator.py
==============================================================================
--- pypy/trunk/src/pypy/translator/test/test_translator.py	(original)
+++ pypy/trunk/src/pypy/translator/test/test_translator.py	Sat Jun  5 13:04:10 2004
@@ -5,64 +5,6 @@
 from pypy.translator.test import snippet
 
 
-class TranslatorTestCase(testit.IntTestCase):
-
-    def test_set_attr(self):
-        t = Translator(snippet.set_attr)
-        t.simplify()
-        t.annotate([])
-        set_attr = t.compile()
-        self.assertEquals(set_attr(), 2)
-
-    def test_inheritance2(self):
-        t = Translator(snippet.inheritance2)
-        t.simplify()
-        t.annotate([])
-        inheritance2 = t.compile()
-        self.assertEquals(inheritance2(), ((-12, -12), (3, "world")))
-
-    def test_factorial2(self):
-        t = Translator(snippet.factorial2)
-        t.simplify()
-        t.annotate([int])
-        factorial2 = t.compile()
-        self.assertEquals(factorial2(5), 120)
-
-    def test_factorial(self):
-        t = Translator(snippet.factorial)
-        t.simplify()
-        t.annotate([int])
-        factorial = t.compile()
-        self.assertEquals(factorial(5), 120)
-
-    def test_simple_method(self):
-        t = Translator(snippet.simple_method)
-        t.simplify()
-        t.annotate([int]).simplify()
-        simple_method = t.compile()
-        self.assertEquals(simple_method(55), 55)
-
-    def test_sieve_of_eratosthenes(self):
-        t = Translator(snippet.sieve_of_eratosthenes)
-        t.simplify()
-        t.annotate([])#.simplify()
-        #t.view()
-        sieve_of_eratosthenes = t.compile()
-        self.assertEquals(sieve_of_eratosthenes(), 1028)
-
-    def test_nested_whiles(self):
-        t = Translator(snippet.nested_whiles)
-        t.simplify()
-        t.annotate([int,int])
-        nested_whiles = t.compile()
-        self.assertEquals(nested_whiles(5,3), '!!!!!')
-
-    def xxxtest_call_five(self):
-        t = Translator(snippet.call_five)
-        t.simplify()
-        t.annotate([])
-        call_five = t.compile()
-        self.assertEquals(call_five(), [5])
 
 if __name__ == '__main__':
     testit.main()



More information about the Pypy-commit mailing list