def test_parse_digit_string(self): from rpython.rlib.rbigint import parse_digit_string class Parser: def __init__(self, base, sign, digits): self.base = base self.sign = sign self.next_digit = iter(digits + [-1]).next x = parse_digit_string(Parser(10, 1, [6])) assert x.eq(rbigint.fromint(6)) x = parse_digit_string(Parser(10, 1, [6, 2, 3])) assert x.eq(rbigint.fromint(623)) x = parse_digit_string(Parser(10, -1, [6, 2, 3])) assert x.eq(rbigint.fromint(-623)) x = parse_digit_string(Parser(16, 1, [0xA, 0x4, 0xF])) assert x.eq(rbigint.fromint(0xA4F)) num = 0 for i in range(36): x = parse_digit_string(Parser(36, 1, range(i))) assert x.eq(rbigint.fromlong(num)) num = num * 36 + i x = parse_digit_string(Parser(16, -1, range(15,-1,-1)*99)) assert x.eq(rbigint.fromlong(long('-0x' + 'FEDCBA9876543210'*99, 16))) assert x.tobool() is True x = parse_digit_string(Parser(7, 1, [0, 0, 0])) assert x.tobool() is False x = parse_digit_string(Parser(7, -1, [0, 0, 0])) assert x.tobool() is False
def test_pow_lln(self): x = 10L y = 2L f1 = rbigint.fromlong(x) f2 = rbigint.fromlong(y) v = f1.pow(f2) assert v.tolong() == x ** y
def test_lt(self): val = [0, 0x111111111111, 0x111111111112, 0x111111111112FFFF] for x in gen_signs(val): for y in gen_signs(val): f1 = rbigint.fromlong(x) f2 = rbigint.fromlong(y) assert (x < y) == f1.lt(f2)
def test_left_shift_ovf(self, space): w_res = space.execute("return 4 << 90") assert space.bigint_w(w_res) == rbigint.fromlong(4951760157141521099596496896) w_res = space.execute("return %d << 2" % sys.maxint) assert self.unwrap(space, w_res) == rbigint.fromlong(sys.maxint << 2) w_res = space.execute("return 4 << -90") assert space.int_w(w_res) == 0
def test_pow_lll_bug2(self): x = rbigint.fromlong(2) y = rbigint.fromlong(5100894665148900058249470019412564146962964987365857466751243988156579407594163282788332839328303748028644825680244165072186950517295679131100799612871613064597) z = rbigint.fromlong(538564) expected = rbigint.fromlong(163464) got = x.pow(y, z) assert got.eq(expected)
def test_invert(self): x = 3 ** 40 f1 = rbigint.fromlong(x) f2 = rbigint.fromlong(-x) r1 = f1.invert() r2 = f2.invert() assert r1.tolong() == -(x + 1) assert r2.tolong() == -(-x + 1)
def test_sub(self): x = 12378959520302182384345L y = 88961284756491823819191823L for i in [-1, 1]: for j in [-1, 1]: f1 = rbigint.fromlong(x * i) f2 = rbigint.fromlong(y * j) result = f1.sub(f2) assert result.tolong() == x * i - y * j
def test_bitwise(self): for x in gen_signs([0, 1, 5, 11, 42, 43, 3 ** 30]): for y in gen_signs([0, 1, 5, 11, 42, 43, 3 ** 30, 3 ** 31]): lx = rbigint.fromlong(x) ly = rbigint.fromlong(y) for mod in "xor and_ or_".split(): res1 = getattr(lx, mod)(ly).tolong() res2 = getattr(operator, mod)(x, y) assert res1 == res2
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))
def test_truediv_overflow2(self): overflowing = 2**1024 - 2**(1024-53-1) op1 = rbigint.fromlong(2*overflowing - 10) op2 = rbigint.fromlong(2) f = op1.truediv(op2) assert f == 1.7976931348623157e+308 # exactly op2 = rbigint.fromlong(-2) f = op1.truediv(op2) assert f == -1.7976931348623157e+308 # exactly
def test_add(self): x = 123456789123456789000000L y = 123858582373821923936744221L for i in [-1, 1]: for j in [-1, 1]: f1 = rbigint.fromlong(x * i) f2 = rbigint.fromlong(y * j) result = f1.add(f2) assert result.tolong() == x * i + y * j
def test_coerce(self, space): w_res = space.execute("return 18446744073709551628.coerce 12") assert self.unwrap(space, w_res) == [rbigint.fromint(12), rbigint.fromlong(18446744073709551628)] w_res = space.execute("return 18446744073709551628.coerce 18446744073709551628") assert self.unwrap(space, w_res) == [rbigint.fromlong(18446744073709551628), rbigint.fromlong(18446744073709551628)] with self.raises(space, "TypeError", "can't coerce String to Bignum"): space.execute("18446744073709551628.coerce 'hello'")
def test_mul(self): for x in gen_signs(long_vals): f1 = rbigint.fromlong(x) for y in gen_signs(long_vals_not_too_big): f2 = rbigint.fromlong(y) result = f1.mul(f2) assert result.tolong() == x * y # there's a special case for a is b result = f1.mul(f1) assert result.tolong() == x * x
def test_longlong(self): max = 1L << (r_longlong.BITS-1) f1 = rbigint.fromlong(max-1) # fits in r_longlong f2 = rbigint.fromlong(-max) # fits in r_longlong f3 = rbigint.fromlong(max) # overflows f4 = rbigint.fromlong(-max-1) # overflows assert f1.tolonglong() == max-1 assert f2.tolonglong() == -max py.test.raises(OverflowError, f3.tolonglong) py.test.raises(OverflowError, f4.tolonglong)
def test_truediv(self): for op1 in gen_signs(long_vals_not_too_big): for op2 in gen_signs(long_vals): if not op2: continue rl_op1 = rbigint.fromlong(op1) rl_op2 = rbigint.fromlong(op2) r1 = rl_op1.truediv(rl_op2) r2 = op1 / op2 assert r1 == r2
def test_mul(self): x = -1238585838347L y = 585839391919233L f1 = rbigint.fromlong(x) f2 = rbigint.fromlong(y) result = f1.mul(f2) assert result.tolong() == x * y # also test a * a, it has special code result = f1.mul(f1) assert result.tolong() == x * x
def test_floordiv(self): for op1 in gen_signs(long_vals): for op2 in gen_signs(long_vals): if not op2: continue rl_op1 = rbigint.fromlong(op1) rl_op2 = rbigint.fromlong(op2) r1 = rl_op1.floordiv(rl_op2) r2 = op1 // op2 assert r1.tolong() == r2
def test_tofloat(self): x = 12345678901234567890L ** 10 f1 = rbigint.fromlong(x) d = f1.tofloat() assert d == float(x) x = x ** 100 f1 = rbigint.fromlong(x) assert py.test.raises(OverflowError, f1.tofloat) f2 = rbigint.fromlong(2097152 << SHIFT) d = f2.tofloat() assert d == float(2097152 << SHIFT)
def test_eq(self): x = 5858393919192332223L y = 585839391919233111223311112332L f1 = rbigint.fromlong(x) f2 = rbigint.fromlong(-x) f3 = rbigint.fromlong(y) assert f1.eq(f1) assert f2.eq(f2) assert f3.eq(f3) assert not f1.eq(f2) assert not f1.eq(f3)
def test_mod(self): for op1 in gen_signs(long_vals): for op2 in gen_signs(long_vals): if not op2: continue rl_op1 = rbigint.fromlong(op1) rl_op2 = rbigint.fromlong(op2) r1 = rl_op1.mod(rl_op2) r2 = op1 % op2 print op1, op2 assert r1.tolong() == r2
def test_int_conversion(self): f1 = rbigint.fromlong(12332) f2 = rbigint.fromint(12332) assert f2.tolong() == f1.tolong() assert f2.toint() assert rbigint.fromlong(42).tolong() == 42 assert rbigint.fromlong(-42).tolong() == -42 u = f2.touint() assert u == 12332 assert type(u) is r_uint
def test_args_from_uint(self): BASE = 1 << SHIFT assert rbigint.fromrarith_int(r_uint(0)).eq(bigint([0], 0)) assert rbigint.fromrarith_int(r_uint(17)).eq(bigint([17], 1)) assert rbigint.fromrarith_int(r_uint(BASE-1)).eq(bigint([intmask(BASE-1)], 1)) assert rbigint.fromrarith_int(r_uint(BASE)).eq(bigint([0, 1], 1)) #assert rbigint.fromrarith_int(r_uint(BASE**2)).eq(bigint([0], 0)) assert rbigint.fromrarith_int(r_uint(sys.maxint)).eq( rbigint.fromint(sys.maxint)) assert rbigint.fromrarith_int(r_uint(sys.maxint+1)).eq( rbigint.fromlong(sys.maxint+1)) assert rbigint.fromrarith_int(r_uint(2*sys.maxint+1)).eq( rbigint.fromlong(2*sys.maxint+1))
def test_tostring(self): z = rbigint.fromlong(0) assert z.str() == '0' assert z.repr() == '0L' assert z.hex() == '0x0L' assert z.oct() == '0L' x = rbigint.fromlong(-18471379832321) assert x.str() == '-18471379832321' assert x.repr() == '-18471379832321L' assert x.hex() == '-0x10ccb4088e01L' assert x.oct() == '-0414626402107001L' assert x.format('.!') == ( '-!....!!..!!..!.!!.!......!...!...!!!........!') assert x.format('abcdefghijkl', '<<', '>>') == '-<<cakdkgdijffjf>>'
def test_large_positive_integer_1word_at_put(): target = W_LargeIntegerWord(space, space.w_LargePositiveInteger, r_uint(0), constants.BYTES_PER_MACHINE_INT) source = W_LargeIntegerWord(space, space.w_LargePositiveInteger, r_uint(2**constants.LONG_BIT-1), constants.BYTES_PER_MACHINE_INT) for i in range(constants.BYTES_PER_MACHINE_INT): target.atput0(space, i, source.at0(space, i)) assert target.at0(space, i) == source.at0(space, i) assert hex(r_uint(target.unwrap_long_untranslated(space))) == hex(r_uint(source.unwrap_long_untranslated(space))) target = W_LargeIntegerBig(space, space.w_LargePositiveInteger, rbigint.fromlong(0), constants.BYTES_PER_MACHINE_INT) source = W_LargeIntegerBig(space, space.w_LargePositiveInteger, rbigint.fromlong(2**constants.LONG_BIT-1), constants.BYTES_PER_MACHINE_INT) for i in range(constants.BYTES_PER_MACHINE_INT): target.atput0(space, i, source.at0(space, i)) assert target.at0(space, i) == source.at0(space, i) assert hex(r_uint(target.unwrap_long_untranslated(space))) == hex(r_uint(source.unwrap_long_untranslated(space)))
def test__x_divrem2(self): Rx = 1 << 130 Rx2 = 1 << 150 Ry = 1 << 127 Ry2 = 1<< 150 for i in range(10): x = long(randint(Rx, Rx2)) y = long(randint(Ry, Ry2)) f1 = rbigint.fromlong(x) f2 = rbigint.fromlong(y) div, rem = lobj._x_divrem(f1, f2) _div, _rem = divmod(x, y) assert div.tolong() == _div assert rem.tolong() == _rem
def test_pow_lll(self): x = 10L y = 2L z = 13L f1 = rbigint.fromlong(x) f2 = rbigint.fromlong(y) f3 = rbigint.fromlong(z) v = f1.pow(f2, f3) assert v.tolong() == pow(x, y, z) f3n = f3.neg() v = f1.pow(f2, f3n) assert v.tolong() == pow(x, y, -z) # f1, f2, f3 = [rbigint.fromlong(i) for i in (10L, -1L, 42L)] py.test.raises(TypeError, f1.pow, f2, f3) f1, f2, f3 = [rbigint.fromlong(i) for i in (10L, 5L, 0L)] py.test.raises(ValueError, f1.pow, f2, f3) # MAX = 1E20 x = long(random() * MAX) + 1 y = long(random() * MAX) + 1 z = long(random() * MAX) + 1 f1 = rbigint.fromlong(x) f2 = rbigint.fromlong(y) f3 = rbigint.fromlong(z) print f1 print f2 print f3 v = f1.pow(f2, f3) print '--->', v assert v.tolong() == pow(x, y, z)
def test_args_from_long(self): BASE = 1 << SHIFT assert rbigint.fromlong(0).eq(bigint([0], 0)) assert rbigint.fromlong(17).eq(bigint([17], 1)) assert rbigint.fromlong(BASE-1).eq(bigint([intmask(BASE-1)], 1)) assert rbigint.fromlong(BASE).eq(bigint([0, 1], 1)) assert rbigint.fromlong(BASE**2).eq(bigint([0, 0, 1], 1)) assert rbigint.fromlong(-17).eq(bigint([17], -1)) assert rbigint.fromlong(-(BASE-1)).eq(bigint([intmask(BASE-1)], -1)) assert rbigint.fromlong(-BASE).eq(bigint([0, 1], -1)) assert rbigint.fromlong(-(BASE**2)).eq(bigint([0, 0, 1], -1))
def test_parse_digit_string(self): from rpython.rlib.rbigint import parse_digit_string class Parser: def __init__(self, base, sign, digits): self.base = base self.sign = sign self.i = 0 self._digits = digits def next_digit(self): i = self.i if i == len(self._digits): return -1 self.i = i + 1 return self._digits[i] def prev_digit(self): i = self.i - 1 assert i >= 0 self.i = i return self._digits[i] x = parse_digit_string(Parser(10, 1, [6])) assert x.eq(rbigint.fromint(6)) x = parse_digit_string(Parser(10, 1, [6, 2, 3])) assert x.eq(rbigint.fromint(623)) x = parse_digit_string(Parser(10, -1, [6, 2, 3])) assert x.eq(rbigint.fromint(-623)) x = parse_digit_string(Parser(16, 1, [0xA, 0x4, 0xF])) assert x.eq(rbigint.fromint(0xA4F)) num = 0 for i in range(36): x = parse_digit_string(Parser(36, 1, range(i))) assert x.eq(rbigint.fromlong(num)) num = num * 36 + i x = parse_digit_string(Parser(16, -1, range(15,-1,-1)*99)) assert x.eq(rbigint.fromlong(long('-0x' + 'FEDCBA9876543210'*99, 16))) assert x.tobool() is True x = parse_digit_string(Parser(7, 1, [0, 0, 0])) assert x.tobool() is False x = parse_digit_string(Parser(7, -1, [0, 0, 0])) assert x.tobool() is False for base in [2, 4, 8, 16, 32]: for inp in [[0], [1], [1, 0], [0, 1], [1, 0, 1], [1, 0, 0, 1], [1, 0, 0, base-1, 0, 1], [base-1, 1, 0, 0, 0, 1, 0], [base-1]]: inp = inp * 97 x = parse_digit_string(Parser(base, -1, inp)) num = sum(inp[i] * (base ** (len(inp)-1-i)) for i in range(len(inp))) assert x.eq(rbigint.fromlong(-num))
def test__x_divrem(self): x = 12345678901234567890L for i in range(100): y = long(randint(1, 1 << 60)) y <<= 60 y += randint(1, 1 << 60) if y > x: x <<= 100 f1 = rbigint.fromlong(x) f2 = rbigint.fromlong(y) div, rem = lobj._x_divrem(f1, f2) _div, _rem = divmod(x, y) assert div.tolong() == _div assert rem.tolong() == _rem
def test_divmod(self): x = 12345678901234567890L for i in range(100): y = long(randint(0, 1 << 60)) y <<= 60 y += randint(0, 1 << 60) for sx, sy in (1, 1), (1, -1), (-1, -1), (-1, 1): sx *= x sy *= y f1 = rbigint.fromlong(sx) f2 = rbigint.fromlong(sy) div, rem = f1.divmod(f2) _div, _rem = divmod(sx, sy) assert div.tolong() == _div assert rem.tolong() == _rem
def test(a, b, res): g = rbigint.fromlong(a).gcd(rbigint.fromlong(b)).tolong() assert g == res
def test_mul_eq_shift(self): p2 = rbigint.fromlong(1).lshift(63) f1 = rbigint.fromlong(0).lshift(63) f2 = rbigint.fromlong(0).mul(p2) assert f1.eq(f2)
def test_overzelous_assertion(self): a = rbigint.fromlong(-1<<10000) b = rbigint.fromlong(-1<<3000) assert a.mul(b).tolong() == (-1<<10000)*(-1<<3000)
def looooong(val): return rbigint.fromlong(val)
def test_floordiv2(self): n1 = rbigint.fromlong(sys.maxint + 1) n2 = rbigint.fromlong(-(sys.maxint + 1)) assert n1.floordiv(n2).tolong() == -1L assert n2.floordiv(n1).tolong() == -1L
def w_l(largeInteger): if largeInteger >= 0 and largeInteger <= constants.TAGGED_MAXINT: return space.wrap_int(intmask(largeInteger)) else: return space.wrap_int(rbigint.fromlong(largeInteger))
def test_int_bitwise_and_mul(self, x, y): lx = rbigint.fromlong(x) for mod in "xor and_ or_ mul".split(): res1 = getattr(lx, 'int_' + mod)(y).tolong() res2 = getattr(operator, mod)(x, y) assert res1 == res2
def test_int_comparison(self, x, y): lx = rbigint.fromlong(x) assert lx.int_lt(y) == (x < y) assert lx.int_eq(y) == (x == y) assert lx.int_le(y) == (x <= y)
def test_int_mul(self): for x in gen_signs(long_vals): f1 = rbigint.fromlong(x) for y in signed_int_vals: result = f1.int_mul(y) assert result.tolong() == x * y
def assert_converts_to_bigint(f, x, n): expected = wrap_bigint(rbigint.fromlong(n)) result = f.invoke1(x) assert type(result) == WBigInt assert result._bigint_value.eq(expected._bigint_value)
def assert_converts_to_bigint(f, v): expected = wrap_bigint(rbigint.fromlong(v)) result = f.invoke2(v1, v2) assert type(result) == WBigInt assert result._bigint_value.eq(expected._bigint_value)
def test_abs(self, x): assert rbigint.fromlong(x).abs().tolong() == abs(x)
def gcd_long(a, b): return gcd(rbigint.fromlong(a), rbigint.fromlong(b)).tolong()
def test_int_mul(self): for x in gen_signs([39, 128, 111111111, 123456789123456789000000L, 1 << 100, 3 ** 10000]): for y in gen_signs([0, 1, 8888, sys.maxint, 2 ** 19, 2 ** 18 - 1]): f1 = rbigint.fromlong(x) result = f1.int_mul(y) assert result.tolong() == x * y
def test_log2(self): assert rbigint.fromlong(1).log(2.0) == 0.0 assert rbigint.fromlong(2).log(2.0) == 1.0 assert rbigint.fromlong(2**1023).log(2.0) == 1023.0
def test_truediv_precision(self): op1 = rbigint.fromlong(12345*2**30) op2 = rbigint.fromlong(98765*7**81) f = op1.truediv(op2) assert f == 4.7298422347492634e-61 # exactly
def fromlong(v): return rbigint.fromlong(v)
masks_list = [int((1 << i) - 1) for i in range(1, r_uint.BITS - 1)] for x in gen_signs([3L**30L, 5L**20L, 7**300, 0L, 1L]): f1 = rbigint.fromlong(x) py.test.raises(ValueError, f1.lshift, negative) py.test.raises(ValueError, f1.rshift, negative) for y in [0L, 1L, 32L, 2304L, 11233L, 3**9]: res1 = f1.lshift(int(y)).tolong() res2 = f1.rshift(int(y)).tolong() assert res1 == x << y assert res2 == x >> y for mask in masks_list: res3 = f1.abs_rshift_and_mask(r_ulonglong(y), mask) assert res3 == (abs(x) >> y) & mask # test special optimization case in rshift: assert rbigint.fromlong(-(1 << 100)).rshift( 5).tolong() == -(1 << 100) >> 5 def test_qshift(self): for x in range(10): for y in range(1, 161, 16): num = (x << y) + x f1 = rbigint.fromlong(num) nf1 = rbigint.fromlong(-num) for z in range(1, 31): res1 = f1.lqshift(z).tolong() res3 = nf1.lqshift(z).tolong() assert res1 == num << z assert res3 == -num << z
def test_subtraction_ovf(self, space): w_res = space.execute( "return 0 - (2 << (0.size * 8 - 3)) - (2 << (0.size * 8 - 3)) - (2 << (0.size * 8 - 3))" ) assert space.bigint_w(w_res) == rbigint.fromlong((2 << (LONG_BIT - 3)) * -3)
def test_multiplication_bigint(self, space): w_res = space.execute("return 1 * %d" % (sys.maxint + 1)) assert self.unwrap(space, w_res) == rbigint.fromlong(sys.maxint + 1)
def test_int_comparison2(self, x, y): lx = rbigint.fromlong(x) ly = rbigint.fromlong(y) assert lx.lt(ly) == (x < y) assert lx.eq(ly) == (x == y) assert lx.le(ly) == (x <= y)
def test_addition_bigint(self, space): w_res = space.execute("return 2 + %d" % (sys.maxint + 1)) assert self.unwrap(space, w_res) == rbigint.fromlong(sys.maxint + 3)
def test_substraction_bigint(self, space): w_res = space.execute("return 1 - %d" % (sys.maxint + 1)) assert self.unwrap(space, w_res) == rbigint.fromlong(1 - sys.maxint - 1)
def test_uintmask(self): assert rbigint.fromint(-1).uintmask() == r_uint(-1) assert rbigint.fromint(0).uintmask() == r_uint(0) assert (rbigint.fromint(sys.maxint).uintmask() == r_uint(sys.maxint)) assert (rbigint.fromlong(sys.maxint + 1).uintmask() == r_uint(-sys.maxint - 1))
def fromlong(l): return W_LongObject(rbigint.fromlong(l))
def test_int_add(self): for x in gen_signs(long_vals): for y in gen_signs([0, 1, 9999, sys.maxint, 2 ** 19, 2 ** 18 - 1]): f1 = rbigint.fromlong(x) result = f1.int_add(y) assert result.tolong() == x + y
def test_multiplication_ovf(self, space): w_res = space.execute( "return (2 << (0.size * 8 - 3)) * (2 << (0.size * 8 - 3))") assert space.bigint_w(w_res) == rbigint.fromlong((2 << (LONG_BIT - 3))**2)
def test_integer_strategy_with_w_long(self): w = W_LongObject(rbigint.fromlong(42)) w_tuple = self.space.newtuple([w, w]) assert w_tuple.__class__.__name__ == 'W_SpecialisedTupleObject_ii'