[pypy-svn] r10616 - in pypy/dist/pypy/translator/genc: . test

arigo at codespeak.net arigo at codespeak.net
Thu Apr 14 15:09:09 CEST 2005


Author: arigo
Date: Thu Apr 14 15:09:09 2005
New Revision: 10616

Added:
   pypy/dist/pypy/translator/genc/test/test_operation.py   (contents, props changed)
Modified:
   pypy/dist/pypy/translator/genc/pyobj_include.h
Log:
Added a test file covering most operations.  Completed and fixed the
operations in pyobj_include.h according to the failures.



Modified: pypy/dist/pypy/translator/genc/pyobj_include.h
==============================================================================
--- pypy/dist/pypy/translator/genc/pyobj_include.h	(original)
+++ pypy/dist/pypy/translator/genc/pyobj_include.h	Thu Apr 14 15:09:09 2005
@@ -24,6 +24,7 @@
 #define OP_IS_(x,y,r,err) op_bool(r,err,(x == y))
 
 #define OP_IS_TRUE(x,r,err) op_bool(r,err,PyObject_IsTrue(x))
+#define OP_NONZERO(x,r,err) op_bool(r,err,PyObject_IsTrue(x))
 
 #define OP_LEN(x,r,err) { \
 		int _retval = PyObject_Size(x); \
@@ -44,6 +45,7 @@
 #define OP_FLOORDIV(x,y,r,err)    if (!(r=PyNumber_FloorDivide(x,y)))FAIL(err)
 #define OP_DIV(x,y,r,err)         if (!(r=PyNumber_Divide(x,y)))     FAIL(err)
 #define OP_MOD(x,y,r,err)         if (!(r=PyNumber_Remainder(x,y)))  FAIL(err)
+#define OP_DIVMOD(x,y,r,err)      if (!(r=PyNumber_Divmod(x,y)))     FAIL(err)
 #define OP_POW(x,y,z,r,err)       if (!(r=PyNumber_Power(x,y,z)))    FAIL(err)
 #define OP_LSHIFT(x,y,r,err)      if (!(r=PyNumber_Lshift(x,y)))     FAIL(err)
 #define OP_RSHIFT(x,y,r,err)      if (!(r=PyNumber_Rshift(x,y)))     FAIL(err)
@@ -121,6 +123,7 @@
 	}
 
 #define OP_STR(x,r,err)           if (!(r=PyObject_Str(x)))          FAIL(err)
+#define OP_REPR(x,r,err)          if (!(r=PyObject_Repr(x)))         FAIL(err)
 #define OP_ORD(s,r,err) { \
 	char *__c = PyString_AsString(s); \
 	int __len; \
@@ -134,6 +137,51 @@
 	if (!(r = PyInt_FromLong((unsigned char)(__c[0])))) \
 	    FAIL(err) \
     }
+#define OP_ID(x,r,err)    if (!(r=PyLong_FromVoidPtr(x))) FAIL(err)
+#define OP_HASH(x,r,err)  { \
+	long __hash = PyObject_Hash(x); \
+	if (__hash == -1 && PyErr_Occurred()) FAIL(err) \
+	if (!(r = PyInt_FromLong(__hash))) FAIL(err) \
+    }
+
+#define OP_HEX(x,r,err)   { \
+	PyNumberMethods *__nb; \
+	if ((__nb = x->ob_type->tp_as_number) == NULL || \
+	    __nb->nb_hex == NULL) { \
+		PyErr_SetString(PyExc_TypeError, \
+			   "hex() argument can't be converted to hex"); \
+		FAIL(err) \
+	} \
+	if (!(r = (*__nb->nb_hex)(x))) FAIL(err) \
+    }
+#define OP_OCT(x,r,err)   { \
+	PyNumberMethods *__nb; \
+	if ((__nb = x->ob_type->tp_as_number) == NULL || \
+	    __nb->nb_oct == NULL) { \
+		PyErr_SetString(PyExc_TypeError, \
+			   "oct() argument can't be converted to oct"); \
+		FAIL(err) \
+	} \
+	if (!(r = (*__nb->nb_oct)(x))) FAIL(err) \
+    }
+
+#define OP_INT(x,r,err)   { \
+	long __val = PyInt_AsLong(x); \
+	if (__val == -1 && PyErr_Occurred()) FAIL(err) \
+	if (!(r = PyInt_FromLong(__val))) FAIL (err) \
+    }
+#define OP_FLOAT(x,r,err)   { \
+	double __val = PyFloat_AsDouble(x); \
+	if (PyErr_Occurred()) FAIL(err) \
+	if (!(r = PyFloat_FromDouble(__val))) FAIL (err) \
+    }
+
+#define OP_CMP(x,y,r,err)   { \
+	int __val = PyObject_Compare(x, y); \
+	if (PyErr_Occurred()) FAIL(err) \
+	if (!(r = PyInt_FromLong(__val))) FAIL (err) \
+    }
+
 
 #define OP_SIMPLE_CALL(args,r,err) if (!(r=PyObject_CallFunctionObjArgs args)) \
 					FAIL(err)
@@ -152,7 +200,7 @@
 
 /* Needs to act like instance(x,y) */
 #define OP_ISSUBTYPE(x,y,r,err)  \
