diff options
Diffstat (limited to 'spec/ruby/core/float/comparison_spec.rb')
| -rw-r--r-- | spec/ruby/core/float/comparison_spec.rb | 75 |
1 files changed, 61 insertions, 14 deletions
diff --git a/spec/ruby/core/float/comparison_spec.rb b/spec/ruby/core/float/comparison_spec.rb index cbab3a0fa1..e9adf2fd6a 100644 --- a/spec/ruby/core/float/comparison_spec.rb +++ b/spec/ruby/core/float/comparison_spec.rb @@ -7,13 +7,32 @@ describe "Float#<=>" do ((bignum_value*1.1) <=> bignum_value).should == 1 end - it "returns nil when either argument is NaN" do - (nan_value <=> 71.2).should be_nil - (1771.176 <=> nan_value).should be_nil + it "returns nil if one side is NaN" do + [1.0, 42, bignum_value].each { |n| + (nan_value <=> n).should == nil + (n <=> nan_value).should == nil + } + end + + it "handles positive infinity" do + [1.0, 42, bignum_value].each { |n| + (infinity_value <=> n).should == 1 + (n <=> infinity_value).should == -1 + } + end + + it "handles negative infinity" do + [1.0, 42, bignum_value].each { |n| + (-infinity_value <=> n).should == -1 + (n <=> -infinity_value).should == 1 + } end it "returns nil when the given argument is not a Float" do - (1.0 <=> "1").should be_nil + (1.0 <=> "1").should == nil + (1.0 <=> "1".freeze).should == nil + (1.0 <=> :one).should == nil + (1.0 <=> true).should == nil end it "compares using #coerce when argument is not a Float" do @@ -43,24 +62,52 @@ describe "Float#<=>" do bad_coercible = klass.new -> { 4.2 <=> bad_coercible - }.should raise_error(TypeError, "coerce must return [x, y]") + }.should.raise(TypeError, "coerce must return [x, y]") end - # The 4 tests below are taken from matz's revision 23730 for Ruby trunk - # - it "returns 1 when self is Infinity and other is an Integer" do + it "returns the correct result when one side is infinite" do (infinity_value <=> Float::MAX.to_i*2).should == 1 + (-Float::MAX.to_i*2 <=> infinity_value).should == -1 + (-infinity_value <=> -Float::MAX.to_i*2).should == -1 + (-Float::MAX.to_i*2 <=> -infinity_value).should == 1 end - it "returns -1 when self is negative and other is Infinity" do - (-Float::MAX.to_i*2 <=> infinity_value).should == -1 + it "returns 0 when self is Infinity and other is infinite?=1" do + obj = Object.new + def obj.infinite? + 1 + end + (infinity_value <=> obj).should == 0 end - it "returns -1 when self is -Infinity and other is negative" do - (-infinity_value <=> -Float::MAX.to_i*2).should == -1 + it "returns 1 when self is Infinity and other is infinite?=-1" do + obj = Object.new + def obj.infinite? + -1 + end + (infinity_value <=> obj).should == 1 end - it "returns 1 when self is negative and other is -Infinity" do - (-Float::MAX.to_i*2 <=> -infinity_value).should == 1 + it "returns 1 when self is Infinity and other is infinite?=nil (which means finite)" do + obj = Object.new + def obj.infinite? + nil + end + (infinity_value <=> obj).should == 1 + end + + it "returns 0 for -0.0 and 0.0" do + (-0.0 <=> 0.0).should == 0 + (0.0 <=> -0.0).should == 0 + end + + it "returns 0 for -0.0 and 0" do + (-0.0 <=> 0).should == 0 + (0 <=> -0.0).should == 0 + end + + it "returns 0 for 0.0 and 0" do + (0.0 <=> 0).should == 0 + (0 <=> 0.0).should == 0 end end |
