diff options
Diffstat (limited to 'spec/ruby/core/float')
-rw-r--r-- | spec/ruby/core/float/comparison_spec.rb | 35 | ||||
-rw-r--r-- | spec/ruby/core/float/divide_spec.rb | 4 | ||||
-rw-r--r-- | spec/ruby/core/float/divmod_spec.rb | 2 | ||||
-rw-r--r-- | spec/ruby/core/float/gt_spec.rb | 21 | ||||
-rw-r--r-- | spec/ruby/core/float/gte_spec.rb | 21 | ||||
-rw-r--r-- | spec/ruby/core/float/lt_spec.rb | 21 | ||||
-rw-r--r-- | spec/ruby/core/float/lte_spec.rb | 21 | ||||
-rw-r--r-- | spec/ruby/core/float/magnitude_spec.rb | 1 | ||||
-rw-r--r-- | spec/ruby/core/float/round_spec.rb | 64 | ||||
-rw-r--r-- | spec/ruby/core/float/shared/equal.rb | 21 | ||||
-rw-r--r-- | spec/ruby/core/float/shared/to_i.rb | 4 |
11 files changed, 199 insertions, 16 deletions
diff --git a/spec/ruby/core/float/comparison_spec.rb b/spec/ruby/core/float/comparison_spec.rb index 53e7ec332a..1373b3a1fb 100644 --- a/spec/ruby/core/float/comparison_spec.rb +++ b/spec/ruby/core/float/comparison_spec.rb @@ -7,9 +7,25 @@ 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 @@ -49,21 +65,10 @@ describe "Float#<=>" do }.should raise_error(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 - end - - it "returns -1 when self is negative and other is Infinity" do (-Float::MAX.to_i*2 <=> infinity_value).should == -1 - end - - it "returns -1 when self is -Infinity and other is negative" do (-infinity_value <=> -Float::MAX.to_i*2).should == -1 - end - - it "returns 1 when self is negative and other is -Infinity" do (-Float::MAX.to_i*2 <=> -infinity_value).should == 1 end diff --git a/spec/ruby/core/float/divide_spec.rb b/spec/ruby/core/float/divide_spec.rb index d8f71a6b98..72ab7527bd 100644 --- a/spec/ruby/core/float/divide_spec.rb +++ b/spec/ruby/core/float/divide_spec.rb @@ -36,4 +36,8 @@ describe "Float#/" do -> { 13.0 / "10" }.should raise_error(TypeError) -> { 13.0 / :symbol }.should raise_error(TypeError) end + + it "divides correctly by Rational numbers" do + (1.2345678901234567 / Rational(1, 10000000000000000000)).should == 1.2345678901234567e+19 + end end diff --git a/spec/ruby/core/float/divmod_spec.rb b/spec/ruby/core/float/divmod_spec.rb index 523217ac1f..dad45a9b89 100644 --- a/spec/ruby/core/float/divmod_spec.rb +++ b/spec/ruby/core/float/divmod_spec.rb @@ -23,7 +23,7 @@ describe "Float#divmod" do # Behaviour established as correct in r23953 it "raises a FloatDomainError if other is NaN" do - -> { 1.divmod(nan_value) }.should raise_error(FloatDomainError) + -> { 1.0.divmod(nan_value) }.should raise_error(FloatDomainError) end # Behaviour established as correct in r23953 diff --git a/spec/ruby/core/float/gt_spec.rb b/spec/ruby/core/float/gt_spec.rb index 0d73f1c3df..33078e07ce 100644 --- a/spec/ruby/core/float/gt_spec.rb +++ b/spec/ruby/core/float/gt_spec.rb @@ -14,4 +14,25 @@ describe "Float#>" do -> { 5.0 > "4" }.should raise_error(ArgumentError) -> { 5.0 > mock('x') }.should raise_error(ArgumentError) end + + it "returns false if one side is NaN" do + [1.0, 42, bignum_value].each { |n| + (nan_value > n).should == false + (n > nan_value).should == false + } + end + + it "handles positive infinity" do + [1.0, 42, bignum_value].each { |n| + (infinity_value > n).should == true + (n > infinity_value).should == false + } + end + + it "handles negative infinity" do + [1.0, 42, bignum_value].each { |n| + (-infinity_value > n).should == false + (n > -infinity_value).should == true + } + end end diff --git a/spec/ruby/core/float/gte_spec.rb b/spec/ruby/core/float/gte_spec.rb index 98ec60b70b..44c0a81b43 100644 --- a/spec/ruby/core/float/gte_spec.rb +++ b/spec/ruby/core/float/gte_spec.rb @@ -14,4 +14,25 @@ describe "Float#>=" do -> { 5.0 >= "4" }.should raise_error(ArgumentError) -> { 5.0 >= mock('x') }.should raise_error(ArgumentError) end + + it "returns false if one side is NaN" do + [1.0, 42, bignum_value].each { |n| + (nan_value >= n).should == false + (n >= nan_value).should == false + } + end + + it "handles positive infinity" do + [1.0, 42, bignum_value].each { |n| + (infinity_value >= n).should == true + (n >= infinity_value).should == false + } + end + + it "handles negative infinity" do + [1.0, 42, bignum_value].each { |n| + (-infinity_value >= n).should == false + (n >= -infinity_value).should == true + } + end end diff --git a/spec/ruby/core/float/lt_spec.rb b/spec/ruby/core/float/lt_spec.rb index c01b6e0e02..94dcfc42f8 100644 --- a/spec/ruby/core/float/lt_spec.rb +++ b/spec/ruby/core/float/lt_spec.rb @@ -14,4 +14,25 @@ describe "Float#<" do -> { 5.0 < "4" }.should raise_error(ArgumentError) -> { 5.0 < mock('x') }.should raise_error(ArgumentError) end + + it "returns false if one side is NaN" do + [1.0, 42, bignum_value].each { |n| + (nan_value < n).should == false + (n < nan_value).should == false + } + end + + it "handles positive infinity" do + [1.0, 42, bignum_value].each { |n| + (infinity_value < n).should == false + (n < infinity_value).should == true + } + end + + it "handles negative infinity" do + [1.0, 42, bignum_value].each { |n| + (-infinity_value < n).should == true + (n < -infinity_value).should == false + } + end end diff --git a/spec/ruby/core/float/lte_spec.rb b/spec/ruby/core/float/lte_spec.rb index 66f2ddc2c7..7b5a86ee76 100644 --- a/spec/ruby/core/float/lte_spec.rb +++ b/spec/ruby/core/float/lte_spec.rb @@ -15,4 +15,25 @@ describe "Float#<=" do -> { 5.0 <= "4" }.should raise_error(ArgumentError) -> { 5.0 <= mock('x') }.should raise_error(ArgumentError) end + + it "returns false if one side is NaN" do + [1.0, 42, bignum_value].each { |n| + (nan_value <= n).should == false + (n <= nan_value).should == false + } + end + + it "handles positive infinity" do + [1.0, 42, bignum_value].each { |n| + (infinity_value <= n).should == false + (n <= infinity_value).should == true + } + end + + it "handles negative infinity" do + [1.0, 42, bignum_value].each { |n| + (-infinity_value <= n).should == true + (n <= -infinity_value).should == false + } + end end diff --git a/spec/ruby/core/float/magnitude_spec.rb b/spec/ruby/core/float/magnitude_spec.rb index db577c15c5..7cdd8ef28a 100644 --- a/spec/ruby/core/float/magnitude_spec.rb +++ b/spec/ruby/core/float/magnitude_spec.rb @@ -1,3 +1,4 @@ +require_relative "../../spec_helper" require_relative 'shared/abs' describe "Float#magnitude" do diff --git a/spec/ruby/core/float/round_spec.rb b/spec/ruby/core/float/round_spec.rb index 4bd2dc460c..9b4c307f9d 100644 --- a/spec/ruby/core/float/round_spec.rb +++ b/spec/ruby/core/float/round_spec.rb @@ -103,6 +103,70 @@ describe "Float#round" do 5.55.round(1, half: :up).should eql(5.6) 5.55.round(1, half: :down).should eql(5.5) 5.55.round(1, half: :even).should eql(5.6) + -5.55.round(1, half: nil).should eql(-5.6) + -5.55.round(1, half: :up).should eql(-5.6) + -5.55.round(1, half: :down).should eql(-5.5) + -5.55.round(1, half: :even).should eql(-5.6) + end + + it "preserves cases where neighbouring floating pointer number increase the decimal places" do + 4.8100000000000005.round(5, half: nil).should eql(4.81) + 4.8100000000000005.round(5, half: :up).should eql(4.81) + 4.8100000000000005.round(5, half: :down).should eql(4.81) + 4.8100000000000005.round(5, half: :even).should eql(4.81) + -4.8100000000000005.round(5, half: nil).should eql(-4.81) + -4.8100000000000005.round(5, half: :up).should eql(-4.81) + -4.8100000000000005.round(5, half: :down).should eql(-4.81) + -4.8100000000000005.round(5, half: :even).should eql(-4.81) + 4.81.round(5, half: nil).should eql(4.81) + 4.81.round(5, half: :up).should eql(4.81) + 4.81.round(5, half: :down).should eql(4.81) + 4.81.round(5, half: :even).should eql(4.81) + -4.81.round(5, half: nil).should eql(-4.81) + -4.81.round(5, half: :up).should eql(-4.81) + -4.81.round(5, half: :down).should eql(-4.81) + -4.81.round(5, half: :even).should eql(-4.81) + 4.809999999999999.round(5, half: nil).should eql(4.81) + 4.809999999999999.round(5, half: :up).should eql(4.81) + 4.809999999999999.round(5, half: :down).should eql(4.81) + 4.809999999999999.round(5, half: :even).should eql(4.81) + -4.809999999999999.round(5, half: nil).should eql(-4.81) + -4.809999999999999.round(5, half: :up).should eql(-4.81) + -4.809999999999999.round(5, half: :down).should eql(-4.81) + -4.809999999999999.round(5, half: :even).should eql(-4.81) + end + + ruby_bug "#19318", ""..."3.3" do + # These numbers are neighbouring floating point numbers round a + # precise value. They test that the rounding modes work correctly + # round that value and precision is not lost which might cause + # incorrect results. + it "does not lose precision during the rounding process" do + 767573.1875850001.round(5, half: nil).should eql(767573.18759) + 767573.1875850001.round(5, half: :up).should eql(767573.18759) + 767573.1875850001.round(5, half: :down).should eql(767573.18759) + 767573.1875850001.round(5, half: :even).should eql(767573.18759) + -767573.1875850001.round(5, half: nil).should eql(-767573.18759) + -767573.1875850001.round(5, half: :up).should eql(-767573.18759) + -767573.1875850001.round(5, half: :down).should eql(-767573.18759) + -767573.1875850001.round(5, half: :even).should eql(-767573.18759) + 767573.187585.round(5, half: nil).should eql(767573.18759) + 767573.187585.round(5, half: :up).should eql(767573.18759) + 767573.187585.round(5, half: :down).should eql(767573.18758) + 767573.187585.round(5, half: :even).should eql(767573.18758) + -767573.187585.round(5, half: nil).should eql(-767573.18759) + -767573.187585.round(5, half: :up).should eql(-767573.18759) + -767573.187585.round(5, half: :down).should eql(-767573.18758) + -767573.187585.round(5, half: :even).should eql(-767573.18758) + 767573.1875849998.round(5, half: nil).should eql(767573.18758) + 767573.1875849998.round(5, half: :up).should eql(767573.18758) + 767573.1875849998.round(5, half: :down).should eql(767573.18758) + 767573.1875849998.round(5, half: :even).should eql(767573.18758) + -767573.1875849998.round(5, half: nil).should eql(-767573.18758) + -767573.1875849998.round(5, half: :up).should eql(-767573.18758) + -767573.1875849998.round(5, half: :down).should eql(-767573.18758) + -767573.1875849998.round(5, half: :even).should eql(-767573.18758) + end end it "raises FloatDomainError for exceptional values with a half option" do diff --git a/spec/ruby/core/float/shared/equal.rb b/spec/ruby/core/float/shared/equal.rb index 668aa069b5..4d524e1cf2 100644 --- a/spec/ruby/core/float/shared/equal.rb +++ b/spec/ruby/core/float/shared/equal.rb @@ -14,4 +14,25 @@ describe :float_equal, shared: true do 1.0.send(@method, x).should == false 2.0.send(@method, x).should == true end + + it "returns false if one side is NaN" do + [1.0, 42, bignum_value].each { |n| + (nan_value.send(@method, n)).should == false + (n.send(@method, nan_value)).should == false + } + end + + it "handles positive infinity" do + [1.0, 42, bignum_value].each { |n| + (infinity_value.send(@method, n)).should == false + (n.send(@method, infinity_value)).should == false + } + end + + it "handles negative infinity" do + [1.0, 42, bignum_value].each { |n| + ((-infinity_value).send(@method, n)).should == false + (n.send(@method, -infinity_value)).should == false + } + end end diff --git a/spec/ruby/core/float/shared/to_i.rb b/spec/ruby/core/float/shared/to_i.rb index 960295f095..33b32ca533 100644 --- a/spec/ruby/core/float/shared/to_i.rb +++ b/spec/ruby/core/float/shared/to_i.rb @@ -7,4 +7,8 @@ describe :float_to_i, shared: true do -9223372036854775808.1.send(@method).should eql(-9223372036854775808) 9223372036854775808.1.send(@method).should eql(9223372036854775808) end + + it "raises a FloatDomainError for NaN" do + -> { nan_value.send(@method) }.should raise_error(FloatDomainError) + end end |