-		op_bool(r,err,PyClass_IsSubclass(x, y))
+		op_bool(r,err,PyObject_IsSubclass(x, y))
 
 
 /*** operations with a variable number of arguments ***/

Added: pypy/dist/pypy/translator/genc/test/test_operation.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/genc/test/test_operation.py	Thu Apr 14 15:09:09 2005
@@ -0,0 +1,131 @@
+import autopath
+from pypy.objspace.flow.model import *
+from pypy.objspace.flow.operation import FunctionByName
+from pypy.translator.tool.buildpyxmodule import skip_missing_compiler
+from pypy.translator.translator import Translator
+
+# XXX this tries to make compiling faster for full-scale testing
+from pypy.translator.tool import buildpyxmodule
+buildpyxmodule.enable_fast_compilation()
+
+
+TESTCASES = [
+    ('is_',             [], []),
+    ('id',              False),
+    ('id',              True),
+    ('type',            42),
+    ('issubtype',       bool, int),
+    ('issubtype',       int, int),
+    ('issubtype',       int, bool),
+    ('repr',            'hi'),
+    ('repr',            42),
+    ('str',             'hi'),
+    ('str',             42),
+    ('len',             [1,3,5,7]),
+    ('len',             'hello world'),
+    ('hash',            'hello world'),
+    ('pos',             42),
+    ('neg',             42),
+    ('nonzero',         42),
+    ('abs' ,            -42),
+    ('hex',             42),
+    ('oct',             42),
+    ('ord',             '*'),
+    ('invert',          42),
+    ('add',             40, 2),
+    ('add',             'hello ', 'world'),
+    ('add',             [1,3,5], [2,4]),
+    ('sub',             40, 2),
+    ('mul',             6, 7),
+    ('mul',             [5], 4),
+    ('mul',             5, [4]),
+    ('truediv',         7, 2),
+    ('floordiv',        7, 2),
+    ('div',             7, 2),
+    ('mod',             7, 2),
+    ('divmod',          7, 2),
+    ('pow',             5, 2, 7),
+    ('lshift',          21, 1),
+    ('rshift',          21, 1),
+    ('and_',            21, 7),
+    ('or_',             21, 7),
+    ('xor',             21, 7),
+    ('int',             42.5),
+    ('float',           42),
+    ('inplace_add',     'hello ', 'world'),
+    ('inplace_sub',     32, 49),
+    ('inplace_mul',     41, 12),
+    ('inplace_truediv', 965, 22),
+    ('inplace_floordiv',847, 31),
+    ('inplace_div',     984, 12),
+    ('inplace_mod',     148, 20),
+    ('inplace_pow',     10, 6),
+    ('inplace_lshift',  9148, 3),
+    ('inplace_rshift',  1029, 2),
+    ('inplace_and',     18711, 98172),
+    ('inplace_or',      8722, 19837),
+    ('inplace_xor',     91487, 18320),
+    ('lt',              5, 7),
+    ('lt',              5, 5),
+    ('lt',              'hello', 'world'),
+    ('le',              5, 7),
+    ('le',              5, 5),
+    ('le',              'hello', 'world'),
+    ('eq',              5, 7),
+    ('eq',              5, 5),
+    ('eq',              'hello', 'world'),
+    ('ne',              5, 7),
+    ('ne',              5, 5),
+    ('ne',              'hello', 'world'),
+    ('gt',              5, 7),
+    ('gt',              5, 5),
+    ('gt',              'hello', 'world'),
+    ('ge',              5, 7),
+    ('ge',              5, 5),
+    ('ge',              'hello', 'world'),
+    ('cmp',             5, 7),
+    ('cmp',             5, 5),
+    ('cmp',             'hello', 'world'),
+    ('contains',        [1,3,5,7], 4),
+    ('contains',        [1,3,5,7], 5),
+    ]
+
+def operationtestfn():
+    pass
+
+
+class TestOperations:
+    objspacename = 'flow'
+
+    def build_cfunc(self, graph):
+        t = Translator()
+        t.entrypoint = operationtestfn
+        t.functions.append(operationtestfn)
+        t.flowgraphs[operationtestfn] = graph
+        return skip_missing_compiler(t.ccompile)
+
+    def test_operations(self):
+        expected = []
+        resvars = []
+        block = Block([])
+        for testcase in TESTCASES:
+            opname = testcase[0]
+            args = testcase[1:]
+            op = SpaceOperation(opname, [Constant(x) for x in args], Variable())
+            block.operations.append(op)
+            expected.append(FunctionByName[opname](*args))
+            resvars.append(op.result)
+        op = SpaceOperation('newtuple', resvars, Variable())
+        block.operations.append(op)
+        graph = FunctionGraph('operationtestfn', block)
+        block.closeblock(Link([op.result], graph.returnblock))
+
+        fn = self.build_cfunc(graph)
+        results = fn()
+
+        assert len(results) == len(TESTCASES)
+        for testcase, expected, result in zip(TESTCASES, expected, results):
+            assert (type(expected) == type(result) and expected == result), (
+                "%s(%s) computed %r instead of %r" % (
+                testcase[0], ', '.join([repr(x) for x in testcase[1:]]),
+                result, expected))



More information about the Pypy-commit mailing list