void test_sub() { Fixnum* one = Fixnum::from(1); Fixnum* zero = as<Fixnum>(one->sub(state, one)); TS_ASSERT_EQUALS(0, zero->to_native()); TS_ASSERT_EQUALS(Fixnum::from(0), zero); }
void test_coerce_bignum() { Fixnum* one = Fixnum::from(1); Bignum* e = Bignum::create(state, one); Array* ary = one->coerce(state, e); Fixnum* a = try_as<Fixnum>(ary->get(state, 0)); Fixnum* b = try_as<Fixnum>(ary->get(state, 1)); TS_ASSERT_EQUALS(2, ary->size()); TS_ASSERT(a); TS_ASSERT(b); TS_ASSERT_EQUALS(one, a); TS_ASSERT_EQUALS(one, b); Bignum* f = Bignum::from(state, 9223372036854775807LL); ary = one->coerce(state, f); Bignum* c = try_as<Bignum>(ary->get(state, 0)); Bignum* d = try_as<Bignum>(ary->get(state, 1)); TS_ASSERT_EQUALS(2, ary->size()); TS_ASSERT(c); TS_ASSERT(d); TS_ASSERT_EQUALS(cTrue, c->equal(state, f)); TS_ASSERT_EQUALS(cTrue, d->equal(state, e)); }
void test_sub_overflows_to_bignum() { Fixnum* max = Fixnum::from(FIXNUM_MAX); Integer* max_plus1 = max->sub(state, Fixnum::from(-1)); TS_ASSERT(kind_of<Bignum>(max_plus1)); TS_ASSERT_EQUALS(FIXNUM_MAX+1, max_plus1->to_native()); }
Object* MatchData::nth_capture(STATE, native_int which) { if(region_->num_fields() <= which) return cNil; Tuple* sub = try_as<Tuple>(region_->at(state, which)); if(!sub) return cNil; Fixnum* beg = try_as<Fixnum>(sub->at(state, 0)); Fixnum* fin = try_as<Fixnum>(sub->at(state, 1)); native_int b = beg->to_native(); native_int f = fin->to_native(); native_int max = source_->byte_size(); if(!beg || !fin || f > max || b < 0) { return cNil; } const char* str = (char*)source_->byte_address(); native_int sz = f - b; if(sz > max) sz = max; String* string = String::create(state, str + b, sz); string->encoding(state, source_->encoding()); return string; }
void test_sub_underflows_to_bignum() { Fixnum* min = Fixnum::from(FIXNUM_MIN); Integer* min_minus1 = min->sub(state, Fixnum::from(1)); TS_ASSERT(kind_of<Bignum>(min_minus1)); TS_ASSERT_EQUALS(FIXNUM_MIN-1, min_minus1->to_native()); }
String* MatchData::post_matched(STATE) { Fixnum* fin = try_as<Fixnum>(full_->at(state, 1)); native_int f = fin->to_native(); native_int max = source_->byte_size(); String* string; if(!fin || f >= max) { string = String::create(state, 0, 0); } else { const char* str = (char*)source_->byte_address(); native_int sz = max - f; if(sz > max) sz = max; string = String::create(state, str + f, sz); } source_->infect(state, string); string->encoding_from(state, source_); string->klass(state, source_->class_object(state)); return string; }
bool NativeFunction::ffi_arg_info(STATE, Object* type, FFIArgInfo* args_info) { if(type->fixnum_p()) { args_info->type = as<Fixnum>(type)->to_int(); args_info->enum_obj = NULL; args_info->callback = NULL; return true; } else if(type->symbol_p()) { LookupTable* tbl = try_as<LookupTable>(G(ffi)->get_const(state, "TypeDefs")); if(!tbl) return false; Fixnum* fix = try_as<Fixnum>(tbl->aref(state, type)); if(!fix) return false; args_info->type = fix->to_int(); args_info->enum_obj = NULL; args_info->callback = NULL; return true; } else if(NativeFunction* cb = try_as<NativeFunction>(type)) { args_info->type = RBX_FFI_TYPE_CALLBACK; args_info->enum_obj = NULL; args_info->callback = cb; return true; } else if(CBOOL(type->respond_to(state, state->symbol("[]"), cTrue))) { args_info->type = RBX_FFI_TYPE_ENUM; args_info->enum_obj = type; args_info->callback = NULL; return true; } return false; }
void test_add() { Fixnum* one = Fixnum::from(1); Fixnum* two = as<Fixnum>(one->add(state, one)); TS_ASSERT_EQUALS(Fixnum::from(2), two); TS_ASSERT_EQUALS(2, two->to_native()); }
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))); }
/** @todo Is it a valid assumption that the position always increases? */ void test_control_tells_current_position() { char *dir = make_directory(); String* path = String::create(state, dir); d->open(state, path); Fixnum* pos = (Fixnum*)d->control(state, Fixnum::from(2), Fixnum::from(0)); d->read(state); Fixnum* pos2 = (Fixnum*)d->control(state, Fixnum::from(2), Fixnum::from(0)); TS_ASSERT_LESS_THAN(pos->to_native(), pos2->to_native()); remove_directory(dir); }
void test_divmod_with_a_float() { Fixnum* one = as<Fixnum>(Fixnum::from(15)); Array* ary1 = one->divmod(state, Float::create(state, -3.3)); Object* o1 = ary1->get(state, 0); Object* o2 = ary1->get(state, 1); TS_ASSERT(o1->fixnum_p()); TS_ASSERT_EQUALS(as<Integer>(o1)->to_native(), -5); check_float(as<Float>(o2), Float::create(state, -1.5)); }
void test_div() { Fixnum* one = as<Fixnum>(Fixnum::from(4)); Fixnum* two = as<Fixnum>(one->div(state, one)); TS_ASSERT_EQUALS(two->to_native(), 1); Fixnum* zero = Fixnum::from(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_with_bignum() { Fixnum* one = as<Fixnum>(Fixnum::from(2)); Bignum* two = Bignum::from(state, (native_int)FIXNUM_MAX + 10); Integer* three = one->mul(state, two); TS_ASSERT_EQUALS(three->class_object(state), G(bignum)); Bignum* expected = as<Bignum>(two->mul(state, Fixnum::from(2))); TS_ASSERT_EQUALS(cTrue, as<Bignum>(three)->equal(state, expected)); }
void test_divmod_with_a_bignum() { Fixnum* one = as<Fixnum>(Fixnum::from(15)); Bignum* two = Bignum::from(state, (native_int)4); Array* ary1 = one->divmod(state, two); Object* o1 = ary1->get(state, 0); Object* o2 = ary1->get(state, 1); TS_ASSERT(o1->fixnum_p()); TS_ASSERT_EQUALS(as<Integer>(o1)->to_native(), 3); TS_ASSERT(o2->fixnum_p()); TS_ASSERT_EQUALS(as<Integer>(o2)->to_native(), 3); }
String* MatchData::post_matched(STATE) { Fixnum* fin = try_as<Fixnum>(full_->at(state, 1)); if(!fin || fin->to_native() >= source_->size()) { return String::create(state, 0, 0); } const char* str = source_->c_str(); native_int sz = (native_int)source_->size() - fin->to_native(); return String::create(state, str + fin->to_native(), sz); }
String* MatchData::pre_matched(STATE) { Fixnum* beg = try_as<Fixnum>(full_->at(state, 0)); if(!beg || beg->to_native() <= 0) { return String::create(state, 0, 0); } const char* str = source_->c_str(); native_int sz = beg->to_native(); return String::create(state, str, sz); }
Object* PackedObject::get_packed_ivar(STATE, Symbol* sym) { LookupTable* tbl = this->reference_class()->packed_ivar_info(); assert(tbl && !tbl->nil_p()); Fixnum* which = try_as<Fixnum>(tbl->fetch(state, sym)); if(!which) { return get_table_ivar(state, sym); } Object* obj = body_as_array()[which->to_native()]; if(obj == Qundef) return Qnil; return obj; }
Object* PackedObject::set_packed_ivar(STATE, Symbol* sym, Object* val) { LookupTable* tbl = this->reference_class()->packed_ivar_info(); bool found = false; Fixnum* which = try_as<Fixnum>(tbl->fetch(state, sym, &found)); if(!found) { return set_table_ivar(state, sym, val); } body_as_array()[which->to_native()] = val; if(val->reference_p()) write_barrier(state, val); return val; }
Object* PackedObject::packed_ivar_defined(STATE, Symbol* sym) { LookupTable* tbl = this->reference_class()->packed_ivar_info(); bool found = false; Fixnum* which = try_as<Fixnum>(tbl->fetch(state, sym, &found)); if(!found) { return table_ivar_defined(state, sym); } Object* obj = body_as_array()[which->to_native()]; if(obj == Qundef) return Qfalse; return Qtrue; }
void test_mul_overflows_to_bignum() { Fixnum* half = Fixnum::from((FIXNUM_MAX + 1) / 2); Integer* max_plus1 = half->mul(state, Fixnum::from(2)); TS_ASSERT(kind_of<Bignum>(max_plus1)); TS_ASSERT_EQUALS(FIXNUM_MAX+1, max_plus1->to_native()); Fixnum* neg_half = Fixnum::from((FIXNUM_MIN - 1) / 2); max_plus1 = neg_half->mul(state, Fixnum::from(-2)); TS_ASSERT(kind_of<Bignum>(max_plus1)); TS_ASSERT_EQUALS(FIXNUM_MAX+1, max_plus1->to_native()); }
int Encoding::find_index(STATE, const char* name) { Object* obj = encoding_map(state)->fetch(state, encoding_symbol(state, name)); if(Tuple* ref = try_as<Tuple>(obj)) { Fixnum* index = try_as<Fixnum>(ref->at(1)); if(index) { return index->to_native(); } else { return -1; } } else { return -1; } }
void test_coerce_fixnum() { Fixnum* one = Fixnum::from(1); Fixnum* two = Fixnum::from(2); Array* ary = one->coerce(state, two); Fixnum* a = try_as<Fixnum>(ary->get(state, 0)); Fixnum* b = try_as<Fixnum>(ary->get(state, 1)); TS_ASSERT_EQUALS(2, ary->size()); TS_ASSERT(a); TS_ASSERT(b); TS_ASSERT_EQUALS(two, a); TS_ASSERT_EQUALS(one, b); }
String* MatchData::pre_matched(STATE) { Fixnum* beg = try_as<Fixnum>(full_->at(state, 0)); native_int max = source_->size(); native_int sz = beg->to_native(); if(!beg || sz <= 0) { return String::create(state, 0, 0); } if(sz > max) sz = max; const char* str = (char*)source_->byte_address(); return String::create(state, str, sz); }
Object* PackedObject::packed_ivar_delete(STATE, Symbol* sym, bool* removed) { LookupTable* tbl = this->reference_class()->packed_ivar_info(); bool found = false; Fixnum* which = try_as<Fixnum>(tbl->fetch(state, sym, &found)); if(!found) { return del_table_ivar(state, sym, removed); } if(removed) *removed = true; Object* val = body_as_array()[which->to_native()]; body_as_array()[which->to_native()] = Qundef; return val; }
void test_divmod() { Fixnum* one = as<Fixnum>(Fixnum::from(15)); Fixnum* two = as<Fixnum>(Fixnum::from(4)); Array* ary1 = one->divmod(state, two); Object* o1 = ary1->get(state, 0); Object* o2 = ary1->get(state, 1); TS_ASSERT(o1->fixnum_p()); TS_ASSERT_EQUALS(as<Integer>(o1)->to_native(), 3); TS_ASSERT(o2->fixnum_p()); TS_ASSERT_EQUALS(as<Integer>(o2)->to_native(), 3); Fixnum* zero = Fixnum::from(0); TS_ASSERT_THROWS_ASSERT(one->divmod(state, zero), const RubyException &e, TS_ASSERT(Exception::zero_division_error_p(state, e.exception))); }
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)); }
String* MatchData::post_matched(STATE) { Fixnum* fin = try_as<Fixnum>(full_->at(state, 1)); native_int f = fin->to_native(); native_int max = source_->size(); if(!fin || f >= max) { return String::create(state, 0, 0); } const char* str = (char*)source_->byte_address(); native_int sz = max - f; if(sz > max) sz = max; return String::create(state, str + f, sz); }
String* MatchData::matched_string(STATE) { Fixnum* beg = try_as<Fixnum>(full_->at(state, 0)); Fixnum* fin = try_as<Fixnum>(full_->at(state, 1)); native_int max = source_->byte_size(); native_int f = fin->to_native(); native_int b = beg->to_native(); String* string; if(!beg || !fin || f > max || b > max || b < 0) { string = String::create(state, 0, 0); } else { const char* str = (char*)source_->byte_address(); native_int sz = fin->to_native() - beg->to_native(); string = String::create(state, str + beg->to_native(), sz); } source_->infect(state, string); string->encoding_from(state, source_); string->klass(state, source_->class_object(state)); return string; }