aboutsummaryrefslogtreecommitdiffstats
path: root/spec/rubyspec/optional/capi/fixnum_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/rubyspec/optional/capi/fixnum_spec.rb')
-rw-r--r--spec/rubyspec/optional/capi/fixnum_spec.rb52
1 files changed, 51 insertions, 1 deletions
diff --git a/spec/rubyspec/optional/capi/fixnum_spec.rb b/spec/rubyspec/optional/capi/fixnum_spec.rb
index 87f257fec7..d9e9d1946d 100644
--- a/spec/rubyspec/optional/capi/fixnum_spec.rb
+++ b/spec/rubyspec/optional/capi/fixnum_spec.rb
@@ -7,6 +7,40 @@ describe "CApiFixnumSpecs" do
@s = CApiFixnumSpecs.new
end
+ describe "FIX2INT" do
+ it "converts a Fixnum to a native int" do
+ @s.FIX2INT(42).should == 42
+ @s.FIX2INT(-14).should == -14
+ end
+
+ max_int = (1 << 31) - 1
+ min_int = -(1 << 31)
+
+ guard -> { fixnum_min <= min_int and max_int <= fixnum_max } do
+ it "converts a Fixnum representing the minimum and maximum native int" do
+ @s.FIX2INT(max_int).should == max_int
+ @s.FIX2INT(min_int).should == min_int
+ end
+ end
+
+ end
+
+ describe "FIX2UINT" do
+ it "converts a Fixnum to a native int" do
+ @s.FIX2UINT(42).should == 42
+ @s.FIX2UINT(0).should == 0
+ end
+
+ max_uint = (1 << 32) - 1
+
+ guard -> { max_uint <= fixnum_max } do
+ it "converts a Fixnum representing the maximum native uint" do
+ @s.FIX2UINT(max_uint).should == max_uint
+ end
+ end
+
+ end
+
platform_is wordsize: 64 do
describe "rb_fix2uint" do
it "raises a TypeError if passed nil" do
@@ -14,6 +48,7 @@ describe "CApiFixnumSpecs" do
end
it "converts a Fixnum" do
+ @s.rb_fix2uint(0).should == 0
@s.rb_fix2uint(1).should == 1
end
@@ -25,6 +60,12 @@ describe "CApiFixnumSpecs" do
@s.rb_fix2uint(25.4567).should == 25
end
+ it "raises a RangeError if the value does not fit a native uint" do
+ # Interestingly, on MRI rb_fix2uint(-1) is allowed
+ lambda { @s.rb_fix2uint(0xffff_ffff+1) }.should raise_error(RangeError)
+ lambda { @s.rb_fix2uint(-(1 << 31) - 1) }.should raise_error(RangeError)
+ end
+
it "raises a RangeError if the value is more than 32bits" do
lambda { @s.rb_fix2uint(0xffff_ffff+1) }.should raise_error(RangeError)
end
@@ -44,7 +85,11 @@ describe "CApiFixnumSpecs" do
@s.rb_fix2int(1).should == 1
end
- it "converts the maximum uint value" do
+ it "converts the minimum int value" do
+ @s.rb_fix2int(-(1 << 31)).should == -(1 << 31)
+ end
+
+ it "converts the maximum int value" do
@s.rb_fix2int(0x7fff_ffff).should == 0x7fff_ffff
end
@@ -56,6 +101,11 @@ describe "CApiFixnumSpecs" do
@s.rb_fix2int(-2147442171).should == -2147442171
end
+ it "raises a RangeError if the value does not fit a native int" do
+ lambda { @s.rb_fix2int(0x7fff_ffff+1) }.should raise_error(RangeError)
+ lambda { @s.rb_fix2int(-(1 << 31) - 1) }.should raise_error(RangeError)
+ end
+
it "raises a RangeError if the value is more than 32bits" do
lambda { @s.rb_fix2int(0xffff_ffff+1) }.should raise_error(RangeError)
end