diff options
Diffstat (limited to 'spec/ruby/library/bigdecimal')
67 files changed, 1251 insertions, 782 deletions
diff --git a/spec/ruby/library/bigdecimal/BigDecimal_spec.rb b/spec/ruby/library/bigdecimal/BigDecimal_spec.rb new file mode 100644 index 0000000000..6adebabe84 --- /dev/null +++ b/spec/ruby/library/bigdecimal/BigDecimal_spec.rb @@ -0,0 +1,239 @@ +require_relative '../../spec_helper' +require 'bigdecimal' + +describe "BigDecimal" do + it "is not defined unless it is required" do + ruby_exe('puts Object.const_defined?(:BigDecimal)').should == "false\n" + end +end + +describe "Kernel#BigDecimal" do + + it "creates a new object of class BigDecimal" do + BigDecimal("3.14159").should.is_a?(BigDecimal) + (0..9).each {|i| + BigDecimal("1#{i}").should == 10 + i + BigDecimal("-1#{i}").should == -10 - i + BigDecimal("1E#{i}").should == 10**i + BigDecimal("1000000E-#{i}").should == 10**(6-i).to_f + # ^ to_f to avoid Rational type + } + (1..9).each {|i| + BigDecimal("100.#{i}").to_s.should =~ /\A0\.100#{i}E3\z/i + BigDecimal("-100.#{i}").to_s.should =~ /\A-0\.100#{i}E3\z/i + } + end + + it "BigDecimal(Rational) with bigger-than-double numerator" do + rational = 99999999999999999999/100r + rational.numerator.should > 2**64 + BigDecimal(rational, 100).to_s.should == "0.99999999999999999999e18" + end + + it "accepts significant digits >= given precision" do + BigDecimal("3.1415923", 10).should == BigDecimal("3.1415923") + end + + it "determines precision from initial value" do + pi_string = "3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593014782083152134043" + BigDecimal(pi_string).precision.should == pi_string.size-1 + end + + it "ignores leading and trailing whitespace" do + BigDecimal(" \t\n \r1234\t\r\n ").should == BigDecimal("1234") + BigDecimal(" \t\n \rNaN \n").should.nan? + BigDecimal(" \t\n \rInfinity \n").infinite?.should == 1 + BigDecimal(" \t\n \r-Infinity \n").infinite?.should == -1 + end + + it "coerces the value argument with #to_str" do + initial = mock("value") + initial.should_receive(:to_str).and_return("123") + BigDecimal(initial).should == BigDecimal("123") + end + + it "does not ignores trailing garbage" do + -> { BigDecimal("123E45ruby") }.should.raise(ArgumentError) + -> { BigDecimal("123x45") }.should.raise(ArgumentError) + -> { BigDecimal("123.4%E5") }.should.raise(ArgumentError) + -> { BigDecimal("1E2E3E4E5E") }.should.raise(ArgumentError) + end + + it "raises ArgumentError for invalid strings" do + -> { BigDecimal("ruby") }.should.raise(ArgumentError) + -> { BigDecimal(" \t\n \r-\t\t\tInfinity \n") }.should.raise(ArgumentError) + end + + it "allows omitting the integer part" do + BigDecimal(".123").should == BigDecimal("0.123") + end + + it "process underscores as Float()" do + reference = BigDecimal("12345.67E89") + + BigDecimal("12_345.67E89").should == reference + -> { BigDecimal("1_2_3_4_5_._6____7_E89") }.should.raise(ArgumentError) + -> { BigDecimal("12345_.67E_8__9_") }.should.raise(ArgumentError) + end + + it "accepts NaN and [+-]Infinity" do + BigDecimal("NaN").should.nan? + + pos_inf = BigDecimal("Infinity") + pos_inf.should_not.finite? + pos_inf.should > 0 + pos_inf.should == BigDecimal("+Infinity") + + neg_inf = BigDecimal("-Infinity") + neg_inf.should_not.finite? + neg_inf.should < 0 + end + + describe "with exception: false" do + it "returns nil for invalid strings" do + BigDecimal("invalid", exception: false).should == nil + BigDecimal("0invalid", exception: false).should == nil + BigDecimal("invalid0", exception: false).should == nil + if BigDecimal::VERSION >= "3.1.9" + BigDecimal("0.", exception: false).to_i.should == 0 + else + BigDecimal("0.", exception: false).should == nil + end + end + end + + describe "accepts NaN and [+-]Infinity as Float values" do + it "works without an explicit precision" do + BigDecimal(Float::NAN).should.nan? + + pos_inf = BigDecimal(Float::INFINITY) + pos_inf.should_not.finite? + pos_inf.should > 0 + pos_inf.should == BigDecimal("+Infinity") + + neg_inf = BigDecimal(-Float::INFINITY) + neg_inf.should_not.finite? + neg_inf.should < 0 + end + + it "works with an explicit precision" do + BigDecimal(Float::NAN, Float::DIG).should.nan? + + pos_inf = BigDecimal(Float::INFINITY, Float::DIG) + pos_inf.should_not.finite? + pos_inf.should > 0 + pos_inf.should == BigDecimal("+Infinity") + + neg_inf = BigDecimal(-Float::INFINITY, Float::DIG) + neg_inf.should_not.finite? + neg_inf.should < 0 + end + end + + it "allows for [eEdD] as exponent separator" do + reference = BigDecimal("12345.67E89") + + BigDecimal("12345.67e89").should == reference + BigDecimal("12345.67E89").should == reference + BigDecimal("12345.67d89").should == reference + BigDecimal("12345.67D89").should == reference + end + + it "allows for varying signs" do + reference = BigDecimal("123.456E1") + + BigDecimal("+123.456E1").should == reference + BigDecimal("-123.456E1").should == -reference + BigDecimal("123.456E+1").should == reference + BigDecimal("12345.6E-1").should == reference + BigDecimal("+123.456E+1").should == reference + BigDecimal("+12345.6E-1").should == reference + BigDecimal("-123.456E+1").should == -reference + BigDecimal("-12345.6E-1").should == -reference + end + + version_is BigDecimal::VERSION, "3.3.0" do + it "allows Float without precision" do + BigDecimal(1.2).should == BigDecimal("1.2") + end + end + + it "returns appropriate BigDecimal zero for signed zero" do + BigDecimal(-0.0, Float::DIG).sign.should == -1 + BigDecimal(0.0, Float::DIG).sign.should == 1 + end + + it "pre-coerces long integers" do + BigDecimal(3).add(1 << 50, 3).should == BigDecimal('0.113e16') + end + + it "does not call to_s when calling inspect" do + value = BigDecimal('44.44') + value.to_s.should == '0.4444e2' + value.inspect.should == '0.4444e2' + + ruby_exe( <<-'EOF').should == "cheese 0.4444e2" + require 'bigdecimal' + module BigDecimalOverride + def to_s; "cheese"; end + end + BigDecimal.prepend BigDecimalOverride + value = BigDecimal('44.44') + print "#{value.to_s} #{value.inspect}" + EOF + end + + describe "when interacting with Rational" do + before :each do + @a = BigDecimal('166.666666666') + @b = Rational(500, 3) + @c = @a - @b + end + + # Check the input is as we understand it + + it "has the LHS print as expected" do + @a.to_s.should == "0.166666666666e3" + @a.to_f.to_s.should == "166.666666666" + Float(@a).to_s.should == "166.666666666" + end + + it "has the RHS print as expected" do + @b.to_s.should == "500/3" + @b.to_f.to_s.should == "166.66666666666666" + Float(@b).to_s.should == "166.66666666666666" + end + + it "produces the expected result when done via Float" do + (Float(@a) - Float(@b)).to_s.should == "-6.666596163995564e-10" + end + + it "produces the expected result when done via to_f" do + (@a.to_f - @b.to_f).to_s.should == "-6.666596163995564e-10" + end + + # Check underlying methods work as we understand + + it "BigDecimal(Rational, 18) produces the result we expect" do + BigDecimal(@b, 18).to_s.should == "0.166666666666666667e3" + end + + # Check the top-level expression works as we expect + + it "produces a BigDecimal" do + @c.class.should == BigDecimal + end + + it "produces the expected result" do + @c.round(15).should == BigDecimal("-0.666667e-9") + @c.round(15).to_s.should == "-0.666667e-9" + end + + it "produces the correct class for other arithmetic operators" do + (@a + @b).class.should == BigDecimal + (@a * @b).class.should == BigDecimal + (@a / @b).class.should == BigDecimal + (@a % @b).class.should == BigDecimal + end + end +end diff --git a/spec/ruby/library/bigdecimal/abs_spec.rb b/spec/ruby/library/bigdecimal/abs_spec.rb index 9027abfb47..95dc45a905 100644 --- a/spec/ruby/library/bigdecimal/abs_spec.rb +++ b/spec/ruby/library/bigdecimal/abs_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'bigdecimal' describe "BigDecimal#abs" do @@ -38,7 +38,7 @@ describe "BigDecimal#abs" do it "properly handles special values" do @infinity.abs.should == @infinity @infinity_minus.abs.should == @infinity - @nan.abs.nan?.should == true # have to do it this way, since == doesn't work on NaN + @nan.abs.should.nan? # have to do it this way, since == doesn't work on NaN @zero.abs.should == 0 @zero.abs.sign.should == BigDecimal::SIGN_POSITIVE_ZERO @zero_pos.abs.should == 0 diff --git a/spec/ruby/library/bigdecimal/add_spec.rb b/spec/ruby/library/bigdecimal/add_spec.rb index 6136c9dccb..a4237298f4 100644 --- a/spec/ruby/library/bigdecimal/add_spec.rb +++ b/spec/ruby/library/bigdecimal/add_spec.rb @@ -1,5 +1,5 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../fixtures/classes', __FILE__) +require_relative '../../spec_helper' +require_relative 'fixtures/classes' require 'bigdecimal' @@ -24,7 +24,7 @@ describe "BigDecimal#add" do end it "returns a + b with given precision" do - # documentation states, that precision ist optional, but it ain't, + # documentation states that precision is optional, but it ain't, @two.add(@one, 1).should == @three @one .add(@two, 1).should == @three @one.add(@one_minus, 1).should == @zero @@ -60,7 +60,7 @@ describe "BigDecimal#add" do end # TODO: -# http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/17374 +# https://blade.ruby-lang.org/ruby-core/17374 # # This doesn't work on MRI and looks like a bug to me: # one can use BigDecimal + Float, but not Bigdecimal.add(Float) @@ -73,6 +73,12 @@ describe "BigDecimal#add" do # BigDecimal("0.88").add(0.0, 1).should == BigDecimal("0.9") # end + describe "with Rational" do + it "produces a BigDecimal" do + (@three + Rational(500, 2)).should == BigDecimal("0.253e3") + end + end + it "favors the precision specified in the second argument over the global limit" do BigDecimalSpecs.with_limit(1) do BigDecimal('0.888').add(@zero, 3).should == BigDecimal('0.888') @@ -122,8 +128,8 @@ describe "BigDecimal#add" do end it "returns NaN if NaN is involved" do - @one.add(@nan, 10000).nan?.should == true - @nan.add(@one, 1).nan?.should == true + @one.add(@nan, 10000).should.nan? + @nan.add(@one, 1).should.nan? end it "returns Infinity or -Infinity if these are involved" do @@ -152,28 +158,28 @@ describe "BigDecimal#add" do end it "returns NaN if Infinity + (- Infinity)" do - @infinity.add(@infinity_minus, 10000).nan?.should == true - @infinity_minus.add(@infinity, 10000).nan?.should == true + @infinity.add(@infinity_minus, 10000).should.nan? + @infinity_minus.add(@infinity, 10000).should.nan? end it "raises TypeError when adds nil" do - lambda { + -> { @one.add(nil, 10) - }.should raise_error(TypeError) - lambda { + }.should.raise(TypeError) + -> { @one.add(nil, 0) - }.should raise_error(TypeError) + }.should.raise(TypeError) end it "raises TypeError when precision parameter is nil" do - lambda { + -> { @one.add(@one, nil) - }.should raise_error(TypeError) + }.should.raise(TypeError) end it "raises ArgumentError when precision parameter is negative" do - lambda { + -> { @one.add(@one, -10) - }.should raise_error(ArgumentError) + }.should.raise(ArgumentError) end end diff --git a/spec/ruby/library/bigdecimal/case_compare_spec.rb b/spec/ruby/library/bigdecimal/case_compare_spec.rb index dcbde80e85..fac6714356 100644 --- a/spec/ruby/library/bigdecimal/case_compare_spec.rb +++ b/spec/ruby/library/bigdecimal/case_compare_spec.rb @@ -1,7 +1,7 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../shared/eql.rb', __FILE__) +require_relative '../../spec_helper' +require_relative 'shared/eql' describe "BigDecimal#===" do - it_behaves_like(:bigdecimal_eql, :===) + it_behaves_like :bigdecimal_eql, :=== end diff --git a/spec/ruby/library/bigdecimal/ceil_spec.rb b/spec/ruby/library/bigdecimal/ceil_spec.rb index d8829411b9..3d94b8e578 100644 --- a/spec/ruby/library/bigdecimal/ceil_spec.rb +++ b/spec/ruby/library/bigdecimal/ceil_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'bigdecimal' describe "BigDecimal#ceil" do @@ -48,9 +48,9 @@ describe "BigDecimal#ceil" do end it "raise exception, if self is special value" do - lambda { @infinity.ceil }.should raise_error(FloatDomainError) - lambda { @infinity_neg.ceil }.should raise_error(FloatDomainError) - lambda { @nan.ceil }.should raise_error(FloatDomainError) + -> { @infinity.ceil }.should.raise(FloatDomainError) + -> { @infinity_neg.ceil }.should.raise(FloatDomainError) + -> { @nan.ceil }.should.raise(FloatDomainError) end it "returns n digits right of the decimal point if given n > 0" do diff --git a/spec/ruby/library/bigdecimal/clone_spec.rb b/spec/ruby/library/bigdecimal/clone_spec.rb new file mode 100644 index 0000000000..b3a1c61d6a --- /dev/null +++ b/spec/ruby/library/bigdecimal/clone_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require_relative 'shared/clone' + +describe "BigDecimal#dup" do + it_behaves_like :bigdecimal_clone, :clone +end diff --git a/spec/ruby/library/bigdecimal/coerce_spec.rb b/spec/ruby/library/bigdecimal/coerce_spec.rb index 1c63ddf2bc..1e5c73f969 100644 --- a/spec/ruby/library/bigdecimal/coerce_spec.rb +++ b/spec/ruby/library/bigdecimal/coerce_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'bigdecimal' describe "BigDecimal#coerce" do diff --git a/spec/ruby/library/bigdecimal/comparison_spec.rb b/spec/ruby/library/bigdecimal/comparison_spec.rb index c4de8348b2..c53187b727 100644 --- a/spec/ruby/library/bigdecimal/comparison_spec.rb +++ b/spec/ruby/library/bigdecimal/comparison_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'bigdecimal' describe "BigDecimal#<=>" do @@ -18,7 +18,7 @@ describe "BigDecimal#<=>" do def coerce(other) return [other, BigDecimal('123')] end - def >= (other) + def >=(other) BigDecimal('123') >= other end end diff --git a/spec/ruby/library/bigdecimal/constants_spec.rb b/spec/ruby/library/bigdecimal/constants_spec.rb new file mode 100644 index 0000000000..f2cc42dfc9 --- /dev/null +++ b/spec/ruby/library/bigdecimal/constants_spec.rb @@ -0,0 +1,70 @@ +require_relative '../../spec_helper' +require 'bigdecimal' + +describe "BigDecimal constants" do + it "defines a VERSION value" do + BigDecimal.const_defined?(:VERSION).should == true + end + + it "has a BASE value" do + # The actual one is decided based on HAVE_INT64_T in MRI, + # which is hard to check here. + [10000, 1000000000].should.include?(BigDecimal::BASE) + end + + it "has a NaN value" do + BigDecimal::NAN.nan?.should == true + end + + it "has an INFINITY value" do + BigDecimal::INFINITY.infinite?.should == 1 + end + + describe "exception-related constants" do + [ + [:EXCEPTION_ALL, 0xff], + [:EXCEPTION_INFINITY, 0x01], + [:EXCEPTION_NaN, 0x02], + [:EXCEPTION_UNDERFLOW, 0x04], + [:EXCEPTION_OVERFLOW, 0x01], + [:EXCEPTION_ZERODIVIDE, 0x10] + ].each do |const, value| + it "has a #{const} value" do + BigDecimal.const_get(const).should == value + end + end + end + + describe "rounding-related constants" do + [ + [:ROUND_MODE, 0x100], + [:ROUND_UP, 1], + [:ROUND_DOWN, 2], + [:ROUND_HALF_UP, 3], + [:ROUND_HALF_DOWN, 4], + [:ROUND_CEILING, 5], + [:ROUND_FLOOR, 6], + [:ROUND_HALF_EVEN, 7] + ].each do |const, value| + it "has a #{const} value" do + BigDecimal.const_get(const).should == value + end + end + end + + describe "sign-related constants" do + [ + [:SIGN_NaN, 0], + [:SIGN_POSITIVE_ZERO, 1], + [:SIGN_NEGATIVE_ZERO, -1], + [:SIGN_POSITIVE_FINITE, 2], + [:SIGN_NEGATIVE_FINITE, -2], + [:SIGN_POSITIVE_INFINITE, 3], + [:SIGN_NEGATIVE_INFINITE, -3] + ].each do |const, value| + it "has a #{const} value" do + BigDecimal.const_get(const).should == value + end + end + end +end diff --git a/spec/ruby/library/bigdecimal/core_spec.rb b/spec/ruby/library/bigdecimal/core_spec.rb new file mode 100644 index 0000000000..8f64fdf388 --- /dev/null +++ b/spec/ruby/library/bigdecimal/core_spec.rb @@ -0,0 +1,62 @@ +require_relative '../../spec_helper' +require 'bigdecimal' + +describe "Core extension by bigdecimal" do + context "Integer#coerce" do + it "produces Floats" do + x, y = 3.coerce(BigDecimal("3.4")) + x.class.should == Float + x.should == 3.4 + y.class.should == Float + y.should == 3.0 + end + end + + describe "Time.at passed BigDecimal" do + it "doesn't round input value" do + Time.at(BigDecimal('1.1')).to_f.should == 1.1 + end + end + + describe "BigDecimal#log" do + it "handles high-precision Rational arguments" do + # log(BigDecimal(r, 50), 50) + result1 = BigDecimal('0.22314354220170971436137296411949880462556361100856e0') + # log(BigDecimal(r, 1000), 50) + result2 = BigDecimal('0.22314354220170971436137296411949880462556361100853e0') + r = Rational(1_234_567_890, 987_654_321) + [result1, result2].should.include?(BigMath.log(r, 50).mult(1, 50)) + end + end + + describe "Rational#coerce" do + it "returns the passed argument, self as Float, when given a Float" do + result = Rational(3, 4).coerce(1.0) + result.should == [1.0, 0.75] + result.first.is_a?(Float).should == true + result.last.is_a?(Float).should == true + end + + it "returns the passed argument, self as Rational, when given an Integer" do + result = Rational(3, 4).coerce(10) + result.should == [Rational(10, 1), Rational(3, 4)] + result.first.is_a?(Rational).should == true + result.last.is_a?(Rational).should == true + end + + it "coerces to Rational, when given a Complex" do + Rational(3, 4).coerce(Complex(5)).should == [Rational(5, 1), Rational(3, 4)] + Rational(12, 4).coerce(Complex(5, 1)).should == [Complex(5, 1), Complex(3)] + end + + it "returns [argument, self] when given a Rational" do + Rational(3, 7).coerce(Rational(9, 2)).should == [Rational(9, 2), Rational(3, 7)] + end + + it "raises an error when passed a BigDecimal" do + -> { + Rational(500, 3).coerce(BigDecimal('166.666666666')) + }.should.raise(TypeError, /BigDecimal can't be coerced into Rational/) + end + end +end diff --git a/spec/ruby/library/bigdecimal/div_spec.rb b/spec/ruby/library/bigdecimal/div_spec.rb index f8f8ac4e5e..967d8b5221 100644 --- a/spec/ruby/library/bigdecimal/div_spec.rb +++ b/spec/ruby/library/bigdecimal/div_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' require 'bigdecimal' describe "BigDecimal#div with precision set to 0" do @@ -42,10 +42,18 @@ describe "BigDecimal#div" do } end + describe "with Object" do + it "tries to coerce the other operand to self" do + object = mock("Object") + object.should_receive(:coerce).with(@one).and_return([@one, @two]) + @one.div(object).should == @zero + end + end + it "raises FloatDomainError if NaN is involved" do - lambda { @one.div(@nan) }.should raise_error(FloatDomainError) - lambda { @nan.div(@one) }.should raise_error(FloatDomainError) - lambda { @nan.div(@nan) }.should raise_error(FloatDomainError) + -> { @one.div(@nan) }.should.raise(FloatDomainError) + -> { @nan.div(@one) }.should.raise(FloatDomainError) + -> { @nan.div(@nan) }.should.raise(FloatDomainError) end it "returns 0 if divided by Infinity and no precision given" do @@ -61,30 +69,30 @@ describe "BigDecimal#div" do end it "raises ZeroDivisionError if divided by zero and no precision given" do - lambda { @one.div(@zero) }.should raise_error(ZeroDivisionError) - lambda { @one.div(@zero_plus) }.should raise_error(ZeroDivisionError) - lambda { @one.div(@zero_minus) }.should raise_error(ZeroDivisionError) - - lambda { @zero.div(@zero) }.should raise_error(ZeroDivisionError) - lambda { @zero_minus.div(@zero_plus) }.should raise_error(ZeroDivisionError) - lambda { @zero_minus.div(@zero_minus) }.should raise_error(ZeroDivisionError) - lambda { @zero_plus.div(@zero_minus) }.should raise_error(ZeroDivisionError) + -> { @one.div(@zero) }.should.raise(ZeroDivisionError) + -> { @one.div(@zero_plus) }.should.raise(ZeroDivisionError) + -> { @one.div(@zero_minus) }.should.raise(ZeroDivisionError) + + -> { @zero.div(@zero) }.should.raise(ZeroDivisionError) + -> { @zero_minus.div(@zero_plus) }.should.raise(ZeroDivisionError) + -> { @zero_minus.div(@zero_minus) }.should.raise(ZeroDivisionError) + -> { @zero_plus.div(@zero_minus) }.should.raise(ZeroDivisionError) end it "returns NaN if zero is divided by zero" do - @zero.div(@zero, 0).nan?.should == true - @zero_minus.div(@zero_plus, 0).nan?.should == true - @zero_plus.div(@zero_minus, 0).nan?.should == true + @zero.div(@zero, 0).should.nan? + @zero_minus.div(@zero_plus, 0).should.nan? + @zero_plus.div(@zero_minus, 0).should.nan? - @zero.div(@zero, 10).nan?.should == true - @zero_minus.div(@zero_plus, 10).nan?.should == true - @zero_plus.div(@zero_minus, 10).nan?.should == true + @zero.div(@zero, 10).should.nan? + @zero_minus.div(@zero_plus, 10).should.nan? + @zero_plus.div(@zero_minus, 10).should.nan? end it "raises FloatDomainError if (+|-) Infinity divided by 1 and no precision given" do - lambda { @infinity_minus.div(@one) }.should raise_error(FloatDomainError) - lambda { @infinity.div(@one) }.should raise_error(FloatDomainError) - lambda { @infinity_minus.div(@one_minus) }.should raise_error(FloatDomainError) + -> { @infinity_minus.div(@one) }.should.raise(FloatDomainError) + -> { @infinity.div(@one) }.should.raise(FloatDomainError) + -> { @infinity_minus.div(@one_minus) }.should.raise(FloatDomainError) end it "returns (+|-)Infinity if (+|-)Infinity by 1 and precision given" do @@ -94,8 +102,8 @@ describe "BigDecimal#div" do end it "returns NaN if Infinity / ((+|-) Infinity)" do - @infinity.div(@infinity_minus, 100000).nan?.should == true - @infinity_minus.div(@infinity, 1).nan?.should == true + @infinity.div(@infinity_minus, 100000).should.nan? + @infinity_minus.div(@infinity, 1).should.nan? end diff --git a/spec/ruby/library/bigdecimal/divide_spec.rb b/spec/ruby/library/bigdecimal/divide_spec.rb index 4dfe2702bb..c62b23557d 100644 --- a/spec/ruby/library/bigdecimal/divide_spec.rb +++ b/spec/ruby/library/bigdecimal/divide_spec.rb @@ -1,7 +1,17 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../shared/quo', __FILE__) +require_relative '../../spec_helper' +require_relative 'shared/quo' require 'bigdecimal' describe "BigDecimal#/" do it_behaves_like :bigdecimal_quo, :/, [] + + before :each do + @three = BigDecimal("3") + end + + describe "with Rational" do + it "produces a BigDecimal" do + (@three / Rational(500, 2)).should == BigDecimal("0.12e-1") + end + end end diff --git a/spec/ruby/library/bigdecimal/divmod_spec.rb b/spec/ruby/library/bigdecimal/divmod_spec.rb index 839a289ab6..d170c6f845 100644 --- a/spec/ruby/library/bigdecimal/divmod_spec.rb +++ b/spec/ruby/library/bigdecimal/divmod_spec.rb @@ -1,12 +1,12 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../shared/modulo', __FILE__) +require_relative '../../spec_helper' +require_relative 'shared/modulo' require 'bigdecimal' module DivmodSpecs def self.check_both_nan(array) array.length.should == 2 - array[0].nan?.should == true - array[1].nan?.should == true + array[0].should.nan? + array[1].should.nan? end def self.check_both_bigdecimal(array) array.length.should == 2 @@ -33,14 +33,16 @@ describe "BigDecimal#mod_part_of_divmod" do end end - it_behaves_like :bigdecimal_modulo, :mod_part_of_divmod + version_is BigDecimal::VERSION, ""..."4.0.0" do + it_behaves_like :bigdecimal_modulo, :mod_part_of_divmod + end it "raises ZeroDivisionError if other is zero" do - bd5667 = BigDecimal.new("5667.19") - - lambda { bd5667.send(@method, 0) }.should raise_error(ZeroDivisionError) - lambda { bd5667.send(@method, BigDecimal("0")) }.should raise_error(ZeroDivisionError) - lambda { @zero.send(@method, @zero) }.should raise_error(ZeroDivisionError) + bd5667 = BigDecimal("5667.19") + zero = BigDecimal("0") + -> { bd5667.mod_part_of_divmod(0) }.should.raise(ZeroDivisionError) + -> { bd5667.mod_part_of_divmod(BigDecimal("0")) }.should.raise(ZeroDivisionError) + -> { zero.mod_part_of_divmod(zero) }.should.raise(ZeroDivisionError) end end @@ -73,14 +75,25 @@ describe "BigDecimal#divmod" do @zeroes = [@zero, @zero_pos, @zero_neg] end - it "divides value, returns an array" do - res = @a.divmod(5) - res.kind_of?(Array).should == true + version_is BigDecimal::VERSION, ""..."4.0.0" do + it "divides value, returns [BigDecimal, BigDecimal]" do + res = @a.divmod(5) + res.kind_of?(Array).should == true + DivmodSpecs.check_both_bigdecimal(res) + end + end + + version_is BigDecimal::VERSION, "4.0.0" do + it "divides value, returns [Integer, BigDecimal]" do + res = @a.divmod(5) + res.kind_of?(Array).should == true + res[0].kind_of?(Integer).should == true + res[1].kind_of?(BigDecimal).should == true + end end it "array contains quotient and modulus as BigDecimal" do res = @a.divmod(5) - DivmodSpecs.check_both_bigdecimal(res) res[0].should == BigDecimal('0.8E1') res[1].should == BigDecimal('2.00000000000000000001') @@ -96,8 +109,8 @@ describe "BigDecimal#divmod" do it "can be reversed with * and +" do # Example taken from BigDecimal documentation - a = BigDecimal.new("42") - b = BigDecimal.new("9") + a = BigDecimal("42") + b = BigDecimal("9") q, m = a.divmod(b) c = q * b + m a.should == c @@ -123,47 +136,66 @@ describe "BigDecimal#divmod" do values_and_zeroes.each do |val1| values.each do |val2| res = val1.divmod(val2) - DivmodSpecs.check_both_bigdecimal(res) res[0].should == ((val1/val2).floor) res[1].should == (val1 - res[0] * val2) end end end - it "returns an array of two NaNs if NaN is involved" do - (@special_vals + @regular_vals + @zeroes).each do |val| - DivmodSpecs.check_both_nan(val.divmod(@nan)) - DivmodSpecs.check_both_nan(@nan.divmod(val)) + version_is BigDecimal::VERSION, "4.0.0" do + it "raise FloatDomainError error if NaN is involved" do + (@special_vals + @regular_vals + @zeroes).each do |val| + -> { val.divmod(@nan) }.should.raise(FloatDomainError) + -> { @nan.divmod(val) }.should.raise(FloatDomainError) + end + end + end + + version_is BigDecimal::VERSION, ""..."4.0.0" do + it "returns an array of two NaNs if NaN is involved" do + (@special_vals + @regular_vals + @zeroes).each do |val| + DivmodSpecs.check_both_nan(val.divmod(@nan)) + DivmodSpecs.check_both_nan(@nan.divmod(val)) + end end end it "raises ZeroDivisionError if the divisor is zero" do (@special_vals + @regular_vals + @zeroes - [@nan]).each do |val| @zeroes.each do |zero| - lambda { val.divmod(zero) }.should raise_error(ZeroDivisionError) + -> { val.divmod(zero) }.should.raise(ZeroDivisionError) end end end - it "returns an array of Infinity and NaN if the dividend is Infinity" do - @regular_vals.each do |val| - array = @infinity.divmod(val) - array.length.should == 2 - array[0].infinite?.should == (val > 0 ? 1 : -1) - array[1].nan?.should == true + version_is BigDecimal::VERSION, ""..."4.0.0" do + it "returns an array of Infinity and NaN if the dividend is Infinity" do + @regular_vals.each do |val| + array = @infinity.divmod(val) + array.length.should == 2 + array[0].infinite?.should == (val > 0 ? 1 : -1) + array[1].should.nan? + end end end - it "returns an array of zero and the dividend if the divisor is Infinity" do - @regular_vals.each do |val| - array = val.divmod(@infinity) - array.length.should == 2 - array[0].should == @zero - array[1].should == val + version_is BigDecimal::VERSION, "3.3.0" do + it "returns an array of zero and the dividend or minus one and Infinity if the divisor is Infinity" do + @regular_vals.each do |val| + array = val.divmod(@infinity) + array.length.should == 2 + if val >= 0 + array[0].should == @zero + array[1].should == val + else + array[0].should == @one_minus + array[1].should == @infinity + end + end end end - it "returns an array of two zero if the diviend is zero" do + it "returns an array of two zero if the dividend is zero" do @zeroes.each do |zero| @regular_vals.each do |val| zero.divmod(val).should == [@zero, @zero] @@ -172,9 +204,9 @@ describe "BigDecimal#divmod" do end it "raises TypeError if the argument cannot be coerced to BigDecimal" do - lambda { + -> { @one.divmod('1') - }.should raise_error(TypeError) + }.should.raise(TypeError) end end diff --git a/spec/ruby/library/bigdecimal/double_fig_spec.rb b/spec/ruby/library/bigdecimal/double_fig_spec.rb index 3ab5c2f207..f742d68f87 100644 --- a/spec/ruby/library/bigdecimal/double_fig_spec.rb +++ b/spec/ruby/library/bigdecimal/double_fig_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'bigdecimal' describe "BigDecimal.double_fig" do diff --git a/spec/ruby/library/bigdecimal/dup_spec.rb b/spec/ruby/library/bigdecimal/dup_spec.rb new file mode 100644 index 0000000000..bfabaf6e8b --- /dev/null +++ b/spec/ruby/library/bigdecimal/dup_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require_relative 'shared/clone' + +describe "BigDecimal#dup" do + it_behaves_like :bigdecimal_clone, :dup +end diff --git a/spec/ruby/library/bigdecimal/eql_spec.rb b/spec/ruby/library/bigdecimal/eql_spec.rb index f3f525a7ba..1be5862751 100644 --- a/spec/ruby/library/bigdecimal/eql_spec.rb +++ b/spec/ruby/library/bigdecimal/eql_spec.rb @@ -1,6 +1,6 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../shared/eql.rb', __FILE__) +require_relative '../../spec_helper' +require_relative 'shared/eql' describe "BigDecimal#eql?" do - it_behaves_like(:bigdecimal_eql, :eql?) + it_behaves_like :bigdecimal_eql, :eql? end diff --git a/spec/ruby/library/bigdecimal/equal_value_spec.rb b/spec/ruby/library/bigdecimal/equal_value_spec.rb index bd07217b98..933060eada 100644 --- a/spec/ruby/library/bigdecimal/equal_value_spec.rb +++ b/spec/ruby/library/bigdecimal/equal_value_spec.rb @@ -1,7 +1,7 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../shared/eql.rb', __FILE__) +require_relative '../../spec_helper' +require_relative 'shared/eql' describe "BigDecimal#==" do - it_behaves_like(:bigdecimal_eql, :==) + it_behaves_like :bigdecimal_eql, :== end diff --git a/spec/ruby/library/bigdecimal/exponent_spec.rb b/spec/ruby/library/bigdecimal/exponent_spec.rb index 6bb678d4ed..8877147955 100644 --- a/spec/ruby/library/bigdecimal/exponent_spec.rb +++ b/spec/ruby/library/bigdecimal/exponent_spec.rb @@ -1,9 +1,9 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../shared/power', __FILE__) +require_relative '../../spec_helper' +require_relative 'shared/power' require 'bigdecimal' describe "BigDecimal#**" do - it_behaves_like(:bigdecimal_power, :**) + it_behaves_like :bigdecimal_power, :** end describe "BigDecimal#exponent" do @@ -18,17 +18,6 @@ describe "BigDecimal#exponent" do BigDecimal("1234567E10").exponent.should == 17 end -# commenting this spec out after discussion with Defiler, since it seems to be an MRI bug, not a real feature -=begin - platform_is wordsize: 32 do - # TODO: write specs for both 32 and 64 bit - it "returns 0 if exponent can't be represented as Fixnum" do - BigDecimal("2E1000000000000000").exponent.should == 0 - BigDecimal("-5E-999999999999999").exponent.should == 0 - end - end -=end - it "returns 0 if self is 0" do BigDecimal("0").exponent.should == 0 BigDecimal("+0").exponent.should == 0 diff --git a/spec/ruby/library/bigdecimal/finite_spec.rb b/spec/ruby/library/bigdecimal/finite_spec.rb index 6d6d8398fa..8fc06029bb 100644 --- a/spec/ruby/library/bigdecimal/finite_spec.rb +++ b/spec/ruby/library/bigdecimal/finite_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'bigdecimal' describe "BigDecimal#finite?" do @@ -21,15 +21,14 @@ describe "BigDecimal#finite?" do end it "is false if Infinity or NaN" do - @infinity.finite?.should == false - @infinity_minus.finite?.should == false - @nan.finite?.should == false + @infinity.should_not.finite? + @infinity_minus.should_not.finite? + @nan.should_not.finite? end it "returns true for finite values" do @finite_vals.each do |val| - val.finite?.should == true + val.should.finite? end end end - diff --git a/spec/ruby/library/bigdecimal/fix_spec.rb b/spec/ruby/library/bigdecimal/fix_spec.rb index 0a4a98c241..dceb2ce867 100644 --- a/spec/ruby/library/bigdecimal/fix_spec.rb +++ b/spec/ruby/library/bigdecimal/fix_spec.rb @@ -1,21 +1,21 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'bigdecimal' describe "BigDecimal#fix" do - before :each do - @zero = BigDecimal("0") - @mixed = BigDecimal("1.23456789") - @pos_int = BigDecimal("2E5555") - @neg_int = BigDecimal("-2E5555") - @pos_frac = BigDecimal("2E-9999") - @neg_frac = BigDecimal("-2E-9999") - - @infinity = BigDecimal("Infinity") - @infinity_neg = BigDecimal("-Infinity") - @nan = BigDecimal("NaN") - @zero_pos = BigDecimal("+0") - @zero_neg = BigDecimal("-0") - end + before :each do + @zero = BigDecimal("0") + @mixed = BigDecimal("1.23456789") + @pos_int = BigDecimal("2E5555") + @neg_int = BigDecimal("-2E5555") + @pos_frac = BigDecimal("2E-9999") + @neg_frac = BigDecimal("-2E-9999") + + @infinity = BigDecimal("Infinity") + @infinity_neg = BigDecimal("-Infinity") + @nan = BigDecimal("NaN") + @zero_pos = BigDecimal("+0") + @zero_neg = BigDecimal("-0") + end it "returns a BigDecimal" do BigDecimal("2E100000000").fix.kind_of?(BigDecimal).should == true @@ -34,7 +34,7 @@ describe "BigDecimal#fix" do it "correctly handles special values" do @infinity.fix.should == @infinity @infinity_neg.fix.should == @infinity_neg - @nan.fix.nan?.should == true + @nan.fix.should.nan? end it "returns 0 if the absolute value is < 1" do @@ -49,9 +49,9 @@ describe "BigDecimal#fix" do end it "does not allow any arguments" do - lambda { + -> { @mixed.fix(10) - }.should raise_error(ArgumentError) + }.should.raise(ArgumentError) end end diff --git a/spec/ruby/library/bigdecimal/floor_spec.rb b/spec/ruby/library/bigdecimal/floor_spec.rb index 0bae6e4ab0..c0666c668c 100644 --- a/spec/ruby/library/bigdecimal/floor_spec.rb +++ b/spec/ruby/library/bigdecimal/floor_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'bigdecimal' describe "BigDecimal#floor" do @@ -41,9 +41,9 @@ describe "BigDecimal#floor" do end it "raise exception, if self is special value" do - lambda { @infinity.floor }.should raise_error(FloatDomainError) - lambda { @infinity_neg.floor }.should raise_error(FloatDomainError) - lambda { @nan.floor }.should raise_error(FloatDomainError) + -> { @infinity.floor }.should.raise(FloatDomainError) + -> { @infinity_neg.floor }.should.raise(FloatDomainError) + -> { @nan.floor }.should.raise(FloatDomainError) end it "returns n digits right of the decimal point if given n > 0" do diff --git a/spec/ruby/library/bigdecimal/frac_spec.rb b/spec/ruby/library/bigdecimal/frac_spec.rb index 9a727a70dd..11ccf03c2f 100644 --- a/spec/ruby/library/bigdecimal/frac_spec.rb +++ b/spec/ruby/library/bigdecimal/frac_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'bigdecimal' describe "BigDecimal#frac" do @@ -42,7 +42,7 @@ describe "BigDecimal#frac" do it "correctly handles special values" do @infinity.frac.should == @infinity @infinity_neg.frac.should == @infinity_neg - @nan.frac.nan?.should == true + @nan.frac.should.nan? end end diff --git a/spec/ruby/library/bigdecimal/gt_spec.rb b/spec/ruby/library/bigdecimal/gt_spec.rb index 2f9ea4fd68..e9c9a60e75 100644 --- a/spec/ruby/library/bigdecimal/gt_spec.rb +++ b/spec/ruby/library/bigdecimal/gt_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'bigdecimal' describe "BigDecimal#>" do @@ -17,7 +17,7 @@ describe "BigDecimal#>" do def coerce(other) return [other, BigDecimal('123')] end - def > (other) + def >(other) BigDecimal('123') > other end end @@ -68,15 +68,13 @@ describe "BigDecimal#>" do (@infinity_neg > @infinity).should == false end - ruby_bug "#13674", ""..."2.4" do - it "properly handles Float infinity values" do - @values.each { |val| - (val > @float_infinity).should == false - (@float_infinity > val).should == true - (val > @float_infinity_neg).should == true - (@float_infinity_neg > val).should == false - } - end + it "properly handles Float infinity values" do + @values.each { |val| + (val > @float_infinity).should == false + (@float_infinity > val).should == true + (val > @float_infinity_neg).should == true + (@float_infinity_neg > val).should == false + } end it "properly handles NaN values" do @@ -88,11 +86,11 @@ describe "BigDecimal#>" do end it "raises an ArgumentError if the argument can't be coerced into a BigDecimal" do - lambda {@zero > nil }.should raise_error(ArgumentError) - lambda {@infinity > nil }.should raise_error(ArgumentError) - lambda {@infinity_neg > nil }.should raise_error(ArgumentError) - lambda {@mixed > nil }.should raise_error(ArgumentError) - lambda {@pos_int > nil }.should raise_error(ArgumentError) - lambda {@neg_frac > nil }.should raise_error(ArgumentError) + -> {@zero > nil }.should.raise(ArgumentError) + -> {@infinity > nil }.should.raise(ArgumentError) + -> {@infinity_neg > nil }.should.raise(ArgumentError) + -> {@mixed > nil }.should.raise(ArgumentError) + -> {@pos_int > nil }.should.raise(ArgumentError) + -> {@neg_frac > nil }.should.raise(ArgumentError) end end diff --git a/spec/ruby/library/bigdecimal/gte_spec.rb b/spec/ruby/library/bigdecimal/gte_spec.rb index aab5338ad6..548f3efe4c 100644 --- a/spec/ruby/library/bigdecimal/gte_spec.rb +++ b/spec/ruby/library/bigdecimal/gte_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'bigdecimal' describe "BigDecimal#>=" do @@ -17,7 +17,7 @@ describe "BigDecimal#>=" do def coerce(other) return [other, BigDecimal('123')] end - def >= (other) + def >=(other) BigDecimal('123') >= other end end @@ -72,15 +72,13 @@ describe "BigDecimal#>=" do (@infinity_neg >= @infinity).should == false end - ruby_bug "#13674", ""..."2.4" do - it "properly handles Float infinity values" do - @values.each { |val| - (val >= @float_infinity).should == false - (@float_infinity >= val).should == true - (val >= @float_infinity_neg).should == true - (@float_infinity_neg >= val).should == false - } - end + it "properly handles Float infinity values" do + @values.each { |val| + (val >= @float_infinity).should == false + (@float_infinity >= val).should == true + (val >= @float_infinity_neg).should == true + (@float_infinity_neg >= val).should == false + } end it "properly handles NaN values" do @@ -92,11 +90,11 @@ describe "BigDecimal#>=" do end it "returns nil if the argument is nil" do - lambda {@zero >= nil }.should raise_error(ArgumentError) - lambda {@infinity >= nil }.should raise_error(ArgumentError) - lambda {@infinity_neg >= nil }.should raise_error(ArgumentError) - lambda {@mixed >= nil }.should raise_error(ArgumentError) - lambda {@pos_int >= nil }.should raise_error(ArgumentError) - lambda {@neg_frac >= nil }.should raise_error(ArgumentError) + -> {@zero >= nil }.should.raise(ArgumentError) + -> {@infinity >= nil }.should.raise(ArgumentError) + -> {@infinity_neg >= nil }.should.raise(ArgumentError) + -> {@mixed >= nil }.should.raise(ArgumentError) + -> {@pos_int >= nil }.should.raise(ArgumentError) + -> {@neg_frac >= nil }.should.raise(ArgumentError) end end diff --git a/spec/ruby/library/bigdecimal/hash_spec.rb b/spec/ruby/library/bigdecimal/hash_spec.rb new file mode 100644 index 0000000000..7581c90f68 --- /dev/null +++ b/spec/ruby/library/bigdecimal/hash_spec.rb @@ -0,0 +1,30 @@ +require_relative '../../spec_helper' +require 'bigdecimal' + +describe "BidDecimal#hash" do + describe "two BigDecimal objects with the same value" do + it "should have the same hash for ordinary values" do + BigDecimal('1.2920').hash.should == BigDecimal('1.2920').hash + end + + it "should have the same hash for infinite values" do + BigDecimal("+Infinity").hash.should == BigDecimal("+Infinity").hash + BigDecimal("-Infinity").hash.should == BigDecimal("-Infinity").hash + end + + it "should have the same hash for NaNs" do + BigDecimal("NaN").hash.should == BigDecimal("NaN").hash + end + + it "should have the same hash for zero values" do + BigDecimal("+0").hash.should == BigDecimal("+0").hash + BigDecimal("-0").hash.should == BigDecimal("-0").hash + end + end + + describe "two BigDecimal objects with numerically equal values" do + it "should have the same hash value" do + BigDecimal("1.2920").hash.should == BigDecimal("1.2920000").hash + end + end +end diff --git a/spec/ruby/library/bigdecimal/infinite_spec.rb b/spec/ruby/library/bigdecimal/infinite_spec.rb index b218ee371c..025386107f 100644 --- a/spec/ruby/library/bigdecimal/infinite_spec.rb +++ b/spec/ruby/library/bigdecimal/infinite_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'bigdecimal' describe "BigDecimal#infinite?" do diff --git a/spec/ruby/library/bigdecimal/inspect_spec.rb b/spec/ruby/library/bigdecimal/inspect_spec.rb index 9831b2be8e..7ce47142b2 100644 --- a/spec/ruby/library/bigdecimal/inspect_spec.rb +++ b/spec/ruby/library/bigdecimal/inspect_spec.rb @@ -1,47 +1,30 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'bigdecimal' describe "BigDecimal#inspect" do before :each do - @bigdec = BigDecimal.new("1234.5678") + @bigdec = BigDecimal("1234.5678") end it "returns String" do @bigdec.inspect.kind_of?(String).should == true end - ruby_version_is ""..."2.4" do - it "returns String starting with #" do - @bigdec.inspect[0].should == ?# - end - - it "encloses information in angle brackets" do - @bigdec.inspect.should =~ /^.<.*>$/ - end - - it "is comma separated list of three items" do - @bigdec.inspect.should =~ /...*,.*,.*/ - end - - it "value after first comma is value as string" do - @bigdec.inspect.split(",")[1].should == "\'0.12345678E4\'" - end - - it "last part is number of significant digits" do - signific_string = "#{@bigdec.precs[0]}(#{@bigdec.precs[1]})" - @bigdec.inspect.split(",")[2].should == signific_string + ">" - end + it "looks like this" do + @bigdec.inspect.should == "0.12345678e4" + end - it "looks like this" do - regex = /^\#\<BigDecimal\:.*,'0\.12345678E4',[0-9]+\([0-9]+\)>$/ - @bigdec.inspect.should =~ regex - end + it "does not add an exponent for zero values" do + BigDecimal("0").inspect.should == "0.0" + BigDecimal("+0").inspect.should == "0.0" + BigDecimal("-0").inspect.should == "-0.0" end - ruby_version_is "2.4" do - it "looks like this" do - @bigdec.inspect.should == "0.12345678e4" - end + it "properly cases non-finite values" do + BigDecimal("NaN").inspect.should == "NaN" + BigDecimal("Infinity").inspect.should == "Infinity" + BigDecimal("+Infinity").inspect.should == "Infinity" + BigDecimal("-Infinity").inspect.should == "-Infinity" end end diff --git a/spec/ruby/library/bigdecimal/limit_spec.rb b/spec/ruby/library/bigdecimal/limit_spec.rb index 41308abcd8..75cbc8b55c 100644 --- a/spec/ruby/library/bigdecimal/limit_spec.rb +++ b/spec/ruby/library/bigdecimal/limit_spec.rb @@ -1,5 +1,5 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../fixtures/classes', __FILE__) +require_relative '../../spec_helper' +require_relative 'fixtures/classes' require 'bigdecimal' describe "BigDecimal.limit" do @@ -11,20 +11,45 @@ describe "BigDecimal.limit" do BigDecimal.limit(old) end - it "use the global limit if no precision is specified" do + it "uses the global limit if no precision is specified" do BigDecimalSpecs.with_limit(0) do (BigDecimal('0.888') + BigDecimal('0')).should == BigDecimal('0.888') + (BigDecimal('0.888') - BigDecimal('0')).should == BigDecimal('0.888') (BigDecimal('0.888') * BigDecimal('3')).should == BigDecimal('2.664') + (BigDecimal('0.888') / BigDecimal('3')).should == BigDecimal('0.296') end BigDecimalSpecs.with_limit(1) do (BigDecimal('0.888') + BigDecimal('0')).should == BigDecimal('0.9') + (BigDecimal('0.888') - BigDecimal('0')).should == BigDecimal('0.9') (BigDecimal('0.888') * BigDecimal('3')).should == BigDecimal('3') + (BigDecimal('0.888') / BigDecimal('3')).should == BigDecimal('0.3') end BigDecimalSpecs.with_limit(2) do (BigDecimal('0.888') + BigDecimal('0')).should == BigDecimal('0.89') + (BigDecimal('0.888') - BigDecimal('0')).should == BigDecimal('0.89') (BigDecimal('0.888') * BigDecimal('3')).should == BigDecimal('2.7') + (BigDecimal('0.888') / BigDecimal('3')).should == BigDecimal('0.30') end end + + it "picks the specified precision over global limit" do + BigDecimalSpecs.with_limit(3) do + BigDecimal('0.888').add(BigDecimal('0'), 2).should == BigDecimal('0.89') + BigDecimal('0.888').sub(BigDecimal('0'), 2).should == BigDecimal('0.89') + BigDecimal('0.888').mult(BigDecimal('3'), 2).should == BigDecimal('2.7') + BigDecimal('0.888').div(BigDecimal('3'), 2).should == BigDecimal('0.30') + end + end + + it "picks the global precision when limit 0 specified" do + BigDecimalSpecs.with_limit(3) do + BigDecimal('0.8888').add(BigDecimal('0'), 0).should == BigDecimal('0.889') + BigDecimal('0.8888').sub(BigDecimal('0'), 0).should == BigDecimal('0.889') + BigDecimal('0.888').mult(BigDecimal('3'), 0).should == BigDecimal('2.66') + BigDecimal('0.8888').div(BigDecimal('3'), 0).should == BigDecimal('0.296') + end + end + end diff --git a/spec/ruby/library/bigdecimal/lt_spec.rb b/spec/ruby/library/bigdecimal/lt_spec.rb index 089e7aef73..c3f3573247 100644 --- a/spec/ruby/library/bigdecimal/lt_spec.rb +++ b/spec/ruby/library/bigdecimal/lt_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'bigdecimal' describe "BigDecimal#<" do @@ -17,7 +17,7 @@ describe "BigDecimal#<" do def coerce(other) return [other, BigDecimal('123')] end - def < (other) + def <(other) BigDecimal('123') < other end end @@ -66,15 +66,13 @@ describe "BigDecimal#<" do (@infinity_neg < @infinity).should == true end - ruby_bug "#13674", ""..."2.4" do - it "properly handles Float infinity values" do - @values.each { |val| - (val < @float_infinity).should == true - (@float_infinity < val).should == false - (val < @float_infinity_neg).should == false - (@float_infinity_neg < val).should == true - } - end + it "properly handles Float infinity values" do + @values.each { |val| + (val < @float_infinity).should == true + (@float_infinity < val).should == false + (val < @float_infinity_neg).should == false + (@float_infinity_neg < val).should == true + } end it "properly handles NaN values" do @@ -86,11 +84,11 @@ describe "BigDecimal#<" do end it "raises an ArgumentError if the argument can't be coerced into a BigDecimal" do - lambda {@zero < nil }.should raise_error(ArgumentError) - lambda {@infinity < nil }.should raise_error(ArgumentError) - lambda {@infinity_neg < nil }.should raise_error(ArgumentError) - lambda {@mixed < nil }.should raise_error(ArgumentError) - lambda {@pos_int < nil }.should raise_error(ArgumentError) - lambda {@neg_frac < nil }.should raise_error(ArgumentError) + -> {@zero < nil }.should.raise(ArgumentError) + -> {@infinity < nil }.should.raise(ArgumentError) + -> {@infinity_neg < nil }.should.raise(ArgumentError) + -> {@mixed < nil }.should.raise(ArgumentError) + -> {@pos_int < nil }.should.raise(ArgumentError) + -> {@neg_frac < nil }.should.raise(ArgumentError) end end diff --git a/spec/ruby/library/bigdecimal/lte_spec.rb b/spec/ruby/library/bigdecimal/lte_spec.rb index 5cda9842bd..7918bde88b 100644 --- a/spec/ruby/library/bigdecimal/lte_spec.rb +++ b/spec/ruby/library/bigdecimal/lte_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'bigdecimal' describe "BigDecimal#<=" do @@ -17,7 +17,7 @@ describe "BigDecimal#<=" do def coerce(other) return [other, BigDecimal('123')] end - def <= (other) + def <=(other) BigDecimal('123') <= other end end @@ -72,15 +72,13 @@ describe "BigDecimal#<=" do (@infinity_neg <= @infinity).should == true end - ruby_bug "#13674", ""..."2.4" do - it "properly handles Float infinity values" do - @values.each { |val| - (val <= @float_infinity).should == true - (@float_infinity <= val).should == false - (val <= @float_infinity_neg).should == false - (@float_infinity_neg <= val).should == true - } - end + it "properly handles Float infinity values" do + @values.each { |val| + (val <= @float_infinity).should == true + (@float_infinity <= val).should == false + (val <= @float_infinity_neg).should == false + (@float_infinity_neg <= val).should == true + } end it "properly handles NaN values" do @@ -92,11 +90,11 @@ describe "BigDecimal#<=" do end it "raises an ArgumentError if the argument can't be coerced into a BigDecimal" do - lambda {@zero <= nil }.should raise_error(ArgumentError) - lambda {@infinity <= nil }.should raise_error(ArgumentError) - lambda {@infinity_neg <= nil }.should raise_error(ArgumentError) - lambda {@mixed <= nil }.should raise_error(ArgumentError) - lambda {@pos_int <= nil }.should raise_error(ArgumentError) - lambda {@neg_frac <= nil }.should raise_error(ArgumentError) + -> {@zero <= nil }.should.raise(ArgumentError) + -> {@infinity <= nil }.should.raise(ArgumentError) + -> {@infinity_neg <= nil }.should.raise(ArgumentError) + -> {@mixed <= nil }.should.raise(ArgumentError) + -> {@pos_int <= nil }.should.raise(ArgumentError) + -> {@neg_frac <= nil }.should.raise(ArgumentError) end end diff --git a/spec/ruby/library/bigdecimal/minus_spec.rb b/spec/ruby/library/bigdecimal/minus_spec.rb index bdd8478465..bd3c19584b 100644 --- a/spec/ruby/library/bigdecimal/minus_spec.rb +++ b/spec/ruby/library/bigdecimal/minus_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'bigdecimal' describe "BigDecimal#-" do @@ -26,18 +26,18 @@ describe "BigDecimal#-" do end it "returns NaN if NaN is involved" do - (@one - @nan).nan?.should == true - (@nan - @one).nan?.should == true - (@nan - @nan).nan?.should == true - (@nan - @infinity).nan?.should == true - (@nan - @infinity_minus).nan?.should == true - (@infinity - @nan).nan?.should == true - (@infinity_minus - @nan).nan?.should == true + (@one - @nan).should.nan? + (@nan - @one).should.nan? + (@nan - @nan).should.nan? + (@nan - @infinity).should.nan? + (@nan - @infinity_minus).should.nan? + (@infinity - @nan).should.nan? + (@infinity_minus - @nan).should.nan? end it "returns NaN both operands are infinite with the same sign" do - (@infinity - @infinity).nan?.should == true - (@infinity_minus - @infinity_minus).nan?.should == true + (@infinity - @infinity).should.nan? + (@infinity_minus - @infinity_minus).should.nan? end it "returns Infinity or -Infinity if these are involved" do @@ -55,4 +55,12 @@ describe "BigDecimal#-" do (@one_minus - @infinity).should == @infinity_minus end + describe "with Object" do + it "tries to coerce the other operand to self" do + object = mock("Object") + object.should_receive(:coerce).with(@one).and_return([@one, BigDecimal("42")]) + (@one - object).should == BigDecimal("-41") + end + end + end diff --git a/spec/ruby/library/bigdecimal/mode_spec.rb b/spec/ruby/library/bigdecimal/mode_spec.rb index d88ac61aaf..26e6d0ea75 100644 --- a/spec/ruby/library/bigdecimal/mode_spec.rb +++ b/spec/ruby/library/bigdecimal/mode_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'bigdecimal' describe "BigDecimal.mode" do @@ -12,7 +12,7 @@ describe "BigDecimal.mode" do end it "returns the appropriate value and continue the computation if the flag is false" do - BigDecimal("NaN").add(BigDecimal("1"),0).nan?.should == true + BigDecimal("NaN").add(BigDecimal("1"),0).should.nan? BigDecimal("0").add(BigDecimal("Infinity"),0).should == BigDecimal("Infinity") BigDecimal("1").quo(BigDecimal("0")).should == BigDecimal("Infinity") end @@ -24,13 +24,13 @@ describe "BigDecimal.mode" do it "raise an exception if the flag is true" do BigDecimal.mode(BigDecimal::EXCEPTION_NaN, true) - lambda { BigDecimal("NaN").add(BigDecimal("1"),0) }.should raise_error(FloatDomainError) + -> { BigDecimal("NaN").add(BigDecimal("1"),0) }.should.raise(FloatDomainError) BigDecimal.mode(BigDecimal::EXCEPTION_INFINITY, true) - lambda { BigDecimal("0").add(BigDecimal("Infinity"),0) }.should raise_error(FloatDomainError) + -> { BigDecimal("0").add(BigDecimal("Infinity"),0) }.should.raise(FloatDomainError) BigDecimal.mode(BigDecimal::EXCEPTION_ZERODIVIDE, true) - lambda { BigDecimal("1").quo(BigDecimal("0")) }.should raise_error(FloatDomainError) + -> { BigDecimal("1").quo(BigDecimal("0")) }.should.raise(FloatDomainError) BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW, true) - lambda { BigDecimal("1E11111111111111111111") }.should raise_error(FloatDomainError) - lambda { (BigDecimal("1E1000000000000000000")**10) }.should raise_error(FloatDomainError) + -> { BigDecimal("1E11111111111111111111") }.should.raise(FloatDomainError) + -> { (BigDecimal("1E1000000000000000000")**10) }.should.raise(FloatDomainError) end end diff --git a/spec/ruby/library/bigdecimal/modulo_spec.rb b/spec/ruby/library/bigdecimal/modulo_spec.rb index 6feeb685eb..035d31bd98 100644 --- a/spec/ruby/library/bigdecimal/modulo_spec.rb +++ b/spec/ruby/library/bigdecimal/modulo_spec.rb @@ -1,12 +1,12 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../shared/modulo', __FILE__) +require_relative '../../spec_helper' +require_relative 'shared/modulo' describe "BigDecimal#%" do - it_behaves_like(:bigdecimal_modulo, :%) - it_behaves_like(:bigdecimal_modulo_zerodivisionerror, :%) + it_behaves_like :bigdecimal_modulo, :% + it_behaves_like :bigdecimal_modulo_zerodivisionerror, :% end describe "BigDecimal#modulo" do - it_behaves_like(:bigdecimal_modulo, :modulo) - it_behaves_like(:bigdecimal_modulo_zerodivisionerror, :modulo) + it_behaves_like :bigdecimal_modulo, :modulo + it_behaves_like :bigdecimal_modulo_zerodivisionerror, :modulo end diff --git a/spec/ruby/library/bigdecimal/mult_spec.rb b/spec/ruby/library/bigdecimal/mult_spec.rb index a140cf899a..2353df9cb8 100644 --- a/spec/ruby/library/bigdecimal/mult_spec.rb +++ b/spec/ruby/library/bigdecimal/mult_spec.rb @@ -1,5 +1,5 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../shared/mult', __FILE__) +require_relative '../../spec_helper' +require_relative 'shared/mult' require 'bigdecimal' describe "BigDecimal#mult" do @@ -9,7 +9,8 @@ end describe "BigDecimal#mult" do before :each do @one = BigDecimal "1" - @e3_minus = BigDecimal "3E-20001" + @e3_minus = BigDecimal("3E-20001") + @e3_plus = BigDecimal("3E20001") @e = BigDecimal "1.00000000000000000000123456789" @tolerance = @e.sub @one, 1000 @tolerance2 = BigDecimal "30001E-20005" @@ -20,5 +21,4 @@ describe "BigDecimal#mult" do @e.mult(@one, 1).should be_close(@one, @tolerance) @e3_minus.mult(@one, 1).should be_close(0, @tolerance2) end - end diff --git a/spec/ruby/library/bigdecimal/multiply_spec.rb b/spec/ruby/library/bigdecimal/multiply_spec.rb index 842e22eaa2..a8ce1da32e 100644 --- a/spec/ruby/library/bigdecimal/multiply_spec.rb +++ b/spec/ruby/library/bigdecimal/multiply_spec.rb @@ -1,5 +1,5 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../shared/mult', __FILE__) +require_relative '../../spec_helper' +require_relative 'shared/mult' require 'bigdecimal' describe "BigDecimal#*" do @@ -8,6 +8,7 @@ end describe "BigDecimal#*" do before :each do + @three = BigDecimal("3") @e3_minus = BigDecimal("3E-20001") @e3_plus = BigDecimal("3E20001") @e = BigDecimal("1.00000000000000000000123456789") @@ -23,4 +24,18 @@ describe "BigDecimal#*" do (@e3_minus * @e3_minus).should == BigDecimal("9E-40002") (@e * @one).should == @e end + + describe "with Object" do + it "tries to coerce the other operand to self" do + object = mock("Object") + object.should_receive(:coerce).with(@e3_minus).and_return([@e3_minus, @e3_plus]) + (@e3_minus * object).should == BigDecimal("9") + end + end + + describe "with Rational" do + it "produces a BigDecimal" do + (@three * Rational(500, 2)).should == BigDecimal("0.75e3") + end + end end diff --git a/spec/ruby/library/bigdecimal/nan_spec.rb b/spec/ruby/library/bigdecimal/nan_spec.rb index 5c291629b3..9eaf69b610 100644 --- a/spec/ruby/library/bigdecimal/nan_spec.rb +++ b/spec/ruby/library/bigdecimal/nan_spec.rb @@ -1,23 +1,23 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'bigdecimal' describe "BigDecimal#nan?" do it "returns true if self is not a number" do - BigDecimal("NaN").nan?.should == true + BigDecimal("NaN").should.nan? end it "returns false if self is not a NaN" do - BigDecimal("Infinity").nan?.should == false - BigDecimal("-Infinity").nan?.should == false - BigDecimal("0").nan?.should == false - BigDecimal("+0").nan?.should == false - BigDecimal("-0").nan?.should == false - BigDecimal("2E40001").nan?.should == false - BigDecimal("3E-20001").nan?.should == false - BigDecimal("0E-200000000").nan?.should == false - BigDecimal("0E200000000000").nan?.should == false - BigDecimal("0.000000000000000000000000").nan?.should == false + BigDecimal("Infinity").should_not.nan? + BigDecimal("-Infinity").should_not.nan? + BigDecimal("0").should_not.nan? + BigDecimal("+0").should_not.nan? + BigDecimal("-0").should_not.nan? + BigDecimal("2E40001").should_not.nan? + BigDecimal("3E-20001").should_not.nan? + BigDecimal("0E-200000000").should_not.nan? + BigDecimal("0E200000000000").should_not.nan? + BigDecimal("0.000000000000000000000000").should_not.nan? end end diff --git a/spec/ruby/library/bigdecimal/new_spec.rb b/spec/ruby/library/bigdecimal/new_spec.rb deleted file mode 100644 index de3aa195ca..0000000000 --- a/spec/ruby/library/bigdecimal/new_spec.rb +++ /dev/null @@ -1,109 +0,0 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require 'bigdecimal' - -describe "BigDecimal.new" do - - it "creates a new object of class BigDecimal" do - BigDecimal.new("3.14159").should be_kind_of(BigDecimal) - (0..9).each {|i| - BigDecimal.new("1#{i}").should == 10 + i - BigDecimal.new("-1#{i}").should == -10 - i - BigDecimal.new("1E#{i}").should == 10**i - BigDecimal.new("1000000E-#{i}").should == 10**(6-i).to_f - # ^ to_f to avoid Rational type - } - (1..9).each {|i| - BigDecimal.new("100.#{i}").to_s.should =~ /\A0\.100#{i}E3\z/i - BigDecimal.new("-100.#{i}").to_s.should =~ /\A-0\.100#{i}E3\z/i - } - end - - it "accepts significant digits >= given precision" do - BigDecimal.new("3.1415923", 10).precs[1].should >= 10 - end - - it "determines precision from initial value" do - pi_string = "3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593014782083152134043" - BigDecimal.new(pi_string).precs[1].should >= pi_string.size-1 - end - - it "ignores leading whitespace" do - BigDecimal.new(" \t\n \r1234").should == BigDecimal.new("1234") - BigDecimal.new(" \t\n \rNaN \n").nan?.should == true - BigDecimal.new(" \t\n \rInfinity \n").infinite?.should == 1 - BigDecimal.new(" \t\n \r-Infinity \n").infinite?.should == -1 - end - - it "ignores trailing garbage" do - BigDecimal.new("123E45ruby").should == BigDecimal.new("123E45") - BigDecimal.new("123x45").should == BigDecimal.new("123") - BigDecimal.new("123.4%E5").should == BigDecimal.new("123.4") - BigDecimal.new("1E2E3E4E5E").should == BigDecimal.new("100") - end - - ruby_version_is ""..."2.4" do - it "treats invalid strings as 0.0" do - BigDecimal.new("ruby").should == BigDecimal.new("0.0") - BigDecimal.new(" \t\n \r-\t\t\tInfinity \n").should == BigDecimal.new("0.0") - end - end - - ruby_version_is "2.4" do - it "raises ArgumentError for invalid strings" do - lambda { BigDecimal.new("ruby") }.should raise_error(ArgumentError) - lambda { BigDecimal.new(" \t\n \r-\t\t\tInfinity \n") }.should raise_error(ArgumentError) - end - end - - it "allows omitting the integer part" do - BigDecimal.new(".123").should == BigDecimal.new("0.123") - end - - it "allows for underscores in all parts" do - reference = BigDecimal.new("12345.67E89") - - BigDecimal.new("12_345.67E89").should == reference - BigDecimal.new("1_2_3_4_5_._6____7_E89").should == reference - BigDecimal.new("12345_.67E_8__9_").should == reference - end - - it "accepts NaN and [+-]Infinity" do - BigDecimal.new("NaN").nan?.should == true - - pos_inf = BigDecimal.new("Infinity") - pos_inf.finite?.should == false - pos_inf.should > 0 - pos_inf.should == BigDecimal.new("+Infinity") - - neg_inf = BigDecimal.new("-Infinity") - neg_inf.finite?.should == false - neg_inf.should < 0 - end - - it "allows for [eEdD] as exponent separator" do - reference = BigDecimal.new("12345.67E89") - - BigDecimal.new("12345.67e89").should == reference - BigDecimal.new("12345.67E89").should == reference - BigDecimal.new("12345.67d89").should == reference - BigDecimal.new("12345.67D89").should == reference - end - - it "allows for varying signs" do - reference = BigDecimal.new("123.456E1") - - BigDecimal.new("+123.456E1").should == reference - BigDecimal.new("-123.456E1").should == -reference - BigDecimal.new("123.456E+1").should == reference - BigDecimal.new("12345.6E-1").should == reference - BigDecimal.new("+123.456E+1").should == reference - BigDecimal.new("+12345.6E-1").should == reference - BigDecimal.new("-123.456E+1").should == -reference - BigDecimal.new("-12345.6E-1").should == -reference - end - - it 'raises ArgumentError when Float is used without precision' do - lambda { BigDecimal(1.0) }.should raise_error(ArgumentError) - end - -end diff --git a/spec/ruby/library/bigdecimal/nonzero_spec.rb b/spec/ruby/library/bigdecimal/nonzero_spec.rb index 336c72a6b2..31421ebdf4 100644 --- a/spec/ruby/library/bigdecimal/nonzero_spec.rb +++ b/spec/ruby/library/bigdecimal/nonzero_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'bigdecimal' describe "BigDecimal#nonzero?" do @@ -10,11 +10,11 @@ describe "BigDecimal#nonzero?" do infinity = BigDecimal("Infinity") infinity_minus = BigDecimal("-Infinity") nan = BigDecimal("NaN") - infinity.nonzero?.should equal(infinity) - infinity_minus.nonzero?.should equal(infinity_minus) - nan.nonzero?.should equal(nan) - e3_minus.nonzero?.should equal(e3_minus) - e2_plus.nonzero?.should equal(e2_plus) + infinity.nonzero?.should.equal?(infinity) + infinity_minus.nonzero?.should.equal?(infinity_minus) + nan.nonzero?.should.equal?(nan) + e3_minus.nonzero?.should.equal?(e3_minus) + e2_plus.nonzero?.should.equal?(e2_plus) end it "returns nil otherwise" do diff --git a/spec/ruby/library/bigdecimal/plus_spec.rb b/spec/ruby/library/bigdecimal/plus_spec.rb index 72ec282075..d1934841c8 100644 --- a/spec/ruby/library/bigdecimal/plus_spec.rb +++ b/spec/ruby/library/bigdecimal/plus_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'bigdecimal' describe "BigDecimal#+" do @@ -30,8 +30,8 @@ describe "BigDecimal#+" do end it "returns NaN if NaN is involved" do - (@one + @nan).nan?.should == true - (@nan + @one).nan?.should == true + (@one + @nan).should.nan? + (@nan + @one).should.nan? end it "returns Infinity or -Infinity if these are involved" do @@ -41,7 +41,14 @@ describe "BigDecimal#+" do end it "returns NaN if Infinity + (- Infinity)" do - (@infinity + @infinity_minus).nan?.should == true + (@infinity + @infinity_minus).should.nan? end + describe "with Object" do + it "tries to coerce the other operand to self" do + object = mock("Object") + object.should_receive(:coerce).with(@one).and_return([@one, BigDecimal("42")]) + (@one + object).should == BigDecimal("43") + end + end end diff --git a/spec/ruby/library/bigdecimal/power_spec.rb b/spec/ruby/library/bigdecimal/power_spec.rb index f2b1a9b5c8..63a45a1887 100644 --- a/spec/ruby/library/bigdecimal/power_spec.rb +++ b/spec/ruby/library/bigdecimal/power_spec.rb @@ -1,6 +1,6 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../shared/power', __FILE__) +require_relative '../../spec_helper' +require_relative 'shared/power' describe "BigDecimal#power" do - it_behaves_like(:bigdecimal_power, :power) + it_behaves_like :bigdecimal_power, :power end diff --git a/spec/ruby/library/bigdecimal/precs_spec.rb b/spec/ruby/library/bigdecimal/precs_spec.rb deleted file mode 100644 index c7700a3819..0000000000 --- a/spec/ruby/library/bigdecimal/precs_spec.rb +++ /dev/null @@ -1,49 +0,0 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require 'bigdecimal' - -describe "BigDecimal#precs" do - - before :each do - @infinity = BigDecimal("Infinity") - @infinity_neg = BigDecimal("-Infinity") - @nan = BigDecimal("NaN") - @zero = BigDecimal("0") - @zero_neg = BigDecimal("-0") - - @arr = [BigDecimal("2E40001"), BigDecimal("3E-20001"),\ - @infinity, @infinity_neg, @nan, @zero, @zero_neg] - @precision = BigDecimal::BASE.to_s.length - 1 - end - - it "returns array of two values" do - @arr.each do |x| - x.precs.kind_of?(Array).should == true - x.precs.size.should == 2 - end - end - - it "returns Integers as array values" do - @arr.each do |x| - x.precs[0].kind_of?(Integer).should == true - x.precs[1].kind_of?(Integer).should == true - end - end - - it "returns the current value of significant digits as the first value" do - BigDecimal("3.14159").precs[0].should >= 6 - BigDecimal('1').precs[0].should == BigDecimal('1' + '0' * 100).precs[0] - [@infinity, @infinity_neg, @nan, @zero, @zero_neg].each do |value| - value.precs[0].should <= @precision - end - end - - it "returns the maximum number of significant digits as the second value" do - BigDecimal("3.14159").precs[1].should >= 6 - BigDecimal('1').precs[1].should >= 1 - BigDecimal('1' + '0' * 100).precs[1] >= 101 - [@infinity, @infinity_neg, @nan, @zero, @zero_neg].each do |value| - value.precs[1].should >= 1 - end - end -end - diff --git a/spec/ruby/library/bigdecimal/quo_spec.rb b/spec/ruby/library/bigdecimal/quo_spec.rb index bc27fd0f5e..65a4330303 100644 --- a/spec/ruby/library/bigdecimal/quo_spec.rb +++ b/spec/ruby/library/bigdecimal/quo_spec.rb @@ -1,13 +1,12 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../shared/quo', __FILE__) +require_relative '../../spec_helper' +require_relative 'shared/quo' require 'bigdecimal' describe "BigDecimal#quo" do it_behaves_like :bigdecimal_quo, :quo, [] it "returns NaN if NaN is involved" do - BigDecimal("1").quo(BigDecimal("NaN")).nan?.should == true - BigDecimal("NaN").quo(BigDecimal("1")).nan?.should == true + BigDecimal("1").quo(BigDecimal("NaN")).should.nan? + BigDecimal("NaN").quo(BigDecimal("1")).should.nan? end end - diff --git a/spec/ruby/library/bigdecimal/remainder_spec.rb b/spec/ruby/library/bigdecimal/remainder_spec.rb index 796522c5c8..27a2986570 100644 --- a/spec/ruby/library/bigdecimal/remainder_spec.rb +++ b/spec/ruby/library/bigdecimal/remainder_spec.rb @@ -1,11 +1,12 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'bigdecimal' describe "BigDecimal#remainder" do before :each do @zero = BigDecimal("0") - @one = BigDecimal("0") + @one = BigDecimal("1") + @three = BigDecimal("3") @mixed = BigDecimal("1.23456789") @pos_int = BigDecimal("2E5555") @neg_int = BigDecimal("-2E5555") @@ -36,9 +37,11 @@ describe "BigDecimal#remainder" do @neg_int.remainder(@pos_frac).should == @neg_int - @pos_frac * (@neg_int / @pos_frac).truncate end - it "returns NaN used with zero" do - @mixed.remainder(@zero).nan?.should == true - @zero.remainder(@zero).nan?.should == true + version_is BigDecimal::VERSION, "3.3.0" do + it "raises ZeroDivisionError used with zero" do + -> { @mixed.remainder(@zero) }.should.raise(ZeroDivisionError) + -> { @zero.remainder(@zero) }.should.raise(ZeroDivisionError) + end end it "returns zero if used on zero" do @@ -46,39 +49,29 @@ describe "BigDecimal#remainder" do end it "returns NaN if NaN is involved" do - @nan.remainder(@nan).nan?.should == true - @nan.remainder(@one).nan?.should == true - @one.remainder(@nan).nan?.should == true - @infinity.remainder(@nan).nan?.should == true - @nan.remainder(@infinity).nan?.should == true - end - - it "returns NaN if Infinity is involved" do - @infinity.remainder(@infinity).nan?.should == true - @infinity.remainder(@one).nan?.should == true - @infinity.remainder(@mixed).nan?.should == true - @infinity.remainder(@one_minus).nan?.should == true - @infinity.remainder(@frac_1).nan?.should == true - @one.remainder(@infinity).nan?.should == true - - @infinity_minus.remainder(@infinity_minus).nan?.should == true - @infinity_minus.remainder(@one).nan?.should == true - @one.remainder(@infinity_minus).nan?.should == true - @frac_2.remainder(@infinity_minus).nan?.should == true - - @infinity.remainder(@infinity_minus).nan?.should == true - @infinity_minus.remainder(@infinity).nan?.should == true + @nan.remainder(@nan).should.nan? + @nan.remainder(@one).should.nan? + @one.remainder(@nan).should.nan? + @infinity.remainder(@nan).should.nan? + @nan.remainder(@infinity).should.nan? end it "coerces arguments to BigDecimal if possible" do - @one.remainder(2).should == @one + @three.remainder(2).should == @one end + describe "with Object" do + it "tries to coerce the other operand to self" do + object = mock("Object") + object.should_receive(:coerce).with(@three).and_return([@three, 2]) + @three.remainder(object).should == @one + end + end it "raises TypeError if the argument cannot be coerced to BigDecimal" do - lambda { + -> { @one.remainder('2') - }.should raise_error(TypeError) + }.should.raise(TypeError) end end diff --git a/spec/ruby/library/bigdecimal/round_spec.rb b/spec/ruby/library/bigdecimal/round_spec.rb index 6c1987c5d8..622e129d93 100644 --- a/spec/ruby/library/bigdecimal/round_spec.rb +++ b/spec/ruby/library/bigdecimal/round_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'bigdecimal' describe "BigDecimal#round" do @@ -62,141 +62,173 @@ describe "BigDecimal#round" do @n2_49.round(0).should == @neg_two end - describe "BigDecimal::ROUND_UP" do - it "rounds values away from zero" do - @p1_50.round(0, BigDecimal::ROUND_UP).should == @two - @p1_51.round(0, BigDecimal::ROUND_UP).should == @two - @p1_49.round(0, BigDecimal::ROUND_UP).should == @two - @n1_50.round(0, BigDecimal::ROUND_UP).should == @neg_two - @n1_51.round(0, BigDecimal::ROUND_UP).should == @neg_two - @n1_49.round(0, BigDecimal::ROUND_UP).should == @neg_two - - @p2_50.round(0, BigDecimal::ROUND_UP).should == @three - @p2_51.round(0, BigDecimal::ROUND_UP).should == @three - @p2_49.round(0, BigDecimal::ROUND_UP).should == @three - @n2_50.round(0, BigDecimal::ROUND_UP).should == @neg_three - @n2_51.round(0, BigDecimal::ROUND_UP).should == @neg_three - @n2_49.round(0, BigDecimal::ROUND_UP).should == @neg_three + ["BigDecimal::ROUND_UP", ":up"].each do |way| + describe way do + it "rounds values away from zero" do + mode = eval(way) + + @p1_50.round(0, mode).should == @two + @p1_51.round(0, mode).should == @two + @p1_49.round(0, mode).should == @two + @n1_50.round(0, mode).should == @neg_two + @n1_51.round(0, mode).should == @neg_two + @n1_49.round(0, mode).should == @neg_two + + @p2_50.round(0, mode).should == @three + @p2_51.round(0, mode).should == @three + @p2_49.round(0, mode).should == @three + @n2_50.round(0, mode).should == @neg_three + @n2_51.round(0, mode).should == @neg_three + @n2_49.round(0, mode).should == @neg_three + end end end - describe "BigDecimal::ROUND_DOWN" do - it "rounds values towards zero" do - @p1_50.round(0, BigDecimal::ROUND_DOWN).should == @one - @p1_51.round(0, BigDecimal::ROUND_DOWN).should == @one - @p1_49.round(0, BigDecimal::ROUND_DOWN).should == @one - @n1_50.round(0, BigDecimal::ROUND_DOWN).should == @neg_one - @n1_51.round(0, BigDecimal::ROUND_DOWN).should == @neg_one - @n1_49.round(0, BigDecimal::ROUND_DOWN).should == @neg_one - - @p2_50.round(0, BigDecimal::ROUND_DOWN).should == @two - @p2_51.round(0, BigDecimal::ROUND_DOWN).should == @two - @p2_49.round(0, BigDecimal::ROUND_DOWN).should == @two - @n2_50.round(0, BigDecimal::ROUND_DOWN).should == @neg_two - @n2_51.round(0, BigDecimal::ROUND_DOWN).should == @neg_two - @n2_49.round(0, BigDecimal::ROUND_DOWN).should == @neg_two + ["BigDecimal::ROUND_DOWN", ":down", ":truncate"].each do |way| + describe way do + it "rounds values towards zero" do + mode = eval(way) + + @p1_50.round(0, mode).should == @one + @p1_51.round(0, mode).should == @one + @p1_49.round(0, mode).should == @one + @n1_50.round(0, mode).should == @neg_one + @n1_51.round(0, mode).should == @neg_one + @n1_49.round(0, mode).should == @neg_one + + @p2_50.round(0, mode).should == @two + @p2_51.round(0, mode).should == @two + @p2_49.round(0, mode).should == @two + @n2_50.round(0, mode).should == @neg_two + @n2_51.round(0, mode).should == @neg_two + @n2_49.round(0, mode).should == @neg_two + end end end - describe "BigDecimal::ROUND_HALF_UP" do - it "rounds values >= 5 up, otherwise down" do - @p1_50.round(0, BigDecimal::ROUND_HALF_UP).should == @two - @p1_51.round(0, BigDecimal::ROUND_HALF_UP).should == @two - @p1_49.round(0, BigDecimal::ROUND_HALF_UP).should == @one - @n1_50.round(0, BigDecimal::ROUND_HALF_UP).should == @neg_two - @n1_51.round(0, BigDecimal::ROUND_HALF_UP).should == @neg_two - @n1_49.round(0, BigDecimal::ROUND_HALF_UP).should == @neg_one - - @p2_50.round(0, BigDecimal::ROUND_HALF_UP).should == @three - @p2_51.round(0, BigDecimal::ROUND_HALF_UP).should == @three - @p2_49.round(0, BigDecimal::ROUND_HALF_UP).should == @two - @n2_50.round(0, BigDecimal::ROUND_HALF_UP).should == @neg_three - @n2_51.round(0, BigDecimal::ROUND_HALF_UP).should == @neg_three - @n2_49.round(0, BigDecimal::ROUND_HALF_UP).should == @neg_two + ["BigDecimal::ROUND_HALF_UP", ":half_up", ":default"].each do |way| + describe way do + it "rounds values >= 5 up, otherwise down" do + mode = eval(way) + + @p1_50.round(0, mode).should == @two + @p1_51.round(0, mode).should == @two + @p1_49.round(0, mode).should == @one + @n1_50.round(0, mode).should == @neg_two + @n1_51.round(0, mode).should == @neg_two + @n1_49.round(0, mode).should == @neg_one + + @p2_50.round(0, mode).should == @three + @p2_51.round(0, mode).should == @three + @p2_49.round(0, mode).should == @two + @n2_50.round(0, mode).should == @neg_three + @n2_51.round(0, mode).should == @neg_three + @n2_49.round(0, mode).should == @neg_two + end end end - describe "BigDecimal::ROUND_HALF_DOWN" do - it "rounds values > 5 up, otherwise down" do - @p1_50.round(0, BigDecimal::ROUND_HALF_DOWN).should == @one - @p1_51.round(0, BigDecimal::ROUND_HALF_DOWN).should == @two - @p1_49.round(0, BigDecimal::ROUND_HALF_DOWN).should == @one - @n1_50.round(0, BigDecimal::ROUND_HALF_DOWN).should == @neg_one - @n1_51.round(0, BigDecimal::ROUND_HALF_DOWN).should == @neg_two - @n1_49.round(0, BigDecimal::ROUND_HALF_DOWN).should == @neg_one - - @p2_50.round(0, BigDecimal::ROUND_HALF_DOWN).should == @two - @p2_51.round(0, BigDecimal::ROUND_HALF_DOWN).should == @three - @p2_49.round(0, BigDecimal::ROUND_HALF_DOWN).should == @two - @n2_50.round(0, BigDecimal::ROUND_HALF_DOWN).should == @neg_two - @n2_51.round(0, BigDecimal::ROUND_HALF_DOWN).should == @neg_three - @n2_49.round(0, BigDecimal::ROUND_HALF_DOWN).should == @neg_two + ["BigDecimal::ROUND_HALF_DOWN", ":half_down"].each do |way| + describe way do + it "rounds values > 5 up, otherwise down" do + mode = eval(way) + + @p1_50.round(0, mode).should == @one + @p1_51.round(0, mode).should == @two + @p1_49.round(0, mode).should == @one + @n1_50.round(0, mode).should == @neg_one + @n1_51.round(0, mode).should == @neg_two + @n1_49.round(0, mode).should == @neg_one + + @p2_50.round(0, mode).should == @two + @p2_51.round(0, mode).should == @three + @p2_49.round(0, mode).should == @two + @n2_50.round(0, mode).should == @neg_two + @n2_51.round(0, mode).should == @neg_three + @n2_49.round(0, mode).should == @neg_two + end end end - describe "BigDecimal::ROUND_CEILING" do - it "rounds values towards +infinity" do - @p1_50.round(0, BigDecimal::ROUND_CEILING).should == @two - @p1_51.round(0, BigDecimal::ROUND_CEILING).should == @two - @p1_49.round(0, BigDecimal::ROUND_CEILING).should == @two - @n1_50.round(0, BigDecimal::ROUND_CEILING).should == @neg_one - @n1_51.round(0, BigDecimal::ROUND_CEILING).should == @neg_one - @n1_49.round(0, BigDecimal::ROUND_CEILING).should == @neg_one - - @p2_50.round(0, BigDecimal::ROUND_CEILING).should == @three - @p2_51.round(0, BigDecimal::ROUND_CEILING).should == @three - @p2_49.round(0, BigDecimal::ROUND_CEILING).should == @three - @n2_50.round(0, BigDecimal::ROUND_CEILING).should == @neg_two - @n2_51.round(0, BigDecimal::ROUND_CEILING).should == @neg_two - @n2_49.round(0, BigDecimal::ROUND_CEILING).should == @neg_two + ["BigDecimal::ROUND_CEILING", ":ceiling", ":ceil"].each do |way| + describe way do + it "rounds values towards +infinity" do + mode = eval(way) + + @p1_50.round(0, mode).should == @two + @p1_51.round(0, mode).should == @two + @p1_49.round(0, mode).should == @two + @n1_50.round(0, mode).should == @neg_one + @n1_51.round(0, mode).should == @neg_one + @n1_49.round(0, mode).should == @neg_one + + @p2_50.round(0, mode).should == @three + @p2_51.round(0, mode).should == @three + @p2_49.round(0, mode).should == @three + @n2_50.round(0, mode).should == @neg_two + @n2_51.round(0, mode).should == @neg_two + @n2_49.round(0, mode).should == @neg_two + end end end - describe "BigDecimal::ROUND_FLOOR" do - it "rounds values towards -infinity" do - @p1_50.round(0, BigDecimal::ROUND_FLOOR).should == @one - @p1_51.round(0, BigDecimal::ROUND_FLOOR).should == @one - @p1_49.round(0, BigDecimal::ROUND_FLOOR).should == @one - @n1_50.round(0, BigDecimal::ROUND_FLOOR).should == @neg_two - @n1_51.round(0, BigDecimal::ROUND_FLOOR).should == @neg_two - @n1_49.round(0, BigDecimal::ROUND_FLOOR).should == @neg_two - - @p2_50.round(0, BigDecimal::ROUND_FLOOR).should == @two - @p2_51.round(0, BigDecimal::ROUND_FLOOR).should == @two - @p2_49.round(0, BigDecimal::ROUND_FLOOR).should == @two - @n2_50.round(0, BigDecimal::ROUND_FLOOR).should == @neg_three - @n2_51.round(0, BigDecimal::ROUND_FLOOR).should == @neg_three - @n2_49.round(0, BigDecimal::ROUND_FLOOR).should == @neg_three + ["BigDecimal::ROUND_FLOOR", ":floor"].each do |way| + describe way do + it "rounds values towards -infinity" do + mode = eval(way) + + @p1_50.round(0, mode).should == @one + @p1_51.round(0, mode).should == @one + @p1_49.round(0, mode).should == @one + @n1_50.round(0, mode).should == @neg_two + @n1_51.round(0, mode).should == @neg_two + @n1_49.round(0, mode).should == @neg_two + + @p2_50.round(0, mode).should == @two + @p2_51.round(0, mode).should == @two + @p2_49.round(0, mode).should == @two + @n2_50.round(0, mode).should == @neg_three + @n2_51.round(0, mode).should == @neg_three + @n2_49.round(0, mode).should == @neg_three + end end end - describe "BigDecimal::ROUND_HALF_EVEN" do - it "rounds values > 5 up, < 5 down and == 5 towards even neighbor" do - @p1_50.round(0, BigDecimal::ROUND_HALF_EVEN).should == @two - @p1_51.round(0, BigDecimal::ROUND_HALF_EVEN).should == @two - @p1_49.round(0, BigDecimal::ROUND_HALF_EVEN).should == @one - @n1_50.round(0, BigDecimal::ROUND_HALF_EVEN).should == @neg_two - @n1_51.round(0, BigDecimal::ROUND_HALF_EVEN).should == @neg_two - @n1_49.round(0, BigDecimal::ROUND_HALF_EVEN).should == @neg_one - - @p2_50.round(0, BigDecimal::ROUND_HALF_EVEN).should == @two - @p2_51.round(0, BigDecimal::ROUND_HALF_EVEN).should == @three - @p2_49.round(0, BigDecimal::ROUND_HALF_EVEN).should == @two - @n2_50.round(0, BigDecimal::ROUND_HALF_EVEN).should == @neg_two - @n2_51.round(0, BigDecimal::ROUND_HALF_EVEN).should == @neg_three - @n2_49.round(0, BigDecimal::ROUND_HALF_EVEN).should == @neg_two + ["BigDecimal::ROUND_HALF_EVEN", ":half_even", ":banker"].each do |way| + describe way do + it "rounds values > 5 up, < 5 down and == 5 towards even neighbor" do + mode = eval(way) + + @p1_50.round(0, mode).should == @two + @p1_51.round(0, mode).should == @two + @p1_49.round(0, mode).should == @one + @n1_50.round(0, mode).should == @neg_two + @n1_51.round(0, mode).should == @neg_two + @n1_49.round(0, mode).should == @neg_one + + @p2_50.round(0, mode).should == @two + @p2_51.round(0, mode).should == @three + @p2_49.round(0, mode).should == @two + @n2_50.round(0, mode).should == @neg_two + @n2_51.round(0, mode).should == @neg_three + @n2_49.round(0, mode).should == @neg_two + end end end it 'raise exception, if self is special value' do - lambda { BigDecimal('NaN').round }.should raise_error(FloatDomainError) - lambda { BigDecimal('Infinity').round }.should raise_error(FloatDomainError) - lambda { BigDecimal('-Infinity').round }.should raise_error(FloatDomainError) + -> { BigDecimal('NaN').round }.should.raise(FloatDomainError) + -> { BigDecimal('Infinity').round }.should.raise(FloatDomainError) + -> { BigDecimal('-Infinity').round }.should.raise(FloatDomainError) end it 'do not raise exception, if self is special value and precision is given' do - lambda { BigDecimal('NaN').round(2) }.should_not raise_error(FloatDomainError) - lambda { BigDecimal('Infinity').round(2) }.should_not raise_error(FloatDomainError) - lambda { BigDecimal('-Infinity').round(2) }.should_not raise_error(FloatDomainError) + -> { BigDecimal('NaN').round(2) }.should_not.raise(FloatDomainError) + -> { BigDecimal('Infinity').round(2) }.should_not.raise(FloatDomainError) + -> { BigDecimal('-Infinity').round(2) }.should_not.raise(FloatDomainError) + end + + it 'raise for a non-existent round mode' do + -> { @p1_50.round(0, :nonsense) }.should.raise(ArgumentError, "invalid rounding mode (nonsense)") end end diff --git a/spec/ruby/library/bigdecimal/shared/clone.rb b/spec/ruby/library/bigdecimal/shared/clone.rb new file mode 100644 index 0000000000..03de6d0fc3 --- /dev/null +++ b/spec/ruby/library/bigdecimal/shared/clone.rb @@ -0,0 +1,13 @@ +require 'bigdecimal' + +describe :bigdecimal_clone, shared: true do + before :each do + @obj = BigDecimal("1.2345") + end + + it "returns self" do + copy = @obj.public_send(@method) + + copy.should.equal?(@obj) + end +end diff --git a/spec/ruby/library/bigdecimal/shared/eql.rb b/spec/ruby/library/bigdecimal/shared/eql.rb index eaad272e9a..8e3e388bab 100644 --- a/spec/ruby/library/bigdecimal/shared/eql.rb +++ b/spec/ruby/library/bigdecimal/shared/eql.rb @@ -2,8 +2,8 @@ require 'bigdecimal' describe :bigdecimal_eql, shared: true do before :each do - @bg6543_21 = BigDecimal.new("6543.21") - @bg5667_19 = BigDecimal.new("5667.19") + @bg6543_21 = BigDecimal("6543.21") + @bg5667_19 = BigDecimal("5667.19") @a = BigDecimal("1.0000000000000000000000000000000000000000005") @b = BigDecimal("1.00000000000000000000000000000000000000000005") @bigint = BigDecimal("1000.0") diff --git a/spec/ruby/library/bigdecimal/shared/modulo.rb b/spec/ruby/library/bigdecimal/shared/modulo.rb index 78ebe8360d..63470d0977 100644 --- a/spec/ruby/library/bigdecimal/shared/modulo.rb +++ b/spec/ruby/library/bigdecimal/shared/modulo.rb @@ -18,8 +18,8 @@ describe :bigdecimal_modulo, shared: true do end it "returns self modulo other" do - bd6543 = BigDecimal.new("6543.21") - bd5667 = BigDecimal.new("5667.19") + bd6543 = BigDecimal("6543.21") + bd5667 = BigDecimal("5667.19") a = BigDecimal("1.0000000000000000000000000000000000000000005") b = BigDecimal("1.00000000000000000000000000000000000000000005") @@ -70,47 +70,62 @@ describe :bigdecimal_modulo, shared: true do res.kind_of?(BigDecimal).should == true end + describe "with Object" do + it "tries to coerce the other operand to self" do + bd6543 = BigDecimal("6543.21") + object = mock("Object") + object.should_receive(:coerce).with(bd6543).and_return([bd6543, 137]) + bd6543.send(@method, object, *@object).should == BigDecimal("104.21") + end + end + it "returns NaN if NaN is involved" do - @nan.send(@method, @nan).nan?.should == true - @nan.send(@method, @one).nan?.should == true - @one.send(@method, @nan).nan?.should == true - @infinity.send(@method, @nan).nan?.should == true - @nan.send(@method, @infinity).nan?.should == true + @nan.send(@method, @nan).should.nan? + @nan.send(@method, @one).should.nan? + @one.send(@method, @nan).should.nan? + @infinity.send(@method, @nan).should.nan? + @nan.send(@method, @infinity).should.nan? end it "returns NaN if the dividend is Infinity" do - @infinity.send(@method, @infinity).nan?.should == true - @infinity.send(@method, @one).nan?.should == true - @infinity.send(@method, @mixed).nan?.should == true - @infinity.send(@method, @one_minus).nan?.should == true - @infinity.send(@method, @frac_1).nan?.should == true + @infinity.send(@method, @infinity).should.nan? + @infinity.send(@method, @one).should.nan? + @infinity.send(@method, @mixed).should.nan? + @infinity.send(@method, @one_minus).should.nan? + @infinity.send(@method, @frac_1).should.nan? - @infinity_minus.send(@method, @infinity_minus).nan?.should == true - @infinity_minus.send(@method, @one).nan?.should == true + @infinity_minus.send(@method, @infinity_minus).should.nan? + @infinity_minus.send(@method, @one).should.nan? - @infinity.send(@method, @infinity_minus).nan?.should == true - @infinity_minus.send(@method, @infinity).nan?.should == true + @infinity.send(@method, @infinity_minus).should.nan? + @infinity_minus.send(@method, @infinity).should.nan? end - it "returns the dividend if the divisor is Infinity" do - @one.send(@method, @infinity).should == @one - @one.send(@method, @infinity_minus).should == @one - @frac_2.send(@method, @infinity_minus).should == @frac_2 + version_is BigDecimal::VERSION, "3.3.0" do + it "returns the dividend if the divisor is Infinity and signs are same" do + @one.send(@method, @infinity).should == @one + (-@frac_2).send(@method, @infinity_minus).should == -@frac_2 + end + + it "returns the divisor if the divisor is Infinity and signs are different" do + (-@one).send(@method, @infinity).should == @infinity + @frac_2.send(@method, @infinity_minus).should == @infinity_minus + end end it "raises TypeError if the argument cannot be coerced to BigDecimal" do - lambda { + -> { @one.send(@method, '2') - }.should raise_error(TypeError) + }.should.raise(TypeError) end end describe :bigdecimal_modulo_zerodivisionerror, shared: true do it "raises ZeroDivisionError if other is zero" do - bd5667 = BigDecimal.new("5667.19") + bd5667 = BigDecimal("5667.19") - lambda { bd5667.send(@method, 0) }.should raise_error(ZeroDivisionError) - lambda { bd5667.send(@method, BigDecimal("0")) }.should raise_error(ZeroDivisionError) - lambda { @zero.send(@method, @zero) }.should raise_error(ZeroDivisionError) + -> { bd5667.send(@method, 0) }.should.raise(ZeroDivisionError) + -> { bd5667.send(@method, BigDecimal("0")) }.should.raise(ZeroDivisionError) + -> { @zero.send(@method, @zero) }.should.raise(ZeroDivisionError) end end diff --git a/spec/ruby/library/bigdecimal/shared/mult.rb b/spec/ruby/library/bigdecimal/shared/mult.rb index b94c9385de..c613df04c4 100644 --- a/spec/ruby/library/bigdecimal/shared/mult.rb +++ b/spec/ruby/library/bigdecimal/shared/mult.rb @@ -51,8 +51,8 @@ describe :bigdecimal_mult, shared: true do values = @regular_vals + @zeroes values.each do |val| - @nan.send(@method, val, *@object).nan?.should == true - val.send(@method, @nan, *@object).nan?.should == true + @nan.send(@method, val, *@object).should.nan? + val.send(@method, @nan, *@object).should.nan? end end @@ -62,9 +62,9 @@ describe :bigdecimal_mult, shared: true do values.each do |val| @zeroes.each do |zero| zero.send(@method, val, *@object).should == 0 - zero.send(@method, val, *@object).zero?.should == true + zero.send(@method, val, *@object).should.zero? val.send(@method, zero, *@object).should == 0 - val.send(@method, zero, *@object).zero?.should == true + val.send(@method, zero, *@object).should.zero? end end end @@ -75,8 +75,8 @@ describe :bigdecimal_mult, shared: true do values.each do |val| infs.each do |inf| - inf.send(@method, val, *@object).finite?.should == false - val.send(@method, inf, *@object).finite?.should == false + inf.send(@method, val, *@object).should_not.finite? + val.send(@method, inf, *@object).should_not.finite? end end @@ -89,9 +89,9 @@ describe :bigdecimal_mult, shared: true do end it "returns NaN if the result is undefined" do - @zero.send(@method, @infinity, *@object).nan?.should == true - @zero.send(@method, @infinity_minus, *@object).nan?.should == true - @infinity.send(@method, @zero, *@object).nan?.should == true - @infinity_minus.send(@method, @zero, *@object).nan?.should == true + @zero.send(@method, @infinity, *@object).should.nan? + @zero.send(@method, @infinity_minus, *@object).should.nan? + @infinity.send(@method, @zero, *@object).should.nan? + @infinity_minus.send(@method, @zero, *@object).should.nan? end end diff --git a/spec/ruby/library/bigdecimal/shared/power.rb b/spec/ruby/library/bigdecimal/shared/power.rb index a4848fb2e2..6dafb638e2 100644 --- a/spec/ruby/library/bigdecimal/shared/power.rb +++ b/spec/ruby/library/bigdecimal/shared/power.rb @@ -10,8 +10,8 @@ describe :bigdecimal_power, shared: true do e = BigDecimal("1.00000000000000000000123456789") one = BigDecimal("1") ten = BigDecimal("10") - # The tolerance is dependent upon the size of BASE_FIG - tolerance = BigDecimal("1E-70") + # Accuracy is at least ndigits(== 30) + DOUBLE_FIG(== 16) + tolerance = BigDecimal("1E-46") ten_powers = BigDecimal("1E10000") pi = BigDecimal("3.14159265358979") e3_minus.send(@method, 2).should == e3_minus_power_2 @@ -53,8 +53,8 @@ describe :bigdecimal_power, shared: true do end it "returns NaN if self is NaN" do - BigDecimal("NaN").send(@method, -5).nan?.should == true - BigDecimal("NaN").send(@method, 5).nan?.should == true + BigDecimal("NaN").send(@method, -5).should.nan? + BigDecimal("NaN").send(@method, 5).should.nan? end it "returns 0.0 if self is infinite and argument is negative" do diff --git a/spec/ruby/library/bigdecimal/shared/quo.rb b/spec/ruby/library/bigdecimal/shared/quo.rb index cb51c10d71..18ff2fe9a5 100644 --- a/spec/ruby/library/bigdecimal/shared/quo.rb +++ b/spec/ruby/library/bigdecimal/shared/quo.rb @@ -29,6 +29,15 @@ describe :bigdecimal_quo, shared: true do @one.send(@method, BigDecimal('2E-5555'), *@object).should == BigDecimal('0.5E5555') end + describe "with Object" do + it "tries to coerce the other operand to self" do + skip if @method == :div + object = mock("Object") + object.should_receive(:coerce).with(@one).and_return([@one, @two]) + @one.send(@method, object, *@object).should == BigDecimal("0.5") + end + end + it "returns 0 if divided by Infinity" do @zero.send(@method, @infinity, *@object).should == 0 @frac_2.send(@method, @infinity, *@object).should == 0 @@ -41,8 +50,8 @@ describe :bigdecimal_quo, shared: true do end it "returns NaN if Infinity / ((+|-) Infinity)" do - @infinity.send(@method, @infinity_minus, *@object).nan?.should == true - @infinity_minus.send(@method, @infinity, *@object).nan?.should == true + @infinity.send(@method, @infinity_minus, *@object).should.nan? + @infinity_minus.send(@method, @infinity, *@object).should.nan? end it "returns (+|-) Infinity if divided by zero" do @@ -52,8 +61,8 @@ describe :bigdecimal_quo, shared: true do end it "returns NaN if zero is divided by zero" do - @zero.send(@method, @zero, *@object).nan?.should == true - @zero_minus.send(@method, @zero_plus, *@object).nan?.should == true - @zero_plus.send(@method, @zero_minus, *@object).nan?.should == true + @zero.send(@method, @zero, *@object).should.nan? + @zero_minus.send(@method, @zero_plus, *@object).should.nan? + @zero_plus.send(@method, @zero_minus, *@object).should.nan? end end diff --git a/spec/ruby/library/bigdecimal/shared/to_int.rb b/spec/ruby/library/bigdecimal/shared/to_int.rb index 729a25f511..3c9f3b4f97 100644 --- a/spec/ruby/library/bigdecimal/shared/to_int.rb +++ b/spec/ruby/library/bigdecimal/shared/to_int.rb @@ -1,12 +1,12 @@ require 'bigdecimal' -describe :bigdecimal_to_int , shared: true do +describe :bigdecimal_to_int, shared: true do it "raises FloatDomainError if BigDecimal is infinity or NaN" do - lambda { BigDecimal("Infinity").send(@method) }.should raise_error(FloatDomainError) - lambda { BigDecimal("NaN").send(@method) }.should raise_error(FloatDomainError) + -> { BigDecimal("Infinity").send(@method) }.should.raise(FloatDomainError) + -> { BigDecimal("NaN").send(@method) }.should.raise(FloatDomainError) end - it "returns Integer or Bignum otherwise" do + it "returns Integer otherwise" do BigDecimal("3E-20001").send(@method).should == 0 BigDecimal("2E4000").send(@method).should == 2 * 10 ** 4000 BigDecimal("2").send(@method).should == 2 diff --git a/spec/ruby/library/bigdecimal/sign_spec.rb b/spec/ruby/library/bigdecimal/sign_spec.rb index 85aa74741c..ae2c28e9fd 100644 --- a/spec/ruby/library/bigdecimal/sign_spec.rb +++ b/spec/ruby/library/bigdecimal/sign_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'bigdecimal' describe "BigDecimal#sign" do @@ -44,4 +44,3 @@ describe "BigDecimal#sign" do end end - diff --git a/spec/ruby/library/bigdecimal/split_spec.rb b/spec/ruby/library/bigdecimal/split_spec.rb index e2ba955a3b..53b1f649d9 100644 --- a/spec/ruby/library/bigdecimal/split_spec.rb +++ b/spec/ruby/library/bigdecimal/split_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'bigdecimal' describe "BigDecimal#split" do @@ -58,16 +58,16 @@ describe "BigDecimal#split" do end it "third value: the base (currently always ten)" do - @arr[2].should == 10 - @arr_neg[2].should == 10 - @arr_big[2].should == 10 - @arr_big_neg[2].should == 10 - @huge[2].should == 10 - @infinity.split[2].should == 10 - @nan.split[2].should == 10 - @infinity_neg.split[2].should == 10 - @zero.split[2].should == 10 - @zero_neg.split[2].should == 10 + @arr[2].should == 10 + @arr_neg[2].should == 10 + @arr_big[2].should == 10 + @arr_big_neg[2].should == 10 + @huge[2].should == 10 + @infinity.split[2].should == 10 + @nan.split[2].should == 10 + @infinity_neg.split[2].should == 10 + @zero.split[2].should == 10 + @zero_neg.split[2].should == 10 end it "fourth value: the exponent" do @@ -84,5 +84,3 @@ describe "BigDecimal#split" do end end - - diff --git a/spec/ruby/library/bigdecimal/sqrt_spec.rb b/spec/ruby/library/bigdecimal/sqrt_spec.rb index f677192b33..1f3ef9a8c3 100644 --- a/spec/ruby/library/bigdecimal/sqrt_spec.rb +++ b/spec/ruby/library/bigdecimal/sqrt_spec.rb @@ -1,5 +1,5 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../fixtures/classes', __FILE__) +require_relative '../../spec_helper' +require_relative 'fixtures/classes' require 'bigdecimal' describe "BigDecimal#sqrt" do @@ -36,44 +36,46 @@ describe "BigDecimal#sqrt" do BigDecimal('121').sqrt(5).should be_close(11, 0.00001) end - it "returns square root of 0.9E-99999 with desired precision" do - @frac_2.sqrt(1).to_s.should =~ /\A0\.3E-49999\z/i + platform_is_not c_long_size: 32 do # fails on i686 + it "returns square root of 0.9E-99999 with desired precision" do + @frac_2.sqrt(1).to_s.should =~ /\A0\.3E-49999\z/i + end end it "raises ArgumentError when no argument is given" do - lambda { + -> { @one.sqrt - }.should raise_error(ArgumentError) + }.should.raise(ArgumentError) end it "raises ArgumentError if a negative number is given" do - lambda { + -> { @one.sqrt(-1) - }.should raise_error(ArgumentError) + }.should.raise(ArgumentError) end it "raises ArgumentError if 2 arguments are given" do - lambda { + -> { @one.sqrt(1, 1) - }.should raise_error(ArgumentError) + }.should.raise(ArgumentError) end it "raises TypeError if nil is given" do - lambda { + -> { @one.sqrt(nil) - }.should raise_error(TypeError) + }.should.raise(TypeError) end it "raises TypeError if a string is given" do - lambda { + -> { @one.sqrt("stuff") - }.should raise_error(TypeError) + }.should.raise(TypeError) end it "raises TypeError if a plain Object is given" do - lambda { + -> { @one.sqrt(Object.new) - }.should raise_error(TypeError) + }.should.raise(TypeError) end it "returns 1 if precision is 0 or 1" do @@ -82,25 +84,25 @@ describe "BigDecimal#sqrt" do end it "raises FloatDomainError on negative values" do - lambda { + -> { BigDecimal('-1').sqrt(10) - }.should raise_error(FloatDomainError) + }.should.raise(FloatDomainError) end - it "returns positive infitinity for infinity" do + it "returns positive infinity for infinity" do @infinity.sqrt(1).should == @infinity end it "raises FloatDomainError for negative infinity" do - lambda { + -> { @infinity_minus.sqrt(1) - }.should raise_error(FloatDomainError) + }.should.raise(FloatDomainError) end it "raises FloatDomainError for NaN" do - lambda { + -> { @nan.sqrt(1) - }.should raise_error(FloatDomainError) + }.should.raise(FloatDomainError) end it "returns 0 for 0, +0.0 and -0.0" do diff --git a/spec/ruby/library/bigdecimal/sub_spec.rb b/spec/ruby/library/bigdecimal/sub_spec.rb index 06ad3b79d2..3b62a0c794 100644 --- a/spec/ruby/library/bigdecimal/sub_spec.rb +++ b/spec/ruby/library/bigdecimal/sub_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'bigdecimal' describe "BigDecimal#sub" do @@ -7,12 +7,15 @@ describe "BigDecimal#sub" do @one = BigDecimal("1") @zero = BigDecimal("0") @two = BigDecimal("2") + @three = BigDecimal("3") @nan = BigDecimal("NaN") @infinity = BigDecimal("Infinity") @infinity_minus = BigDecimal("-Infinity") @one_minus = BigDecimal("-1") @frac_1 = BigDecimal("1E-99999") @frac_2 = BigDecimal("0.9E-99999") + @frac_3 = BigDecimal("12345E10") + @frac_4 = BigDecimal("98765E10") end it "returns a - b with given precision" do @@ -32,14 +35,20 @@ describe "BigDecimal#sub" do @frac_1.sub(@frac_1, 1000000).should == @zero end + describe "with Rational" do + it "produces a BigDecimal" do + (@three - Rational(500, 2)).should == BigDecimal('-0.247e3') + end + end + it "returns NaN if NaN is involved" do - @one.sub(@nan, 1).nan?.should == true - @nan.sub(@one, 1).nan?.should == true + @one.sub(@nan, 1).should.nan? + @nan.sub(@one, 1).should.nan? end it "returns NaN if both values are infinite with the same signs" do - @infinity.sub(@infinity, 1).nan?.should == true - @infinity_minus.sub(@infinity_minus, 1).nan?.should == true + @infinity.sub(@infinity, 1).should.nan? + @infinity_minus.sub(@infinity_minus, 1).should.nan? end it "returns Infinity or -Infinity if these are involved" do diff --git a/spec/ruby/library/bigdecimal/to_d_spec.rb b/spec/ruby/library/bigdecimal/to_d_spec.rb new file mode 100644 index 0000000000..50aea99bf7 --- /dev/null +++ b/spec/ruby/library/bigdecimal/to_d_spec.rb @@ -0,0 +1,10 @@ +require_relative '../../spec_helper' +require 'bigdecimal' +require 'bigdecimal/util' + +describe "Float#to_d" do + it "returns appropriate BigDecimal zero for signed zero" do + -0.0.to_d.sign.should == -1 + 0.0.to_d.sign.should == 1 + end +end diff --git a/spec/ruby/library/bigdecimal/to_f_spec.rb b/spec/ruby/library/bigdecimal/to_f_spec.rb index b490fce4d9..f5220d995a 100644 --- a/spec/ruby/library/bigdecimal/to_f_spec.rb +++ b/spec/ruby/library/bigdecimal/to_f_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'bigdecimal' describe "BigDecimal#to_f" do @@ -20,9 +20,9 @@ describe "BigDecimal#to_f" do end it "returns number of type float" do - BigDecimal("3.14159").to_f.should be_kind_of(Float) - @vals.each { |val| val.to_f.should be_kind_of(Float) } - @spec_vals.each { |val| val.to_f.should be_kind_of(Float) } + BigDecimal("3.14159").to_f.should.is_a?(Float) + @vals.each { |val| val.to_f.should.is_a?(Float) } + @spec_vals.each { |val| val.to_f.should.is_a?(Float) } end it "rounds correctly to Float precision" do @@ -41,7 +41,7 @@ describe "BigDecimal#to_f" do @zero.to_f.should == 0 @zero.to_f.to_s.should == "0.0" - @nan.to_f.nan?.should == true + @nan.to_f.should.nan? @infinity.to_f.infinite?.should == 1 @infinity_minus.to_f.infinite?.should == -1 @@ -52,4 +52,3 @@ describe "BigDecimal#to_f" do @zero_neg.to_f.to_s.should == "-0.0" end end - diff --git a/spec/ruby/library/bigdecimal/to_i_spec.rb b/spec/ruby/library/bigdecimal/to_i_spec.rb index 8db69003c5..e5e65c562e 100644 --- a/spec/ruby/library/bigdecimal/to_i_spec.rb +++ b/spec/ruby/library/bigdecimal/to_i_spec.rb @@ -1,7 +1,7 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../shared/to_int', __FILE__) +require_relative '../../spec_helper' +require_relative 'shared/to_int' require 'bigdecimal' describe "BigDecimal#to_i" do - it_behaves_like(:bigdecimal_to_int, :to_i) + it_behaves_like :bigdecimal_to_int, :to_i end diff --git a/spec/ruby/library/bigdecimal/to_int_spec.rb b/spec/ruby/library/bigdecimal/to_int_spec.rb index 56a60d4a03..4df6749845 100644 --- a/spec/ruby/library/bigdecimal/to_int_spec.rb +++ b/spec/ruby/library/bigdecimal/to_int_spec.rb @@ -1,8 +1,8 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../shared/to_int', __FILE__) +require_relative '../../spec_helper' +require_relative 'shared/to_int' require 'bigdecimal' describe "BigDecimal#to_int" do - it_behaves_like(:bigdecimal_to_int, :to_int) + it_behaves_like :bigdecimal_to_int, :to_int end diff --git a/spec/ruby/library/bigdecimal/to_r_spec.rb b/spec/ruby/library/bigdecimal/to_r_spec.rb index 6f6e5a1bea..a112c002ef 100644 --- a/spec/ruby/library/bigdecimal/to_r_spec.rb +++ b/spec/ruby/library/bigdecimal/to_r_spec.rb @@ -1,16 +1,28 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'bigdecimal' describe "BigDecimal#to_r" do it "returns a Rational" do - BigDecimal("3.14159").to_r.should be_kind_of(Rational) + BigDecimal("3.14159").to_r.should.is_a?(Rational) end it "returns a Rational with bignum values" do - r = BigDecimal.new("3.141592653589793238462643").to_r - r.numerator.should eql(3141592653589793238462643) - r.denominator.should eql(1000000000000000000000000) + r = BigDecimal("3.141592653589793238462643").to_r + r.numerator.should.eql?(3141592653589793238462643) + r.denominator.should.eql?(1000000000000000000000000) + end + + it "returns a Rational from a BigDecimal with an exponent" do + r = BigDecimal("1E2").to_r + r.numerator.should.eql?(100) + r.denominator.should.eql?(1) + end + + it "returns a Rational from a negative BigDecimal with an exponent" do + r = BigDecimal("-1E2").to_r + r.numerator.should.eql?(-100) + r.denominator.should.eql?(1) end end diff --git a/spec/ruby/library/bigdecimal/to_s_spec.rb b/spec/ruby/library/bigdecimal/to_s_spec.rb index b97311c800..5ec54765b5 100644 --- a/spec/ruby/library/bigdecimal/to_s_spec.rb +++ b/spec/ruby/library/bigdecimal/to_s_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'bigdecimal' describe "BigDecimal#to_s" do @@ -8,6 +8,11 @@ describe "BigDecimal#to_s" do @bigneg_str = "-3.1415926535897932384626433832795028841971693993" @bigdec = BigDecimal(@bigdec_str) @bigneg = BigDecimal(@bigneg_str) + @internal = Encoding.default_internal + end + + after :each do + Encoding.default_internal = @internal end it "return type is of class String" do @@ -15,12 +20,18 @@ describe "BigDecimal#to_s" do @bigneg.to_s.kind_of?(String).should == true end - it "the default format looks like 0.xxxxEnn" do - @bigdec.to_s.should =~ /^0\.[0-9]*E[0-9]*$/i + it "the default format looks like 0.xxxxenn" do + @bigdec.to_s.should =~ /^0\.[0-9]*e[0-9]*$/ + end + + it "does not add an exponent for zero values" do + BigDecimal("0").to_s.should == "0.0" + BigDecimal("+0").to_s.should == "0.0" + BigDecimal("-0").to_s.should == "-0.0" end it "takes an optional argument" do - lambda {@bigdec.to_s("F")}.should_not raise_error() + -> {@bigdec.to_s("F")}.should_not.raise() end it "starts with + if + is supplied and value is positive" do @@ -28,17 +39,21 @@ describe "BigDecimal#to_s" do @bigneg.to_s("+").should_not =~ /^\+.*/ end - it "inserts a space every n chars, if integer n is supplied" do + it "inserts a space every n chars to fraction part, if integer n is supplied" do re =\ - /\A0\.314 159 265 358 979 323 846 264 338 327 950 288 419 716 939 937E1\z/i + /\A0\.314 159 265 358 979 323 846 264 338 327 950 288 419 716 939 937E1\z/i @bigdec.to_s(3).should =~ re str1 = '-123.45678 90123 45678 9' - BigDecimal.new("-123.45678901234567890").to_s('5F').should == str1 + BigDecimal("-123.45678901234567890").to_s('5F').should == str1 # trailing zeroes removed - BigDecimal.new("1.00000000000").to_s('1F').should == "1.0" + BigDecimal("1.00000000000").to_s('1F').should == "1.0" # 0 is treated as no spaces - BigDecimal.new("1.2345").to_s('0F').should == "1.2345" + BigDecimal("1.2345").to_s('0F').should == "1.2345" + end + + it "inserts a space every n chars to integer part, if integer n is supplied" do + BigDecimal('1000010').to_s('5F').should == "10 00010.0" end it "can return a leading space for values > 0" do @@ -47,15 +62,15 @@ describe "BigDecimal#to_s" do end it "removes trailing spaces in floating point notation" do - BigDecimal.new('-123.45678901234567890').to_s('F').should == "-123.4567890123456789" - BigDecimal.new('1.2500').to_s('F').should == "1.25" - BigDecimal.new('0000.00000').to_s('F').should == "0.0" - BigDecimal.new('-00.000010000').to_s('F').should == "-0.00001" - BigDecimal.new("5.00000E-2").to_s("F").should == "0.05" - - BigDecimal.new("500000").to_s("F").should == "500000.0" - BigDecimal.new("5E2").to_s("F").should == "500.0" - BigDecimal.new("-5E100").to_s("F").should == "-5" + "0" * 100 + ".0" + BigDecimal('-123.45678901234567890').to_s('F').should == "-123.4567890123456789" + BigDecimal('1.2500').to_s('F').should == "1.25" + BigDecimal('0000.00000').to_s('F').should == "0.0" + BigDecimal('-00.000010000').to_s('F').should == "-0.00001" + BigDecimal("5.00000E-2").to_s("F").should == "0.05" + + BigDecimal("500000").to_s("F").should == "500000.0" + BigDecimal("5E2").to_s("F").should == "500.0" + BigDecimal("-5E100").to_s("F").should == "-5" + "0" * 100 + ".0" end it "can use engineering notation" do @@ -63,11 +78,21 @@ describe "BigDecimal#to_s" do end it "can use conventional floating point notation" do - @bigdec.to_s("F").should == @bigdec_str - @bigneg.to_s("F").should == @bigneg_str - str2 = "+123.45678901 23456789" - BigDecimal.new('123.45678901234567890').to_s('+8F').should == str2 + %w[f F].each do |format_char| + @bigdec.to_s(format_char).should == @bigdec_str + @bigneg.to_s(format_char).should == @bigneg_str + str2 = "+123.45678901 23456789" + BigDecimal('123.45678901234567890').to_s("+8#{format_char}").should == str2 + end end -end + it "returns a String in US-ASCII encoding when Encoding.default_internal is nil" do + Encoding.default_internal = nil + BigDecimal('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 + BigDecimal('1.23').to_s.encoding.should.equal?(Encoding::US_ASCII) + end +end diff --git a/spec/ruby/library/bigdecimal/truncate_spec.rb b/spec/ruby/library/bigdecimal/truncate_spec.rb index 41f0afc8fa..cedc662aeb 100644 --- a/spec/ruby/library/bigdecimal/truncate_spec.rb +++ b/spec/ruby/library/bigdecimal/truncate_spec.rb @@ -1,14 +1,14 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'bigdecimal' describe "BigDecimal#truncate" do before :each do - @arr = ['3.14159', '8.7', "0.314159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593014782083152134043E1"] - @big = BigDecimal("123456.789") - @nan = BigDecimal('NaN') - @infinity = BigDecimal('Infinity') - @infinity_negative = BigDecimal('-Infinity') + @arr = ['3.14159', '8.7', "0.314159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593014782083152134043E1"] + @big = BigDecimal("123456.789") + @nan = BigDecimal('NaN') + @infinity = BigDecimal('Infinity') + @infinity_negative = BigDecimal('-Infinity') end it "returns value of type Integer." do @@ -58,9 +58,9 @@ describe "BigDecimal#truncate" do end it "returns NaN if self is NaN" do - @nan.truncate(-1).nan?.should == true - @nan.truncate(+1).nan?.should == true - @nan.truncate(0).nan?.should == true + @nan.truncate(-1).should.nan? + @nan.truncate(+1).should.nan? + @nan.truncate(0).should.nan? end it "returns Infinity if self is infinite" do @@ -74,8 +74,8 @@ describe "BigDecimal#truncate" do end it "returns the same value if self is special value" do - lambda { @nan.truncate }.should raise_error(FloatDomainError) - lambda { @infinity.truncate }.should raise_error(FloatDomainError) - lambda { @infinity_negative.truncate }.should raise_error(FloatDomainError) + -> { @nan.truncate }.should.raise(FloatDomainError) + -> { @infinity.truncate }.should.raise(FloatDomainError) + -> { @infinity_negative.truncate }.should.raise(FloatDomainError) end end diff --git a/spec/ruby/library/bigdecimal/uminus_spec.rb b/spec/ruby/library/bigdecimal/uminus_spec.rb index 901f9f59ce..c780cdfac5 100644 --- a/spec/ruby/library/bigdecimal/uminus_spec.rb +++ b/spec/ruby/library/bigdecimal/uminus_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'bigdecimal' describe "BigDecimal#-@" do @@ -53,6 +53,6 @@ describe "BigDecimal#-@" do @zero_neg.send(:-@).should == @zero @zero_neg.send(:-@).sign.should == 1 - @nan.send(:-@).nan?.should == true + @nan.send(:-@).should.nan? end end diff --git a/spec/ruby/library/bigdecimal/uplus_spec.rb b/spec/ruby/library/bigdecimal/uplus_spec.rb index 00aadc723c..77483046b7 100644 --- a/spec/ruby/library/bigdecimal/uplus_spec.rb +++ b/spec/ruby/library/bigdecimal/uplus_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'bigdecimal' describe "BigDecimal#+@" do @@ -15,6 +15,3 @@ describe "BigDecimal#+@" do fifth.send(:+@).should == fifth end end - - - diff --git a/spec/ruby/library/bigdecimal/util_spec.rb b/spec/ruby/library/bigdecimal/util_spec.rb new file mode 100644 index 0000000000..69663d4bd2 --- /dev/null +++ b/spec/ruby/library/bigdecimal/util_spec.rb @@ -0,0 +1,40 @@ +require_relative '../../spec_helper' +require 'bigdecimal' +require 'bigdecimal/util' + +describe "BigDecimal's util method definitions" do + describe "#to_d" do + it "should define #to_d on Integer" do + 42.to_d.should == BigDecimal(42) + end + + it "should define #to_d on Float" do + 0.5.to_d.should == BigDecimal(0.5, Float::DIG) + 1.234.to_d(2).should == BigDecimal(1.234, 2) + end + + it "should define #to_d on String" do + "0.5".to_d.should == BigDecimal(0.5, Float::DIG) + "45.67 degrees".to_d.should == BigDecimal(45.67, Float::DIG) + end + + it "should define #to_d on BigDecimal" do + bd = BigDecimal("3.14") + bd.to_d.should.equal?(bd) + end + + it "should define #to_d on Rational" do + Rational(22, 7).to_d(3).should == BigDecimal(3.14, 3) + end + + it "should define #to_d on nil" do + nil.to_d.should == BigDecimal(0) + end + end + + describe "#to_digits" do + it "should define #to_digits on BigDecimal" do + BigDecimal("3.14").to_digits.should == "3.14" + end + end +end diff --git a/spec/ruby/library/bigdecimal/ver_spec.rb b/spec/ruby/library/bigdecimal/ver_spec.rb deleted file mode 100644 index 61b073d8f1..0000000000 --- a/spec/ruby/library/bigdecimal/ver_spec.rb +++ /dev/null @@ -1,11 +0,0 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require 'bigdecimal' - -describe "BigDecimal.ver" do - - it "returns the Version number" do - lambda {BigDecimal.ver }.should_not raise_error() - BigDecimal.ver.should_not == nil - end - -end diff --git a/spec/ruby/library/bigdecimal/zero_spec.rb b/spec/ruby/library/bigdecimal/zero_spec.rb index 7dbb0fa7f0..2563210939 100644 --- a/spec/ruby/library/bigdecimal/zero_spec.rb +++ b/spec/ruby/library/bigdecimal/zero_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'bigdecimal' describe "BigDecimal#zero?" do @@ -6,23 +6,22 @@ describe "BigDecimal#zero?" do it "returns true if self does equal zero" do really_small_zero = BigDecimal("0E-200000000") really_big_zero = BigDecimal("0E200000000000") - really_small_zero.zero?.should == true - really_big_zero.zero?.should == true - BigDecimal("0.000000000000000000000000").zero?.should == true - BigDecimal("0").zero?.should == true - BigDecimal("0E0").zero?.should == true - BigDecimal("+0").zero?.should == true - BigDecimal("-0").zero?.should == true + really_small_zero.should.zero? + really_big_zero.should.zero? + BigDecimal("0.000000000000000000000000").should.zero? + BigDecimal("0").should.zero? + BigDecimal("0E0").should.zero? + BigDecimal("+0").should.zero? + BigDecimal("-0").should.zero? end it "returns false otherwise" do - BigDecimal("0000000001").zero?.should == false - BigDecimal("2E40001").zero?.should == false - BigDecimal("3E-20001").zero?.should == false - BigDecimal("Infinity").zero?.should == false - BigDecimal("-Infinity").zero?.should == false - BigDecimal("NaN").zero?.should == false + BigDecimal("0000000001").should_not.zero? + BigDecimal("2E40001").should_not.zero? + BigDecimal("3E-20001").should_not.zero? + BigDecimal("Infinity").should_not.zero? + BigDecimal("-Infinity").should_not.zero? + BigDecimal("NaN").should_not.zero? end end - |
