diff options
Diffstat (limited to 'spec/ruby/core/float/comparison_spec.rb')
| -rw-r--r-- | spec/ruby/core/float/comparison_spec.rb | 93 |
1 files changed, 69 insertions, 24 deletions
diff --git a/spec/ruby/core/float/comparison_spec.rb b/spec/ruby/core/float/comparison_spec.rb index 2dc993a176..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 @@ -33,36 +52,62 @@ describe "Float#<=>" do coercible.call_count.should == 3 end - ruby_version_is "2.5" do - it "raises TypeError when #coerce misbehaves" do - klass = Class.new do - def coerce(other) - :incorrect - end + it "raises TypeError when #coerce misbehaves" do + klass = Class.new do + def coerce(other) + :incorrect end - - bad_coercible = klass.new - -> { - 4.2 <=> bad_coercible - }.should raise_error(TypeError, "coerce must return [x, y]") end + + bad_coercible = klass.new + -> { + 4.2 <=> bad_coercible + }.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 a Bignum" 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 |
