diff options
Diffstat (limited to 'spec/ruby/core/float')
60 files changed, 949 insertions, 298 deletions
diff --git a/spec/ruby/core/float/abs_spec.rb b/spec/ruby/core/float/abs_spec.rb index 3ff2e4369b..a08601926d 100644 --- a/spec/ruby/core/float/abs_spec.rb +++ b/spec/ruby/core/float/abs_spec.rb @@ -1,5 +1,6 @@ -require File.expand_path('../shared/abs', __FILE__) +require_relative '../../spec_helper' +require_relative 'shared/abs' describe "Float#abs" do - it_behaves_like(:float_abs, :abs) + it_behaves_like :float_abs, :abs end diff --git a/spec/ruby/core/float/angle_spec.rb b/spec/ruby/core/float/angle_spec.rb index 2a5d40ad30..c07249aa97 100644 --- a/spec/ruby/core/float/angle_spec.rb +++ b/spec/ruby/core/float/angle_spec.rb @@ -1,4 +1,5 @@ -require File.expand_path('../../../shared/complex/float/arg', __FILE__) +require_relative '../../spec_helper' +require_relative 'shared/arg' describe "Float#angle" do it_behaves_like :float_arg, :angle diff --git a/spec/ruby/core/float/arg_spec.rb b/spec/ruby/core/float/arg_spec.rb index 20a8ac0a63..d3a50668f8 100644 --- a/spec/ruby/core/float/arg_spec.rb +++ b/spec/ruby/core/float/arg_spec.rb @@ -1,4 +1,5 @@ -require File.expand_path('../../../shared/complex/float/arg', __FILE__) +require_relative '../../spec_helper' +require_relative 'shared/arg' describe "Float#arg" do it_behaves_like :float_arg, :arg diff --git a/spec/ruby/core/float/case_compare_spec.rb b/spec/ruby/core/float/case_compare_spec.rb index c2ad2941e0..b902fbea18 100644 --- a/spec/ruby/core/float/case_compare_spec.rb +++ b/spec/ruby/core/float/case_compare_spec.rb @@ -1,5 +1,5 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../shared/equal', __FILE__) +require_relative '../../spec_helper' +require_relative 'shared/equal' describe "Float#===" do it_behaves_like :float_equal, :=== diff --git a/spec/ruby/core/float/ceil_spec.rb b/spec/ruby/core/float/ceil_spec.rb index f1d9dcd823..75f5610292 100644 --- a/spec/ruby/core/float/ceil_spec.rb +++ b/spec/ruby/core/float/ceil_spec.rb @@ -1,6 +1,11 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' +require_relative '../integer/shared/integer_ceil_precision' describe "Float#ceil" do + context "with precision" 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) @@ -11,13 +16,11 @@ describe "Float#ceil" do +9223372036854775808.1.ceil.should eql(+9223372036854775808) end - ruby_version_is "2.4" do - 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) - 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) end end diff --git a/spec/ruby/core/float/coerce_spec.rb b/spec/ruby/core/float/coerce_spec.rb index 90475f2680..baa831dcf6 100644 --- a/spec/ruby/core/float/coerce_spec.rb +++ b/spec/ruby/core/float/coerce_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' describe "Float#coerce" do it "returns [other, self] both as Floats" do @@ -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 49c3debbe1..0cd290f4ad 100644 --- a/spec/ruby/core/float/comparison_spec.rb +++ b/spec/ruby/core/float/comparison_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' describe "Float#<=>" do it "returns -1, 0, 1 when self is less than, equal, or greater than other" do @@ -7,30 +7,107 @@ 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".freeze).should be_nil + (1.0 <=> :one).should be_nil + (1.0 <=> true).should be_nil 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 - (infinity_value <=> Float::MAX.to_i*2).should == 1 + it "compares using #coerce when argument is not a Float" do + klass = Class.new do + attr_reader :call_count + def coerce(other) + @call_count ||= 0 + @call_count += 1 + [other, 42.0] + end + end + + coercible = klass.new + (2.33 <=> coercible).should == -1 + (42.0 <=> coercible).should == 0 + (43.0 <=> coercible).should == 1 + coercible.call_count.should == 3 end - it "returns -1 when self is negative and other is Infinty" do - (-Float::MAX.to_i*2 <=> infinity_value).should == -1 + it "raises TypeError when #coerce misbehaves" do + klass = Class.new do + def coerce(other) + :incorrect + end + end + + bad_coercible = klass.new + -> { + 4.2 <=> bad_coercible + }.should raise_error(TypeError, "coerce must return [x, y]") end - it "returns -1 when self is -Infinity and other is negative" 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 infinite?=-1" do + obj = Object.new + def obj.infinite? + -1 + end + (infinity_value <=> obj).should == 1 + end + + 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 31930b125a..497e0ae188 100644 --- a/spec/ruby/core/float/constants_spec.rb +++ b/spec/ruby/core/float/constants_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' describe "Float constant" do it "DIG is 15" do diff --git a/spec/ruby/core/float/denominator_spec.rb b/spec/ruby/core/float/denominator_spec.rb index 56f5d288cf..6f4fcfcf23 100644 --- a/spec/ruby/core/float/denominator_spec.rb +++ b/spec/ruby/core/float/denominator_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' describe "Float#denominator" do before :each do diff --git a/spec/ruby/core/float/divide_spec.rb b/spec/ruby/core/float/divide_spec.rb index 0acd7b20b4..72ab7527bd 100644 --- a/spec/ruby/core/float/divide_spec.rb +++ b/spec/ruby/core/float/divide_spec.rb @@ -1,7 +1,10 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../fixtures/coerce.rb', __FILE__) +require_relative '../../spec_helper' +require_relative 'fixtures/coerce' +require_relative 'shared/arithmetic_exception_in_coerce' describe "Float#/" do + it_behaves_like :float_arithmetic_exception_in_coerce, :/ + it "returns self divided by other" do (5.75 / -2).should be_close(-2.875,TOLERANCE) (451.0 / 9.3).should be_close(48.494623655914,TOLERANCE) @@ -30,7 +33,11 @@ describe "Float#/" do end it "raises a TypeError when given a non-Numeric" do - lambda { 13.0 / "10" }.should raise_error(TypeError) - lambda { 13.0 / :symbol }.should raise_error(TypeError) + -> { 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 174f142c86..dad45a9b89 100644 --- a/spec/ruby/core/float/divmod_spec.rb +++ b/spec/ruby/core/float/divmod_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' describe "Float#divmod" do it "returns an [quotient, modulus] from dividing self by other" do @@ -10,7 +10,7 @@ describe "Float#divmod" do 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[1].should be_close(18446744073709551616.0, TOLERANCE) values = -1.0.divmod(1) values[0].should eql(-1) values[1].should eql(0.0) @@ -18,22 +18,22 @@ describe "Float#divmod" do # Behaviour established as correct in r23953 it "raises a FloatDomainError if self is NaN" do - lambda { nan_value.divmod(1) }.should raise_error(FloatDomainError) + -> { nan_value.divmod(1) }.should raise_error(FloatDomainError) end # Behaviour established as correct in r23953 it "raises a FloatDomainError if other is NaN" do - lambda { 1.divmod(nan_value) }.should raise_error(FloatDomainError) + -> { 1.0.divmod(nan_value) }.should raise_error(FloatDomainError) end # Behaviour established as correct in r23953 it "raises a FloatDomainError if self is Infinity" do - lambda { infinity_value.divmod(1) }.should raise_error(FloatDomainError) + -> { infinity_value.divmod(1) }.should raise_error(FloatDomainError) end it "raises a ZeroDivisionError if other is zero" do - lambda { 1.0.divmod(0) }.should raise_error(ZeroDivisionError) - lambda { 1.0.divmod(0.0) }.should raise_error(ZeroDivisionError) + -> { 1.0.divmod(0) }.should raise_error(ZeroDivisionError) + -> { 1.0.divmod(0.0) }.should raise_error(ZeroDivisionError) end # redmine #5276" diff --git a/spec/ruby/core/float/dup_spec.rb b/spec/ruby/core/float/dup_spec.rb index 775dc2913c..294da8e2bc 100644 --- a/spec/ruby/core/float/dup_spec.rb +++ b/spec/ruby/core/float/dup_spec.rb @@ -1,10 +1,8 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' -ruby_version_is '2.4' do - describe "Float#dup" do - it "returns self" do - float = 2.4 - float.dup.should equal(float) - end +describe "Float#dup" do + it "returns self" do + float = 2.4 + 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 7c4eef8523..6b5f91db33 100644 --- a/spec/ruby/core/float/eql_spec.rb +++ b/spec/ruby/core/float/eql_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' describe "Float#eql?" do it "returns true if other is a Float equal to self" do diff --git a/spec/ruby/core/float/equal_value_spec.rb b/spec/ruby/core/float/equal_value_spec.rb index 19ef01fc1c..03eea5108e 100644 --- a/spec/ruby/core/float/equal_value_spec.rb +++ b/spec/ruby/core/float/equal_value_spec.rb @@ -1,5 +1,5 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../shared/equal', __FILE__) +require_relative '../../spec_helper' +require_relative 'shared/equal' describe "Float#==" do it_behaves_like :float_equal, :== diff --git a/spec/ruby/core/float/exponent_spec.rb b/spec/ruby/core/float/exponent_spec.rb index 5cbba43a27..a4c03469a7 100644 --- a/spec/ruby/core/float/exponent_spec.rb +++ b/spec/ruby/core/float/exponent_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' describe "Float#**" do it "returns self raise to the other power" do diff --git a/spec/ruby/core/float/fdiv_spec.rb b/spec/ruby/core/float/fdiv_spec.rb index 632dd0f293..be25ee283b 100644 --- a/spec/ruby/core/float/fdiv_spec.rb +++ b/spec/ruby/core/float/fdiv_spec.rb @@ -1,5 +1,5 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../shared/quo', __FILE__) +require_relative '../../spec_helper' +require_relative 'shared/quo' describe "Float#fdiv" do it_behaves_like :float_quo, :fdiv diff --git a/spec/ruby/core/float/finite_spec.rb b/spec/ruby/core/float/finite_spec.rb index d6a161d4e3..d839b30e32 100644 --- a/spec/ruby/core/float/finite_spec.rb +++ b/spec/ruby/core/float/finite_spec.rb @@ -1,19 +1,19 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' describe "Float#finite?" do it "returns true for finite values" do - 3.14159.finite?.should == true + 3.14159.should.finite? end it "returns false for positive infinity" do - infinity_value.finite?.should == false + infinity_value.should_not.finite? end it "returns false for negative infinity" do - (-infinity_value).finite?.should == false + (-infinity_value).should_not.finite? end it "returns false for NaN" do - nan_value.finite?.should == false + nan_value.should_not.finite? end end diff --git a/spec/ruby/core/float/fixtures/classes.rb b/spec/ruby/core/float/fixtures/classes.rb new file mode 100644 index 0000000000..2d80184e7d --- /dev/null +++ b/spec/ruby/core/float/fixtures/classes.rb @@ -0,0 +1,4 @@ +module FloatSpecs + class CoerceError < StandardError + end +end diff --git a/spec/ruby/core/float/float_spec.rb b/spec/ruby/core/float/float_spec.rb index f2931d184c..263ae82079 100644 --- a/spec/ruby/core/float/float_spec.rb +++ b/spec/ruby/core/float/float_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' describe "Float" do it "includes Comparable" do @@ -6,13 +6,13 @@ describe "Float" do end it ".allocate raises a TypeError" do - lambda do + -> do Float.allocate end.should raise_error(TypeError) end it ".new is undefined" do - lambda do + -> do Float.new end.should raise_error(NoMethodError) end diff --git a/spec/ruby/core/float/floor_spec.rb b/spec/ruby/core/float/floor_spec.rb index 6da5d5c5ee..8b492ef473 100644 --- a/spec/ruby/core/float/floor_spec.rb +++ b/spec/ruby/core/float/floor_spec.rb @@ -1,6 +1,11 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' +require_relative '../integer/shared/integer_floor_precision' describe "Float#floor" do + context "with precision" 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) @@ -11,13 +16,11 @@ describe "Float#floor" do +9223372036854775808.1.floor.should eql(+9223372036854775808) end - ruby_version_is "2.4" do - 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) - 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) end end diff --git a/spec/ruby/core/float/gt_spec.rb b/spec/ruby/core/float/gt_spec.rb index 9725c6acd7..33078e07ce 100644 --- a/spec/ruby/core/float/gt_spec.rb +++ b/spec/ruby/core/float/gt_spec.rb @@ -1,6 +1,9 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' +require_relative 'shared/comparison_exception_in_coerce' describe "Float#>" do + it_behaves_like :float_comparison_exception_in_coerce, :> + it "returns true if self is greater than other" do (1.5 > 1).should == true (2.5 > 3).should == false @@ -8,7 +11,28 @@ describe "Float#>" do end it "raises an ArgumentError when given a non-Numeric" do - lambda { 5.0 > "4" }.should raise_error(ArgumentError) - lambda { 5.0 > mock('x') }.should raise_error(ArgumentError) + -> { 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 2c14651dd7..44c0a81b43 100644 --- a/spec/ruby/core/float/gte_spec.rb +++ b/spec/ruby/core/float/gte_spec.rb @@ -1,6 +1,9 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' +require_relative 'shared/comparison_exception_in_coerce' describe "Float#>=" do + it_behaves_like :float_comparison_exception_in_coerce, :>= + it "returns true if self is greater than or equal to other" do (5.2 >= 5.2).should == true (9.71 >= 1).should == true @@ -8,7 +11,28 @@ describe "Float#>=" do end it "raises an ArgumentError when given a non-Numeric" do - lambda { 5.0 >= "4" }.should raise_error(ArgumentError) - lambda { 5.0 >= mock('x') }.should raise_error(ArgumentError) + -> { 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/hash_spec.rb b/spec/ruby/core/float/hash_spec.rb index c638e99347..5f77e3b4a1 100644 --- a/spec/ruby/core/float/hash_spec.rb +++ b/spec/ruby/core/float/hash_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' describe "Float#hash" do it "is provided" do diff --git a/spec/ruby/core/float/infinite_spec.rb b/spec/ruby/core/float/infinite_spec.rb index 4e31effab7..901c2738aa 100644 --- a/spec/ruby/core/float/infinite_spec.rb +++ b/spec/ruby/core/float/infinite_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' describe "Float#infinite?" do it "returns nil for finite values" do diff --git a/spec/ruby/core/float/inspect_spec.rb b/spec/ruby/core/float/inspect_spec.rb new file mode 100644 index 0000000000..4be1927d84 --- /dev/null +++ b/spec/ruby/core/float/inspect_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require_relative 'shared/to_s' + +describe "Float#inspect" do + it_behaves_like :float_to_s, :inspect +end diff --git a/spec/ruby/core/float/lt_spec.rb b/spec/ruby/core/float/lt_spec.rb index e2e43b0fb7..94dcfc42f8 100644 --- a/spec/ruby/core/float/lt_spec.rb +++ b/spec/ruby/core/float/lt_spec.rb @@ -1,6 +1,9 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' +require_relative 'shared/comparison_exception_in_coerce' describe "Float#<" do + it_behaves_like :float_comparison_exception_in_coerce, :< + it "returns true if self is less than other" do (71.3 < 91.8).should == true (192.6 < -500).should == false @@ -8,7 +11,28 @@ describe "Float#<" do end it "raises an ArgumentError when given a non-Numeric" do - lambda { 5.0 < "4" }.should raise_error(ArgumentError) - lambda { 5.0 < mock('x') }.should raise_error(ArgumentError) + -> { 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 e2e44b2257..7b5a86ee76 100644 --- a/spec/ruby/core/float/lte_spec.rb +++ b/spec/ruby/core/float/lte_spec.rb @@ -1,6 +1,9 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' +require_relative 'shared/comparison_exception_in_coerce' describe "Float#<=" do + it_behaves_like :float_comparison_exception_in_coerce, :>= + it "returns true if self is less than or equal to other" do (2.0 <= 3.14159).should == true (-2.7183 <= -24).should == false @@ -9,7 +12,28 @@ describe "Float#<=" do end it "raises an ArgumentError when given a non-Numeric" do - lambda { 5.0 <= "4" }.should raise_error(ArgumentError) - lambda { 5.0 <= mock('x') }.should raise_error(ArgumentError) + -> { 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 042356f4c4..7cdd8ef28a 100644 --- a/spec/ruby/core/float/magnitude_spec.rb +++ b/spec/ruby/core/float/magnitude_spec.rb @@ -1,5 +1,6 @@ -require File.expand_path('../shared/abs', __FILE__) +require_relative "../../spec_helper" +require_relative 'shared/abs' describe "Float#magnitude" do - it_behaves_like(:float_abs, :magnitude) + it_behaves_like :float_abs, :magnitude end diff --git a/spec/ruby/core/float/minus_spec.rb b/spec/ruby/core/float/minus_spec.rb index d5c0d863ed..a4281a397b 100644 --- a/spec/ruby/core/float/minus_spec.rb +++ b/spec/ruby/core/float/minus_spec.rb @@ -1,9 +1,12 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' +require_relative 'shared/arithmetic_exception_in_coerce' describe "Float#-" do + it_behaves_like :float_arithmetic_exception_in_coerce, :- + 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 f29e3870da..8ae80a0b05 100644 --- a/spec/ruby/core/float/modulo_spec.rb +++ b/spec/ruby/core/float/modulo_spec.rb @@ -1,10 +1,10 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../shared/modulo', __FILE__) +require_relative '../../spec_helper' +require_relative 'shared/modulo' describe "Float#%" do - it_behaves_like(:float_modulo, :%) + it_behaves_like :float_modulo, :% end describe "Float#modulo" do - it_behaves_like(:float_modulo, :modulo) + it_behaves_like :float_modulo, :modulo end diff --git a/spec/ruby/core/float/multiply_spec.rb b/spec/ruby/core/float/multiply_spec.rb index 14680534c4..2adb8796cd 100644 --- a/spec/ruby/core/float/multiply_spec.rb +++ b/spec/ruby/core/float/multiply_spec.rb @@ -1,14 +1,17 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' +require_relative 'shared/arithmetic_exception_in_coerce' describe "Float#*" do + it_behaves_like :float_arithmetic_exception_in_coerce, :* + 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 - lambda { 13.0 * "10" }.should raise_error(TypeError) - lambda { 13.0 * :symbol }.should raise_error(TypeError) + -> { 13.0 * "10" }.should raise_error(TypeError) + -> { 13.0 * :symbol }.should raise_error(TypeError) end end diff --git a/spec/ruby/core/float/nan_spec.rb b/spec/ruby/core/float/nan_spec.rb index 95a61d8872..c1043ef21b 100644 --- a/spec/ruby/core/float/nan_spec.rb +++ b/spec/ruby/core/float/nan_spec.rb @@ -1,9 +1,9 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' describe "Float#nan?" do it "returns true if self is not a valid IEEE floating-point number" do - 0.0.nan?.should == false - -1.5.nan?.should == false - nan_value.nan?.should == true + 0.0.should_not.nan? + -1.5.should_not.nan? + nan_value.should.nan? 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..511d92ade6 --- /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 be_false + end + end + + describe "on zero" do + it "returns false" do + 0.0.negative?.should be_false + end + end + + describe "on negative zero" do + it "returns false" do + -0.0.negative?.should be_false + end + end + + describe "on negative numbers" do + it "returns true" do + -0.1.negative?.should be_true + end + end + + describe "on NaN" do + it "returns false" do + nan_value.negative?.should be_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 d5a7748a06..29e2d31146 100644 --- a/spec/ruby/core/float/next_float_spec.rb +++ b/spec/ruby/core/float/next_float_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' describe "Float#next_float" do it "returns a float the smallest possible step greater than the receiver" do @@ -44,6 +44,6 @@ describe "Float#next_float" do end it "returns NAN if NAN was the receiver" do - Float::NAN.next_float.nan?.should == true + Float::NAN.next_float.should.nan? end end diff --git a/spec/ruby/core/float/numerator_spec.rb b/spec/ruby/core/float/numerator_spec.rb index 9644d01c23..7832e8f056 100644 --- a/spec/ruby/core/float/numerator_spec.rb +++ b/spec/ruby/core/float/numerator_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' describe "Float#numerator" do before :all do diff --git a/spec/ruby/core/float/phase_spec.rb b/spec/ruby/core/float/phase_spec.rb index 95d0053816..2aa84024b4 100644 --- a/spec/ruby/core/float/phase_spec.rb +++ b/spec/ruby/core/float/phase_spec.rb @@ -1,4 +1,5 @@ -require File.expand_path('../../../shared/complex/float/arg', __FILE__) +require_relative '../../spec_helper' +require_relative 'shared/arg' describe "Float#phase" do it_behaves_like :float_arg, :phase diff --git a/spec/ruby/core/float/plus_spec.rb b/spec/ruby/core/float/plus_spec.rb index a49124d303..e3e19d7f39 100644 --- a/spec/ruby/core/float/plus_spec.rb +++ b/spec/ruby/core/float/plus_spec.rb @@ -1,9 +1,12 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' +require_relative 'shared/arithmetic_exception_in_coerce' describe "Float#+" do + it_behaves_like :float_arithmetic_exception_in_coerce, :+ + 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..575f92a720 --- /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 be_true + end + end + + describe "on zero" do + it "returns false" do + 0.0.positive?.should be_false + end + end + + describe "on negative zero" do + it "returns false" do + -0.0.positive?.should be_false + end + end + + describe "on negative numbers" do + it "returns false" do + -0.1.positive?.should be_false + end + end + + describe "on NaN" do + it "returns false" do + nan_value.positive?.should be_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 e07d78c44c..5e50269da7 100644 --- a/spec/ruby/core/float/prev_float_spec.rb +++ b/spec/ruby/core/float/prev_float_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' describe "Float#prev_float" do it "returns a float the smallest possible step smaller than the receiver" do @@ -44,6 +44,6 @@ describe "Float#prev_float" do end it "returns NAN if NAN was the receiver" do - Float::NAN.prev_float.nan?.should == true + Float::NAN.prev_float.should.nan? end end diff --git a/spec/ruby/core/float/quo_spec.rb b/spec/ruby/core/float/quo_spec.rb index dcda8f5f3b..b5c64f9d87 100644 --- a/spec/ruby/core/float/quo_spec.rb +++ b/spec/ruby/core/float/quo_spec.rb @@ -1,5 +1,5 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../shared/quo', __FILE__) +require_relative '../../spec_helper' +require_relative 'shared/quo' describe "Float#quo" do it_behaves_like :float_quo, :quo diff --git a/spec/ruby/core/float/rationalize_spec.rb b/spec/ruby/core/float/rationalize_spec.rb index 541080c48b..0c5bef7ac4 100644 --- a/spec/ruby/core/float/rationalize_spec.rb +++ b/spec/ruby/core/float/rationalize_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' describe "Float#rationalize" do it "returns self as a simplified Rational with no argument" do @@ -29,15 +29,15 @@ describe "Float#rationalize" do end it "raises a FloatDomainError for Infinity" do - lambda {infinity_value.rationalize}.should raise_error(FloatDomainError) + -> {infinity_value.rationalize}.should raise_error(FloatDomainError) end it "raises a FloatDomainError for NaN" do - lambda { nan_value.rationalize }.should raise_error(FloatDomainError) + -> { nan_value.rationalize }.should raise_error(FloatDomainError) end it "raises ArgumentError when passed more than one argument" do - lambda { 0.3.rationalize(0.1, 0.1) }.should raise_error(ArgumentError) - lambda { 0.3.rationalize(0.1, 0.1, 2) }.should raise_error(ArgumentError) + -> { 0.3.rationalize(0.1, 0.1) }.should raise_error(ArgumentError) + -> { 0.3.rationalize(0.1, 0.1, 2) }.should raise_error(ArgumentError) end end diff --git a/spec/ruby/core/float/round_spec.rb b/spec/ruby/core/float/round_spec.rb index 56668d5856..7e8c792051 100644 --- a/spec/ruby/core/float/round_spec.rb +++ b/spec/ruby/core/float/round_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' describe "Float#round" do it "returns the nearest Integer" do @@ -10,17 +10,15 @@ describe "Float#round" do 0.0.round.should == 0 end - platform_is_not :mingw32 do - it "returns the nearest Integer for Float near the limit" do - 0.49999999999999994.round.should == 0 - -0.49999999999999994.round.should == 0 - end + it "returns the nearest Integer for Float near the limit" do + 0.49999999999999994.round.should == 0 + -0.49999999999999994.round.should == 0 end it "raises FloatDomainError for exceptional values" do - lambda { (+infinity_value).round }.should raise_error(FloatDomainError) - lambda { (-infinity_value).round }.should raise_error(FloatDomainError) - lambda { nan_value.round }.should raise_error(FloatDomainError) + -> { (+infinity_value).round }.should raise_error(FloatDomainError) + -> { (-infinity_value).round }.should raise_error(FloatDomainError) + -> { nan_value.round }.should raise_error(FloatDomainError) end it "rounds self to an optionally given precision" do @@ -32,25 +30,30 @@ describe "Float#round" do 12.345678.round(3.999).should == 12.346 end - it "returns zero when passed a negative argument with magitude greater the magitude of the whole number portion of the Float" do + 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) end it "raises a TypeError when its argument can not be converted to an Integer" do - lambda { 1.0.round("4") }.should raise_error(TypeError) - lambda { 1.0.round(nil) }.should raise_error(TypeError) + -> { 1.0.round("4") }.should raise_error(TypeError) + -> { 1.0.round(nil) }.should raise_error(TypeError) end it "raises FloatDomainError for exceptional values when passed a non-positive precision" do - lambda { Float::INFINITY.round( 0) }.should raise_error(FloatDomainError) - lambda { Float::INFINITY.round(-2) }.should raise_error(FloatDomainError) - lambda { (-Float::INFINITY).round( 0) }.should raise_error(FloatDomainError) - lambda { (-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_error(FloatDomainError) + -> { (-Float::INFINITY).round(-2) }.should raise_error(FloatDomainError) end it "raises RangeError for NAN when passed a non-positive precision" do - lambda { Float::NAN.round(0) }.should raise_error(RangeError) - lambda { Float::NAN.round(-2) }.should raise_error(RangeError) + -> { Float::NAN.round(0) }.should raise_error(RangeError) + -> { Float::NAN.round(-2) }.should raise_error(RangeError) end it "returns self for exceptional values when passed a non-negative precision" do @@ -70,6 +73,10 @@ 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 ) @@ -85,17 +92,112 @@ describe "Float#round" do -2.4e200.round(-200).should eql( -2 * 10 ** 200 ) end - ruby_version_is "2.4" do - it "returns different rounded values depending on the half option" do - 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: :up).should eql(4) - 3.5.round(half: :down).should eql(3) - 3.5.round(half: :even).should eql(4) - (-2.5).round(half: :up).should eql(-3) - (-2.5).round(half: :down).should eql(-2) - (-2.5).round(half: :even).should eql(-2) + 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) + 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) + 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 + -> { (+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) + end + + it "raise for a non-existent round mode" do + -> { 14.2.round(half: :nonsense) }.should raise_error(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) == 0 end end end diff --git a/spec/ruby/core/float/shared/abs.rb b/spec/ruby/core/float/shared/abs.rb index c59198bfe2..607983322d 100644 --- a/spec/ruby/core/float/shared/abs.rb +++ b/spec/ruby/core/float/shared/abs.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../../spec_helper', __FILE__) +require_relative '../../../spec_helper' describe :float_abs, shared: true do it "returns the absolute value" do diff --git a/spec/ruby/core/float/shared/arg.rb b/spec/ruby/core/float/shared/arg.rb new file mode 100644 index 0000000000..136cf19ec8 --- /dev/null +++ b/spec/ruby/core/float/shared/arg.rb @@ -0,0 +1,36 @@ +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 new file mode 100644 index 0000000000..eec92d858f --- /dev/null +++ b/spec/ruby/core/float/shared/arithmetic_exception_in_coerce.rb @@ -0,0 +1,11 @@ +require_relative '../fixtures/classes' + +describe :float_arithmetic_exception_in_coerce, shared: true do + it "does not rescue exception raised in other#coerce" do + b = mock("numeric with failed #coerce") + b.should_receive(:coerce).and_raise(FloatSpecs::CoerceError) + + # e.g. 1.0 > b + -> { 1.0.send(@method, b) }.should raise_error(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 new file mode 100644 index 0000000000..3e2c1e28dd --- /dev/null +++ b/spec/ruby/core/float/shared/comparison_exception_in_coerce.rb @@ -0,0 +1,11 @@ +require_relative '../fixtures/classes' + +describe :float_comparison_exception_in_coerce, shared: true do + it "does not rescue exception raised in other#coerce" do + b = mock("numeric with failed #coerce") + b.should_receive(:coerce).and_raise(FloatSpecs::CoerceError) + + # e.g. 1.0 > b + -> { 1.0.send(@method, b) }.should raise_error(FloatSpecs::CoerceError) + end +end 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/modulo.rb b/spec/ruby/core/float/shared/modulo.rb index 6c423a3a28..6700bd4f4e 100644 --- a/spec/ruby/core/float/shared/modulo.rb +++ b/spec/ruby/core/float/shared/modulo.rb @@ -42,7 +42,7 @@ describe :float_modulo, shared: true do end it "raises a ZeroDivisionError if other is zero" do - lambda { 1.0.send(@method, 0) }.should raise_error(ZeroDivisionError) - lambda { 1.0.send(@method, 0.0) }.should raise_error(ZeroDivisionError) + -> { 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 index afc921a2c1..0c90171b87 100644 --- a/spec/ruby/core/float/shared/quo.rb +++ b/spec/ruby/core/float/shared/quo.rb @@ -1,9 +1,9 @@ describe :float_quo, shared: true do - it "performs floating-point division between self and a Fixnum" do + it "performs floating-point division between self and an Integer" do 8.9.send(@method, 7).should == 1.2714285714285716 end - it "performs floating-point division between self and a Bignum" do + it "performs floating-point division between self and an Integer" do 8.9.send(@method, 9999999999999**9).should == 8.900000000008011e-117 end @@ -50,10 +50,10 @@ describe :float_quo, shared: true do end it "raises a TypeError when argument isn't numeric" do - lambda { 27292.2.send(@method, mock('non-numeric')) }.should raise_error(TypeError) + -> { 27292.2.send(@method, mock('non-numeric')) }.should raise_error(TypeError) end it "raises an ArgumentError when passed multiple arguments" do - lambda { 272.221.send(@method, 6,0.2) }.should raise_error(ArgumentError) + -> { 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..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 diff --git a/spec/ruby/core/float/shared/to_s.rb b/spec/ruby/core/float/shared/to_s.rb new file mode 100644 index 0000000000..0925efc0f7 --- /dev/null +++ b/spec/ruby/core/float/shared/to_s.rb @@ -0,0 +1,308 @@ +describe :float_to_s, shared: true do + it "returns 'NaN' for NaN" do + nan_value().send(@method).should == 'NaN' + end + + it "returns 'Infinity' for positive infinity" do + infinity_value().send(@method).should == 'Infinity' + end + + it "returns '-Infinity' for negative infinity" do + (-infinity_value()).send(@method).should == '-Infinity' + end + + it "returns '0.0' for 0.0" do + 0.0.send(@method).should == "0.0" + end + + platform_is_not :openbsd do + it "emits '-' for -0.0" do + -0.0.send(@method).should == "-0.0" + end + end + + it "emits a '-' for negative values" do + -3.14.send(@method).should == "-3.14" + end + + it "emits a trailing '.0' for a whole number" do + 50.0.send(@method).should == "50.0" + end + + it "emits a trailing '.0' for the mantissa in e format" do + 1.0e20.send(@method).should == "1.0e+20" + end + + it "uses non-e format for a positive value with fractional part having 5 significant figures" do + 0.0001.send(@method).should == "0.0001" + end + + it "uses non-e format for a negative value with fractional part having 5 significant figures" do + -0.0001.send(@method).should == "-0.0001" + end + + it "uses e format for a positive value with fractional part having 6 significant figures" do + 0.00001.send(@method).should == "1.0e-05" + end + + it "uses e format for a negative value with fractional part having 6 significant figures" do + -0.00001.send(@method).should == "-1.0e-05" + end + + it "uses non-e format for a positive value with whole part having 15 significant figures" do + 10000000000000.0.send(@method).should == "10000000000000.0" + end + + it "uses non-e format for a negative value with whole part having 15 significant figures" do + -10000000000000.0.send(@method).should == "-10000000000000.0" + end + + it "uses non-e format for a positive value with whole part having 16 significant figures" do + 100000000000000.0.send(@method).should == "100000000000000.0" + end + + it "uses non-e format for a negative value with whole part having 16 significant figures" do + -100000000000000.0.send(@method).should == "-100000000000000.0" + end + + it "uses e format for a positive value with whole part having 18 significant figures" do + 10000000000000000.0.send(@method).should == "1.0e+16" + end + + it "uses e format for a negative value with whole part having 18 significant figures" do + -10000000000000000.0.send(@method).should == "-1.0e+16" + end + + it "uses e format for a positive value with whole part having 17 significant figures" do + 1000000000000000.0.send(@method).should == "1.0e+15" + end + + it "uses e format for a negative value with whole part having 17 significant figures" do + -1000000000000000.0.send(@method).should == "-1.0e+15" + end + + # #3273 + it "outputs the minimal, unique form necessary to recreate the value" do + value = 0.21611564636388508 + string = "0.21611564636388508" + + value.send(@method).should == string + string.to_f.should == value + end + + it "outputs the minimal, unique form to represent the value" do + 0.56.send(@method).should == "0.56" + end + + describe "matches" do + it "random examples in all ranges" do + # 50.times do + # bytes = (0...8).map { rand(256) } + # string = bytes.pack('C8') + # float = string.unpack('D').first + # puts "#{'%.20g' % float}.send(@method).should == #{float.send(@method).inspect}" + # end + + 2.5540217314354050325e+163.send(@method).should == "2.554021731435405e+163" + 2.5492588360356597544e-172.send(@method).should == "2.5492588360356598e-172" + 1.742770260934704852e-82.send(@method).should == "1.7427702609347049e-82" + 6.2108093676180883209e-104.send(@method).should == "6.210809367618088e-104" + -3.3448803488331067402e-143.send(@method).should == "-3.3448803488331067e-143" + -2.2740074343500832557e-168.send(@method).should == "-2.2740074343500833e-168" + 7.0587971678048535732e+191.send(@method).should == "7.058797167804854e+191" + -284438.88327586348169.send(@method).should == "-284438.8832758635" + 3.953272468476091301e+105.send(@method).should == "3.9532724684760913e+105" + -3.6361359552959847853e+100.send(@method).should == "-3.636135955295985e+100" + -1.3222325865575206185e-31.send(@method).should == "-1.3222325865575206e-31" + 1.1440138916932761366e+130.send(@method).should == "1.1440138916932761e+130" + 4.8750891560387561157e-286.send(@method).should == "4.875089156038756e-286" + 5.6101113356591453525e-257.send(@method).should == "5.610111335659145e-257" + -3.829644279545809575e-100.send(@method).should == "-3.8296442795458096e-100" + 1.5342839401396406117e-194.send(@method).should == "1.5342839401396406e-194" + 2.2284972755169921402e-144.send(@method).should == "2.228497275516992e-144" + 2.1825655917065601737e-61.send(@method).should == "2.1825655917065602e-61" + -2.6672271363524338322e-62.send(@method).should == "-2.667227136352434e-62" + -1.9257995160119059415e+21.send(@method).should == "-1.925799516011906e+21" + -8.9096732962887121718e-198.send(@method).should == "-8.909673296288712e-198" + 2.0202075376548644959e-90.send(@method).should == "2.0202075376548645e-90" + -7.7341602581786258961e-266.send(@method).should == "-7.734160258178626e-266" + 3.5134482598733635046e+98.send(@method).should == "3.5134482598733635e+98" + -2.124411722371029134e+154.send(@method).should == "-2.124411722371029e+154" + -4.573908787355718687e+110.send(@method).should == "-4.573908787355719e+110" + -1.9344425934170969879e-232.send(@method).should == "-1.934442593417097e-232" + -1.3274227399979271095e+171.send(@method).should == "-1.3274227399979271e+171" + 9.3495270482104442383e-283.send(@method).should == "9.349527048210444e-283" + -4.2046059371986483233e+307.send(@method).should == "-4.2046059371986483e+307" + 3.6133547278583543004e-117.send(@method).should == "3.613354727858354e-117" + 4.9247416523566613499e-08.send(@method).should == "4.9247416523566613e-08" + 1.6936145488250064007e-71.send(@method).should == "1.6936145488250064e-71" + 2.4455483206829433098e+96.send(@method).should == "2.4455483206829433e+96" + 7.9797449851436455384e+124.send(@method).should == "7.979744985143646e+124" + -1.3873689634457876774e-129.send(@method).should == "-1.3873689634457877e-129" + 3.9761102037533483075e+284.send(@method).should == "3.976110203753348e+284" + -4.2819791952139402486e-303.send(@method).should == "-4.28197919521394e-303" + -5.7981017546689831298e-116.send(@method).should == "-5.798101754668983e-116" + -3.953266497860534199e-28.send(@method).should == "-3.953266497860534e-28" + -2.0659852720290440959e-243.send(@method).should == "-2.065985272029044e-243" + 8.9670488995878688018e-05.send(@method).should == "8.967048899587869e-05" + -1.2317943708113061768e-98.send(@method).should == "-1.2317943708113062e-98" + -3.8930768307633080463e+248.send(@method).should == "-3.893076830763308e+248" + 6.5854032671803925627e-239.send(@method).should == "6.5854032671803926e-239" + 4.6257022188980878952e+177.send(@method).should == "4.625702218898088e+177" + -1.9397155125507235603e-187.send(@method).should == "-1.9397155125507236e-187" + 8.5752156951245705056e+117.send(@method).should == "8.57521569512457e+117" + -2.4784875958162501671e-132.send(@method).should == "-2.4784875958162502e-132" + -4.4125691841230058457e-203.send(@method).should == "-4.412569184123006e-203" + end + + it "random examples in human ranges" do + # 50.times do + # formatted = '' + # rand(1..3).times do + # formatted << rand(10).to_s + # end + # formatted << '.' + # rand(1..9).times do + # formatted << rand(10).to_s + # end + # float = formatted.to_f + # puts "#{'%.20f' % float}.send(@method).should == #{float.send(@method).inspect}" + # end + + 5.17869899999999994122.send(@method).should == "5.178699" + 905.62695729999995819526.send(@method).should == "905.6269573" + 62.75999999999999801048.send(@method).should == "62.76" + 6.93856795800000014651.send(@method).should == "6.938567958" + 4.95999999999999996447.send(@method).should == "4.96" + 32.77993899999999882766.send(@method).should == "32.779939" + 544.12756779999995160324.send(@method).should == "544.1275678" + 66.25801119999999855281.send(@method).should == "66.2580112" + 7.90000000000000035527.send(@method).should == "7.9" + 5.93100000000000004974.send(@method).should == "5.931" + 5.21229313600000043749.send(@method).should == "5.212293136" + 503.44173809000000119340.send(@method).should == "503.44173809" + 79.26000000000000511591.send(@method).should == "79.26" + 8.51524999999999998579.send(@method).should == "8.51525" + 174.00000000000000000000.send(@method).should == "174.0" + 50.39580000000000126192.send(@method).should == "50.3958" + 35.28999999999999914735.send(@method).should == "35.29" + 5.43136675399999990788.send(@method).should == "5.431366754" + 654.07680000000004838512.send(@method).should == "654.0768" + 6.07423700000000010846.send(@method).should == "6.074237" + 102.25779799999999397642.send(@method).should == "102.257798" + 5.08129999999999970584.send(@method).should == "5.0813" + 6.00000000000000000000.send(@method).should == "6.0" + 8.30000000000000071054.send(@method).should == "8.3" + 32.68345999999999662577.send(@method).should == "32.68346" + 581.11170000000004165486.send(@method).should == "581.1117" + 76.31342999999999676675.send(@method).should == "76.31343" + 438.30826000000001840817.send(@method).should == "438.30826" + 482.06631994000002805478.send(@method).should == "482.06631994" + 55.92721026899999969828.send(@method).should == "55.927210269" + 4.00000000000000000000.send(@method).should == "4.0" + 55.86693999999999959982.send(@method).should == "55.86694" + 787.98299999999994724931.send(@method).should == "787.983" + 5.73810511000000023074.send(@method).should == "5.73810511" + 74.51926810000000500622.send(@method).should == "74.5192681" + 892.89999999999997726263.send(@method).should == "892.9" + 68.27299999999999613465.send(@method).should == "68.273" + 904.10000000000002273737.send(@method).should == "904.1" + 5.23200000000000020606.send(@method).should == "5.232" + 4.09628000000000014325.send(@method).should == "4.09628" + 46.05152633699999853434.send(@method).should == "46.051526337" + 142.12884990599999923688.send(@method).should == "142.128849906" + 3.83057023500000015659.send(@method).should == "3.830570235" + 11.81684594699999912848.send(@method).should == "11.816845947" + 80.50000000000000000000.send(@method).should == "80.5" + 382.18215010000000120272.send(@method).should == "382.1821501" + 55.38444606899999911320.send(@method).should == "55.384446069" + 5.78000000000000024869.send(@method).should == "5.78" + 2.88244999999999995666.send(@method).should == "2.88245" + 43.27709999999999723741.send(@method).should == "43.2771" + end + + it "random values from divisions" do + (1.0 / 7).send(@method).should == "0.14285714285714285" + + # 50.times do + # a = rand(10) + # b = rand(10) + # c = rand(10) + # d = rand(10) + # expression = "#{a}.#{b} / #{c}.#{d}" + # puts " (#{expression}).send(@method).should == #{eval(expression).send(@method).inspect}" + # end + + (1.1 / 7.1).send(@method).should == "0.15492957746478875" + (6.5 / 8.8).send(@method).should == "0.7386363636363635" + (4.8 / 4.3).send(@method).should == "1.1162790697674418" + (4.0 / 1.9).send(@method).should == "2.1052631578947367" + (9.1 / 0.8).send(@method).should == "11.374999999999998" + (5.3 / 7.5).send(@method).should == "0.7066666666666667" + (2.8 / 1.8).send(@method).should == "1.5555555555555554" + (2.1 / 2.5).send(@method).should == "0.8400000000000001" + (3.5 / 6.0).send(@method).should == "0.5833333333333334" + (4.6 / 0.3).send(@method).should == "15.333333333333332" + (0.6 / 2.4).send(@method).should == "0.25" + (1.3 / 9.1).send(@method).should == "0.14285714285714288" + (0.3 / 5.0).send(@method).should == "0.06" + (5.0 / 4.2).send(@method).should == "1.1904761904761905" + (3.0 / 2.0).send(@method).should == "1.5" + (6.3 / 2.0).send(@method).should == "3.15" + (5.4 / 6.0).send(@method).should == "0.9" + (9.6 / 8.1).send(@method).should == "1.1851851851851851" + (8.7 / 1.6).send(@method).should == "5.437499999999999" + (1.9 / 7.8).send(@method).should == "0.24358974358974358" + (0.5 / 2.1).send(@method).should == "0.23809523809523808" + (9.3 / 5.8).send(@method).should == "1.6034482758620692" + (2.7 / 8.0).send(@method).should == "0.3375" + (9.7 / 7.8).send(@method).should == "1.2435897435897436" + (8.1 / 2.4).send(@method).should == "3.375" + (7.7 / 2.7).send(@method).should == "2.8518518518518516" + (7.9 / 1.7).send(@method).should == "4.647058823529412" + (6.5 / 8.2).send(@method).should == "0.7926829268292683" + (7.8 / 9.6).send(@method).should == "0.8125" + (2.2 / 4.6).send(@method).should == "0.47826086956521746" + (0.0 / 1.0).send(@method).should == "0.0" + (8.3 / 2.9).send(@method).should == "2.8620689655172415" + (3.1 / 6.1).send(@method).should == "0.5081967213114754" + (2.8 / 7.8).send(@method).should == "0.358974358974359" + (8.0 / 0.1).send(@method).should == "80.0" + (1.7 / 6.4).send(@method).should == "0.265625" + (1.8 / 5.4).send(@method).should == "0.3333333333333333" + (8.0 / 5.8).send(@method).should == "1.3793103448275863" + (5.2 / 4.1).send(@method).should == "1.2682926829268295" + (9.8 / 5.8).send(@method).should == "1.6896551724137934" + (5.4 / 9.5).send(@method).should == "0.5684210526315789" + (8.4 / 4.9).send(@method).should == "1.7142857142857142" + (1.7 / 3.5).send(@method).should == "0.4857142857142857" + (1.2 / 5.1).send(@method).should == "0.23529411764705882" + (1.4 / 2.0).send(@method).should == "0.7" + (4.8 / 8.0).send(@method).should == "0.6" + (9.0 / 2.5).send(@method).should == "3.6" + (0.2 / 0.6).send(@method).should == "0.33333333333333337" + (7.8 / 5.2).send(@method).should == "1.5" + (9.5 / 5.5).send(@method).should == "1.7272727272727273" + end + end + + describe 'encoding' do + before :each do + @internal = Encoding.default_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.send(@method).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.send(@method).encoding.should equal(Encoding::US_ASCII) + end + end +end diff --git a/spec/ruby/core/float/to_f_spec.rb b/spec/ruby/core/float/to_f_spec.rb index 02666ae7d7..6677556cd9 100644 --- a/spec/ruby/core/float/to_f_spec.rb +++ b/spec/ruby/core/float/to_f_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' describe "Float#to_f" do it "returns self" do diff --git a/spec/ruby/core/float/to_i_spec.rb b/spec/ruby/core/float/to_i_spec.rb index 5bf5a1b42a..91d84c5fa3 100644 --- a/spec/ruby/core/float/to_i_spec.rb +++ b/spec/ruby/core/float/to_i_spec.rb @@ -1,6 +1,6 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../shared/to_i', __FILE__) +require_relative '../../spec_helper' +require_relative 'shared/to_i' describe "Float#to_i" do - it_behaves_like(:float_to_i, :to_i) + it_behaves_like :float_to_i, :to_i end diff --git a/spec/ruby/core/float/to_int_spec.rb b/spec/ruby/core/float/to_int_spec.rb index ba31ebc168..084a58b431 100644 --- a/spec/ruby/core/float/to_int_spec.rb +++ b/spec/ruby/core/float/to_int_spec.rb @@ -1,6 +1,6 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../shared/to_i', __FILE__) +require_relative '../../spec_helper' +require_relative 'shared/to_i' describe "Float#to_int" do - it_behaves_like(:float_to_i, :to_int) + it_behaves_like :float_to_i, :to_int end diff --git a/spec/ruby/core/float/to_r_spec.rb b/spec/ruby/core/float/to_r_spec.rb index a0f5cd9700..907ff08f27 100644 --- a/spec/ruby/core/float/to_r_spec.rb +++ b/spec/ruby/core/float/to_r_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' describe "Float#to_r" do it "needs to be reviewed for spec completeness" diff --git a/spec/ruby/core/float/to_s_spec.rb b/spec/ruby/core/float/to_s_spec.rb index e76cfc1b7e..6727a883f8 100644 --- a/spec/ruby/core/float/to_s_spec.rb +++ b/spec/ruby/core/float/to_s_spec.rb @@ -1,120 +1,6 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' +require_relative 'shared/to_s' describe "Float#to_s" do - it "returns 'NaN' for NaN" do - nan_value().to_s.should == 'NaN' - end - - it "returns 'Infinity' for positive infinity" do - infinity_value().to_s.should == 'Infinity' - end - - it "returns '-Infinity' for negative infinity" do - (-infinity_value()).to_s.should == '-Infinity' - end - - it "returns '0.0' for 0.0" do - 0.0.to_s.should == "0.0" - end - - platform_is_not :openbsd do - it "emits '-' for -0.0" do - -0.0.to_s.should == "-0.0" - end - end - - it "emits a '-' for negative values" do - -3.14.to_s.should == "-3.14" - end - - it "emits a trailing '.0' for a whole number" do - 50.0.to_s.should == "50.0" - end - - it "emits a trailing '.0' for the mantissa in e format" do - 1.0e20.to_s.should == "1.0e+20" - end - - it "uses non-e format for a positive value with fractional part having 5 significant figures" do - 0.0001.to_s.should == "0.0001" - end - - it "uses non-e format for a negative value with fractional part having 5 significant figures" do - -0.0001.to_s.should == "-0.0001" - end - - it "uses e format for a positive value with fractional part having 6 significant figures" do - 0.00001.to_s.should == "1.0e-05" - end - - it "uses e format for a negative value with fractional part having 6 significant figures" do - -0.00001.to_s.should == "-1.0e-05" - end - - it "uses non-e format for a positive value with whole part having 15 significant figures" do - 10000000000000.0.to_s.should == "10000000000000.0" - end - - it "uses non-e format for a negative value with whole part having 15 significant figures" do - -10000000000000.0.to_s.should == "-10000000000000.0" - end - - it "uses non-e format for a positive value with whole part having 16 significant figures" do - 100000000000000.0.to_s.should == "100000000000000.0" - end - - it "uses non-e format for a negative value with whole part having 16 significant figures" do - -100000000000000.0.to_s.should == "-100000000000000.0" - end - - it "uses e format for a positive value with whole part having 18 significant figures" do - 10000000000000000.0.to_s.should == "1.0e+16" - end - - it "uses e format for a negative value with whole part having 18 significant figures" do - -10000000000000000.0.to_s.should == "-1.0e+16" - end - - it "uses non-e format for a positive value with whole part having 17 significant figures" do - 1000000000000000.0.to_s.should == "1.0e+15" - end - - it "uses non-e format for a negative value with whole part having 17 significant figures" do - -1000000000000000.0.to_s.should == "-1.0e+15" - end - - # #3273 - it "outputs the minimal, unique form necessary to recreate the value" do - value = 0.21611564636388508 - string = "0.21611564636388508" - - value.to_s.should == string - string.to_f.should == value - end - - it "outputs the minimal, unique form to represent the value" do - 0.56.to_s.should == "0.56" - end -end - -with_feature :encoding do - describe "Float#to_s" do - before :each do - @internal = Encoding.default_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 not nil" do - Encoding.default_internal = Encoding::IBM437 - 5.47.to_s.encoding.should equal(Encoding::US_ASCII) - end - end + it_behaves_like :float_to_s, :to_s end diff --git a/spec/ruby/core/float/truncate_spec.rb b/spec/ruby/core/float/truncate_spec.rb index 7feeb81735..2c80145f9f 100644 --- a/spec/ruby/core/float/truncate_spec.rb +++ b/spec/ruby/core/float/truncate_spec.rb @@ -1,16 +1,14 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../shared/to_i', __FILE__) +require_relative '../../spec_helper' +require_relative 'shared/to_i' describe "Float#truncate" do - it_behaves_like(:float_to_i, :truncate) + it_behaves_like :float_to_i, :truncate - ruby_version_is "2.4" do - 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) - end + 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) end end diff --git a/spec/ruby/core/float/uminus_spec.rb b/spec/ruby/core/float/uminus_spec.rb index 70e2ff0580..57bae0fb4b 100644 --- a/spec/ruby/core/float/uminus_spec.rb +++ b/spec/ruby/core/float/uminus_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' describe "Float#-@" do it "negates self" do @@ -23,6 +23,6 @@ describe "Float#-@" do end it "returns NaN for NaN" do - nan_value.send(:-@).nan?.should == true + nan_value.send(:-@).should.nan? end end diff --git a/spec/ruby/core/float/uplus_spec.rb b/spec/ruby/core/float/uplus_spec.rb index 75f18191dd..936123558c 100644 --- a/spec/ruby/core/float/uplus_spec.rb +++ b/spec/ruby/core/float/uplus_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' describe "Float#+@" do it "returns the same value with same sign (twos complement)" do diff --git a/spec/ruby/core/float/zero_spec.rb b/spec/ruby/core/float/zero_spec.rb index 3a67551a07..1f3de27793 100644 --- a/spec/ruby/core/float/zero_spec.rb +++ b/spec/ruby/core/float/zero_spec.rb @@ -1,9 +1,9 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' describe "Float#zero?" do it "returns true if self is 0.0" do - 0.0.zero?.should == true - 1.0.zero?.should == false - -1.0.zero?.should == false + 0.0.should.zero? + 1.0.should_not.zero? + -1.0.should_not.zero? end end |
