def test_order(self): f6 = rbigint.fromint(6) f7 = rbigint.fromint(7) assert (f6.lt(f6), f6.lt(f7), f7.lt(f6)) == (0,1,0) assert (f6.le(f6), f6.le(f7), f7.le(f6)) == (1,1,0) assert (f6.gt(f6), f6.gt(f7), f7.gt(f6)) == (0,0,1) assert (f6.ge(f6), f6.ge(f7), f7.ge(f6)) == (1,0,1)
def _multiply_int_int(b, a): assert isinstance(a, WInt) assert isinstance(b, WInt) try: return wrap_int(ovfcheck(a._int_value * b._int_value)) except OverflowError: return wrap_bigint(rbigint.fromint(a._int_value).mul(rbigint.fromint(b._int_value)))
def test_int_order(self): f6 = rbigint.fromint(6) f7 = rbigint.fromint(7) assert (f6.int_lt(6), f6.int_lt(7), f7.int_lt(6)) == (0,1,0) assert (f6.int_le(6), f6.int_le(7), f7.int_le(6)) == (1,1,0) assert (f6.int_gt(6), f6.int_gt(7), f7.int_gt(6)) == (0,0,1) assert (f6.int_ge(6), f6.int_ge(7), f7.int_ge(6)) == (1,0,1)
def test_shr(self): assert Number(8).arith_shr(Number(2)).num == 2 assert BigInt(rbigint.fromint(256)).arith_shr(Number(5)).num == 8 assert BigInt(rbigint.fromint(256)).arith_shr(BigInt(rbigint.fromint(5))).num == 8 assert Number(256).arith_shr(BigInt(rbigint.fromint(5))).num == 8 py.test.raises(ValueError, 'BigInt(rbigint.fromint(2)).arith_shr(BigInt(rbigint.fromdecimalstr(\'100000000000000000000000000000000000000000000000\')))')
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_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 test_pow(self): for op1 in [-50, -12, -2, -1, 1, 2, 50, 52]: for op2 in [0, 1, 2, 8, 9, 10, 11]: rl_op1 = rbigint.fromint(op1) rl_op2 = rbigint.fromint(op2) r1 = rl_op1.pow(rl_op2) r2 = op1 ** op2 assert r1.tolong() == r2
def test_floordiv(self): for op1 in [-12, -2, -1, 1, 2, 50]: for op2 in [-4, -2, -1, 1, 2, 8]: rl_op1 = rbigint.fromint(op1) rl_op2 = rbigint.fromint(op2) r1 = rl_op1.floordiv(rl_op2) r2 = op1 // op2 assert r1.tolong() == r2
def test_truediv(self): for op1 in [-12, -2, -1, 1, 2, 50]: for op2 in [-4, -2, -1, 1, 2, 8]: rl_op1 = rbigint.fromint(op1) rl_op2 = rbigint.fromint(op2) r1 = rl_op1.truediv(rl_op2) r2 = op1 / op2 assert r1 == r2
def to_bigint(self, s, neg, i, radix): val = rbigint.fromint(0) bigint_radix = rbigint.fromint(radix) for digit in self._digits(s, i, radix): val = val.mul(bigint_radix).add(rbigint.fromint(digit)) if neg: val = val.neg() return val
def test_mod(self): assert Number(8).arith_mod(Number(2)).num == 0 assert BigInt(rbigint.fromint(46546)).arith_mod(Number(33)).num == 16 assert Number(46546).arith_mod(BigInt(rbigint.fromint(33))).num == 16 py.test.raises(error.CatchableError, 'BigInt(rbigint.fromdecimalstr("12342424234")).arith_mod(BigInt(rbigint.fromint(0)))') py.test.raises(error.CatchableError, 'Number(34535).arith_mod(BigInt(rbigint.fromint(0)))') py.test.raises(error.CatchableError, 'BigInt(rbigint.fromdecimalstr("12342424234")).arith_mod(Number(0))')
def test_min(self): assert Number(5).arith_min(Number(1)).num == 1 assert Float(-1.32).arith_min(Float(4.5)).floatval == -1.32 assert BigInt(rbigint.fromdecimalstr('111111111111111111111111111')).arith_min(BigInt(rbigint.fromdecimalstr('222222222222222222222222222222'))).value.str() == '111111111111111111111111111' assert Number(-1000).arith_min(BigInt(rbigint.fromint(-1001))).num == -1001 assert BigInt(rbigint.fromint(-1001)).arith_min(Number(-1000)).num == -1001 assert BigInt(rbigint.fromdecimalstr('10000')).arith_min(Float(20000)).floatval == 10000.0 assert Float(20000).arith_min(BigInt(rbigint.fromdecimalstr('10000'))).floatval == 10000.0
def read_raw_bigint(rdr): nchars = read_raw_integer(rdr) n = rbigint.fromint(0) for i in range(nchars): a = rbigint.fromint(ord(rdr.read()[0])) a = a.lshift(8 * i) n = n.add(a) return n
def frombigint(n, d=rbigint.fromint(1)): from pycket.arithmetic import gcd g = gcd(n, d) n = n.floordiv(g) d = d.floordiv(g) if d.eq(rbigint.fromint(1)): return W_Bignum.frombigint(n) return W_Rational(n, d)
def test_mod(self): for op1 in [-50, -12, -2, -1, 1, 2, 50, 52]: for op2 in [-4, -2, -1, 1, 2, 8]: rl_op1 = rbigint.fromint(op1) rl_op2 = rbigint.fromint(op2) r1 = rl_op1.mod(rl_op2) r2 = op1 % op2 print op1, op2 assert r1.tolong() == r2
def frombigint(n, d=rbigint.fromint(1), need_to_check=True): from pycket.arithmetic import gcd g = gcd(n, d) n = n.floordiv(g) d = d.floordiv(g) if need_to_check and d.eq(rbigint.fromint(1)): return W_Bignum.frombigint(n) return W_Rational(n, d)
def test_eq_ne_operators(self): a1 = rbigint.fromint(12) a2 = rbigint.fromint(12) a3 = rbigint.fromint(123) assert a1 == a2 assert a1 != a3 assert not (a1 != a2) assert not (a1 == a3)
def test_add(self): x = rbigint.fromint(-2147483647) y = rbigint.fromint(-1) z = rbigint.fromint(-2147483648) def test(): return x.add(y).eq(z) assert test() res = interpret(test, []) assert res
def nextVarInt(self): shift = 0 bi = rbigint.fromint(0) cont = True while cont: b = ord(self.nextByte()) bi = bi.or_(rbigint.fromint(b & 0x7f).lshift(shift)) shift += 7 cont = bool(b & 0x80) return bi
def add(self, other): if isinstance(other, Fixnum): try: res = rarithmetic.ovfcheck(other.fixval + self.fixval) return Fixnum(res) except OverflowError: return Bignum(rbigint.fromint(self.fixval).add(rbigint.fromint(other.fixval))) else: assert isinstance(other, Number) return other.add(self)
def test_str(self): n = 1 r1 = rbigint.fromint(1) three = rbigint.fromint(3) for i in range(300): n *= 3 r1 = r1.mul(three) assert r1.str() == str(n) r2 = r1.neg() assert r2.str() == str(-n)
def test_simple(self): for op1 in [-2, -1, 0, 1, 2, 50]: for op2 in [-2, -1, 0, 1, 2, 50]: rl_op1 = rbigint.fromint(op1) rl_op2 = rbigint.fromint(op2) for op in "add sub mul".split(): r1 = getattr(rl_op1, op)(rl_op2) r2 = getattr(operator, op)(op1, op2) print op, op1, op2 assert r1.tolong() == r2
def fromint(n, d=1, need_to_check=True): assert isinstance(n, int) assert isinstance(d, int) from fractions import gcd g = gcd(n, d) n = n // g d = d // g if need_to_check and d == 1: return W_Fixnum(n) return W_Rational(rbigint.fromint(n), rbigint.fromint(d))
def arith_shl_same(self, other): assert isinstance(other, values.W_Fixnum) if other.value >= r_int.BITS: if not self.value: return values.W_Fixnum.ZERO val = rbigint.fromint(self.value).lshift(other.value) return values.W_Integer.frombigint(val) try: res = rarithmetic.ovfcheck(self.value << other.value) except OverflowError: return self.arith_shl(values.W_Bignum(rbigint.fromint(other.value))) return values.W_Fixnum(res)
def make(num, den): if isinstance(num, W_Fixnum): num = rbigint.fromint(num.value) else: assert isinstance(num, W_Bignum) num = num.value if isinstance(den, W_Fixnum): den = rbigint.fromint(den.value) else: assert isinstance(den, W_Bignum) den = den.value return W_Rational.frombigint(num, den)
def test_bigint_w(self): space = self.space fromlong = lobj.W_LongObject.fromlong assert isinstance(space.bigint_w(fromlong(42)), rbigint) assert space.bigint_w(fromlong(42)).eq(rbigint.fromint(42)) assert space.bigint_w(fromlong(-1)).eq(rbigint.fromint(-1)) w_obj = space.wrap("hello world") space.raises_w(space.w_TypeError, space.bigint_w, w_obj) w_obj = space.wrap(123.456) space.raises_w(space.w_TypeError, space.bigint_w, w_obj) w_obj = fromlong(42) assert space.unwrap(w_obj) == 42
def test_tobytes(self): assert rbigint.fromint(0).tobytes(1, 'big', signed=True) == '\x00' assert rbigint.fromint(1).tobytes(2, 'big', signed=True) == '\x00\x01' py.test.raises(OverflowError, rbigint.fromint(255).tobytes, 1, 'big', signed=True) assert rbigint.fromint(-129).tobytes(2, 'big', signed=True) == '\xff\x7f' assert rbigint.fromint(-129).tobytes(2, 'little', signed=True) == '\x7f\xff' assert rbigint.fromint(65535).tobytes(3, 'big', signed=True) == '\x00\xff\xff' assert rbigint.fromint(-65536).tobytes(3, 'little', signed=True) == '\x00\x00\xff' assert rbigint.fromint(65535).tobytes(2, 'big', signed=False) == '\xff\xff' assert rbigint.fromint(-8388608).tobytes(3, 'little', signed=True) == '\x00\x00\x80' i = rbigint.fromint(-8388608) py.test.raises(InvalidEndiannessError, i.tobytes, 3, 'foo', signed=True) py.test.raises(InvalidSignednessError, i.tobytes, 3, 'little', signed=False) py.test.raises(OverflowError, i.tobytes, 2, 'little', signed=True)
def __init__(self, numerator, denominator): assert isinstance(numerator, RBigInt) assert isinstance(denominator, RBigInt) assert denominator.gt(RBigInt.fromint(0)) common_factor = greatest_common_divisor( numerator.abs(), denominator.abs()) if common_factor.ne(RBigInt.fromint(1)): numerator = numerator.div(common_factor) denominator = denominator.div(common_factor) self.numerator = numerator self.denominator = denominator
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 prim_subtract(self, right, universe): if isinstance(right, BigInteger): r = rbigint.fromint(self._embedded_integer).sub( right.get_embedded_biginteger()) return universe.new_biginteger(r) elif isinstance(right, Double): return self._to_double(universe).prim_subtract(right, universe) else: l = self._embedded_integer r = right.get_embedded_integer() try: result = ovfcheck(l - r) return universe.new_integer(result) except OverflowError: return universe.new_biginteger( rbigint.fromint(l).sub(rbigint.fromint(r)))
def fromint(n, d=1): assert isinstance(n, int) assert isinstance(d, int) return W_Rational.frombigint(rbigint.fromint(n), rbigint.fromint(d))
def testComplement(self): bi = BigInt(rbigint.fromint(6)) result = bi.call(u"complement", []) self.assertTrue(result.bi.int_eq(-7))
def testXorInt(self): bi = BigInt(rbigint.fromint(0xcccc)) result = bi.call(u"xor", [IntObject(0xaaaa)]) self.assertTrue(result.bi.int_eq(0x6666))
def testOpCmpBigInt(self): i = IntObject(2) bi = BigInt(rbigint.fromint(6)) result = i.call(u"op__cmp", [bi]) self.assertEqual(result.getInt(), -1)
def wrap_num(n, type): if type == WInt: return wrap_int(n) elif type == WBigInt: return wrap_bigint(rbigint.fromint(n))
def testShiftRight(self): bi = BigInt(rbigint.fromint(42)) result = bi.call(u"shiftRight", [IntObject(2)]) self.assertEqual(result.getInt(), 10)
def testShiftLeftLarge(self): i = IntObject(0x5c5c) result = i.call(u"shiftLeft", [IntObject(64)]) bi = rbigint.fromint(0x5c5c).lshift(64) self.assertTrue(result.bi.eq(bi))
def test_bigint_w(self): space = self.space assert isinstance(space.bigint_w(space.wrap(42)), rbigint) assert space.bigint_w(space.wrap(42)).eq(rbigint.fromint(42))
def bigint_w(self, space, allow_conversion=True): return rbigint.fromint(self.intval)
howmany) res_w = [None] * howmany v = start for idx in range(howmany): res_w[idx] = space.newint(v) v += step return space.newlist(res_w) def range_withspecialized_implementation(space, start, step, length): assert space.config.objspace.std.withliststrategies from pypy.objspace.std.listobject import make_range_list return make_range_list(space, start, step, length) bigint_one = rbigint.fromint(1) def range_with_longs(space, w_start, w_stop, w_step): start = lo = space.bigint_w(w_start) hi = space.bigint_w(w_stop) step = st = space.bigint_w(w_step) if not step.tobool(): raise oefmt(space.w_ValueError, "step argument must not be zero") elif step.sign < 0: lo, hi, st = hi, lo, st.neg() if lo.lt(hi): diff = hi.sub(lo).sub(bigint_one)
def testFloorDivideDouble(self): bi = BigInt(rbigint.fromint(2).pow(rbigint.fromint(65))) result = bi.call(u"floorDivide", [DoubleObject(2.0)]) self.assertTrue( result.bi.eq(rbigint.fromint(2).pow(rbigint.fromint(64))))
def arith_oddp(self): return values.W_Bool.make(self.value.mod(rbigint.fromint(2)).tobool())
def same_numeric_class(self, other): if isinstance(other, values.W_Fixnum): return self, values.W_Bignum(rbigint.fromint(other.value)) if isinstance(other, values.W_Bignum): return self, other return other.same_numeric_class_reversed(self)
def arith_unarysub(self): try: res = rarithmetic.ovfcheck(-self.value) except OverflowError: return values.W_Bignum(rbigint.fromint(self.value).neg()) return values.W_Fixnum(res)
def testBitLength(self): bi = BigInt(rbigint.fromint(42)) result = bi.call(u"bitLength", []) self.assertEqual(result.getInt(), 6)
def testFloorDivideNaN(self): i = BigInt(rbigint.fromint(0)) self.assertRaises(UserException, i.call, u"floorDivide", [i])
def testXorInt(self): bi = BigInt(rbigint.fromint(0xcccc)) result = bi.call(u"xor", [IntObject(0xaaaa)]) self.assertEqual(result.getInt(), 0x6666)
def testApproxDivideDouble(self): bi = BigInt(rbigint.fromint(1)) result = bi.call(u"approxDivide", [DoubleObject(32.0)]) self.assertAlmostEqual(result._d, 1.0 / 32.0)
def testShiftLeftFar(self): i = IntObject(0x1) result = i.call(u"shiftLeft", [IntObject(65)]) bi = rbigint.fromint(0x1).lshift(65) self.assertTrue(result.bi.eq(bi))
def testApproxDivideNaN(self): i = BigInt(rbigint.fromint(0)) result = i.call(u"approxDivide", [i]) self.assertTrue(math.isnan(result.getDouble()))
def testPow(self): i = IntObject(3) result = i.call(u"pow", [IntObject(100)]) self.assertTrue( result.bi.eq(rbigint.fromint(3).pow(rbigint.fromint(100))))
def testApproxDivide(self): bi = BigInt(rbigint.fromint(7937000378463977)) # Hack. bj = BigInt(rbigint.fromint(1000000000000000000).int_mul(10)) result = bi.call(u"approxDivide", [bj]) self.assertAlmostEqual(result._d, 0.0007937000378463977)
def testApproxDivideBigInt(self): i = IntObject(7937000378463977) # Hack. bi = BigInt(rbigint.fromint(1000000000000000000).int_mul(10)) result = i.call(u"approxDivide", [bi]) self.assertAlmostEqual(result._d, 0.0007937000378463977)
def testOpCmpInt(self): bi = BigInt(rbigint.fromint(6)) i = IntObject(2) result = bi.call(u"op__cmp", [i]) self.assertEqual(result.getInt(), 1)
def testBigIntAndIntEquality(self): first = BigInt(rbigint.fromint(42)) second = IntObject(42) self.assertEqual(optSame(first, second), EQUAL)
def testComplement(self): bi = BigInt(rbigint.fromint(6)) result = bi.call(u"complement", []) self.assertEqual(result.getInt(), -7)
def testAndInt(self): bi = BigInt(rbigint.fromint(0x3fffffffffffffff).int_mul(3)) result = bi.call(u"and", [IntObject(0xffff)]) self.assertTrue(result.bi.int_eq(0xfffd))
def testAndInt(self): bi = BigInt(rbigint.fromint(0x3fffffffffffffff).int_mul(3)) result = bi.call(u"and", [IntObject(0xffff)]) self.assertEqual(result.getInt(), 0xfffd)
def asbigint(self): return rbigint.fromint(self.intval)
def fromint(space, intval): return W_LongObject(rbigint.fromint(intval))