[pypy-svn] r27663 - in pypy/dist/pypy/translator/cli: . src test

antocuni at codespeak.net antocuni at codespeak.net
Wed May 24 23:21:35 CEST 2006


Author: antocuni
Date: Wed May 24 23:21:22 2006
New Revision: 27663

Added:
   pypy/dist/pypy/translator/cli/test/test_runtest.py   (contents, props changed)
Modified:
   pypy/dist/pypy/translator/cli/cts.py
   pypy/dist/pypy/translator/cli/src/pypylib.cs
   pypy/dist/pypy/translator/cli/test/runtest.py
Log:
Added the class CliTest: it is intended to be used togheter with
classes in pypy.rpython.test.tool, so that tests written for rpython
can be used for gencli with no effort.



Modified: pypy/dist/pypy/translator/cli/cts.py
==============================================================================
--- pypy/dist/pypy/translator/cli/cts.py	(original)
+++ pypy/dist/pypy/translator/cli/cts.py	Wed May 24 23:21:22 2006
@@ -27,7 +27,8 @@
     SignedLongLong: 'int64',
     UnsignedLongLong: 'unsigned int64',
     ootype.Bool: 'bool',
-    ootype.Float: 'float64',    
+    ootype.Float: 'float64',
+    ootype.Char: 'char',
     ootype.Class: 'class [mscorlib]System.Type',
 
     # maps generic types to their ordinal

Modified: pypy/dist/pypy/translator/cli/src/pypylib.cs
==============================================================================
--- pypy/dist/pypy/translator/cli/src/pypylib.cs	(original)
+++ pypy/dist/pypy/translator/cli/src/pypylib.cs	Wed May 24 23:21:22 2006
@@ -1,6 +1,27 @@
 using System;
 using System.Collections.Generic;
 
+namespace pypy.test
+{
+    public class Result 
+    {
+        public static string ToPython(int x)    { return x.ToString(); }
+        public static string ToPython(bool x)   { return x.ToString(); }
+        public static string ToPython(double x) { return x.ToString(); }
+        public static string ToPython(char x)   { return string.Format("'{0}'", x); }
+        
+        public static string ToPython<T>(pypy.runtime.List<T> lst)
+        {
+            // TODO: use StringBuilder instead
+            string res = "[";
+            foreach(T item in lst)
+                res += item.ToString() + ","; // XXX: only works for int, bool and double
+            res += "]";
+            return res;
+        }
+    }
+}
+
 namespace pypy.runtime
 {
     public class Utils
@@ -9,6 +30,7 @@
         {
             return t.GetConstructor(new Type[0]).Invoke(new object[0]);
         }
+            
     }
 
     //The public interface List must implement is defined in

Modified: pypy/dist/pypy/translator/cli/test/runtest.py
==============================================================================
--- pypy/dist/pypy/translator/cli/test/runtest.py	(original)
+++ pypy/dist/pypy/translator/cli/test/runtest.py	Wed May 24 23:21:22 2006
@@ -5,6 +5,11 @@
 import py
 from pypy.tool.udir import udir
 from pypy.translator.translator import TranslationContext
+from pypy.rpython.test.tool import BaseRtypingTest, OORtypeMixin
+from pypy.rpython.lltypesystem.lltype import typeOf
+from pypy.rpython.ootypesystem import ootype
+from pypy.annotation.model import lltype_to_annotation
+
 from pypy.translator.cli.option import getoption
 from pypy.translator.cli.gencli import GenCli
 from pypy.translator.cli.function import Function
@@ -44,7 +49,7 @@
     def render(self, ilasm):
         ilasm.begin_function('main', [('string[]', 'argv')], 'void', True, 'static')
 
-        # TODO: only int32 and bool are tested
+        # convert string arguments to their true type
         for i, arg in enumerate(self.graph.getargs()):
             ilasm.opcode('ldarg.0')
             ilasm.opcode('ldc.i4.%d' % i)
