def test_create_int(): a = wrap_int(0) b = wrap_int(1) c = wrap_int(1) assert RT._equiv.invoke2(a, b) is false assert RT._equiv.invoke2(b, c) is true
def test_large_vector(): r = EMPTY_VEC for x in range(1024 * 2): r = RT.conj.invoke2(r, wrap_int(x)) c = RT.count.invoke1(r) assert c._int_value == x + 1 assert r for x in range(1024 * 2): v = RT.nth.invoke2(r, wrap_int(x)) assert RT._equiv.invoke2(v, wrap_int(x)) is true
def test_binop_overflow(): v1 = wrap_int(maxint - 1) v2 = wrap_int(2) def assert_overflows(f): try: f.invoke2(v1, v2) assert False, "Should overflow" except OverflowError: pass 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) assert_overflows(RT._add) assert_overflows(RT._multiply) assert_converts_to_bigint(RT._addP, maxint+1) assert_converts_to_bigint(RT._multiplyP, (maxint-1)*2)
def test_unary_overflow(): pos = wrap_int(maxint) neg = wrap_int(-maxint-1) def assert_overflows(f, x): try: f.invoke1(x) assert False, "Should overflow" except OverflowError: pass 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) assert_overflows(RT._negate, neg) assert_overflows(RT._inc, pos) assert_overflows(RT._dec, neg) assert_converts_to_bigint(RT._negateP, neg, maxint+1) assert_converts_to_bigint(RT._incP, pos, maxint+1) assert_converts_to_bigint(RT._decP, neg, -maxint-2)
def wrap_num(n, type): if type == WInt: return wrap_int(n) elif type == WBigInt: return wrap_bigint(rbigint.fromint(n))
def rt_hash(self): return wrap_int(42)