2
from pypy.objspace.std import intobject as iobj
3
from pypy.objspace.std.objspace import FailedToImplement
4
from pypy.rlib.rarithmetic import r_uint
5
from pypy.rlib.rbigint import rbigint
10
def _longshiftresult(self, x):
11
""" calculate an overflowing shift """
17
if type(ires) is long or lres != ires:
21
def _unwrap_nonimpl(self, func, *args, **kwds):
22
""" make sure that the expected exception occours, and unwrap it """
24
res = func(*args, **kwds)
25
raise Exception, "should have failed but returned '%s'!" %repr(res)
26
except FailedToImplement, arg:
30
assert self.space.int_w(self.space.wrap(42)) == 42
32
def test_uint_w(self):
34
assert space.uint_w(space.wrap(42)) == 42
35
assert isinstance(space.uint_w(space.wrap(42)), r_uint)
36
space.raises_w(space.w_ValueError, space.uint_w, space.wrap(-1))
38
def test_bigint_w(self):
40
assert isinstance(space.bigint_w(space.wrap(42)), rbigint)
41
assert space.bigint_w(space.wrap(42)).eq(rbigint.fromint(42))
45
f1 = iobj.W_IntObject(x)
46
result = iobj.repr__Int(self.space, f1)
47
assert self.space.unwrap(result) == repr(x)
51
f1 = iobj.W_IntObject(x)
52
result = iobj.str__Int(self.space, f1)
53
assert self.space.unwrap(result) == str(x)
57
f1 = iobj.W_IntObject(x)
58
result = iobj.hash__Int(self.space, f1)
59
assert result.intval == hash(x)
61
def test_compare(self):
63
optab = ['lt', 'le', 'eq', 'ne', 'gt', 'ge']
64
for x in (-10, -1, 0, 1, 2, 1000, sys.maxint):
65
for y in (-sys.maxint-1, -11, -9, -2, 0, 1, 3, 1111, sys.maxint):
67
wx = iobj.W_IntObject(x)
68
wy = iobj.W_IntObject(y)
69
res = getattr(operator, op)(x, y)
70
method = getattr(iobj, '%s__Int_Int' % op)
71
myres = method(self.space, wx, wy)
72
assert self.space.unwrap(myres) == res
77
f1 = iobj.W_IntObject(x)
78
f2 = iobj.W_IntObject(y)
79
result = iobj.add__Int_Int(self.space, f1, f2)
80
assert result.intval == x+y
83
f1 = iobj.W_IntObject(x)
84
f2 = iobj.W_IntObject(y)
85
assert self.space.w_OverflowError == (
86
self._unwrap_nonimpl(iobj.add__Int_Int, self.space, f1, f2))
91
f1 = iobj.W_IntObject(x)
92
f2 = iobj.W_IntObject(y)
93
result = iobj.sub__Int_Int(self.space, f1, f2)
94
assert result.intval == x-y
97
f1 = iobj.W_IntObject(x)
98
f2 = iobj.W_IntObject(y)
99
assert self.space.w_OverflowError == (
100
self._unwrap_nonimpl(iobj.sub__Int_Int, self.space, f1, f2))
105
f1 = iobj.W_IntObject(x)
106
f2 = iobj.W_IntObject(y)
107
result = iobj.mul__Int_Int(self.space, f1, f2)
108
assert result.intval == x*y
111
f1 = iobj.W_IntObject(x)
112
f2 = iobj.W_IntObject(y)
113
assert self.space.w_OverflowError == (
114
self._unwrap_nonimpl(iobj.mul__Int_Int, self.space, f1, f2))
119
f1 = iobj.W_IntObject(i)
120
f2 = iobj.W_IntObject(3)
121
result = iobj.div__Int_Int(self.space, f1, f2)
122
assert result.intval == res
125
f1 = iobj.W_IntObject(x)
126
f2 = iobj.W_IntObject(y)
127
assert self.space.w_OverflowError == (
128
self._unwrap_nonimpl(iobj.div__Int_Int, self.space, f1, f2))
133
f1 = iobj.W_IntObject(x)
134
f2 = iobj.W_IntObject(y)
135
v = iobj.mod__Int_Int(self.space, f1, f2)
136
assert v.intval == x % y
137
# not that mod cannot overflow
139
def test_divmod(self):
142
f1 = iobj.W_IntObject(x)
143
f2 = iobj.W_IntObject(y)
144
ret = iobj.divmod__Int_Int(self.space, f1, f2)
145
v, w = self.space.unwrap(ret)
146
assert (v, w) == divmod(x, y)
149
f1 = iobj.W_IntObject(x)
150
f2 = iobj.W_IntObject(y)
151
assert self.space.w_OverflowError == (
152
self._unwrap_nonimpl(iobj.divmod__Int_Int, self.space, f1, f2))
154
def test_pow_iii(self):
158
f1 = iobj.W_IntObject(x)
159
f2 = iobj.W_IntObject(y)
160
f3 = iobj.W_IntObject(z)
161
v = iobj.pow__Int_Int_Int(self.space, f1, f2, f3)
162
assert v.intval == pow(x, y, z)
163
f1, f2, f3 = [iobj.W_IntObject(i) for i in (10, -1, 42)]
164
self.space.raises_w(self.space.w_TypeError,
165
iobj.pow__Int_Int_Int,
166
self.space, f1, f2, f3)
167
f1, f2, f3 = [iobj.W_IntObject(i) for i in (10, 5, 0)]
168
self.space.raises_w(self.space.w_ValueError,
169
iobj.pow__Int_Int_Int,
170
self.space, f1, f2, f3)
172
def test_pow_iin(self):
175
f1 = iobj.W_IntObject(x)
176
f2 = iobj.W_IntObject(y)
177
v = iobj.pow__Int_Int_None(self.space, f1, f2, self.space.w_None)
178
assert v.intval == x ** y
179
f1, f2 = [iobj.W_IntObject(i) for i in (10, 20)]
180
assert self.space.w_OverflowError == (
181
self._unwrap_nonimpl(iobj.pow__Int_Int_None, self.space, f1, f2, self.space.w_None))
185
f1 = iobj.W_IntObject(x)
186
v = iobj.neg__Int(self.space, f1)
187
assert v.intval == -x
189
f1 = iobj.W_IntObject(x)
190
assert self.space.w_OverflowError == (
191
self._unwrap_nonimpl(iobj.neg__Int, self.space, f1))
195
f1 = iobj.W_IntObject(x)
196
v = iobj.pos__Int(self.space, f1)
197
assert v.intval == +x
199
f1 = iobj.W_IntObject(x)
200
v = iobj.pos__Int(self.space, f1)
201
assert v.intval == +x
205
f1 = iobj.W_IntObject(x)
206
v = iobj.abs__Int(self.space, f1)
207
assert v.intval == abs(x)
209
f1 = iobj.W_IntObject(x)
210
v = iobj.abs__Int(self.space, f1)
211
assert v.intval == abs(x)
213
f1 = iobj.W_IntObject(x)
214
assert self.space.w_OverflowError == (
215
self._unwrap_nonimpl(iobj.abs__Int, self.space, f1))
217
def test_invert(self):
219
f1 = iobj.W_IntObject(x)
220
v = iobj.invert__Int(self.space, f1)
221
assert v.intval == ~x
223
def test_lshift(self):
226
f1 = iobj.W_IntObject(x)
227
f2 = iobj.W_IntObject(y)
228
v = iobj.lshift__Int_Int(self.space, f1, f2)
229
assert v.intval == x << y
230
y = self._longshiftresult(x)
231
f1 = iobj.W_IntObject(x)
232
f2 = iobj.W_IntObject(y)
233
assert self.space.w_OverflowError == (
234
self._unwrap_nonimpl(iobj.lshift__Int_Int, self.space, f1, f2))
236
def test_rshift(self):
239
f1 = iobj.W_IntObject(x)
240
f2 = iobj.W_IntObject(y)
241
v = iobj.rshift__Int_Int(self.space, f1, f2)
242
assert v.intval == x >> y
247
f1 = iobj.W_IntObject(x)
248
f2 = iobj.W_IntObject(y)
249
v = iobj.and__Int_Int(self.space, f1, f2)
250
assert v.intval == x & y
255
f1 = iobj.W_IntObject(x)
256
f2 = iobj.W_IntObject(y)
257
v = iobj.xor__Int_Int(self.space, f1, f2)
258
assert v.intval == x ^ y
263
f1 = iobj.W_IntObject(x)
264
f2 = iobj.W_IntObject(y)
265
v = iobj.or__Int_Int(self.space, f1, f2)
266
assert v.intval == x | y
269
f1 = iobj.W_IntObject(1)
270
result = iobj.int__Int(self.space, f1)
273
## def test_long(self):
275
## f1 = iobj.W_IntObject(x)
276
## result = iobj.int_long(self.space, f1)
277
## self.assertEquals(self.space.unwrap(result), long(x))
279
## def test_float(self):
281
## f1 = iobj.W_IntObject(x)
282
## result = iobj.int_float(self.space, f1)
283
## self.assertEquals(self.space.unwrap(result), float(x))
287
f1 = iobj.W_IntObject(x)
288
result = iobj.oct__Int(self.space, f1)
289
assert self.space.unwrap(result) == oct(x)
293
f1 = iobj.W_IntObject(x)
294
result = iobj.hex__Int(self.space, f1)
295
assert self.space.unwrap(result) == hex(x)
299
def test_int_callable(self):
302
def test_int_string(self):
303
assert 42 == int("42")
304
assert 10000000000 == long("10000000000")
306
def test_int_unicode(self):
307
assert 42 == int(unicode('42'))
309
def test_int_float(self):
312
def test_int_str_repr(self):
313
assert "42" == str(42)
314
assert "42" == repr(42)
315
raises(ValueError, int, '0x2A')
317
def test_int_two_param(self):
318
assert 42 == int('0x2A', 0)
319
assert 42 == int('2A', 16)
320
assert 42 == int('42', 10)
321
raises(TypeError, int, 1, 10)
322
raises(TypeError, int, '5', '9')
324
def test_shift_zeros(self):
328
def test_overflow(self):
331
assert isinstance(n, long)
334
assert pow(2, -10) == 1/1024.
336
def test_int_w_long_arg(self):
337
assert int(10000000000) == 10000000000L
338
assert int("10000000000") == 10000000000l
339
raises(ValueError, int, "10000000000JUNK")
340
raises(ValueError, int, "10000000000JUNK", 10)
342
def test_int_subclass_ctr(self):
347
assert isinstance(j(100),j)
348
assert j(100L) == 100
349
assert j("100") == 100
350
assert j("100",2) == 4
351
assert isinstance(j("100",2),j)
352
raises(OverflowError,j,sys.maxint+1)
353
raises(OverflowError,j,str(sys.maxint+1))
355
def test_int_subclass_ops(self):
358
def __add__(self, other):
360
def __iadd__(self, other):
362
def __sub__(self, other):
364
def __isub__(self, other):
366
def __mul__(self, other):
368
def __imul__(self, other):
370
def __lshift__(self, other):
372
def __ilshift__(self, other):
374
assert j(100) + 5 == "add."
375
assert j(100) + str == "add."
376
assert j(100) - 5 == "sub."
377
assert j(100) - str == "sub."
378
assert j(100) * 5 == "mul."
379
assert j(100) * str == "mul."
380
assert j(100) << 5 == "lshift."
381
assert j(100) << str == "lshift."
382
assert (5 + j(100), type(5 + j(100))) == ( 105, int)
383
assert (5 - j(100), type(5 - j(100))) == ( -95, int)
384
assert (5 * j(100), type(5 * j(100))) == ( 500, int)
385
assert (5 << j(100), type(5 << j(100))) == (5 << 100, long)
386
assert (j(100) >> 2, type(j(100) >> 2)) == ( 25, int)
388
def test_special_int(self):
394
raises(TypeError, int, inst)
395
assert inst.ar == True
399
raises((AttributeError,TypeError), int, b())
401
def test_special_long(self):
407
raises(TypeError, long, inst)
408
assert inst.ar == True
412
raises((AttributeError,TypeError), long, b())
414
def test_getnewargs(self):
415
assert 0 .__getnewargs__() == (0,)
417
class AppTestIntOptimizedAdd(AppTestInt):
418
def setup_class(cls):
419
from pypy.conftest import gettestobjspace
420
cls.space = gettestobjspace(**{"objspace.std.optimized_int_add": True})