| 1 |
import pytest |
| 2 |
import sys |
| 3 |
from pypy.objspace.std.smalllongobject import W_SmallLongObject |
| 4 |
from pypy.objspace.std.test import test_longobject |
| 5 |
from pypy.objspace.std.test.test_intobject import AppTestInt, TestW_IntObject |
| 6 |
from rpython.rlib.rarithmetic import r_longlong |
| 7 |
from pypy.interpreter.error import OperationError |
| 8 |
|
| 9 |
|
| 10 |
@pytest.mark.parametrize('spaceconfig', [{"objspace.std.withsmalllong": True}]) |
| 11 |
def test_direct(space): |
| 12 |
w5 = space.wrap(r_longlong(5)) |
| 13 |
assert isinstance(w5, W_SmallLongObject) |
| 14 |
wlarge = space.wrap(r_longlong(0x123456789ABCDEFL)) |
| 15 |
|
| 16 |
assert space.int_w(w5) == 5 |
| 17 |
if sys.maxint < 0x123456789ABCDEFL: |
| 18 |
with pytest.raises(OperationError): |
| 19 |
space.int_w(wlarge) |
| 20 |
else: |
| 21 |
assert space.int_w(wlarge) == 0x123456789ABCDEF |
| 22 |
|
| 23 |
assert space.pos(w5) is w5 |
| 24 |
assert space.abs(w5) is w5 |
| 25 |
wm5 = space.wrap(r_longlong(-5)) |
| 26 |
assert space.int_w(space.abs(wm5)) == 5 |
| 27 |
assert space.int_w(space.neg(w5)) == -5 |
| 28 |
assert space.is_true(w5) is True |
| 29 |
assert space.is_true(wm5) is True |
| 30 |
w0 = space.wrap(r_longlong(0)) |
| 31 |
assert space.is_true(w0) is False |
| 32 |
|
| 33 |
w14000000000000 = space.wrap(r_longlong(0x14000000000000L)) |
| 34 |
assert space.is_true(space.eq( |
| 35 |
space.lshift(w5, space.wrap(49)), w14000000000000)) is False |
| 36 |
assert space.is_true(space.eq( |
| 37 |
space.lshift(w5, space.wrap(50)), w14000000000000)) is True |
| 38 |
|
| 39 |
w_huge = space.sub(space.lshift(w5, space.wrap(150)), space.wrap(1)) |
| 40 |
wx = space.and_(w14000000000000, w_huge) |
| 41 |
assert space.is_true(space.eq(wx, w14000000000000)) |
| 42 |
|
| 43 |
w_obj = W_SmallLongObject.fromint(42) |
| 44 |
assert space.unwrap(w_obj) == 42 |
| 45 |
|
| 46 |
|
| 47 |
@pytest.mark.skipif('config.option.runappdirect') |
| 48 |
class AppTestSmallLong(test_longobject.AppTestLong): |
| 49 |
spaceconfig = {"objspace.std.withsmalllong": True} |
| 50 |
|
| 51 |
def setup_class(cls): |
| 52 |
from pypy.interpreter import gateway |
| 53 |
def w__long(space, w_obj): |
| 54 |
assert space.config.objspace.std.withsmalllong |
| 55 |
b = space.bigint_w(w_obj) |
| 56 |
return space.wraplong(b.tolong()) |
| 57 |
cls.w__long = cls.space.wrap(gateway.interp2app(w__long)) |
| 58 |
|
| 59 |
def test_sl_simple(self): |
| 60 |
import __pypy__ |
| 61 |
s = __pypy__.internal_repr(self._long(5)) |
| 62 |
assert 'SmallLong' in s |
| 63 |
|
| 64 |
def test_sl_hash(self): |
| 65 |
import __pypy__ |
| 66 |
x = self._long(5) |
| 67 |
assert 'SmallLong' in __pypy__.internal_repr(x) |
| 68 |
assert hash(5) == hash(x) |
| 69 |
biglong = self._long(5) |
| 70 |
biglong ^= 2**100 |
| 71 |
biglong ^= 2**100 |
| 72 |
assert biglong == 5 |
| 73 |
assert 'SmallLong' not in __pypy__.internal_repr(biglong) |
| 74 |
assert hash(5) == hash(biglong) |
| 75 |
|
| 76 |
x = self._long(0x123456789ABCDEF) |
| 77 |
assert 'SmallLong' in __pypy__.internal_repr(x) |
| 78 |
biglong = x |
| 79 |
biglong ^= 2**100 |
| 80 |
biglong ^= 2**100 |
| 81 |
assert biglong == x |
| 82 |
assert 'SmallLong' not in __pypy__.internal_repr(biglong) |
| 83 |
assert hash(biglong) == hash(x) |
| 84 |
|
| 85 |
def test_sl_int(self): |
| 86 |
x = self._long(0x123456789ABCDEF) |
| 87 |
two = 2 |
| 88 |
assert int(x) == x |
| 89 |
assert type(int(x)) == type(0x1234567 ** two) |
| 90 |
y = x >> 32 |
| 91 |
assert int(y) == y |
| 92 |
assert type(int(y)) is int |
| 93 |
|
| 94 |
def test_sl_long(self): |
| 95 |
import __pypy__ |
| 96 |
x = self._long(0) |
| 97 |
assert 'SmallLong' in __pypy__.internal_repr(x) |
| 98 |
|
| 99 |
def test_sl_add(self): |
| 100 |
import __pypy__ |
| 101 |
x = self._long(0x123456789ABCDEF) |
| 102 |
assert x + x == 0x2468ACF13579BDE |
| 103 |
assert 'SmallLong' in __pypy__.internal_repr(x + x) |
| 104 |
x = self._long(-0x123456789ABCDEF) |
| 105 |
assert x + x == -0x2468ACF13579BDE |
| 106 |
assert 'SmallLong' in __pypy__.internal_repr(x + x) |
| 107 |
x = self._long(0x723456789ABCDEF0) |
| 108 |
assert x + x == 0xE468ACF13579BDE0 |
| 109 |
assert 'SmallLong' not in __pypy__.internal_repr(x + x) |
| 110 |
x = self._long(-0x723456789ABCDEF0) |
| 111 |
assert x + x == -0xE468ACF13579BDE0 |
| 112 |
assert 'SmallLong' not in __pypy__.internal_repr(x + x) |
| 113 |
|
| 114 |
def test_sl_add_32(self): |
| 115 |
import sys, __pypy__ |
| 116 |
if sys.maxsize == 2147483647: |
| 117 |
x = 2147483647 |
| 118 |
assert x + x == 4294967294 |
| 119 |
assert 'SmallLong' in __pypy__.internal_repr(x + x) |
| 120 |
y = -1 |
| 121 |
assert x - y == 2147483648 |
| 122 |
assert 'SmallLong' in __pypy__.internal_repr(x - y) |
| 123 |
|
| 124 |
def test_sl_lshift(self): |
| 125 |
for x in [1, self._long(1)]: |
| 126 |
x = 1 |
| 127 |
assert x << 1 == 2 |
| 128 |
assert x << 30 == 1073741824 |
| 129 |
assert x << 31 == 2147483648 |
| 130 |
assert x << 32 == 4294967296 |
| 131 |
assert x << 62 == 4611686018427387904 |
| 132 |
assert x << 63 == 9223372036854775808 |
| 133 |
assert x << 64 == 18446744073709551616 |
| 134 |
assert (x << 31) << 31 == 4611686018427387904 |
| 135 |
assert (x << 32) << 32 == 18446744073709551616 |
| 136 |
|
| 137 |
|
| 138 |
class TestW_IntObjectWithSmallLong(TestW_IntObject): |
| 139 |
spaceconfig = {"objspace.std.withsmalllong": True} |
| 140 |
|
| 141 |
def test_mul_int_long_no_conversion(self, monkeypatch): |
| 142 |
pass |
| 143 |
|
| 144 |
class AppTestIntWithSmallLong(AppTestInt): |
| 145 |
spaceconfig = {"objspace.std.withsmalllong": True} |