Debian Codegraph @

rpython/rlib/test/test_rarithmetic.py

raw symbols SCIP
1 from rpython.rtyper.test.tool import BaseRtypingTest
2 from rpython.rtyper.test.test_llinterp import interpret
3 from rpython.rlib import rarithmetic
4 from rpython.rlib.rarithmetic import *
5 from rpython.rlib.rstring import ParseStringError, ParseStringOverflowError
6 from hypothesis import given, strategies, assume
7 import sys
8 import py
9
10 maxint_mask = (sys.maxint*2 + 1)
11 machbits = 0
12 i = 1
13 l = 1L
14 while i == l and type(i) is int:
15 i *= 2
16 l *= 2
17 machbits += 1
18 #print machbits
19
20
21 class Test_r_int:
22 def test__add__(self):
23 self.binary_test(lambda x, y: x + y, includes_floats=True)
24 def test__sub__(self):
25 self.binary_test(lambda x, y: x - y, includes_floats=True)
26 def test__mul__(self):
27 self.binary_test(lambda x, y: x * y, includes_floats=True)
28 x = 3; y = [2]
29 assert x*y == r_int(x)*y
30 assert y*x == y*r_int(x)
31 def test__div__(self):
32 self.binary_test(lambda x, y: x // y)
33 def test__mod__(self):
34 self.binary_test(lambda x, y: x % y)
35 def test__divmod__(self):
36 self.binary_test(divmod)
37 def test__lshift__(self):
38 self.binary_test(lambda x, y: x << y, (1, 2, 3))
39 def test__rshift__(self):
40 self.binary_test(lambda x, y: x >> y, (1, 2, 3))
41 def test__or__(self):
42 self.binary_test(lambda x, y: x | y)
43 def test__and__(self):
44 self.binary_test(lambda x, y: x & y)
45 def test__xor__(self):
46 self.binary_test(lambda x, y: x ^ y)
47 def test__neg__(self):
48 self.unary_test(lambda x: -x)
49 def test__pos__(self):
50 self.unary_test(lambda x: +x)
51 def test__invert__(self):
52 self.unary_test(lambda x: ~x)
53 def test__pow__(self):
54 self.binary_test(lambda x, y: x**y, (2, 3))
55 self.binary_test(lambda x, y: pow(x, y, 42L), (2, 3, 5, 1000))
56
57 def unary_test(self, f):
58 for arg in (-10, -1, 0, 3, 12345):
59 res = f(arg)
60 cmp = f(r_int(arg))
61 assert res == cmp
62
63 def binary_test(self, f, rargs=None, includes_floats=False):
64 if not rargs:
65 rargs = (-10, -1, 3, 55)
66 types_list = [(int, r_int), (r_int, int), (r_int, r_int)]
67 if includes_floats:
68 types_list += [(float, r_int), (r_int, float)]
69 for larg in (-10, -1, 0, 3, 1234):
70 for rarg in rargs:
71 for types in types_list:
72 res = f(larg, rarg)
73 left, right = types
74 cmp = f(left(larg), right(rarg))
75 assert res == cmp
76
77 class Test_r_uint:
78 def test__add__(self):
79 self.binary_test(lambda x, y: x + y)
80 def test__sub__(self):
81 self.binary_test(lambda x, y: x - y)
82 def test__mul__(self):
83 self.binary_test(lambda x, y: x * y)
84 x = 3; y = [2]
85 assert x*y == r_uint(x)*y
86 assert y*x == y*r_uint(x)
87 def test__div__(self):
88 self.binary_test(lambda x, y: x // y)
89 def test__mod__(self):
90 self.binary_test(lambda x, y: x % y)
91 def test__divmod__(self):
92 self.binary_test(divmod)
93 def test__lshift__(self):
94 self.binary_test(lambda x, y: x << y, (1, 2, 3))
95 def test__rshift__(self):
96 self.binary_test(lambda x, y: x >> y, (1, 2, 3))
97 def test__or__(self):
98 self.binary_test(lambda x, y: x | y)
99 def test__and__(self):
100 self.binary_test(lambda x, y: x & y)
101 def test__xor__(self):
102 self.binary_test(lambda x, y: x ^ y)
103 def test__neg__(self):
104 self.unary_test(lambda x: -x)
105 def test__pos__(self):
106 self.unary_test(lambda x: +x)
107 def test__invert__(self):
108 self.unary_test(lambda x: ~x)
109 def test__pow__(self):
110 self.binary_test(lambda x, y: x**y, (2, 3))
111 # pow is buggy, dowsn't allow our type
112 #self.binary_test(lambda x, y: pow(x, y, 42), (2, 3, 5, 1000))
113
114 def test_back_to_int(self):
115 #assert int(r_uint(-1)) == -1
116 # ^^^ that looks wrong IMHO: int(x) should not by itself return
117 # an integer that has a different value than x, especially
118 # if x is a subclass of long.
119 assert int(r_uint(1)) == 1
120
121 def unary_test(self, f):
122 for arg in (0, 3, 12345):
123 res = f(arg) & maxint_mask
124 cmp = f(r_uint(arg))
125 assert res == cmp
126
127 def binary_test(self, f, rargs = None, translated=False):
128 mask = maxint_mask
129 if not rargs:
130 rargs = (1, 3, 55)
131 # when translated merging different int types is not allowed
132 if translated:
133 alltypes = [(r_uint, r_uint)]
134 else:
135 alltypes = [(int, r_uint), (r_uint, int), (r_uint, r_uint)]
136 for larg in (0, 1, 2, 3, 1234):
137 for rarg in rargs:
138 for types in alltypes:
139 res = f(larg, rarg)
140 left, right = types
141 cmp = f(left(larg), right(rarg))
142 if type(res) is tuple:
143 res = res[0] & mask, res[1] & mask
144 else:
145 res = res & mask
146 assert res == cmp
147
148 def test_from_float(self):
149 assert r_uint(2.3) == 2
150 assert r_uint(sys.maxint * 1.234) == long(sys.maxint * 1.234)
151
152 def test_to_float(self):
153 assert float(r_uint(2)) == 2.0
154 val = long(sys.maxint * 1.234)
155 assert float(r_uint(val)) == float(val)
156
157 def test_mixed_types():
158 types = [r_uint, r_ulonglong]
159 for left in types:
160 for right in types:
161 x = left(3) + right(5)
162 expected = max(types.index(left), types.index(right))
163 assert types.index(type(x)) == expected
164
165 def test_limits():
166 for cls in r_uint, r_ulonglong:
167 mask = cls.MASK
168 assert cls(mask) == mask
169 assert cls(mask+1) == 0
170
171 for cls in r_int, r_longlong:
172 mask = cls.MASK>>1
173 assert cls(mask) == mask
174 assert cls(-mask-1) == -mask-1
175 py.test.raises(OverflowError, "cls(mask) + 1")
176 py.test.raises(OverflowError, "cls(-mask-1) - 1")
177
178 def test_intmask():
179 assert intmask(1) == 1
180 assert intmask(sys.maxint) == sys.maxint
181 minint = -sys.maxint-1
182 assert intmask(minint) == minint
183 assert intmask(2*sys.maxint+1) == -1
184 assert intmask(sys.maxint*2) == -2
185 assert intmask(sys.maxint*2+2) == 0
186 assert intmask(2*(sys.maxint*1+1)) == 0
187 assert intmask(1 << (machbits-1)) == 1 << (machbits-1)
188 assert intmask(sys.maxint+1) == minint
189 assert intmask(minint-1) == sys.maxint
190 assert intmask(r_uint(-1)) == -1
191 assert intmask(r_ulonglong(-1)) == -1
192
193 def test_intmask_small():
194 from rpython.rtyper.lltypesystem import rffi
195 for tp in [rffi.r_signedchar, rffi.r_short, rffi.r_int,
196 rffi.r_long, rffi.r_longlong]:
197 x = intmask(tp(5))
198 assert (type(x), x) == (int, 5)
199 x = intmask(tp(-5))
200 assert (type(x), x) == (int, -5)
201 for tp in [rffi.r_uchar, rffi.r_ushort, rffi.r_uint,
202 rffi.r_ulong, rffi.r_ulonglong]:
203 x = intmask(tp(5))
204 assert (type(x), x) == (int, 5)
205
206 def test_bug_creating_r_int():
207 minint = -sys.maxint-1
208 assert r_int(r_int(minint)) == minint
209
210 def test_ovfcheck():
211 one = 1
212 x = sys.maxint
213 minusx = -sys.maxint
214 n = -sys.maxint-1
215 y = sys.maxint-1
216 # sanity
217 py.test.raises(AssertionError, ovfcheck, r_uint(0))
218
219 # not overflowing
220 try:
221 ovfcheck(y+one)
222 except OverflowError:
223 assert False
224 else:
225 pass
226 try:
227 ovfcheck(minusx-one)
228 except OverflowError:
229 assert False
230 else:
231 pass
232 try:
233 ovfcheck(x-x)
234 except OverflowError:
235 assert False
236 else:
237 pass
238 try:
239 ovfcheck(n-n)
240 except OverflowError:
241 assert False
242 else:
243 pass
244
245 # overflowing
246 try:
247 ovfcheck(x+one)
248 except OverflowError:
249 pass
250 else:
251 assert False
252 try:
253 ovfcheck(x+x)
254 except OverflowError:
255 pass
256 else:
257 assert False
258 try:
259 ovfcheck(n-one)
260 except OverflowError:
261 pass
262 else:
263 assert False
264 try:
265 ovfcheck(n-y)
266 except OverflowError:
267 pass
268 else:
269 assert False
270
271 def test_ovfcheck_float_to_int():
272 assert ovfcheck_float_to_int(1.0) == 1
273 assert ovfcheck_float_to_int(0.0) == 0
274 assert ovfcheck_float_to_int(13.0) == 13
275 assert ovfcheck_float_to_int(-1.0) == -1
276 assert ovfcheck_float_to_int(-13.0) == -13
277
278 # strange things happening for float to int on 64 bit:
279 # int(float(i)) != i because of rounding issues
280 x = sys.maxint
281 while int(float(x)) > sys.maxint:
282 x -= 1
283 assert ovfcheck_float_to_int(float(x)) == int(float(x))
284
285 x = sys.maxint + 1
286 while int(float(x)) <= sys.maxint:
287 x += 1
288 py.test.raises(OverflowError, ovfcheck_float_to_int, x)
289
290 x = -sys.maxint-1
291 while int(float(x)) < -sys.maxint-1:
292 x += 1
293 assert ovfcheck_float_to_int(float(x)) == int(float(x))
294
295 x = -sys.maxint-1
296 while int(float(x)) >= -sys.maxint-1:
297 x -= 1
298 py.test.raises(OverflowError, ovfcheck_float_to_int, x)
299
300
301 def test_abs():
302 assert type(abs(r_longlong(1))) is r_longlong
303
304
305 def test_r_singlefloat():
306 x = r_singlefloat(2.5) # exact number
307 assert float(x) == 2.5
308 x = r_singlefloat(2.1) # approximate number, bits are lost
309 assert float(x) != 2.1
310 assert abs(float(x) - 2.1) < 1E-6
311
312 def test_r_singlefloat_eq():
313 x = r_singlefloat(2.5) # exact number
314 y = r_singlefloat(2.5)
315 assert x == y
316 assert not x != y
317 assert not x == 2.5
318 assert x != 2.5
319 py.test.raises(TypeError, "x>y")
320
321 class TestRarithmetic(BaseRtypingTest):
322 def test_compare_singlefloat_crashes(self):
323 from rpython.rlib.rarithmetic import r_singlefloat
324 from rpython.rtyper.error import MissingRTypeOperation
325 def f(x):
326 a = r_singlefloat(x)
327 b = r_singlefloat(x+1)
328 return a == b
329 py.test.raises(MissingRTypeOperation, "self.interpret(f, [42.0])")
330
331 def test_is_valid_int(self):
332 def f(x):
333 return (is_valid_int(x) * 4 +
334 is_valid_int(x > 0) * 2 +
335 is_valid_int(x + 0.5))
336 assert f(123) == 4 + 2
337 res = self.interpret(f, [123])
338 assert res == 4 + 2
339
340 def test_string_to_int_translates(self):
341 def f(s):
342 return string_to_int(str(s))
343 self.interpret(f, [123]) == 123
344
345 def test_int_real_union():
346 from rpython.rtyper.lltypesystem.rffi import r_int_real
347 assert compute_restype(r_int_real, r_int_real) is r_int_real
348
349 def test_compute_restype_incompatible():
350 from rpython.rtyper.lltypesystem.rffi import r_int_real, r_short, r_ushort
351 testcases = [(r_uint, r_longlong), (r_int_real, r_uint),
352 (r_short, r_ushort)]
353 for t1, t2 in testcases:
354 py.test.raises(AssertionError, compute_restype, t1, t2)
355 py.test.raises(AssertionError, compute_restype, t2, t1)
356
357 def test_most_neg_value_of():
358 assert most_neg_value_of_same_type(123) == -sys.maxint-1
359 assert most_neg_value_of_same_type(r_uint(123)) == 0
360 llmin = -(2**(r_longlong.BITS-1))
361 assert most_neg_value_of_same_type(r_longlong(123)) == llmin
362 assert most_neg_value_of_same_type(r_ulonglong(123)) == 0
363
364 def test_most_pos_value_of():
365 assert most_pos_value_of_same_type(123) == sys.maxint
366 assert most_pos_value_of_same_type(r_uint(123)) == 2 * sys.maxint + 1
367 llmax_sign = (2**(r_longlong.BITS-1))-1
368 llmax_unsign = (2**r_longlong.BITS)-1
369 assert most_pos_value_of_same_type(r_longlong(123)) == llmax_sign
370 assert most_pos_value_of_same_type(r_ulonglong(123)) == llmax_unsign
371
372 def test_is_signed_integer_type():
373 from rpython.rtyper.lltypesystem import lltype, rffi
374 assert is_signed_integer_type(lltype.Signed)
375 assert is_signed_integer_type(rffi.SIGNEDCHAR)
376 assert is_signed_integer_type(lltype.SignedLongLong)
377 assert not is_signed_integer_type(lltype.Unsigned)
378 assert not is_signed_integer_type(lltype.UnsignedLongLong)
379 assert not is_signed_integer_type(lltype.Char)
380 assert not is_signed_integer_type(lltype.UniChar)
381 assert not is_signed_integer_type(lltype.Bool)
382
383 def test_r_ulonglong():
384 x = r_longlong(-1)
385 y = r_ulonglong(x)
386 assert long(y) == 2**r_ulonglong.BITS - 1
387
388 def test_highest_bit():
389 py.test.raises(AssertionError, highest_bit, 0)
390 py.test.raises(AssertionError, highest_bit, 14)
391 for i in xrange(31):
392 assert highest_bit(2**i) == i
393
394 def test_int_between():
395 assert int_between(1, 1, 3)
396 assert int_between(1, 2, 3)
397 assert not int_between(1, 0, 2)
398 assert not int_between(1, 5, 2)
399 assert not int_between(1, 2, 2)
400 assert not int_between(1, 1, 1)
401
402 def test_int_force_ge_zero():
403 assert int_force_ge_zero(42) == 42
404 assert int_force_ge_zero(0) == 0
405 assert int_force_ge_zero(-42) == 0
406
407 @given(strategies.integers(min_value=0, max_value=sys.maxint),
408 strategies.integers(min_value=1, max_value=sys.maxint))
409 def test_int_c_div_mod(x, y):
410 assert int_c_div(~x, y) == -(abs(~x) // y)
411 assert int_c_div( x,-y) == -(x // y)
412
413 @given(strategies.integers(min_value=0, max_value=sys.maxint),
414 strategies.integers(min_value=1, max_value=sys.maxint))
415 def test_int_c_div_mod_2(x, y):
416 assume((x, y) != (sys.maxint, 1)) # This case would overflow
417 assert int_c_div(~x,-y) == +(abs(~x) // y)
418 for x1 in [x, ~x]:
419 for y1 in [y, -y]:
420 assert int_c_div(x1, y1) * y1 + int_c_mod(x1, y1) == x1
421
422 # these can't be prebuilt on 32bit
423 U1 = r_ulonglong(0x0102030405060708L)
424 U2 = r_ulonglong(0x0807060504030201L)
425 S1 = r_longlong(0x0102030405060708L)
426 S2 = r_longlong(0x0807060504030201L)
427
428 def test_byteswap():
429 from rpython.rtyper.lltypesystem import rffi, lltype
430
431 assert rffi.cast(lltype.Signed, byteswap(rffi.cast(rffi.USHORT, 0x0102))) == 0x0201
432 assert rffi.cast(lltype.Signed, byteswap(rffi.cast(rffi.INT, 0x01020304))) == 0x04030201
433 assert byteswap(U1) == U2
434 assert byteswap(S1) == S2
435 assert ((byteswap(2.3) - 1.903598566252326e+185) / 1e185) < 0.000001
436 assert (rffi.cast(lltype.Float, byteswap(rffi.cast(lltype.SingleFloat, 2.3))) - 4.173496037651603e-08) < 1e-16
437
438 def test_byteswap_interpret():
439 interpret(test_byteswap, [])
440
441
442 class TestStringToInt:
443
444 def test_string_to_int(self):
445 cases = [('0', 0),
446 ('1', 1),
447 ('9', 9),
448 ('10', 10),
449 ('09', 9),
450 ('0000101', 101), # not octal unless base 0 or 8
451 ('5123', 5123),
452 (' 0', 0),
453 ('0 ', 0),
454 (' \t \n 32313 \f \v \r \n\r ', 32313),
455 ('+12', 12),
456 ('-5', -5),
457 ('- 5', -5),
458 ('+ 5', 5),
459 (' -123456789 ', -123456789),
460 ]
461 for s, expected in cases:
462 assert string_to_int(s) == expected
463 #assert string_to_bigint(s).tolong() == expected
464
465 def test_string_to_int_base(self):
466 cases = [('111', 2, 7),
467 ('010', 2, 2),
468 ('102', 3, 11),
469 ('103', 4, 19),
470 ('107', 8, 71),
471 ('109', 10, 109),
472 ('10A', 11, 131),
473 ('10a', 11, 131),
474 ('10f', 16, 271),
475 ('10F', 16, 271),
476 ('0x10f', 16, 271),
477 ('0x10F', 16, 271),
478 ('10z', 36, 1331),
479 ('10Z', 36, 1331),
480 ('12', 0, 12),
481 ('015', 0, 13),
482 ('0x10', 0, 16),
483 ('0XE', 0, 14),
484 ('0', 0, 0),
485 ('0b11', 2, 3),
486 ('0B10', 2, 2),
487 ('0o77', 8, 63),
488 ]
489 for s, base, expected in cases:
490 assert string_to_int(s, base) == expected
491 assert string_to_int('+'+s, base) == expected
492 assert string_to_int('-'+s, base) == -expected
493 assert string_to_int(s+'\n', base) == expected
494 assert string_to_int(' +'+s, base) == expected
495 assert string_to_int('-'+s+' ', base) == -expected
496
497 def test_string_to_int_error(self):
498 cases = ['0x123', # must use base 0 or 16
499 ' 0X12 ',
500 '0b01',
501 '0o01',
502 '',
503 '++12',
504 '+-12',
505 '-+12',
506 '--12',
507 '12a6',
508 '12A6',
509 'f',
510 'Z',
511 '.',
512 '@',
513 ]
514 for s in cases:
515 py.test.raises(ParseStringError, string_to_int, s)
516 py.test.raises(ParseStringError, string_to_int, ' '+s)
517 py.test.raises(ParseStringError, string_to_int, s+' ')
518 py.test.raises(ParseStringError, string_to_int, '+'+s)
519 py.test.raises(ParseStringError, string_to_int, '-'+s)
520 py.test.raises(ParseStringError, string_to_int, '0x', 16)
521 py.test.raises(ParseStringError, string_to_int, '-0x', 16)
522 py.test.raises(ParseStringError, string_to_int, '+ 42',
523 disallow_whitespace_after_sign=True)
524
525 exc = py.test.raises(ParseStringError, string_to_int, '')
526 assert exc.value.msg == "invalid literal for int() with base 10"
527 exc = py.test.raises(ParseStringError, string_to_int, '', 0)
528 assert exc.value.msg == "invalid literal for int() with base 0"
529
530 def test_string_to_int_overflow(self):
531 import sys
532 py.test.raises(ParseStringOverflowError, string_to_int,
533 str(sys.maxint*17))
534
535 def test_string_to_int_not_overflow(self):
536 import sys
537 for x in [-sys.maxint-1, sys.maxint]:
538 y = string_to_int(str(x))
539 assert y == x
540
541 def test_string_to_int_base_error(self):
542 cases = [('1', 1),
543 ('1', 37),
544 ('a', 0),
545 ('9', 9),
546 ('0x123', 7),
547 ('145cdf', 15),
548 ('12', 37),
549 ('12', 98172),
550 ('12', -1),
551 ('12', -908),
552 ('12.3', 10),
553 ('12.3', 13),
554 ('12.3', 16),
555 ]
556 for s, base in cases:
557 py.test.raises(ParseStringError, string_to_int, s, base)
558 py.test.raises(ParseStringError, string_to_int, ' '+s, base)
559 py.test.raises(ParseStringError, string_to_int, s+' ', base)
560 py.test.raises(ParseStringError, string_to_int, '+'+s, base)
561 py.test.raises(ParseStringError, string_to_int, '-'+s, base)
562
563 @py.test.mark.parametrize('s', [
564 '0_0_0',
565 '4_2',
566 '1_0000_0000',
567 '0b1001_0100',
568 '0xfff_ffff',
569 '0o5_7_7',
570 '0b_0',
571 '0x_f',
572 '0o_5',
573 ])
574 def test_valid_underscores(self, s):
575 result = string_to_int(
576 s, base=0, allow_underscores=True, no_implicit_octal=True)
577 assert result == int(s.replace('_', ''), base=0)
578
579 @py.test.mark.parametrize('s', [
580 # Leading underscores
581 '_100',
582 '_',
583 '_0b1001_0100',
584 # Trailing underscores:
585 '0_',
586 '42_',
587 '1.4j_',
588 '0x_',
589 '0b1_',
590 '0xf_',
591 '0o5_',
592 # Underscores in the base selector:
593 '0_b0',
594 '0_xf',
595 '0_o5',
596 # Old-style octal, still disallowed:
597 '09_99',
598 # Multiple consecutive underscores:
599 '4_______2',
600 '0b1001__0100',
601 '0xfff__ffff',
602 '0x___',
603 '0o5__77',
604 '1e1__0',
605 ])
606 def test_invalid_underscores(self, s):
607 with py.test.raises(ParseStringError):
608 string_to_int(s, base=0, allow_underscores=True)
609
610 def test_no_implicit_octal(self):
611 TESTS = ['00', '000', '00_00', '02', '0377', '02_34']
612 for x in TESTS:
613 for valid_underscore in [False, True]:
614 for no_implicit_octal in [False, True]:
615 print x, valid_underscore, no_implicit_octal
616 expected_ok = True
617 if no_implicit_octal and any('1' <= c <= '7' for c in x):
618 expected_ok = False
619 if not valid_underscore and '_' in x:
620 expected_ok = False
621 if expected_ok:
622 y = string_to_int(x, base=0,
623 allow_underscores=valid_underscore,
624 no_implicit_octal=no_implicit_octal)
625 assert y == int(x.replace('_', ''), base=8)
626 else:
627 py.test.raises(ParseStringError, string_to_int, x,
628 base=0,
629 allow_underscores=valid_underscore,
630 no_implicit_octal=no_implicit_octal)
631
632
633 class TestExplicitIntsizes:
634
635 _32_max = 2147483647
636 _32_min = -2147483648
637 _32_umax = 4294967295
638 _64_max = 9223372036854775807
639 _64_min = -9223372036854775808
640 _64_umax = 18446744073709551615
641
642 def test_explicit_32(self):
643
644 assert type(r_int32(0)) == r_int32
645 assert type(r_int32(self._32_max)) == r_int32
646 assert type(r_int32(self._32_min)) == r_int32
647
648 assert type(r_uint32(0)) == r_uint32
649 assert type(r_uint32(self._32_umax)) == r_uint32
650
651 with py.test.raises(OverflowError):
652 ovfcheck(r_int32(self._32_max) + r_int32(1))
653 ovfcheck(r_int32(self._32_min) - r_int32(1))
654
655 assert most_pos_value_of_same_type(r_int32(1)) == self._32_max
656 assert most_neg_value_of_same_type(r_int32(1)) == self._32_min
657
658 assert most_pos_value_of_same_type(r_uint32(1)) == self._32_umax
659 assert most_neg_value_of_same_type(r_uint32(1)) == 0
660
661 assert r_uint32(self._32_umax) + r_uint32(1) == r_uint32(0)
662 assert r_uint32(0) - r_uint32(1) == r_uint32(self._32_umax)
663
664 def test_explicit_64(self):
665
666 assert type(r_int64(0)) == r_int64
667 assert type(r_int64(self._64_max)) == r_int64
668 assert type(r_int64(self._64_min)) == r_int64
669
670 assert type(r_uint64(0)) == r_uint64
671 assert type(r_uint64(self._64_umax)) == r_uint64
672
673 with py.test.raises(OverflowError):
674 ovfcheck(r_int64(self._64_max) + r_int64(1))
675 ovfcheck(r_int64(self._64_min) - r_int64(1))
676
677 assert most_pos_value_of_same_type(r_int64(1)) == self._64_max
678 assert most_neg_value_of_same_type(r_int64(1)) == self._64_min
679
680 assert most_pos_value_of_same_type(r_uint64(1)) == self._64_umax
681 assert most_neg_value_of_same_type(r_uint64(1)) == 0
682
683 assert r_uint64(self._64_umax) + r_uint64(1) == r_uint64(0)
684 assert r_uint64(0) - r_uint64(1) == r_uint64(self._64_umax)
685
686
687 def test_operation_with_float():
688 def f(x):
689 assert r_longlong(x) + 0.5 == 43.5
690 assert r_longlong(x) - 0.5 == 42.5
691 assert r_longlong(x) * 0.5 == 21.5
692 assert r_longlong(x) / 0.8 == 53.75
693 f(43)
694 interpret(f, [43])
695
696 def test_int64_plus_int32():
697 assert r_uint64(1234567891234) + r_uint32(1) == r_uint64(1234567891235)
698
699 def test_fallback_paths():
700
701 def make(pattern):
702 def method(self, other, *extra):
703 if extra:
704 assert extra == (None,) # for 'pow'
705 if type(other) is long:
706 return pattern % other
707 else:
708 return NotImplemented
709 return method
710
711 class A(object):
712 __add__ = make("a+%d")
713 __radd__ = make("%d+a")
714 __sub__ = make("a-%d")
715 __rsub__ = make("%d-a")
716 __mul__ = make("a*%d")
717 __rmul__ = make("%d*a")
718 __div__ = make("a/%d")
719 __rdiv__ = make("%d/a")
720 __floordiv__ = make("a//%d")
721 __rfloordiv__ = make("%d//a")
722 __mod__ = make("a%%%d")
723 __rmod__ = make("%d%%a")
724 __and__ = make("a&%d")
725 __rand__ = make("%d&a")
726 __or__ = make("a|%d")
727 __ror__ = make("%d|a")
728 __xor__ = make("a^%d")
729 __rxor__ = make("%d^a")
730 __pow__ = make("a**%d")
731 __rpow__ = make("%d**a")
732
733 a = A()
734 assert r_uint32(42) + a == "42+a"
735 assert a + r_uint32(42) == "a+42"
736 assert r_uint32(42) - a == "42-a"
737 assert a - r_uint32(42) == "a-42"
738 assert r_uint32(42) * a == "42*a"
739 assert a * r_uint32(42) == "a*42"
740 assert r_uint32(42) / a == "42/a"
741 assert a / r_uint32(42) == "a/42"
742 assert r_uint32(42) // a == "42//a"
743 assert a // r_uint32(42) == "a//42"
744 assert r_uint32(42) % a == "42%a"
745 assert a % r_uint32(42) == "a%42"
746 py.test.raises(TypeError, "a << r_uint32(42)")
747 py.test.raises(TypeError, "r_uint32(42) << a")
748 py.test.raises(TypeError, "a >> r_uint32(42)")
749 py.test.raises(TypeError, "r_uint32(42) >> a")
750 assert r_uint32(42) & a == "42&a"
751 assert a & r_uint32(42) == "a&42"
752 assert r_uint32(42) | a == "42|a"
753 assert a | r_uint32(42) == "a|42"
754 assert r_uint32(42) ^ a == "42^a"
755 assert a ^ r_uint32(42) == "a^42"
756 assert r_uint32(42) ** a == "42**a"
757 assert a ** r_uint32(42) == "a**42"
758
759 def test_ovfcheck_int32():
760 assert ovfcheck_int32_add(-2**30, -2**30) == -2**31
761 py.test.raises(OverflowError, ovfcheck_int32_add, 2**30, 2**30)
762 assert ovfcheck_int32_sub(-2**30, 2**30) == -2**31
763 py.test.raises(OverflowError, ovfcheck_int32_sub, 2**30, -2**30)
764 assert ovfcheck_int32_mul(-2**16, 2**15) == -2**31
765 py.test.raises(OverflowError, ovfcheck_int32_mul, -2**16, -2**15)
766
767 @given(strategies.integers(min_value=-sys.maxint-1, max_value=sys.maxint),
768 strategies.integers(min_value=-sys.maxint-1, max_value=sys.maxint),
769 strategies.integers(min_value=1, max_value=sys.maxint))
770 def test_mulmod(a, b, c):
771 assert mulmod(a, b, c) == (a * b) % c
772 #
773 import rpython.rlib.rbigint # import before patching check_support_int128
774 prev = rarithmetic.check_support_int128
775 try:
776 rarithmetic.check_support_int128 = lambda: False
777 assert mulmod(a, b, c) == (a * b) % c
778 finally:
779 rarithmetic.check_support_int128 = prev
780
781 @given(strategies.integers(min_value=0, max_value=2**LONG_BIT - 1),
782 strategies.integers(min_value=0, max_value=2**LONG_BIT - 1))
783 def test_uint_mul_high(a, b):
784 ua = r_uint(a)
785 ub = r_uint(b)
786 assert uint_mul_high(ua, ub) == (a * b) >> LONG_BIT
787 #
788 import rpython.rlib.rbigint # import before patching check_support_int128
789 prev = rarithmetic.check_support_int128
790 try:
791 rarithmetic.check_support_int128 = lambda: False
792 assert uint_mul_high(ua, ub) == (a * b) >> LONG_BIT
793 finally:
794 rarithmetic.check_support_int128 = prev