Author: Armin Rigo <ar...@tunes.org>
Branch: int-float-list-strategy
Changeset: r78388:2e22e8a3d2d4
Date: 2015-07-01 22:15 +0200
http://bitbucket.org/pypy/pypy/changeset/2e22e8a3d2d4/

Log:    Start writing the strategy

diff --git a/pypy/objspace/std/listobject.py b/pypy/objspace/std/listobject.py
--- a/pypy/objspace/std/listobject.py
+++ b/pypy/objspace/std/listobject.py
@@ -1716,6 +1716,41 @@
                     return i
         raise ValueError
 
+class IntOrFloatListStrategy(ListStrategy):
+    import_from_mixin(AbstractUnwrappedStrategy)
+
+    _none_value = longlong2float.float2longlong(0.0)
+
+    def wrap(self, llval):
+        if longlong2float.is_int32_from_longlong_nan(llval):
+            intval = longlong2float.decode_int32_from_longlong_nan(llval)
+            return self.space.wrap(intval)
+        else:
+            floatval = longlong2float.longlong2float(llval)
+            return self.space.wrap(floatval)
+
+    def unwrap(self, w_int_or_float):
+        if type(w_int_or_float) is W_IntObject:
+            intval = self.space.int_w(w_int_or_float)
+            return longlong2float.encode_int32_into_longlong_nan(intval)
+        else:
+            floatval = self.space.float_w(w_int_or_float)
+            return longlong2float.float2longlong(floatval)
+
+    erase, unerase = rerased.new_erasing_pair("longlong")
+    erase = staticmethod(erase)
+    unerase = staticmethod(unerase)
+
+    def is_correct_type(self, w_obj):
+        if type(w_obj) is W_IntObject:
+            intval = self.space.int_w(w_obj)
+            return longlong2float.can_encode_int32(intval)
+        elif type(w_obj) is W_FloatObject:
+            floatval = self.space.float_w(w_obj)
+            return longlong2float.can_encode_float(floatval)
+        else:
+            return False
+
 
 class BytesListStrategy(ListStrategy):
     import_from_mixin(AbstractUnwrappedStrategy)
diff --git a/pypy/objspace/std/test/test_liststrategies.py 
b/pypy/objspace/std/test/test_liststrategies.py
--- a/pypy/objspace/std/test/test_liststrategies.py
+++ b/pypy/objspace/std/test/test_liststrategies.py
@@ -3,7 +3,8 @@
 from pypy.objspace.std.listobject import (
     W_ListObject, EmptyListStrategy, ObjectListStrategy, IntegerListStrategy,
     FloatListStrategy, BytesListStrategy, RangeListStrategy,
-    SimpleRangeListStrategy, make_range_list, UnicodeListStrategy)
+    SimpleRangeListStrategy, make_range_list, UnicodeListStrategy,
+    IntOrFloatListStrategy)
 from pypy.objspace.std import listobject
 from pypy.objspace.std.test.test_listobject import TestW_ListObject
 
@@ -759,9 +760,23 @@
         assert isinstance(w_l.strategy, IntOrFloatListStrategy)
 
     def test_int_or_float(self):
+        from rpython.rlib.rfloat import INFINITY, NAN
         space = self.space
+        w = space.wrap
         w_l = W_ListObject(space, [space.wrap(1), space.wrap(2.3)])
-        xxxxx
+        assert isinstance(w_l.strategy, IntOrFloatListStrategy)
+        w_l.append(w(int(2**31-1)))
+        assert isinstance(w_l.strategy, IntOrFloatListStrategy)
+        w_l.append(w(-5.1))
+        assert isinstance(w_l.strategy, IntOrFloatListStrategy)
+        w_l.append(w(INFINITY))
+        assert isinstance(w_l.strategy, IntOrFloatListStrategy)
+        w_l.append(w(NAN))
+        assert isinstance(w_l.strategy, IntOrFloatListStrategy)
+        w_l.append(w(-NAN))
+        assert isinstance(w_l.strategy, IntOrFloatListStrategy)
+        w_l.append(space.newlist([]))
+        assert isinstance(w_l.strategy, ObjectListStrategy)
 
 
 class TestW_ListStrategiesDisabled:
diff --git a/rpython/rlib/test/test_longlong2float.py 
b/rpython/rlib/test/test_longlong2float.py
--- a/rpython/rlib/test/test_longlong2float.py
+++ b/rpython/rlib/test/test_longlong2float.py
@@ -1,7 +1,7 @@
 from rpython.translator.c.test.test_genc import compile
 from rpython.rlib.longlong2float import longlong2float, float2longlong
 from rpython.rlib.longlong2float import uint2singlefloat, singlefloat2uint
-from rpython.rlib.rarithmetic import r_singlefloat
+from rpython.rlib.rarithmetic import r_singlefloat, r_longlong
 from rpython.rtyper.test.test_llinterp import interpret
 
 
@@ -21,6 +21,9 @@
     yield -inf
     yield inf / inf     # nan
 
+def test_float2longlong():
+    assert float2longlong(0.0) == r_longlong(0)
+
 def test_longlong_as_float():
     for x in enum_floats():
         res = fn(x)
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to