本文整理汇总了C++中Fixnum类的典型用法代码示例。如果您正苦于以下问题:C++ Fixnum类的具体用法?C++ Fixnum怎么用?C++ Fixnum使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Fixnum类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: test_sub
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);
}
示例2: test_coerce_bignum
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));
}
示例3: test_sub_overflows_to_bignum
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());
}
示例4: nth_capture
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;
}
示例5: test_sub_underflows_to_bignum
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());
}
示例6: post_matched
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;
}
示例7: if
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;
}
示例8: test_add
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());
}
示例9: test_sub_a_bignum
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));
}
示例10: test_add_a_bignum
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);
}
示例11: test_div_a_bignum
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)));
}
示例12: test_control_tells_current_position
/** @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);
}
示例13: test_divmod_with_a_float
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));
}
示例14: test_div
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)));
}
示例15: test_mul_with_bignum
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));
}