@@ -55,13 +60,23 @@
 
         ilasm.call(cts.graph_to_signature(self.graph))
 
-        # print the result using the appropriate WriteLine overload
-        ret_type, ret_var = cts.llvar_to_cts(self.graph.getreturnvar())
-        ilasm.call('void class [mscorlib]System.Console::WriteLine(%s)' % ret_type)
+        # convert result to a string containing a valid python expression
+        var = self.graph.getreturnvar()
+        ilasm.call('string class [pypylib]pypy.test.Result::%s' %
+                   self.__output_method(var.concretetype))
+        ilasm.call('void class [mscorlib]System.Console::WriteLine(string)')
         ilasm.opcode('ret')
         ilasm.end_function()
         self.db.pending_function(self.graph)
 
+    def __output_method(self, TYPE):
+        if isinstance(TYPE, ootype.List):
+            item_type = cts.lltype_to_cts(TYPE._ITEMTYPE)
+            return 'ToPython<%s> (class [pypylib]pypy.runtime.List`1<!!0>)' % item_type
+        else:
+            type_ = cts.lltype_to_cts(TYPE)
+            return 'ToPython(%s)' % type_
+
     def __convert_method(self, arg_type):
         _conv = {
             'int32': 'ToInt32',
@@ -69,7 +84,8 @@
             'int64': 'ToInt64',
             'unsigned int64': 'ToUInt64',
             'bool': 'ToBoolean',
-            'float64': 'ToDouble'
+            'float64': 'ToDouble',
+            'char': 'ToChar',
             }
 
         try:
@@ -140,12 +156,24 @@
         retval = mono.wait()
         assert retval == 0, stderr
 
-        ret_type, ret_var = cts.llvar_to_cts(self.graph.getreturnvar())
-        if 'int' in ret_type:
-            return int(stdout)
-        elif ret_type == 'float64':
-            return float(stdout)
-        elif ret_type == 'bool':
-            return stdout.strip().lower() == 'true'
-        else:
-            assert False, 'Return type %s is not supported' % ret_type
+        return eval(stdout)
+##        ret_type, ret_var = cts.llvar_to_cts(self.graph.getreturnvar())
+##        if 'int' in ret_type:
+##            return int(stdout)
+##        elif ret_type == 'float64':
+##            return float(stdout)
+##        elif ret_type == 'bool':
+##            return stdout.strip().lower() == 'true'
+##        else:
+##            assert False, 'Return type %s is not supported' % ret_type
+
+
+class CliTest(BaseRtypingTest, OORtypeMixin):
+    def interpret(self, fn, args):
+        ann = [lltype_to_annotation(typeOf(x)) for x in args]
+        f = compile_function(fn, ann)
+        return f(*args)
+
+    def interpret_raises(exc, func, args):
+        py.test.skip("CLI tests don't support interpret_raises")
+    

Added: pypy/dist/pypy/translator/cli/test/test_runtest.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/cli/test/test_runtest.py	Wed May 24 23:21:22 2006
@@ -0,0 +1,27 @@
+from pypy.translator.cli.test.runtest import CliTest
+from pypy.translator.cli.test.runtest import FLOAT_PRECISION
+
+def ident(x):
+    return x
+
+class TestRunTest(CliTest):
+
+    def test_int(self):
+        assert self.interpret(ident, [42]) == 42
+    
+    def test_bool(self):
+        assert self.interpret(ident, [True]) == True
+        assert self.interpret(ident, [False]) == False
+
+    def test_float(self):
+        x = 10/3.0
+        res = self.interpret(ident, [x])
+        assert round(x, FLOAT_PRECISION) == round(res, FLOAT_PRECISION)
+
+    def test_char(self):
+        assert self.interpret(ident, ['a']) == 'a'
+
+    def test_list(self):
+        def fn():
+            return [1, 2, 3]
+        assert self.interpret(fn, []) == [1, 2, 3]



More information about the Pypy-commit mailing list