diff options
Diffstat (limited to 'spec/ruby/optional/capi/fixnum_spec.rb')
| -rw-r--r-- | spec/ruby/optional/capi/fixnum_spec.rb | 113 |
1 files changed, 45 insertions, 68 deletions
diff --git a/spec/ruby/optional/capi/fixnum_spec.rb b/spec/ruby/optional/capi/fixnum_spec.rb index d9e9d1946d..73751c4895 100644 --- a/spec/ruby/optional/capi/fixnum_spec.rb +++ b/spec/ruby/optional/capi/fixnum_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../spec_helper', __FILE__) +require_relative 'spec_helper' load_extension("fixnum") @@ -8,14 +8,16 @@ describe "CApiFixnumSpecs" do end describe "FIX2INT" do + max_int = (1 << 31) - 1 + min_int = -(1 << 31) + it "converts a Fixnum to a native int" do @s.FIX2INT(42).should == 42 @s.FIX2INT(-14).should == -14 + @s.FIX2INT(-1).should == -1 + @s.FIX2INT(1).should == 1 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 @@ -23,101 +25,76 @@ describe "CApiFixnumSpecs" do 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 + platform_is c_long_size: 64 do # sizeof(long) > sizeof(int) it "raises a TypeError if passed nil" do - lambda { @s.rb_fix2uint(nil) }.should raise_error(TypeError) - end - - it "converts a Fixnum" do - @s.rb_fix2uint(0).should == 0 - @s.rb_fix2uint(1).should == 1 + -> { @s.FIX2INT(nil) }.should.raise(TypeError) end - it "converts the maximum uint value" do - @s.rb_fix2uint(0xffff_ffff).should == 0xffff_ffff + it "converts a Float" do + @s.FIX2INT(25.4567).should == 25 end - it "converts a Float" do - @s.rb_fix2uint(25.4567).should == 25 + it "converts a negative Bignum into an signed number" do + @s.FIX2INT(-2147442171).should == -2147442171 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) + it "raises a RangeError if the value does not fit a native int" do + -> { @s.FIX2INT(0x7fff_ffff+1) }.should.raise(RangeError) + -> { @s.FIX2INT(-(1 << 31) - 1) }.should.raise(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) + -> { @s.FIX2INT(0xffff_ffff+1) }.should.raise(RangeError) end it "raises a RangeError if the value is more than 64bits" do - lambda { @s.rb_fix2uint(0xffff_ffff_ffff_ffff+1) }.should raise_error(RangeError) + -> { @s.FIX2INT(0xffff_ffff_ffff_ffff+1) }.should.raise(RangeError) end - end - describe "rb_fix2int" do - it "raises a TypeError if passed nil" do - lambda { @s.rb_fix2int(nil) }.should raise_error(TypeError) + it "calls #to_int to coerce the value" do + obj = mock("number") + obj.should_receive(:to_int).and_return(2) + @s.FIX2INT(obj).should == 2 end + end + end - it "converts a Fixnum" do - @s.rb_fix2int(-1).should == -1 - @s.rb_fix2int(1).should == 1 - end + describe "FIX2UINT" do + max_uint = (1 << 32) - 1 - it "converts the minimum int value" do - @s.rb_fix2int(-(1 << 31)).should == -(1 << 31) - end + it "converts a Fixnum" do + @s.FIX2UINT(0).should == 0 + @s.FIX2UINT(1).should == 1 + @s.FIX2UINT(42).should == 42 + end - it "converts the maximum int value" do - @s.rb_fix2int(0x7fff_ffff).should == 0x7fff_ffff + 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 - it "converts a Float" do - @s.rb_fix2int(25.4567).should == 25 + platform_is c_long_size: 64 do # sizeof(long) > sizeof(int) + it "raises a TypeError if passed nil" do + -> { @s.FIX2UINT(nil) }.should.raise(TypeError) end - it "converts a negative Bignum into an signed number" do - @s.rb_fix2int(-2147442171).should == -2147442171 + it "converts a Float" do + @s.FIX2UINT(25.4567).should == 25 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) + it "raises a RangeError if the value does not fit a native uint" do + # Interestingly, on MRI FIX2UINT(-1) is allowed + -> { @s.FIX2UINT(0xffff_ffff+1) }.should.raise(RangeError) + -> { @s.FIX2UINT(-(1 << 31) - 1) }.should.raise(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) + -> { @s.FIX2UINT(0xffff_ffff+1) }.should.raise(RangeError) end it "raises a RangeError if the value is more than 64bits" do - lambda { @s.rb_fix2int(0xffff_ffff_ffff_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_fix2int(obj).should == 2 + -> { @s.FIX2UINT(0xffff_ffff_ffff_ffff+1) }.should.raise(RangeError) end end end |
