diff options
Diffstat (limited to 'spec/ruby/core/float')
47 files changed, 687 insertions, 349 deletions
diff --git a/spec/ruby/core/float/angle_spec.rb b/spec/ruby/core/float/angle_spec.rb index c07249aa97..ac182c5b73 100644 --- a/spec/ruby/core/float/angle_spec.rb +++ b/spec/ruby/core/float/angle_spec.rb @@ -1,6 +1,7 @@ require_relative '../../spec_helper' -require_relative 'shared/arg' describe "Float#angle" do - it_behaves_like :float_arg, :angle + it "is an alias of Float#arg" do + Float.instance_method(:angle).should == Float.instance_method(:arg) + end end diff --git a/spec/ruby/core/float/arg_spec.rb b/spec/ruby/core/float/arg_spec.rb index d3a50668f8..c9c602bbf6 100644 --- a/spec/ruby/core/float/arg_spec.rb +++ b/spec/ruby/core/float/arg_spec.rb @@ -1,6 +1,38 @@ require_relative '../../spec_helper' -require_relative 'shared/arg' describe "Float#arg" do - it_behaves_like :float_arg, :arg + it "returns NaN if NaN" do + f = nan_value + f.arg.nan?.should == true + end + + it "returns self if NaN" do + f = nan_value + f.arg.should.equal?(f) + end + + it "returns 0 if positive" do + 1.0.arg.should == 0 + end + + it "returns 0 if +0.0" do + 0.0.arg.should == 0 + end + + it "returns 0 if +Infinity" do + infinity_value.arg.should == 0 + end + + it "returns Pi if negative" do + (-1.0).arg.should == Math::PI + end + + # This was established in r23960 + it "returns Pi if -0.0" do + (-0.0).arg.should == Math::PI + end + + it "returns Pi if -Infinity" do + (-infinity_value).arg.should == Math::PI + end end diff --git a/spec/ruby/core/float/case_compare_spec.rb b/spec/ruby/core/float/case_compare_spec.rb index b902fbea18..c82803642d 100644 --- a/spec/ruby/core/float/case_compare_spec.rb +++ b/spec/ruby/core/float/case_compare_spec.rb @@ -1,6 +1,7 @@ require_relative '../../spec_helper' -require_relative 'shared/equal' describe "Float#===" do - it_behaves_like :float_equal, :=== + it "is an alias of Float#==" do + Float.instance_method(:===).should == Float.instance_method(:==) + end end diff --git a/spec/ruby/core/float/ceil_spec.rb b/spec/ruby/core/float/ceil_spec.rb index 7fc18d304c..efd1e6feb2 100644 --- a/spec/ruby/core/float/ceil_spec.rb +++ b/spec/ruby/core/float/ceil_spec.rb @@ -1,21 +1,28 @@ require_relative '../../spec_helper' +require_relative '../integer/shared/integer_ceil_precision' describe "Float#ceil" do + context "with values equal to integers" do + it_behaves_like :integer_ceil_precision, :Float + end + it "returns the smallest Integer greater than or equal to self" do - -1.2.ceil.should eql( -1) - -1.0.ceil.should eql( -1) - 0.0.ceil.should eql( 0 ) - 1.3.ceil.should eql( 2 ) - 3.0.ceil.should eql( 3 ) - -9223372036854775808.1.ceil.should eql(-9223372036854775808) - +9223372036854775808.1.ceil.should eql(+9223372036854775808) + -1.2.ceil.should.eql?( -1) + -1.0.ceil.should.eql?( -1) + 0.0.ceil.should.eql?( 0 ) + 1.3.ceil.should.eql?( 2 ) + 3.0.ceil.should.eql?( 3 ) + -9223372036854775808.1.ceil.should.eql?(-9223372036854775808) + +9223372036854775808.1.ceil.should.eql?(+9223372036854775808) end it "returns the smallest number greater than or equal to self with an optionally given precision" do - 2.1679.ceil(0).should eql(3) - 214.94.ceil(-1).should eql(220) - 7.0.ceil(1).should eql(7.0) - -1.234.ceil(2).should eql(-1.23) - 5.123812.ceil(4).should eql(5.1239) + 2.1679.ceil(0).should.eql?(3) + 214.94.ceil(-1).should.eql?(220) + 7.0.ceil(1).should.eql?(7.0) + 200.0.ceil(-2).should.eql?(200) + -1.234.ceil(2).should.eql?(-1.23) + 5.123812.ceil(4).should.eql?(5.1239) + 10.00001.ceil(5).should.eql?(10.00001) end end diff --git a/spec/ruby/core/float/coerce_spec.rb b/spec/ruby/core/float/coerce_spec.rb index ea108f3303..baa831dcf6 100644 --- a/spec/ruby/core/float/coerce_spec.rb +++ b/spec/ruby/core/float/coerce_spec.rb @@ -9,10 +9,10 @@ describe "Float#coerce" do 1.0.coerce(3.14).should == [3.14, 1.0] a, b = -0.0.coerce(bignum_value) - a.should be_close(9223372036854775808.0, TOLERANCE) + a.should be_close(18446744073709551616.0, TOLERANCE) b.should be_close(-0.0, TOLERANCE) a, b = 1.0.coerce(bignum_value) - a.should be_close(9223372036854775808.0, TOLERANCE) + a.should be_close(18446744073709551616.0, TOLERANCE) b.should be_close(1.0, TOLERANCE) end end diff --git a/spec/ruby/core/float/comparison_spec.rb b/spec/ruby/core/float/comparison_spec.rb index 51bb5a52db..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 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 diff --git a/spec/ruby/core/float/constants_spec.rb b/spec/ruby/core/float/constants_spec.rb index 497e0ae188..1b71ee8adf 100644 --- a/spec/ruby/core/float/constants_spec.rb +++ b/spec/ruby/core/float/constants_spec.rb @@ -50,6 +50,6 @@ describe "Float constant" do end it "NAN is 'not a number'" do - Float::NAN.nan?.should be_true + Float::NAN.nan?.should == true end end diff --git a/spec/ruby/core/float/denominator_spec.rb b/spec/ruby/core/float/denominator_spec.rb index 6f4fcfcf23..85beaa98cd 100644 --- a/spec/ruby/core/float/denominator_spec.rb +++ b/spec/ruby/core/float/denominator_spec.rb @@ -12,7 +12,7 @@ describe "Float#denominator" do it "returns an Integer" do @numbers.each do |number| - number.denominator.should be_kind_of(Integer) + number.denominator.should.is_a?(Integer) end end diff --git a/spec/ruby/core/float/divide_spec.rb b/spec/ruby/core/float/divide_spec.rb index d8f71a6b98..68a2c550a7 100644 --- a/spec/ruby/core/float/divide_spec.rb +++ b/spec/ruby/core/float/divide_spec.rb @@ -16,24 +16,28 @@ describe "Float#/" do end it "returns +Infinity when dividing non-zero by zero of the same sign" do - (1.0 / 0.0).should be_positive_infinity - (-1.0 / -0.0).should be_positive_infinity + (1.0 / 0.0).should.infinite? == 1 + (-1.0 / -0.0).should.infinite? == 1 end it "returns -Infinity when dividing non-zero by zero of opposite sign" do - (-1.0 / 0.0).should be_negative_infinity - (1.0 / -0.0).should be_negative_infinity + (-1.0 / 0.0).should.infinite? == -1 + (1.0 / -0.0).should.infinite? == -1 end it "returns NaN when dividing zero by zero" do - (0.0 / 0.0).should be_nan - (-0.0 / 0.0).should be_nan - (0.0 / -0.0).should be_nan - (-0.0 / -0.0).should be_nan + (0.0 / 0.0).should.nan? + (-0.0 / 0.0).should.nan? + (0.0 / -0.0).should.nan? + (-0.0 / -0.0).should.nan? end it "raises a TypeError when given a non-Numeric" do - -> { 13.0 / "10" }.should raise_error(TypeError) - -> { 13.0 / :symbol }.should raise_error(TypeError) + -> { 13.0 / "10" }.should.raise(TypeError) + -> { 13.0 / :symbol }.should.raise(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 ec55dd3681..7ed6cd3487 100644 --- a/spec/ruby/core/float/divmod_spec.rb +++ b/spec/ruby/core/float/divmod_spec.rb @@ -3,41 +3,41 @@ require_relative '../../spec_helper' describe "Float#divmod" do it "returns an [quotient, modulus] from dividing self by other" do values = 3.14.divmod(2) - values[0].should eql(1) + values[0].should.eql?(1) values[1].should be_close(1.14, TOLERANCE) values = 2.8284.divmod(3.1415) - values[0].should eql(0) + values[0].should.eql?(0) values[1].should be_close(2.8284, TOLERANCE) values = -1.0.divmod(bignum_value) - values[0].should eql(-1) - values[1].should be_close(9223372036854775808.000, TOLERANCE) + values[0].should.eql?(-1) + values[1].should be_close(18446744073709551616.0, TOLERANCE) values = -1.0.divmod(1) - values[0].should eql(-1) - values[1].should eql(0.0) + values[0].should.eql?(-1) + values[1].should.eql?(0.0) end # Behaviour established as correct in r23953 it "raises a FloatDomainError if self is NaN" do - -> { nan_value.divmod(1) }.should raise_error(FloatDomainError) + -> { nan_value.divmod(1) }.should.raise(FloatDomainError) end # 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(FloatDomainError) end # Behaviour established as correct in r23953 it "raises a FloatDomainError if self is Infinity" do - -> { infinity_value.divmod(1) }.should raise_error(FloatDomainError) + -> { infinity_value.divmod(1) }.should.raise(FloatDomainError) end it "raises a ZeroDivisionError if other is zero" do - -> { 1.0.divmod(0) }.should raise_error(ZeroDivisionError) - -> { 1.0.divmod(0.0) }.should raise_error(ZeroDivisionError) + -> { 1.0.divmod(0) }.should.raise(ZeroDivisionError) + -> { 1.0.divmod(0.0) }.should.raise(ZeroDivisionError) end # redmine #5276" it "returns the correct [quotient, modulus] even for large quotient" do - 0.59.divmod(7.761021455128987e-11).first.should eql(7602092113) + 0.59.divmod(7.761021455128987e-11).first.should.eql?(7602092113) end end diff --git a/spec/ruby/core/float/dup_spec.rb b/spec/ruby/core/float/dup_spec.rb index 294da8e2bc..b474e21325 100644 --- a/spec/ruby/core/float/dup_spec.rb +++ b/spec/ruby/core/float/dup_spec.rb @@ -3,6 +3,6 @@ require_relative '../../spec_helper' describe "Float#dup" do it "returns self" do float = 2.4 - float.dup.should equal(float) + float.dup.should.equal?(float) end end diff --git a/spec/ruby/core/float/eql_spec.rb b/spec/ruby/core/float/eql_spec.rb index 6b5f91db33..cf1ad8416f 100644 --- a/spec/ruby/core/float/eql_spec.rb +++ b/spec/ruby/core/float/eql_spec.rb @@ -2,15 +2,15 @@ require_relative '../../spec_helper' describe "Float#eql?" do it "returns true if other is a Float equal to self" do - 0.0.eql?(0.0).should be_true + 0.0.eql?(0.0).should == true end it "returns false if other is a Float not equal to self" do - 1.0.eql?(1.1).should be_false + 1.0.eql?(1.1).should == false end it "returns false if other is not a Float" do - 1.0.eql?(1).should be_false - 1.0.eql?(:one).should be_false + 1.0.eql?(1).should == false + 1.0.eql?(:one).should == false end end diff --git a/spec/ruby/core/float/equal_value_spec.rb b/spec/ruby/core/float/equal_value_spec.rb index 03eea5108e..37d0e162d3 100644 --- a/spec/ruby/core/float/equal_value_spec.rb +++ b/spec/ruby/core/float/equal_value_spec.rb @@ -1,6 +1,40 @@ require_relative '../../spec_helper' -require_relative 'shared/equal' describe "Float#==" do - it_behaves_like :float_equal, :== + it "returns true if self has the same value as other" do + (1.0 == 1).should == true + (2.71828 == 1.428).should == false + (-4.2 == 4.2).should == false + end + + it "calls 'other == self' if coercion fails" do + x = mock('other') + def x.==(other) + 2.0 == other + end + + (1.0 == x).should == false + (2.0 == x).should == true + 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 == false + } + end + + it "handles negative infinity" do + [1.0, 42, bignum_value].each { |n| + ((-infinity_value) == n).should == false + (n == -infinity_value).should == false + } + end end diff --git a/spec/ruby/core/float/fdiv_spec.rb b/spec/ruby/core/float/fdiv_spec.rb index be25ee283b..8a3ead4880 100644 --- a/spec/ruby/core/float/fdiv_spec.rb +++ b/spec/ruby/core/float/fdiv_spec.rb @@ -1,6 +1,61 @@ require_relative '../../spec_helper' -require_relative 'shared/quo' describe "Float#fdiv" do - it_behaves_like :float_quo, :fdiv + it "performs floating-point division between self and an Integer" do + 8.9.fdiv(7).should == 1.2714285714285716 + end + + it "performs floating-point division between self and an Integer" do + 8.9.fdiv(9999999999999**9).should == 8.900000000008011e-117 + end + + it "performs floating-point division between self and a Float" do + 2827.22.fdiv(872.111111).should == 3.2418116961704433 + end + + it "returns NaN when the argument is NaN" do + -1819.999999.fdiv(nan_value).nan?.should == true + 11109.1981271.fdiv(nan_value).nan?.should == true + end + + it "returns Infinity when the argument is 0.0" do + 2827.22.fdiv(0.0).infinite?.should == 1 + end + + it "returns -Infinity when the argument is 0.0 and self is negative" do + -48229.282.fdiv(0.0).infinite?.should == -1 + end + + it "returns Infinity when the argument is 0" do + 2827.22.fdiv(0).infinite?.should == 1 + end + + it "returns -Infinity when the argument is 0 and self is negative" do + -48229.282.fdiv(0).infinite?.should == -1 + end + + it "returns 0.0 when the argument is Infinity" do + 47292.2821.fdiv(infinity_value).should == 0.0 + end + + it "returns -0.0 when the argument is -Infinity" do + 1.9999918.fdiv(-infinity_value).should == -0.0 + end + + it "performs floating-point division between self and a Rational" do + 74620.09.fdiv(Rational(2,3)).should == 111930.135 + end + + it "performs floating-point division between self and a Complex" do + 74620.09.fdiv(Complex(8,2)).should == Complex( + 8778.834117647059, -2194.7085294117646) + end + + it "raises a TypeError when argument isn't numeric" do + -> { 27292.2.fdiv(mock('non-numeric')) }.should.raise(TypeError) + end + + it "raises an ArgumentError when passed multiple arguments" do + -> { 272.221.fdiv(6,0.2) }.should.raise(ArgumentError) + end end diff --git a/spec/ruby/core/float/float_spec.rb b/spec/ruby/core/float/float_spec.rb index 263ae82079..46b2eff372 100644 --- a/spec/ruby/core/float/float_spec.rb +++ b/spec/ruby/core/float/float_spec.rb @@ -8,12 +8,12 @@ describe "Float" do it ".allocate raises a TypeError" do -> do Float.allocate - end.should raise_error(TypeError) + end.should.raise(TypeError) end it ".new is undefined" do -> do Float.new - end.should raise_error(NoMethodError) + end.should.raise(NoMethodError) end end diff --git a/spec/ruby/core/float/floor_spec.rb b/spec/ruby/core/float/floor_spec.rb index 046216d36d..f77300eb04 100644 --- a/spec/ruby/core/float/floor_spec.rb +++ b/spec/ruby/core/float/floor_spec.rb @@ -1,21 +1,28 @@ require_relative '../../spec_helper' +require_relative '../integer/shared/integer_floor_precision' describe "Float#floor" do + context "with values equal to integers" do + it_behaves_like :integer_floor_precision, :Float + end + it "returns the largest Integer less than or equal to self" do - -1.2.floor.should eql( -2) - -1.0.floor.should eql( -1) - 0.0.floor.should eql( 0 ) - 1.0.floor.should eql( 1 ) - 5.9.floor.should eql( 5 ) - -9223372036854775808.1.floor.should eql(-9223372036854775808) - +9223372036854775808.1.floor.should eql(+9223372036854775808) + -1.2.floor.should.eql?( -2) + -1.0.floor.should.eql?( -1) + 0.0.floor.should.eql?( 0 ) + 1.0.floor.should.eql?( 1 ) + 5.9.floor.should.eql?( 5 ) + -9223372036854775808.1.floor.should.eql?(-9223372036854775808) + +9223372036854775808.1.floor.should.eql?(+9223372036854775808) end it "returns the largest number less than or equal to self with an optionally given precision" do - 2.1679.floor(0).should eql(2) - 214.94.floor(-1).should eql(210) - 7.0.floor(1).should eql(7.0) - -1.234.floor(2).should eql(-1.24) - 5.123812.floor(4).should eql(5.1238) + 2.1679.floor(0).should.eql?(2) + 214.94.floor(-1).should.eql?(210) + 7.0.floor(1).should.eql?(7.0) + 200.0.floor(-2).should.eql?(200) + -1.234.floor(2).should.eql?(-1.24) + 5.123812.floor(4).should.eql?(5.1238) + 10.00001.floor(5).should.eql?(10.00001) end end diff --git a/spec/ruby/core/float/gt_spec.rb b/spec/ruby/core/float/gt_spec.rb index 0d73f1c3df..5194796b46 100644 --- a/spec/ruby/core/float/gt_spec.rb +++ b/spec/ruby/core/float/gt_spec.rb @@ -11,7 +11,28 @@ describe "Float#>" do end it "raises an ArgumentError when given a non-Numeric" do - -> { 5.0 > "4" }.should raise_error(ArgumentError) - -> { 5.0 > mock('x') }.should raise_error(ArgumentError) + -> { 5.0 > "4" }.should.raise(ArgumentError) + -> { 5.0 > mock('x') }.should.raise(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..4a62725d53 100644 --- a/spec/ruby/core/float/gte_spec.rb +++ b/spec/ruby/core/float/gte_spec.rb @@ -11,7 +11,28 @@ describe "Float#>=" do end it "raises an ArgumentError when given a non-Numeric" do - -> { 5.0 >= "4" }.should raise_error(ArgumentError) - -> { 5.0 >= mock('x') }.should raise_error(ArgumentError) + -> { 5.0 >= "4" }.should.raise(ArgumentError) + -> { 5.0 >= mock('x') }.should.raise(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/inspect_spec.rb b/spec/ruby/core/float/inspect_spec.rb new file mode 100644 index 0000000000..3827167c17 --- /dev/null +++ b/spec/ruby/core/float/inspect_spec.rb @@ -0,0 +1,7 @@ +require_relative '../../spec_helper' + +describe "Float#inspect" do + it "is an alias of Float#to_s" do + Float.instance_method(:inspect).should == Float.instance_method(:to_s) + end +end diff --git a/spec/ruby/core/float/lt_spec.rb b/spec/ruby/core/float/lt_spec.rb index c01b6e0e02..0f0e1752bd 100644 --- a/spec/ruby/core/float/lt_spec.rb +++ b/spec/ruby/core/float/lt_spec.rb @@ -11,7 +11,28 @@ describe "Float#<" do end it "raises an ArgumentError when given a non-Numeric" do - -> { 5.0 < "4" }.should raise_error(ArgumentError) - -> { 5.0 < mock('x') }.should raise_error(ArgumentError) + -> { 5.0 < "4" }.should.raise(ArgumentError) + -> { 5.0 < mock('x') }.should.raise(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..afb64ade09 100644 --- a/spec/ruby/core/float/lte_spec.rb +++ b/spec/ruby/core/float/lte_spec.rb @@ -12,7 +12,28 @@ describe "Float#<=" do end it "raises an ArgumentError when given a non-Numeric" do - -> { 5.0 <= "4" }.should raise_error(ArgumentError) - -> { 5.0 <= mock('x') }.should raise_error(ArgumentError) + -> { 5.0 <= "4" }.should.raise(ArgumentError) + -> { 5.0 <= mock('x') }.should.raise(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..4a753267e0 100644 --- a/spec/ruby/core/float/magnitude_spec.rb +++ b/spec/ruby/core/float/magnitude_spec.rb @@ -1,5 +1,14 @@ +require_relative "../../spec_helper" require_relative 'shared/abs' describe "Float#magnitude" do - it_behaves_like :float_abs, :magnitude + ruby_version_is ""..."3.4" do + it_behaves_like :float_abs, :magnitude + end + + ruby_version_is "3.4" do + it "is an alias of Float#abs" do + Float.instance_method(:magnitude).should == Float.instance_method(:abs) + end + end end diff --git a/spec/ruby/core/float/minus_spec.rb b/spec/ruby/core/float/minus_spec.rb index 5626cbdac2..a4281a397b 100644 --- a/spec/ruby/core/float/minus_spec.rb +++ b/spec/ruby/core/float/minus_spec.rb @@ -6,7 +6,7 @@ describe "Float#-" do it "returns self minus other" do (9_237_212.5280 - 5_280).should be_close(9231932.528, TOLERANCE) - (2_560_496.1691 - bignum_value).should be_close(-9223372036852215808.000, TOLERANCE) + (2_560_496.1691 - bignum_value).should be_close(-18446744073706991616.0, TOLERANCE) (5.5 - 5.5).should be_close(0.0,TOLERANCE) end end diff --git a/spec/ruby/core/float/modulo_spec.rb b/spec/ruby/core/float/modulo_spec.rb index 8ae80a0b05..8b7aedf822 100644 --- a/spec/ruby/core/float/modulo_spec.rb +++ b/spec/ruby/core/float/modulo_spec.rb @@ -1,10 +1,56 @@ require_relative '../../spec_helper' -require_relative 'shared/modulo' describe "Float#%" do - it_behaves_like :float_modulo, :% + it "returns self modulo other" do + (6543.21 % 137).should be_close(104.21, TOLERANCE) + (5667.19 % bignum_value).should be_close(5667.19, TOLERANCE) + (6543.21 % 137.24).should be_close(92.9299999999996, TOLERANCE) + + (-1.0 % 1).should == 0 + end + + it "returns self when modulus is +Infinity" do + (4.2 % Float::INFINITY).should == 4.2 + end + + it "returns -Infinity when modulus is -Infinity" do + (4.2 % -Float::INFINITY).should == -Float::INFINITY + end + + it "returns NaN when called on NaN or Infinities" do + (Float::NAN % 42).should.nan? + (Float::INFINITY % 42).should.nan? + (-Float::INFINITY % 42).should.nan? + end + + it "returns NaN when modulus is NaN" do + (4.2 % Float::NAN).should.nan? + end + + it "returns -0.0 when called on -0.0 with a non zero modulus" do + r = -0.0 % 42 + r.should == 0 + (1/r).should < 0 + + r = -0.0 % Float::INFINITY + r.should == 0 + (1/r).should < 0 + end + + it "tries to coerce the modulus" do + obj = mock("modulus") + obj.should_receive(:coerce).with(1.25).and_return([1.25, 0.5]) + (1.25 % obj).should == 0.25 + end + + it "raises a ZeroDivisionError if other is zero" do + -> { 1.0 % 0 }.should.raise(ZeroDivisionError) + -> { 1.0 % 0.0 }.should.raise(ZeroDivisionError) + end end describe "Float#modulo" do - it_behaves_like :float_modulo, :modulo + it "is an alias of Float#%" do + Float.instance_method(:modulo).should == Float.instance_method(:%) + end end diff --git a/spec/ruby/core/float/multiply_spec.rb b/spec/ruby/core/float/multiply_spec.rb index eca0b52c4f..edaaba7e61 100644 --- a/spec/ruby/core/float/multiply_spec.rb +++ b/spec/ruby/core/float/multiply_spec.rb @@ -7,11 +7,11 @@ describe "Float#*" do it "returns self multiplied by other" do (4923.98221 * 2).should be_close(9847.96442, TOLERANCE) (6712.5 * 0.25).should be_close(1678.125, TOLERANCE) - (256.4096 * bignum_value).should be_close(2364961134621118431232.000, TOLERANCE) + (256.4096 * bignum_value).should be_close(4729922269242236862464.0, TOLERANCE) end it "raises a TypeError when given a non-Numeric" do - -> { 13.0 * "10" }.should raise_error(TypeError) - -> { 13.0 * :symbol }.should raise_error(TypeError) + -> { 13.0 * "10" }.should.raise(TypeError) + -> { 13.0 * :symbol }.should.raise(TypeError) end end diff --git a/spec/ruby/core/float/negative_spec.rb b/spec/ruby/core/float/negative_spec.rb new file mode 100644 index 0000000000..484e636adb --- /dev/null +++ b/spec/ruby/core/float/negative_spec.rb @@ -0,0 +1,33 @@ +require_relative '../../spec_helper' + +describe "Float#negative?" do + describe "on positive numbers" do + it "returns false" do + 0.1.negative?.should == false + end + end + + describe "on zero" do + it "returns false" do + 0.0.negative?.should == false + end + end + + describe "on negative zero" do + it "returns false" do + -0.0.negative?.should == false + end + end + + describe "on negative numbers" do + it "returns true" do + -0.1.negative?.should == true + end + end + + describe "on NaN" do + it "returns false" do + nan_value.negative?.should == false + end + end +end diff --git a/spec/ruby/core/float/next_float_spec.rb b/spec/ruby/core/float/next_float_spec.rb index 29e2d31146..59892be343 100644 --- a/spec/ruby/core/float/next_float_spec.rb +++ b/spec/ruby/core/float/next_float_spec.rb @@ -9,7 +9,7 @@ describe "Float#next_float" do barely_positive.should < barely_positive.next_float midpoint = barely_positive / 2 - [0.0, barely_positive].should include midpoint + [0.0, barely_positive].should.include? midpoint end it "returns Float::INFINITY for Float::INFINITY" do diff --git a/spec/ruby/core/float/numerator_spec.rb b/spec/ruby/core/float/numerator_spec.rb index 7832e8f056..53b32fdd3d 100644 --- a/spec/ruby/core/float/numerator_spec.rb +++ b/spec/ruby/core/float/numerator_spec.rb @@ -15,7 +15,7 @@ describe "Float#numerator" do it "converts self to a Rational object then returns its numerator" do @numbers.each do |number| - number.infinite?.should be_nil + number.infinite?.should == nil number.numerator.should == Rational(number).numerator end end @@ -25,7 +25,7 @@ describe "Float#numerator" do end it "returns NaN for NaN" do - nan_value.numerator.nan?.should be_true + nan_value.numerator.nan?.should == true end it "returns Infinity for Infinity" do diff --git a/spec/ruby/core/float/phase_spec.rb b/spec/ruby/core/float/phase_spec.rb index 2aa84024b4..ad112ac9fe 100644 --- a/spec/ruby/core/float/phase_spec.rb +++ b/spec/ruby/core/float/phase_spec.rb @@ -1,6 +1,7 @@ require_relative '../../spec_helper' -require_relative 'shared/arg' describe "Float#phase" do - it_behaves_like :float_arg, :phase + it "is an alias of Float#arg" do + Float.instance_method(:phase).should == Float.instance_method(:arg) + end end diff --git a/spec/ruby/core/float/plus_spec.rb b/spec/ruby/core/float/plus_spec.rb index 06b136a06b..e3e19d7f39 100644 --- a/spec/ruby/core/float/plus_spec.rb +++ b/spec/ruby/core/float/plus_spec.rb @@ -6,7 +6,7 @@ describe "Float#+" do it "returns self plus other" do (491.213 + 2).should be_close(493.213, TOLERANCE) - (9.99 + bignum_value).should be_close(9223372036854775808.000, TOLERANCE) + (9.99 + bignum_value).should be_close(18446744073709551616.0, TOLERANCE) (1001.99 + 5.219).should be_close(1007.209, TOLERANCE) end end diff --git a/spec/ruby/core/float/positive_spec.rb b/spec/ruby/core/float/positive_spec.rb new file mode 100644 index 0000000000..aa87c03151 --- /dev/null +++ b/spec/ruby/core/float/positive_spec.rb @@ -0,0 +1,33 @@ +require_relative '../../spec_helper' + +describe "Float#positive?" do + describe "on positive numbers" do + it "returns true" do + 0.1.positive?.should == true + end + end + + describe "on zero" do + it "returns false" do + 0.0.positive?.should == false + end + end + + describe "on negative zero" do + it "returns false" do + -0.0.positive?.should == false + end + end + + describe "on negative numbers" do + it "returns false" do + -0.1.positive?.should == false + end + end + + describe "on NaN" do + it "returns false" do + nan_value.positive?.should == false + end + end +end diff --git a/spec/ruby/core/float/prev_float_spec.rb b/spec/ruby/core/float/prev_float_spec.rb index 5e50269da7..2d1f136254 100644 --- a/spec/ruby/core/float/prev_float_spec.rb +++ b/spec/ruby/core/float/prev_float_spec.rb @@ -9,7 +9,7 @@ describe "Float#prev_float" do barely_negative.should > barely_negative.prev_float midpoint = barely_negative / 2 - [0.0, barely_negative].should include midpoint + [0.0, barely_negative].should.include? midpoint end it "returns -Float::INFINITY for -Float::INFINITY" do diff --git a/spec/ruby/core/float/quo_spec.rb b/spec/ruby/core/float/quo_spec.rb index b5c64f9d87..0e9a7a0a0c 100644 --- a/spec/ruby/core/float/quo_spec.rb +++ b/spec/ruby/core/float/quo_spec.rb @@ -1,6 +1,7 @@ require_relative '../../spec_helper' -require_relative 'shared/quo' describe "Float#quo" do - it_behaves_like :float_quo, :quo + it "is an alias of Float#fdiv" do + Float.instance_method(:quo).should == Float.instance_method(:fdiv) + end end diff --git a/spec/ruby/core/float/rationalize_spec.rb b/spec/ruby/core/float/rationalize_spec.rb index 0c5bef7ac4..fa8fb5c841 100644 --- a/spec/ruby/core/float/rationalize_spec.rb +++ b/spec/ruby/core/float/rationalize_spec.rb @@ -29,15 +29,15 @@ describe "Float#rationalize" do end it "raises a FloatDomainError for Infinity" do - -> {infinity_value.rationalize}.should raise_error(FloatDomainError) + -> {infinity_value.rationalize}.should.raise(FloatDomainError) end it "raises a FloatDomainError for NaN" do - -> { nan_value.rationalize }.should raise_error(FloatDomainError) + -> { nan_value.rationalize }.should.raise(FloatDomainError) end it "raises ArgumentError when passed more than one argument" do - -> { 0.3.rationalize(0.1, 0.1) }.should raise_error(ArgumentError) - -> { 0.3.rationalize(0.1, 0.1, 2) }.should raise_error(ArgumentError) + -> { 0.3.rationalize(0.1, 0.1) }.should.raise(ArgumentError) + -> { 0.3.rationalize(0.1, 0.1, 2) }.should.raise(ArgumentError) end end diff --git a/spec/ruby/core/float/round_spec.rb b/spec/ruby/core/float/round_spec.rb index e143682362..63c1d5689c 100644 --- a/spec/ruby/core/float/round_spec.rb +++ b/spec/ruby/core/float/round_spec.rb @@ -16,51 +16,57 @@ describe "Float#round" do end it "raises FloatDomainError for exceptional values" do - -> { (+infinity_value).round }.should raise_error(FloatDomainError) - -> { (-infinity_value).round }.should raise_error(FloatDomainError) - -> { nan_value.round }.should raise_error(FloatDomainError) + -> { (+infinity_value).round }.should.raise(FloatDomainError) + -> { (-infinity_value).round }.should.raise(FloatDomainError) + -> { nan_value.round }.should.raise(FloatDomainError) end it "rounds self to an optionally given precision" do - 5.5.round(0).should eql(6) - 5.7.round(1).should eql(5.7) + 5.5.round(0).should.eql?(6) + 5.7.round(1).should.eql?(5.7) 1.2345678.round(2).should == 1.23 - 123456.78.round(-2).should eql(123500) # rounded up - -123456.78.round(-2).should eql(-123500) + 123456.78.round(-2).should.eql?(123500) # rounded up + -123456.78.round(-2).should.eql?(-123500) 12.345678.round(3.999).should == 12.346 end + it "correctly rounds exact floats with a numerous digits in a fraction part" do + 0.8241000000000004.round(10).should == 0.8241 + 0.8241000000000002.round(10).should == 0.8241 + end + it "returns zero when passed a negative argument with magnitude greater than magnitude of the whole number portion of the Float" do - 0.8346268.round(-1).should eql(0) + 0.8346268.round(-1).should.eql?(0) end it "raises a TypeError when its argument can not be converted to an Integer" do - -> { 1.0.round("4") }.should raise_error(TypeError) - -> { 1.0.round(nil) }.should raise_error(TypeError) + -> { 1.0.round("4") }.should.raise(TypeError) + -> { 1.0.round(nil) }.should.raise(TypeError) end it "raises FloatDomainError for exceptional values when passed a non-positive precision" do - -> { Float::INFINITY.round( 0) }.should raise_error(FloatDomainError) - -> { Float::INFINITY.round(-2) }.should raise_error(FloatDomainError) - -> { (-Float::INFINITY).round( 0) }.should raise_error(FloatDomainError) - -> { (-Float::INFINITY).round(-2) }.should raise_error(FloatDomainError) + -> { Float::INFINITY.round( 0) }.should.raise(FloatDomainError) + -> { Float::INFINITY.round(-2) }.should.raise(FloatDomainError) + -> { (-Float::INFINITY).round( 0) }.should.raise(FloatDomainError) + -> { (-Float::INFINITY).round(-2) }.should.raise(FloatDomainError) end it "raises RangeError for NAN when passed a non-positive precision" do - -> { Float::NAN.round(0) }.should raise_error(RangeError) - -> { Float::NAN.round(-2) }.should raise_error(RangeError) + -> { Float::NAN.round(0) }.should.raise(RangeError) + -> { Float::NAN.round(-2) }.should.raise(RangeError) end it "returns self for exceptional values when passed a non-negative precision" do Float::INFINITY.round(2).should == Float::INFINITY (-Float::INFINITY).round(2).should == -Float::INFINITY - Float::NAN.round(2).should be_nan + Float::NAN.round(2).should.nan? end # redmine:5227 it "works for corner cases" do - 42.0.round(308).should eql(42.0) - 1.0e307.round(2).should eql(1.0e307) + 42.0.round(308).should.eql?(42.0) + 1.0e307.round(2).should.eql?(1.0e307) + 120.0.round(-1).should.eql?(120) end # redmine:5271 @@ -68,50 +74,135 @@ describe "Float#round" do 0.42.round(2.0**30).should == 0.42 end + it "returns rounded values for not so big argument" do + 0.42.round(2.0**23).should == 0.42 + end + it "returns big values rounded to nearest" do - +2.5e20.round(-20).should eql( +3 * 10 ** 20 ) - -2.5e20.round(-20).should eql( -3 * 10 ** 20 ) + +2.5e20.round(-20).should.eql?( +3 * 10 ** 20 ) + -2.5e20.round(-20).should.eql?( -3 * 10 ** 20 ) end # redmine #5272 it "returns rounded values for big values" do - +2.4e20.round(-20).should eql( +2 * 10 ** 20 ) - -2.4e20.round(-20).should eql( -2 * 10 ** 20 ) - +2.5e200.round(-200).should eql( +3 * 10 ** 200 ) - +2.4e200.round(-200).should eql( +2 * 10 ** 200 ) - -2.5e200.round(-200).should eql( -3 * 10 ** 200 ) - -2.4e200.round(-200).should eql( -2 * 10 ** 200 ) + +2.4e20.round(-20).should.eql?( +2 * 10 ** 20 ) + -2.4e20.round(-20).should.eql?( -2 * 10 ** 20 ) + +2.5e200.round(-200).should.eql?( +3 * 10 ** 200 ) + +2.4e200.round(-200).should.eql?( +2 * 10 ** 200 ) + -2.5e200.round(-200).should.eql?( -3 * 10 ** 200 ) + -2.4e200.round(-200).should.eql?( -2 * 10 ** 200 ) end it "returns different rounded values depending on the half option" do - 2.5.round(half: nil).should eql(3) - 2.5.round(half: :up).should eql(3) - 2.5.round(half: :down).should eql(2) - 2.5.round(half: :even).should eql(2) - 3.5.round(half: nil).should eql(4) - 3.5.round(half: :up).should eql(4) - 3.5.round(half: :down).should eql(3) - 3.5.round(half: :even).should eql(4) - (-2.5).round(half: nil).should eql(-3) - (-2.5).round(half: :up).should eql(-3) - (-2.5).round(half: :down).should eql(-2) - (-2.5).round(half: :even).should eql(-2) + 2.5.round(half: nil).should.eql?(3) + 2.5.round(half: :up).should.eql?(3) + 2.5.round(half: :down).should.eql?(2) + 2.5.round(half: :even).should.eql?(2) + 3.5.round(half: nil).should.eql?(4) + 3.5.round(half: :up).should.eql?(4) + 3.5.round(half: :down).should.eql?(3) + 3.5.round(half: :even).should.eql?(4) + (-2.5).round(half: nil).should.eql?(-3) + (-2.5).round(half: :up).should.eql?(-3) + (-2.5).round(half: :down).should.eql?(-2) + (-2.5).round(half: :even).should.eql?(-2) end it "rounds self to an optionally given precision with a half option" do - 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) + 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) + -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 + + # 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 it "raises FloatDomainError for exceptional values with a half option" do - -> { (+infinity_value).round(half: :up) }.should raise_error(FloatDomainError) - -> { (-infinity_value).round(half: :down) }.should raise_error(FloatDomainError) - -> { nan_value.round(half: :even) }.should raise_error(FloatDomainError) + -> { (+infinity_value).round(half: :up) }.should.raise(FloatDomainError) + -> { (-infinity_value).round(half: :down) }.should.raise(FloatDomainError) + -> { nan_value.round(half: :even) }.should.raise(FloatDomainError) end it "raise for a non-existent round mode" do - -> { 14.2.round(half: :nonsense) }.should raise_error(ArgumentError, "invalid rounding mode: nonsense") + -> { 14.2.round(half: :nonsense) }.should.raise(ArgumentError, "invalid rounding mode: nonsense") + end + + describe "when 0.0 is given" do + it "returns self for positive ndigits" do + (0.0).round(5).inspect.should == "0.0" + (-0.0).round(1).inspect.should == "-0.0" + end + + it "returns 0 for 0 or undefined ndigits" do + (0.0).round.should == 0 + (-0.0).round(0).should == 0 + (0.0).round(half: :up).should == 0 + end + + it "returns 0 for negative ndigits" do + (0.0).round(-1).should == 0 + (-0.0).round(-1).should == 0 + (0.0).round(-1, half: :up).should == 0 + end end end diff --git a/spec/ruby/core/float/shared/abs.rb b/spec/ruby/core/float/shared/abs.rb index 607983322d..ab21480e24 100644 --- a/spec/ruby/core/float/shared/abs.rb +++ b/spec/ruby/core/float/shared/abs.rb @@ -16,6 +16,6 @@ describe :float_abs, shared: true do end it "returns NaN if NaN" do - nan_value.send(@method).nan?.should be_true + nan_value.send(@method).nan?.should == true end end diff --git a/spec/ruby/core/float/shared/arg.rb b/spec/ruby/core/float/shared/arg.rb deleted file mode 100644 index 136cf19ec8..0000000000 --- a/spec/ruby/core/float/shared/arg.rb +++ /dev/null @@ -1,36 +0,0 @@ -describe :float_arg, shared: true do - it "returns NaN if NaN" do - f = nan_value - f.send(@method).nan?.should be_true - end - - it "returns self if NaN" do - f = nan_value - f.send(@method).should equal(f) - end - - it "returns 0 if positive" do - 1.0.send(@method).should == 0 - end - - it "returns 0 if +0.0" do - 0.0.send(@method).should == 0 - end - - it "returns 0 if +Infinity" do - infinity_value.send(@method).should == 0 - end - - it "returns Pi if negative" do - (-1.0).send(@method).should == Math::PI - end - - # This was established in r23960 - it "returns Pi if -0.0" do - (-0.0).send(@method).should == Math::PI - end - - it "returns Pi if -Infinity" do - (-infinity_value).send(@method).should == Math::PI - end -end diff --git a/spec/ruby/core/float/shared/arithmetic_exception_in_coerce.rb b/spec/ruby/core/float/shared/arithmetic_exception_in_coerce.rb index eec92d858f..bd3bf9019f 100644 --- a/spec/ruby/core/float/shared/arithmetic_exception_in_coerce.rb +++ b/spec/ruby/core/float/shared/arithmetic_exception_in_coerce.rb @@ -6,6 +6,6 @@ describe :float_arithmetic_exception_in_coerce, shared: true do b.should_receive(:coerce).and_raise(FloatSpecs::CoerceError) # e.g. 1.0 > b - -> { 1.0.send(@method, b) }.should raise_error(FloatSpecs::CoerceError) + -> { 1.0.send(@method, b) }.should.raise(FloatSpecs::CoerceError) end end diff --git a/spec/ruby/core/float/shared/comparison_exception_in_coerce.rb b/spec/ruby/core/float/shared/comparison_exception_in_coerce.rb index 3e2c1e28dd..eec5d8daf9 100644 --- a/spec/ruby/core/float/shared/comparison_exception_in_coerce.rb +++ b/spec/ruby/core/float/shared/comparison_exception_in_coerce.rb @@ -6,6 +6,6 @@ describe :float_comparison_exception_in_coerce, shared: true do b.should_receive(:coerce).and_raise(FloatSpecs::CoerceError) # e.g. 1.0 > b - -> { 1.0.send(@method, b) }.should raise_error(FloatSpecs::CoerceError) + -> { 1.0.send(@method, b) }.should.raise(FloatSpecs::CoerceError) end end diff --git a/spec/ruby/core/float/shared/equal.rb b/spec/ruby/core/float/shared/equal.rb deleted file mode 100644 index 668aa069b5..0000000000 --- a/spec/ruby/core/float/shared/equal.rb +++ /dev/null @@ -1,17 +0,0 @@ -describe :float_equal, shared: true do - it "returns true if self has the same value as other" do - 1.0.send(@method, 1).should == true - 2.71828.send(@method, 1.428).should == false - -4.2.send(@method, 4.2).should == false - end - - it "calls 'other == self' if coercion fails" do - x = mock('other') - def x.==(other) - 2.0 == other - end - - 1.0.send(@method, x).should == false - 2.0.send(@method, x).should == true - end -end diff --git a/spec/ruby/core/float/shared/modulo.rb b/spec/ruby/core/float/shared/modulo.rb deleted file mode 100644 index 6700bd4f4e..0000000000 --- a/spec/ruby/core/float/shared/modulo.rb +++ /dev/null @@ -1,48 +0,0 @@ -describe :float_modulo, shared: true do - it "returns self modulo other" do - 6543.21.send(@method, 137).should be_close(104.21, TOLERANCE) - 5667.19.send(@method, bignum_value).should be_close(5667.19, TOLERANCE) - 6543.21.send(@method, 137.24).should be_close(92.9299999999996, TOLERANCE) - - -1.0.send(@method, 1).should == 0 - end - - it "returns self when modulus is +Infinity" do - 4.2.send(@method, Float::INFINITY).should == 4.2 - end - - it "returns -Infinity when modulus is -Infinity" do - 4.2.send(@method, -Float::INFINITY).should == -Float::INFINITY - end - - it "returns NaN when called on NaN or Infinities" do - Float::NAN.send(@method, 42).should be_nan - Float::INFINITY.send(@method, 42).should be_nan - (-Float::INFINITY).send(@method, 42).should be_nan - end - - it "returns NaN when modulus is NaN" do - 4.2.send(@method, Float::NAN).should be_nan - end - - it "returns -0.0 when called on -0.0 with a non zero modulus" do - r = (-0.0).send(@method, 42) - r.should == 0 - (1/r).should < 0 - - r = (-0.0).send(@method, Float::INFINITY) - r.should == 0 - (1/r).should < 0 - end - - it "tries to coerce the modulus" do - obj = mock("modulus") - obj.should_receive(:coerce).with(1.25).and_return([1.25, 0.5]) - (1.25 % obj).should == 0.25 - end - - it "raises a ZeroDivisionError if other is zero" do - -> { 1.0.send(@method, 0) }.should raise_error(ZeroDivisionError) - -> { 1.0.send(@method, 0.0) }.should raise_error(ZeroDivisionError) - end -end diff --git a/spec/ruby/core/float/shared/quo.rb b/spec/ruby/core/float/shared/quo.rb deleted file mode 100644 index 3487824f70..0000000000 --- a/spec/ruby/core/float/shared/quo.rb +++ /dev/null @@ -1,59 +0,0 @@ -describe :float_quo, shared: true do - it "performs floating-point division between self and a Fixnum" do - 8.9.send(@method, 7).should == 1.2714285714285716 - end - - it "performs floating-point division between self and a Bignum" do - 8.9.send(@method, 9999999999999**9).should == 8.900000000008011e-117 - end - - it "performs floating-point division between self and a Float" do - 2827.22.send(@method, 872.111111).should == 3.2418116961704433 - end - - it "returns NaN when the argument is NaN" do - -1819.999999.send(@method, nan_value).nan?.should be_true - 11109.1981271.send(@method, nan_value).nan?.should be_true - end - - it "returns Infinity when the argument is 0.0" do - 2827.22.send(@method, 0.0).infinite?.should == 1 - end - - it "returns -Infinity when the argument is 0.0 and self is negative" do - -48229.282.send(@method, 0.0).infinite?.should == -1 - end - - it "returns Infinity when the argument is 0" do - 2827.22.send(@method, 0).infinite?.should == 1 - end - - it "returns -Infinity when the argument is 0 and self is negative" do - -48229.282.send(@method, 0).infinite?.should == -1 - end - - it "returns 0.0 when the argument is Infinity" do - 47292.2821.send(@method, infinity_value).should == 0.0 - end - - it "returns -0.0 when the argument is -Infinity" do - 1.9999918.send(@method, -infinity_value).should == -0.0 - end - - it "performs floating-point division between self and a Rational" do - 74620.09.send(@method, Rational(2,3)).should == 111930.135 - end - - it "performs floating-point division between self and a Complex" do - 74620.09.send(@method, Complex(8,2)).should == Complex( - 8778.834117647059, -2194.7085294117646) - end - - it "raises a TypeError when argument isn't numeric" do - -> { 27292.2.send(@method, mock('non-numeric')) }.should raise_error(TypeError) - end - - it "raises an ArgumentError when passed multiple arguments" do - -> { 272.221.send(@method, 6,0.2) }.should raise_error(ArgumentError) - end -end diff --git a/spec/ruby/core/float/shared/to_i.rb b/spec/ruby/core/float/shared/to_i.rb index 960295f095..1e6f941467 100644 --- a/spec/ruby/core/float/shared/to_i.rb +++ b/spec/ruby/core/float/shared/to_i.rb @@ -1,10 +1,14 @@ describe :float_to_i, shared: true do it "returns self truncated to an Integer" do - 899.2.send(@method).should eql(899) - -1.122256e-45.send(@method).should eql(0) - 5_213_451.9201.send(@method).should eql(5213451) - 1.233450999123389e+12.send(@method).should eql(1233450999123) - -9223372036854775808.1.send(@method).should eql(-9223372036854775808) - 9223372036854775808.1.send(@method).should eql(9223372036854775808) + 899.2.send(@method).should.eql?(899) + -1.122256e-45.send(@method).should.eql?(0) + 5_213_451.9201.send(@method).should.eql?(5213451) + 1.233450999123389e+12.send(@method).should.eql?(1233450999123) + -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(FloatDomainError) end end diff --git a/spec/ruby/core/float/to_int_spec.rb b/spec/ruby/core/float/to_int_spec.rb index 084a58b431..ff70d508ff 100644 --- a/spec/ruby/core/float/to_int_spec.rb +++ b/spec/ruby/core/float/to_int_spec.rb @@ -1,6 +1,7 @@ require_relative '../../spec_helper' -require_relative 'shared/to_i' describe "Float#to_int" do - it_behaves_like :float_to_i, :to_int + it "is an alias of Float#to_i" do + Float.instance_method(:to_int).should == Float.instance_method(:to_i) + end end diff --git a/spec/ruby/core/float/to_s_spec.rb b/spec/ruby/core/float/to_s_spec.rb index ad04bc4fb6..3fd64581c2 100644 --- a/spec/ruby/core/float/to_s_spec.rb +++ b/spec/ruby/core/float/to_s_spec.rb @@ -287,24 +287,24 @@ describe "Float#to_s" do (9.5 / 5.5).to_s.should == "1.7272727272727273" end end -end -describe "Float#to_s" do - before :each do - @internal = Encoding.default_internal - end + describe 'encoding' do + before :each do + @internal = Encoding.default_internal + end - after :each do - Encoding.default_internal = @internal - end + after :each do + Encoding.default_internal = @internal + end - it "returns a String in US-ASCII encoding when Encoding.default_internal is nil" do - Encoding.default_internal = nil - 1.23.to_s.encoding.should equal(Encoding::US_ASCII) - end + it "returns a String in US-ASCII encoding when Encoding.default_internal is nil" do + Encoding.default_internal = nil + 1.23.to_s.encoding.should.equal?(Encoding::US_ASCII) + end - it "returns a String in US-ASCII encoding when Encoding.default_internal is not nil" do - Encoding.default_internal = Encoding::IBM437 - 5.47.to_s.encoding.should equal(Encoding::US_ASCII) + it "returns a String in US-ASCII encoding when Encoding.default_internal is not nil" do + Encoding.default_internal = Encoding::IBM437 + 5.47.to_s.encoding.should.equal?(Encoding::US_ASCII) + end end end diff --git a/spec/ruby/core/float/truncate_spec.rb b/spec/ruby/core/float/truncate_spec.rb index 2c80145f9f..1750e3fdbc 100644 --- a/spec/ruby/core/float/truncate_spec.rb +++ b/spec/ruby/core/float/truncate_spec.rb @@ -5,10 +5,10 @@ describe "Float#truncate" do it_behaves_like :float_to_i, :truncate it "returns self truncated to an optionally given precision" do - 2.1679.truncate(0).should eql(2) - 7.1.truncate(1).should eql(7.1) - 214.94.truncate(-1).should eql(210) - -1.234.truncate(2).should eql(-1.23) - 5.123812.truncate(4).should eql(5.1238) + 2.1679.truncate(0).should.eql?(2) + 7.1.truncate(1).should.eql?(7.1) + 214.94.truncate(-1).should.eql?(210) + -1.234.truncate(2).should.eql?(-1.23) + 5.123812.truncate(4).should.eql?(5.1238) end end diff --git a/spec/ruby/core/float/uplus_spec.rb b/spec/ruby/core/float/uplus_spec.rb index 936123558c..b979b2717a 100644 --- a/spec/ruby/core/float/uplus_spec.rb +++ b/spec/ruby/core/float/uplus_spec.rb @@ -4,6 +4,6 @@ describe "Float#+@" do it "returns the same value with same sign (twos complement)" do 34.56.send(:+@).should == 34.56 -34.56.send(:+@).should == -34.56 - 0.0.send(:+@).should eql(0.0) + 0.0.send(:+@).should.eql?(0.0) end end |
