3
#from pypy.objspace.std.model import WITHSMALLINT
5
# py.test.skip("WITHSMALLINT is not enabled")
7
from pypy.objspace.std.inttype import wrapint
8
from pypy.objspace.std.objspace import FailedToImplement
9
from pypy.rlib.rarithmetic import r_uint
11
from pypy.conftest import gettestobjspace
13
class TestW_IntObject:
16
cls.space = gettestobjspace(**{"objspace.std.withsmallint": True})
19
assert self.space.int_w(self.space.wrap(42)) == 42
21
def test_uint_w(self):
23
assert space.uint_w(space.wrap(42)) == 42
24
assert isinstance(space.uint_w(space.wrap(42)), r_uint)
25
space.raises_w(space.w_ValueError, space.uint_w, space.wrap(-1))
29
f1 = wrapint(self.space, x)
30
result = self.space.repr(f1)
31
assert self.space.unwrap(result) == repr(x)
35
f1 = wrapint(self.space, x)
36
result = self.space.str(f1)
37
assert self.space.unwrap(result) == str(x)
41
f1 = wrapint(self.space, x)
42
result = self.space.hash(f1)
43
assert result.intval == hash(x)
45
def test_compare(self):
47
optab = ['lt', 'le', 'eq', 'ne', 'gt', 'ge']
48
for x in (-10, -1, 0, 1, 2, 1000, sys.maxint):
49
for y in (-sys.maxint-1, -11, -9, -2, 0, 1, 3, 1111, sys.maxint):
51
wx = wrapint(self.space, x)
52
wy = wrapint(self.space, y)
53
res = getattr(operator, op)(x, y)
54
method = getattr(self.space, op)
55
myres = method(wx, wy)
56
assert self.space.unwrap(myres) == res
59
for x in [1, 100, sys.maxint // 2 - 50,
60
sys.maxint // 2, sys.maxint - 1000, sys.maxint]:
61
for y in [1, 100, sys.maxint // 2 - 50,
62
sys.maxint // 2, sys.maxint - 1000, sys.maxint]:
63
f1 = wrapint(self.space, x)
64
f2 = wrapint(self.space, y)
65
result = self.space.unwrap(self.space.add(f1, f2))
66
assert result == x+y and type(result) == type(x+y)
69
for x in [1, 100, sys.maxint // 2 - 50,
70
sys.maxint // 2, sys.maxint - 1000, sys.maxint]:
71
for y in [1, 100, sys.maxint // 2 - 50,
72
sys.maxint // 2, sys.maxint - 1000, sys.maxint]:
73
f1 = wrapint(self.space, x)
74
f2 = wrapint(self.space, y)
75
result = self.space.unwrap(self.space.sub(f1, f2))
76
assert result == x-y and type(result) == type(x-y)
79
for x in [0, 1, 100, sys.maxint // 2 - 50, sys.maxint - 1000]:
80
for y in [0, 1, 100, sys.maxint // 2 - 50, sys.maxint - 1000]:
81
f1 = wrapint(self.space, x)
82
f2 = wrapint(self.space, y)
83
result = self.space.unwrap(self.space.mul(f1, f2))
84
assert result == x*y and type(result) == type(x*y)
89
f1 = wrapint(self.space, i)
90
f2 = wrapint(self.space, 3)
91
result = self.space.div(f1, f2)
92
assert result.intval == res
97
f1 = wrapint(self.space, x)
98
f2 = wrapint(self.space, y)
99
v = self.space.mod(f1, f2)
100
assert v.intval == x % y
101
# not that mod cannot overflow
103
def test_divmod(self):
106
f1 = wrapint(self.space, x)
107
f2 = wrapint(self.space, y)
108
ret = self.space.divmod(f1, f2)
109
v, w = self.space.unwrap(ret)
110
assert (v, w) == divmod(x, y)
112
def test_pow_iii(self):
116
f1 = wrapint(self.space, x)
117
f2 = wrapint(self.space, y)
118
f3 = wrapint(self.space, z)
119
v = self.space.pow(f1, f2, f3)
120
assert v.intval == pow(x, y, z)
121
f1, f2, f3 = [wrapint(self.space, i) for i in (10, -1, 42)]
122
self.space.raises_w(self.space.w_TypeError,
125
f1, f2, f3 = [wrapint(self.space, i) for i in (10, 5, 0)]
126
self.space.raises_w(self.space.w_ValueError,
130
def test_pow_iin(self):
133
f1 = wrapint(self.space, x)
134
f2 = wrapint(self.space, y)
135
v = self.space.pow(f1, f2, self.space.w_None)
136
assert v.intval == x ** y
140
f1 = wrapint(self.space, x)
141
v = self.space.neg(f1)
142
assert v.intval == -x
146
f1 = wrapint(self.space, x)
147
v = self.space.pos(f1)
148
assert v.intval == +x
150
f1 = wrapint(self.space, x)
151
v = self.space.pos(f1)
152
assert v.intval == +x
156
f1 = wrapint(self.space, x)
157
v = self.space.abs(f1)
158
assert v.intval == abs(x)
160
f1 = wrapint(self.space, x)
161
v = self.space.abs(f1)
162
assert v.intval == abs(x)
164
def test_invert(self):
166
f1 = wrapint(self.space, x)
167
v = self.space.invert(f1)
168
assert v.intval == ~x
170
def test_lshift(self):
173
f1 = wrapint(self.space, x)
174
f2 = wrapint(self.space, y)
175
v = self.space.lshift(f1, f2)
176
assert v.intval == x << y
178
def test_rshift(self):
181
f1 = wrapint(self.space, x)
182
f2 = wrapint(self.space, y)
183
v = self.space.rshift(f1, f2)
184
assert v.intval == x >> y
189
f1 = wrapint(self.space, x)
190
f2 = wrapint(self.space, y)
191
v = self.space.and_(f1, f2)
192
assert v.intval == x & y
197
f1 = wrapint(self.space, x)
198
f2 = wrapint(self.space, y)
199
v = self.space.xor(f1, f2)
200
assert v.intval == x ^ y
205
f1 = wrapint(self.space, x)
206
f2 = wrapint(self.space, y)
207
v = self.space.or_(f1, f2)
208
assert v.intval == x | y
211
f1 = wrapint(self.space, 1)
212
result = self.space.int(f1)
217
f1 = wrapint(self.space, x)
218
result = self.space.oct(f1)
219
assert self.space.unwrap(result) == oct(x)
223
f1 = wrapint(self.space, x)
224
result = self.space.hex(f1)
225
assert self.space.unwrap(result) == hex(x)