Skip to content

Commit

Permalink
Add specs for rb_num2(u)int
Browse files Browse the repository at this point in the history
  • Loading branch information
dbussink committed Sep 5, 2013
1 parent 953a85f commit 74ca3df
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
20 changes: 20 additions & 0 deletions spec/ruby/optional/capi/ext/numeric_spec.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,24 @@ static VALUE numeric_spec_rb_num2dbl(VALUE self, VALUE num) {
}
#endif

#ifdef HAVE_RB_NUM2LONG
static VALUE numeric_spec_rb_num2int(VALUE self, VALUE num) {
return LONG2NUM(rb_num2int(num));
}
#endif

#ifdef HAVE_RB_NUM2LONG
static VALUE numeric_spec_rb_num2long(VALUE self, VALUE num) {
return LONG2NUM(rb_num2long(num));
}
#endif

#ifdef HAVE_RB_NUM2UINT
static VALUE numeric_spec_rb_num2uint(VALUE self, VALUE num) {
return ULONG2NUM(rb_num2uint(num));
}
#endif

#ifdef HAVE_RB_NUM2ULONG
static VALUE numeric_spec_rb_num2ulong(VALUE self, VALUE num) {
return ULONG2NUM(rb_num2ulong(num));
Expand Down Expand Up @@ -102,10 +114,18 @@ void Init_numeric_spec() {
rb_define_method(cls, "rb_num2dbl", numeric_spec_rb_num2dbl, 1);
#endif

#ifdef HAVE_RB_NUM2INT
rb_define_method(cls, "rb_num2int", numeric_spec_rb_num2int, 1);
#endif

#ifdef HAVE_RB_NUM2LONG
rb_define_method(cls, "rb_num2long", numeric_spec_rb_num2long, 1);
#endif

#ifdef HAVE_RB_NUM2UINT
rb_define_method(cls, "rb_num2uint", numeric_spec_rb_num2uint, 1);
#endif

#ifdef HAVE_RB_NUM2ULONG
rb_define_method(cls, "rb_num2ulong", numeric_spec_rb_num2ulong, 1);
#endif
Expand Down
2 changes: 2 additions & 0 deletions spec/ruby/optional/capi/ext/rubyspec.h
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,9 @@
#define HAVE_RB_INTEGER 1
#define HAVE_RB_LL2INUM 1
#define HAVE_RB_NUM2DBL 1
#define HAVE_RB_NUM2INT 1
#define HAVE_RB_NUM2LONG 1
#define HAVE_RB_NUM2UINT 1
#define HAVE_RB_NUM2ULONG 1
#define HAVE_RB_NUM_COERCE_BIN 1
#define HAVE_RB_NUM_COERCE_CMP 1
Expand Down
70 changes: 70 additions & 0 deletions spec/ruby/optional/capi/numeric_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,76 @@
@s = CApiNumericSpecs.new
end

describe "rb_num2int" do
it "raises a TypeError if passed nil" do
lambda { @s.rb_num2int(nil) }.should raise_error(TypeError)
end

it "converts a Float" do
@s.rb_num2int(4.2).should == 4
end

it "converts a Bignum" do
@s.rb_num2int(0x7fff_ffff).should == 0x7fff_ffff
end

it "converts a Fixnum" do
@s.rb_num2int(5).should == 5
end

it "converts -1 to an signed number" do
@s.rb_num2int(-1).should == -1
end

it "converts a negative Bignum into an signed number" do
@s.rb_num2int(-2147442171).should == -2147442171
end

it "raises a RangeError if the value is more than 32bits" do
lambda { @s.rb_num2int(0xffff_ffff+1) }.should raise_error(RangeError)
end

it "calls #to_int to coerce the value" do
obj = mock("number")
obj.should_receive(:to_int).and_return(2)
@s.rb_num2long(obj).should == 2
end
end

describe "rb_num2uint" do
it "raises a TypeError if passed nil" do
lambda { @s.rb_num2uint(nil) }.should raise_error(TypeError)
end

it "converts a Float" do
@s.rb_num2uint(4.2).should == 4
end

it "converts a Bignum" do
@s.rb_num2uint(0xffff_ffff).should == 0xffff_ffff
end

it "converts a Fixnum" do
@s.rb_num2uint(5).should == 5
end

it "raises a RangeError if the value is more than 32bits" do
lambda { @s.rb_num2uint(0xffff_ffff+1) }.should raise_error(RangeError)
end

it "raises a RangeError if the value is more than 64bits" do
lambda do
@s.rb_num2uint(0xffff_ffff_ffff_ffff+1)
end.should raise_error(RangeError)
end

it "calls #to_int to coerce the value" do
obj = mock("number")
obj.should_receive(:to_int).and_return(2)
@s.rb_num2ulong(obj).should == 2
end
end

describe "rb_num2long" do
it "raises a TypeError if passed nil" do
lambda { @s.rb_num2long(nil) }.should raise_error(TypeError)
Expand Down

0 comments on commit 74ca3df

Please sign in to comment.