void test_right_shift_negative_bits_overflows_to_bignum() { Integer* fix = Fixnum::from(1)->right_shift(state, Fixnum::from(-(FIXNUM_MAX_WIDTH-1))); TS_ASSERT(kind_of<Fixnum>(fix)); TS_ASSERT_EQUALS(Fixnum::from(1L << (FIXNUM_MAX_WIDTH-1)), fix); fix = Fixnum::from(-1)->right_shift(state, Fixnum::from(-(FIXNUM_MIN_WIDTH-1))); TS_ASSERT(kind_of<Fixnum>(fix)); TS_ASSERT_EQUALS(FIXNUM_MIN, fix->to_native()); Integer* max_plus1 = Fixnum::from(1)->right_shift(state, Fixnum::from(-FIXNUM_MAX_WIDTH)); TS_ASSERT(kind_of<Bignum>(max_plus1)); TS_ASSERT_EQUALS(FIXNUM_MAX + 1, max_plus1->to_native()); Integer* min = Fixnum::from(-1)->right_shift(state, Fixnum::from(-FIXNUM_MAX_WIDTH)); TS_ASSERT(kind_of<Fixnum>(min)); TS_ASSERT_EQUALS(FIXNUM_MIN, min->to_native()); Integer* min_minus1 = Bignum::from(state, min->to_native())->sub(state, Fixnum::from(1)); TS_ASSERT(kind_of<Bignum>(min_minus1)); TS_ASSERT_EQUALS(FIXNUM_MIN - 1, min_minus1->to_native()); }
void test_from_unsigned_long() { Integer* obj = Integer::from(state, (unsigned long)13); TS_ASSERT_EQUALS(obj->to_native(), (native_int) 13); obj = Integer::from(state, (unsigned long)2147483647); TS_ASSERT_EQUALS(obj->to_native(), (native_int)2147483647); }
void test_from_unsigned_long_long() { Integer* obj = Integer::from(state, (unsigned long long)13); TS_ASSERT_EQUALS(obj->to_native(), (native_int) 13); obj = Integer::from(state, (unsigned long long)-13); TS_ASSERT_EQUALS(obj->to_native(), (native_int)-13); obj = Integer::from(state, 9223372036854775808ULL); TS_ASSERT_EQUALS(as<Bignum>(obj)->to_ulong_long(), 9223372036854775808ULL); }
void test_from_int() { Integer* obj = Integer::from(state, (int)13); TS_ASSERT_EQUALS(obj->to_native(), (native_int) 13); obj = Integer::from(state, (int)-13); TS_ASSERT_EQUALS(obj->to_native(), (native_int)-13); obj = Integer::from(state, (int)2147483647); TS_ASSERT_EQUALS(obj->to_native(), (native_int)2147483647); }
void test_neg() { Fixnum* min = Fixnum::from(FIXNUM_MIN); Integer* b = min->neg(state); TS_ASSERT(kind_of<Bignum>(b)); TS_ASSERT_EQUALS(FIXNUM_MAX + 1, b->to_native()); Fixnum* max = Fixnum::from(FIXNUM_MAX); b = max->neg(state); TS_ASSERT(kind_of<Fixnum>(b)); TS_ASSERT_EQUALS(FIXNUM_MIN + 1, b->to_native()); TS_ASSERT_EQUALS(Fixnum::from(3)->neg(state), Fixnum::from(-3)); TS_ASSERT_EQUALS(Fixnum::from(-3)->neg(state), Fixnum::from(3)); }
void test_sub_a_bignum() { Fixnum* one = as<Fixnum>(Fixnum::from(13)); Bignum* obj = Bignum::from(state, (native_int)FIXNUM_MAX + 28); Integer* res = one->sub(state, obj); TS_ASSERT(kind_of<Bignum>(res)); TS_ASSERT_EQUALS(res->to_native(), 13 - (FIXNUM_MAX + 28)); }
void test_add_a_bignum() { Fixnum* one = Fixnum::from(13); Bignum* obj = Bignum::from(state, (native_int)FIXNUM_MAX - 10); Integer* res = one->add(state, obj); TS_ASSERT(kind_of<Bignum>(res)); TS_ASSERT_EQUALS(res->to_native(), FIXNUM_MAX + 3); }
void test_div_a_bignum() { Fixnum* one = Fixnum::from(13); Integer* res = one->div(state, Bignum::from(state, (native_int)FIXNUM_MAX + 10)); TS_ASSERT_EQUALS(res->to_native(), 0); Bignum* zero = Bignum::from(state, (native_int)0); TS_ASSERT_THROWS_ASSERT(one->div(state, zero), const RubyException &e, TS_ASSERT(Exception::zero_division_error_p(state, e.exception))); }
void test_mul_underflows_to_bignum() { Fixnum* half = Fixnum::from((FIXNUM_MAX + 1) / 2); Integer* min = half->mul(state, Fixnum::from(-2)); TS_ASSERT(kind_of<Fixnum>(min)); TS_ASSERT_EQUALS(FIXNUM_MIN, min->to_native()); Integer* min_minus1 = Bignum::from(state, min->to_native())->sub(state, Fixnum::from(1)); TS_ASSERT(kind_of<Bignum>(min_minus1)); TS_ASSERT_EQUALS(FIXNUM_MIN - 1, min_minus1->to_native()); Fixnum* neg_half = as<Fixnum>(Bignum::from(state, FIXNUM_MIN - 1)->div(state, Fixnum::from(2))); Integer* min_minus2 = neg_half->mul(state, Fixnum::from(2)); TS_ASSERT(kind_of<Bignum>(min_minus2)); TS_ASSERT_EQUALS(FIXNUM_MIN - 2, min_minus2->to_native()); }
void test_delete_inplace() { Tuple *tuple = new_tuple(); tuple->put(state, 1, Qnil); Integer *count = tuple->delete_inplace(state, Fixnum::from(0), Fixnum::from(3), Qnil); TS_ASSERT_EQUALS(1, count->to_native()); TS_ASSERT_EQUALS(Fixnum::from(1), as<Fixnum>(tuple->at(state, 0))); TS_ASSERT_EQUALS(Fixnum::from(9), as<Fixnum>(tuple->at(state, 1))); TS_ASSERT_EQUALS(Qnil, tuple->at(state, 2)); }
void test_mul() { Fixnum* third = Fixnum::from(FIXNUM_MAX / 3); Integer* a = third->mul(state, Fixnum::from(3)); TS_ASSERT(kind_of<Fixnum>(a)); TS_ASSERT_EQUALS(FIXNUM_MAX, a->to_native()); Integer* min = third->mul(state, Fixnum::from(-3)); TS_ASSERT(kind_of<Fixnum>(min)); TS_ASSERT_EQUALS(FIXNUM_MIN + 1, min->to_native()); Fixnum* neg_third = as<Fixnum>(Fixnum::from((FIXNUM_MIN))->div(state, Fixnum::from(3))); a = neg_third->mul(state, Fixnum::from(-3)); TS_ASSERT(kind_of<Bignum>(a)); TS_ASSERT_EQUALS(-FIXNUM_MIN + 2, a->to_native()); a = neg_third->mul(state, Fixnum::from(3)); TS_ASSERT(kind_of<Bignum>(a)); TS_ASSERT_EQUALS(FIXNUM_MIN - 2, a->to_native()); }