[pypy-svn] r50643 - in pypy/branch/applevel-ctypes2/pypy/rlib: . test

arigo at codespeak.net arigo at codespeak.net
Tue Jan 15 17:45:14 CET 2008


Author: arigo
Date: Tue Jan 15 17:45:14 2008
New Revision: 50643

Modified:
   pypy/branch/applevel-ctypes2/pypy/rlib/rbigint.py
   pypy/branch/applevel-ctypes2/pypy/rlib/test/test_rbigint.py
Log:
(fijal, arigo)
Add a method ulonglongmask() on rbigints, to truncate the value
down to a r_ulonglong.


Modified: pypy/branch/applevel-ctypes2/pypy/rlib/rbigint.py
==============================================================================
--- pypy/branch/applevel-ctypes2/pypy/rlib/rbigint.py	(original)
+++ pypy/branch/applevel-ctypes2/pypy/rlib/rbigint.py	Tue Jan 15 17:45:14 2008
@@ -200,6 +200,10 @@
             raise ValueError("cannot convert negative integer to unsigned int")
         return _AsULonglong_ignore_sign(self)
 
+    def ulonglongmask(self):
+        """Return r_ulonglong(self), truncating."""
+        return _AsULonglong_mask(self)
+
     def tofloat(self):
         return _AsDouble(self)
 
@@ -1584,6 +1588,17 @@
         i -= 1
     return x
 
+def _AsULonglong_mask(v):
+    x = r_ulonglong(0)
+    i = len(v.digits) - 1
+    while i >= 0:
+        prev = x
+        x = (x << SHIFT) + v.digits[i]
+        i -= 1
+    if v.sign < 0:
+        x = -x
+    return x
+
 def _hash(v):
     # This is designed so that Python ints and longs with the
     # same value hash to the same value, otherwise comparisons

Modified: pypy/branch/applevel-ctypes2/pypy/rlib/test/test_rbigint.py
==============================================================================
--- pypy/branch/applevel-ctypes2/pypy/rlib/test/test_rbigint.py	(original)
+++ pypy/branch/applevel-ctypes2/pypy/rlib/test/test_rbigint.py	Tue Jan 15 17:45:14 2008
@@ -3,7 +3,7 @@
 from random import random, randint
 from pypy.rlib.rbigint import rbigint, SHIFT, MASK
 from pypy.rlib import rbigint as lobj
-from pypy.rlib.rarithmetic import r_uint, r_longlong
+from pypy.rlib.rarithmetic import r_uint, r_longlong, r_ulonglong
 import operator, sys
 
 class TestRLong(object):
@@ -419,6 +419,16 @@
         py.test.raises(OverflowError, f3.tolonglong)
         py.test.raises(OverflowError, f4.tolonglong)
 
+    def test_ulonglongmask(self):
+        assert rbigint.fromlong(-1).ulonglongmask() == r_ulonglong(-1)
+        assert rbigint.fromlong(0).ulonglongmask() == r_ulonglong(0)
+        assert (rbigint.fromlong(sys.maxint).ulonglongmask() ==
+                r_ulonglong(sys.maxint))
+        assert (rbigint.fromlong(9**50).ulonglongmask() ==
+                r_ulonglong(9**50))
+        assert (rbigint.fromlong(-9**50).ulonglongmask() ==
+                r_ulonglong(-9**50))
+
 
 class TestTranslatable(object):
 



More information about the Pypy-commit mailing list