summaryrefslogtreecommitdiff
path: root/spec/ruby/library/bigdecimal
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/library/bigdecimal')
-rw-r--r--spec/ruby/library/bigdecimal/BigDecimal_spec.rb239
-rw-r--r--spec/ruby/library/bigdecimal/abs_spec.rb50
-rw-r--r--spec/ruby/library/bigdecimal/add_spec.rb185
-rw-r--r--spec/ruby/library/bigdecimal/case_compare_spec.rb7
-rw-r--r--spec/ruby/library/bigdecimal/ceil_spec.rb104
-rw-r--r--spec/ruby/library/bigdecimal/clone_spec.rb6
-rw-r--r--spec/ruby/library/bigdecimal/coerce_spec.rb26
-rw-r--r--spec/ruby/library/bigdecimal/comparison_spec.rb81
-rw-r--r--spec/ruby/library/bigdecimal/constants_spec.rb70
-rw-r--r--spec/ruby/library/bigdecimal/core_spec.rb62
-rw-r--r--spec/ruby/library/bigdecimal/div_spec.rb110
-rw-r--r--spec/ruby/library/bigdecimal/divide_spec.rb17
-rw-r--r--spec/ruby/library/bigdecimal/divmod_spec.rb212
-rw-r--r--spec/ruby/library/bigdecimal/double_fig_spec.rb9
-rw-r--r--spec/ruby/library/bigdecimal/dup_spec.rb6
-rw-r--r--spec/ruby/library/bigdecimal/eql_spec.rb6
-rw-r--r--spec/ruby/library/bigdecimal/equal_value_spec.rb7
-rw-r--r--spec/ruby/library/bigdecimal/exponent_spec.rb27
-rw-r--r--spec/ruby/library/bigdecimal/finite_spec.rb34
-rw-r--r--spec/ruby/library/bigdecimal/fix_spec.rb57
-rw-r--r--spec/ruby/library/bigdecimal/fixtures/classes.rb17
-rw-r--r--spec/ruby/library/bigdecimal/floor_spec.rb100
-rw-r--r--spec/ruby/library/bigdecimal/frac_spec.rb48
-rw-r--r--spec/ruby/library/bigdecimal/gt_spec.rb96
-rw-r--r--spec/ruby/library/bigdecimal/gte_spec.rb100
-rw-r--r--spec/ruby/library/bigdecimal/hash_spec.rb30
-rw-r--r--spec/ruby/library/bigdecimal/infinite_spec.rb32
-rw-r--r--spec/ruby/library/bigdecimal/inspect_spec.rb30
-rw-r--r--spec/ruby/library/bigdecimal/limit_spec.rb55
-rw-r--r--spec/ruby/library/bigdecimal/lt_spec.rb94
-rw-r--r--spec/ruby/library/bigdecimal/lte_spec.rb100
-rw-r--r--spec/ruby/library/bigdecimal/minus_spec.rb66
-rw-r--r--spec/ruby/library/bigdecimal/mode_spec.rb36
-rw-r--r--spec/ruby/library/bigdecimal/modulo_spec.rb12
-rw-r--r--spec/ruby/library/bigdecimal/mult_spec.rb24
-rw-r--r--spec/ruby/library/bigdecimal/multiply_spec.rb41
-rw-r--r--spec/ruby/library/bigdecimal/nan_spec.rb23
-rw-r--r--spec/ruby/library/bigdecimal/nonzero_spec.rb29
-rw-r--r--spec/ruby/library/bigdecimal/plus_spec.rb54
-rw-r--r--spec/ruby/library/bigdecimal/power_spec.rb6
-rw-r--r--spec/ruby/library/bigdecimal/quo_spec.rb12
-rw-r--r--spec/ruby/library/bigdecimal/remainder_spec.rb77
-rw-r--r--spec/ruby/library/bigdecimal/round_spec.rb234
-rw-r--r--spec/ruby/library/bigdecimal/shared/clone.rb13
-rw-r--r--spec/ruby/library/bigdecimal/shared/eql.rb61
-rw-r--r--spec/ruby/library/bigdecimal/shared/modulo.rb131
-rw-r--r--spec/ruby/library/bigdecimal/shared/mult.rb97
-rw-r--r--spec/ruby/library/bigdecimal/shared/power.rb72
-rw-r--r--spec/ruby/library/bigdecimal/shared/quo.rb68
-rw-r--r--spec/ruby/library/bigdecimal/shared/to_int.rb16
-rw-r--r--spec/ruby/library/bigdecimal/sign_spec.rb46
-rw-r--r--spec/ruby/library/bigdecimal/split_spec.rb86
-rw-r--r--spec/ruby/library/bigdecimal/sqrt_spec.rb114
-rw-r--r--spec/ruby/library/bigdecimal/sub_spec.rb62
-rw-r--r--spec/ruby/library/bigdecimal/to_d_spec.rb10
-rw-r--r--spec/ruby/library/bigdecimal/to_f_spec.rb54
-rw-r--r--spec/ruby/library/bigdecimal/to_i_spec.rb7
-rw-r--r--spec/ruby/library/bigdecimal/to_int_spec.rb8
-rw-r--r--spec/ruby/library/bigdecimal/to_r_spec.rb28
-rw-r--r--spec/ruby/library/bigdecimal/to_s_spec.rb98
-rw-r--r--spec/ruby/library/bigdecimal/truncate_spec.rb81
-rw-r--r--spec/ruby/library/bigdecimal/uminus_spec.rb58
-rw-r--r--spec/ruby/library/bigdecimal/uplus_spec.rb17
-rw-r--r--spec/ruby/library/bigdecimal/util_spec.rb40
-rw-r--r--spec/ruby/library/bigdecimal/zero_spec.rb27
65 files changed, 3825 insertions, 0 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
new file mode 100644
index 0000000000..95dc45a905
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/abs_spec.rb
@@ -0,0 +1,50 @@
+require_relative '../../spec_helper'
+require 'bigdecimal'
+
+describe "BigDecimal#abs" do
+ before :each do
+ @one = BigDecimal("1")
+ @zero = BigDecimal("0")
+ @zero_pos = BigDecimal("+0")
+ @zero_neg = BigDecimal("-0")
+ @two = BigDecimal("2")
+ @three = BigDecimal("3")
+ @mixed = BigDecimal("1.23456789")
+ @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")
+ end
+
+ it "returns the absolute value" do
+ pos_int = BigDecimal("2E5555")
+ neg_int = BigDecimal("-2E5555")
+ pos_frac = BigDecimal("2E-9999")
+ neg_frac = BigDecimal("-2E-9999")
+
+ pos_int.abs.should == pos_int
+ neg_int.abs.should == pos_int
+ pos_frac.abs.should == pos_frac
+ neg_frac.abs.should == pos_frac
+ @one.abs.should == 1
+ @two.abs.should == 2
+ @three.abs.should == 3
+ @mixed.abs.should == @mixed
+ @one_minus.abs.should == @one
+ end
+
+ it "properly handles special values" do
+ @infinity.abs.should == @infinity
+ @infinity_minus.abs.should == @infinity
+ @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
+ @zero_pos.abs.sign.should == BigDecimal::SIGN_POSITIVE_ZERO
+ @zero_neg.abs.should == 0
+ @zero_neg.abs.sign.should == BigDecimal::SIGN_POSITIVE_ZERO
+ end
+
+end
diff --git a/spec/ruby/library/bigdecimal/add_spec.rb b/spec/ruby/library/bigdecimal/add_spec.rb
new file mode 100644
index 0000000000..a4237298f4
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/add_spec.rb
@@ -0,0 +1,185 @@
+require_relative '../../spec_helper'
+require_relative 'fixtures/classes'
+
+require 'bigdecimal'
+
+describe "BigDecimal#add" do
+
+ before :each do
+ @one = BigDecimal("1")
+ @zero = BigDecimal("0")
+ @two = BigDecimal("2")
+ @three = BigDecimal("3")
+ @ten = BigDecimal("10")
+ @eleven = BigDecimal("11")
+ @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")
+ @dot_ones = BigDecimal("0.1111111111")
+ end
+
+ it "returns a + b with given precision" do
+ # 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
+ @ten.add(@one, 2).should == @eleven
+ @zero.add(@one, 1).should == @one
+ @frac_2.add(@frac_1, 10000).should == BigDecimal("1.9E-99999")
+ @frac_1.add(@frac_1, 10000).should == BigDecimal("2E-99999")
+ @frac_3.add(@frac_4, 0).should == BigDecimal("0.11111E16")
+ @frac_3.add(@frac_4, 1).should == BigDecimal("0.1E16")
+ @frac_3.add(@frac_4, 2).should == BigDecimal("0.11E16")
+ @frac_3.add(@frac_4, 3).should == BigDecimal("0.111E16")
+ @frac_3.add(@frac_4, 4).should == BigDecimal("0.1111E16")
+ @frac_3.add(@frac_4, 5).should == BigDecimal("0.11111E16")
+ @frac_3.add(@frac_4, 6).should == BigDecimal("0.11111E16")
+ end
+
+ it "returns a + [Fixnum value] with given precision" do
+ (1..10).each {|precision|
+ @dot_ones.add(0, precision).should == BigDecimal("0." + "1" * precision)
+ }
+ BigDecimal("0.88").add(0, 1).should == BigDecimal("0.9")
+ end
+
+ it "returns a + [Bignum value] with given precision" do
+ bignum = 10000000000000000000
+ (1..20).each {|precision|
+ @dot_ones.add(bignum, precision).should == BigDecimal("0.1E20")
+ }
+ (21..30).each {|precision|
+ @dot_ones.add(bignum, precision).should == BigDecimal(
+ "0.10000000000000000000" + "1" * (precision - 20) + "E20")
+ }
+ end
+
+# TODO:
+# 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)
+#
+# it "returns a + [Float value] with given precision" do
+# (1..10).each {|precision|
+# @dot_ones.add(0.0, precision).should == BigDecimal("0." + "1" * precision)
+# }
+#
+# 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')
+ end
+
+ BigDecimalSpecs.with_limit(2) do
+ BigDecimal('0.888').add(@zero, 1).should == BigDecimal('0.9')
+ end
+ end
+
+ it "uses the current rounding mode if rounding is needed" do
+ BigDecimalSpecs.with_rounding(BigDecimal::ROUND_UP) do
+ BigDecimal('0.111').add(@zero, 1).should == BigDecimal('0.2')
+ BigDecimal('-0.111').add(@zero, 1).should == BigDecimal('-0.2')
+ end
+ BigDecimalSpecs.with_rounding(BigDecimal::ROUND_DOWN) do
+ BigDecimal('0.999').add(@zero, 1).should == BigDecimal('0.9')
+ BigDecimal('-0.999').add(@zero, 1).should == BigDecimal('-0.9')
+ end
+ BigDecimalSpecs.with_rounding(BigDecimal::ROUND_HALF_UP) do
+ BigDecimal('0.85').add(@zero, 1).should == BigDecimal('0.9')
+ BigDecimal('-0.85').add(@zero, 1).should == BigDecimal('-0.9')
+ end
+ BigDecimalSpecs.with_rounding(BigDecimal::ROUND_HALF_DOWN) do
+ BigDecimal('0.85').add(@zero, 1).should == BigDecimal('0.8')
+ BigDecimal('-0.85').add(@zero, 1).should == BigDecimal('-0.8')
+ end
+ BigDecimalSpecs.with_rounding(BigDecimal::ROUND_HALF_EVEN) do
+ BigDecimal('0.75').add(@zero, 1).should == BigDecimal('0.8')
+ BigDecimal('0.85').add(@zero, 1).should == BigDecimal('0.8')
+ BigDecimal('-0.75').add(@zero, 1).should == BigDecimal('-0.8')
+ BigDecimal('-0.85').add(@zero, 1).should == BigDecimal('-0.8')
+ end
+ BigDecimalSpecs.with_rounding(BigDecimal::ROUND_CEILING) do
+ BigDecimal('0.85').add(@zero, 1).should == BigDecimal('0.9')
+ BigDecimal('-0.85').add(@zero, 1).should == BigDecimal('-0.8')
+ end
+ BigDecimalSpecs.with_rounding(BigDecimal::ROUND_FLOOR) do
+ BigDecimal('0.85').add(@zero, 1).should == BigDecimal('0.8')
+ BigDecimal('-0.85').add(@zero, 1).should == BigDecimal('-0.9')
+ end
+ end
+
+ it "uses the default ROUND_HALF_UP rounding if it wasn't explicitly changed" do
+ BigDecimal('0.85').add(@zero, 1).should == BigDecimal('0.9')
+ BigDecimal('-0.85').add(@zero, 1).should == BigDecimal('-0.9')
+ end
+
+ it "returns NaN if NaN is involved" do
+ @one.add(@nan, 10000).should.nan?
+ @nan.add(@one, 1).should.nan?
+ end
+
+ it "returns Infinity or -Infinity if these are involved" do
+ @zero.add(@infinity, 1).should == @infinity
+ @frac_2.add(@infinity, 1).should == @infinity
+ @one_minus.add(@infinity, 1).should == @infinity
+ @two.add(@infinity, 1).should == @infinity
+
+ @zero.add(@infinity_minus, 1).should == @infinity_minus
+ @frac_2.add(@infinity_minus, 1).should == @infinity_minus
+ @one_minus.add(@infinity_minus, 1).should == @infinity_minus
+ @two.add(@infinity_minus, 1).should == @infinity_minus
+
+ @infinity.add(@zero, 1).should == @infinity
+ @infinity.add(@frac_2, 1).should == @infinity
+ @infinity.add(@one_minus, 1).should == @infinity
+ @infinity.add(@two, 1).should == @infinity
+
+ @infinity_minus.add(@zero, 1).should == @infinity_minus
+ @infinity_minus.add(@frac_2, 1).should == @infinity_minus
+ @infinity_minus.add(@one_minus, 1).should == @infinity_minus
+ @infinity_minus.add(@two, 1).should == @infinity_minus
+
+ @infinity.add(@infinity, 10000).should == @infinity
+ @infinity_minus.add(@infinity_minus, 10000).should == @infinity_minus
+ end
+
+ it "returns NaN if Infinity + (- Infinity)" do
+ @infinity.add(@infinity_minus, 10000).should.nan?
+ @infinity_minus.add(@infinity, 10000).should.nan?
+ end
+
+ it "raises TypeError when adds nil" do
+ -> {
+ @one.add(nil, 10)
+ }.should.raise(TypeError)
+ -> {
+ @one.add(nil, 0)
+ }.should.raise(TypeError)
+ end
+
+ it "raises TypeError when precision parameter is nil" do
+ -> {
+ @one.add(@one, nil)
+ }.should.raise(TypeError)
+ end
+
+ it "raises ArgumentError when precision parameter is negative" do
+ -> {
+ @one.add(@one, -10)
+ }.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
new file mode 100644
index 0000000000..fac6714356
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/case_compare_spec.rb
@@ -0,0 +1,7 @@
+require_relative '../../spec_helper'
+require_relative 'shared/eql'
+
+
+describe "BigDecimal#===" do
+ it_behaves_like :bigdecimal_eql, :===
+end
diff --git a/spec/ruby/library/bigdecimal/ceil_spec.rb b/spec/ruby/library/bigdecimal/ceil_spec.rb
new file mode 100644
index 0000000000..3d94b8e578
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/ceil_spec.rb
@@ -0,0 +1,104 @@
+require_relative '../../spec_helper'
+require 'bigdecimal'
+
+describe "BigDecimal#ceil" do
+ before :each do
+ @zero = BigDecimal("0")
+ @one = BigDecimal("1")
+ @three = BigDecimal("3")
+ @four = BigDecimal("4")
+ @mixed = BigDecimal("1.23456789")
+ @mixed_big = BigDecimal("1.23456789E100")
+ @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 an Integer, if n is unspecified" do
+ @mixed.ceil.kind_of?(Integer).should == true
+ end
+
+ it "returns a BigDecimal, if n is specified" do
+ @pos_int.ceil(2).kind_of?(BigDecimal).should == true
+ end
+
+ it "returns the smallest integer greater or equal to self, if n is unspecified" do
+ @pos_int.ceil.should == @pos_int
+ @neg_int.ceil.should == @neg_int
+ @pos_frac.ceil.should == BigDecimal("1")
+ @neg_frac.ceil.should == @zero
+ @zero.ceil.should == 0
+ @zero_pos.ceil.should == @zero_pos
+ @zero_neg.ceil.should == @zero_neg
+
+
+ BigDecimal('2.3').ceil.should == 3
+ BigDecimal('2.5').ceil.should == 3
+ BigDecimal('2.9999').ceil.should == 3
+ BigDecimal('-2.3').ceil.should == -2
+ BigDecimal('-2.5').ceil.should == -2
+ BigDecimal('-2.9999').ceil.should == -2
+ end
+
+ it "raise exception, if self is special value" do
+ -> { @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
+ @mixed.ceil(1).should == BigDecimal("1.3")
+ @mixed.ceil(5).should == BigDecimal("1.23457")
+
+ BigDecimal("-0.03").ceil(1).should == BigDecimal("0")
+ BigDecimal("0.03").ceil(1).should == BigDecimal("0.1")
+
+ BigDecimal("23.45").ceil(0).should == BigDecimal('24')
+ BigDecimal("23.45").ceil(1).should == BigDecimal('23.5')
+ BigDecimal("23.45").ceil(2).should == BigDecimal('23.45')
+
+ BigDecimal("-23.45").ceil(0).should == BigDecimal('-23')
+ BigDecimal("-23.45").ceil(1).should == BigDecimal('-23.4')
+ BigDecimal("-23.45").ceil(2).should == BigDecimal('-23.45')
+
+ BigDecimal("2E-10").ceil(0).should == @one
+ BigDecimal("2E-10").ceil(9).should == BigDecimal('1E-9')
+ BigDecimal("2E-10").ceil(10).should == BigDecimal('2E-10')
+ BigDecimal("2E-10").ceil(11).should == BigDecimal('2E-10')
+
+ (1..10).each do |n|
+ # 0.4, 0.34, 0.334, etc.
+ (@one.div(@three,20)).ceil(n).should == BigDecimal("0.#{'3'*(n-1)}4")
+ # 1.4, 1.34, 1.334, etc.
+ (@four.div(@three,20)).ceil(n).should == BigDecimal("1.#{'3'*(n-1)}4")
+ (BigDecimal('31').div(@three,20)).ceil(n).should == BigDecimal("10.#{'3'*(n-1)}4")
+ end
+ (1..10).each do |n|
+ # -0.4, -0.34, -0.334, etc.
+ (-@one.div(@three,20)).ceil(n).should == BigDecimal("-0.#{'3'* n}")
+ end
+ (1..10).each do |n|
+ (@three.div(@one,20)).ceil(n).should == @three
+ end
+ (1..10).each do |n|
+ (-@three.div(@one,20)).ceil(n).should == -@three
+ end
+ end
+
+ it "sets n digits left of the decimal point to 0, if given n < 0" do
+ BigDecimal("13345.234").ceil(-2).should == BigDecimal("13400.0")
+ @mixed_big.ceil(-99).should == BigDecimal("0.13E101")
+ @mixed_big.ceil(-100).should == BigDecimal("0.2E101")
+ @mixed_big.ceil(-95).should == BigDecimal("0.123457E101")
+ BigDecimal("1E10").ceil(-30).should == BigDecimal('1E30')
+ BigDecimal("-1E10").ceil(-30).should == @zero
+ end
+
+end
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
new file mode 100644
index 0000000000..1e5c73f969
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/coerce_spec.rb
@@ -0,0 +1,26 @@
+require_relative '../../spec_helper'
+require 'bigdecimal'
+
+describe "BigDecimal#coerce" do
+
+ it "returns [other, self] both as BigDecimal" do
+ one = BigDecimal("1.0")
+ five_point_28 = BigDecimal("5.28")
+ zero_minus = BigDecimal("-0.0")
+ some_value = 32434234234234234234
+
+ BigDecimal("1.2").coerce(1).should == [one, BigDecimal("1.2")]
+ five_point_28.coerce(1.0).should == [one, BigDecimal("5.28")]
+ one.coerce(one).should == [one, one]
+ one.coerce(2.5).should == [2.5, one]
+ BigDecimal("1").coerce(3.14).should == [3.14, one]
+ a, b = zero_minus.coerce(some_value)
+ a.should == BigDecimal(some_value.to_s)
+ b.should == zero_minus
+ a, b = one.coerce(some_value)
+ a.should == BigDecimal(some_value.to_s)
+ b.to_f.should be_close(1.0, TOLERANCE) # can we take out the to_f once BigDecimal#- is implemented?
+ b.should == one
+ end
+
+end
diff --git a/spec/ruby/library/bigdecimal/comparison_spec.rb b/spec/ruby/library/bigdecimal/comparison_spec.rb
new file mode 100644
index 0000000000..c53187b727
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/comparison_spec.rb
@@ -0,0 +1,81 @@
+require_relative '../../spec_helper'
+require 'bigdecimal'
+
+describe "BigDecimal#<=>" do
+ before :each do
+ @zero = BigDecimal("0")
+ @zero_pos = BigDecimal("+0")
+ @zero_neg = BigDecimal("-0")
+ @mixed = BigDecimal("1.23456789")
+ @mixed_big = BigDecimal("1.23456789E100")
+ @pos_int = BigDecimal("2E5555")
+ @neg_int = BigDecimal("-2E5555")
+ @pos_frac = BigDecimal("2E-9999")
+ @neg_frac = BigDecimal("-2E-9999")
+
+ @int_mock = mock('123')
+ class << @int_mock
+ def coerce(other)
+ return [other, BigDecimal('123')]
+ end
+ def >=(other)
+ BigDecimal('123') >= other
+ end
+ end
+
+ @values = [@mixed, @pos_int, @neg_int, @pos_frac, @neg_frac,
+ -2**32, -2**31, -2**30, -2**16, -2**8, -100, -10, -1,
+ @zero , 1, 2, 10, 2**8, 2**16, 2**32, @int_mock, @zero_pos, @zero_neg]
+
+ @infinity = BigDecimal("Infinity")
+ @infinity_neg = BigDecimal("-Infinity")
+ @nan = BigDecimal("NaN")
+ end
+
+
+ it "returns 0 if a == b" do
+ (@pos_int <=> @pos_int).should == 0
+ (@neg_int <=> @neg_int).should == 0
+ (@pos_frac <=> @pos_frac).should == 0
+ (@neg_frac <=> @neg_frac).should == 0
+ (@zero <=> @zero).should == 0
+ (@infinity <=> @infinity).should == 0
+ (@infinity_neg <=> @infinity_neg).should == 0
+ end
+
+ it "returns 1 if a > b" do
+ (@pos_int <=> @neg_int).should == 1
+ (@pos_frac <=> @neg_frac).should == 1
+ (@pos_frac <=> @zero).should == 1
+ @values.each { |val|
+ (@infinity <=> val).should == 1
+ }
+ end
+
+ it "returns -1 if a < b" do
+ (@zero <=> @pos_frac).should == -1
+ (@neg_int <=> @pos_frac).should == -1
+ (@pos_frac <=> @pos_int).should == -1
+ @values.each { |val|
+ (@infinity_neg <=> val).should == -1
+ }
+ end
+
+ it "returns nil if NaN is involved" do
+ @values += [@infinity, @infinity_neg, @nan]
+ @values << nil
+ @values << Object.new
+ @values.each { |val|
+ (@nan <=> val).should == nil
+ }
+ end
+
+ it "returns nil if the argument is nil" do
+ (@zero <=> nil).should == nil
+ (@infinity <=> nil).should == nil
+ (@infinity_neg <=> nil).should == nil
+ (@mixed <=> nil).should == nil
+ (@pos_int <=> nil).should == nil
+ (@neg_frac <=> nil).should == nil
+ 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
new file mode 100644
index 0000000000..967d8b5221
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/div_spec.rb
@@ -0,0 +1,110 @@
+require_relative '../../spec_helper'
+require_relative 'shared/quo'
+require 'bigdecimal'
+
+describe "BigDecimal#div with precision set to 0" do
+ # TODO: figure out if there is a better way to do these
+ # shared specs rather than sending [0]. See other specs
+ # that share :bigdecimal_quo.
+ it_behaves_like :bigdecimal_quo, :div, [0]
+end
+
+describe "BigDecimal#div" do
+
+ before :each do
+ @one = BigDecimal("1")
+ @zero = BigDecimal("0")
+ @zero_plus = BigDecimal("+0")
+ @zero_minus = 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")
+ end
+
+ it "returns a / b with optional precision" do
+ @two.div(@one).should == @two
+ @one.div(@two).should == @zero
+ # ^^ is this really intended for a class with arbitrary precision?
+ @one.div(@two, 1).should == BigDecimal("0.5")
+ @one.div(@one_minus).should == @one_minus
+ @one_minus.div(@one_minus).should == @one
+ @frac_2.div(@frac_1, 1).should == BigDecimal("0.9")
+ @frac_1.div(@frac_1).should == @one
+
+ res = "0." + "3" * 1000
+ (1..100).each { |idx|
+ @one.div(@three, idx).to_s("F").should == "0." + res[2, idx]
+ }
+ 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
+ -> { @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
+ @zero.div(@infinity).should == 0
+ @frac_2.div(@infinity).should == 0
+ end
+
+ it "returns 0 if divided by Infinity with given precision" do
+ @zero.div(@infinity, 0).should == 0
+ @frac_2.div(@infinity, 1).should == 0
+ @zero.div(@infinity, 100000).should == 0
+ @frac_2.div(@infinity, 100000).should == 0
+ end
+
+ it "raises ZeroDivisionError if divided by zero and no precision given" do
+ -> { @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).should.nan?
+ @zero_minus.div(@zero_plus, 0).should.nan?
+ @zero_plus.div(@zero_minus, 0).should.nan?
+
+ @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
+ -> { @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
+ @infinity_minus.div(@one, 0).should == @infinity_minus
+ @infinity.div(@one, 0).should == @infinity
+ @infinity_minus.div(@one_minus, 0).should == @infinity
+ end
+
+ it "returns NaN if Infinity / ((+|-) Infinity)" do
+ @infinity.div(@infinity_minus, 100000).should.nan?
+ @infinity_minus.div(@infinity, 1).should.nan?
+ end
+
+
+end
diff --git a/spec/ruby/library/bigdecimal/divide_spec.rb b/spec/ruby/library/bigdecimal/divide_spec.rb
new file mode 100644
index 0000000000..c62b23557d
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/divide_spec.rb
@@ -0,0 +1,17 @@
+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
new file mode 100644
index 0000000000..d170c6f845
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/divmod_spec.rb
@@ -0,0 +1,212 @@
+require_relative '../../spec_helper'
+require_relative 'shared/modulo'
+require 'bigdecimal'
+
+module DivmodSpecs
+ def self.check_both_nan(array)
+ array.length.should == 2
+ array[0].should.nan?
+ array[1].should.nan?
+ end
+ def self.check_both_bigdecimal(array)
+ array.length.should == 2
+ array[0].kind_of?(BigDecimal).should == true
+ array[1].kind_of?(BigDecimal).should == true
+ end
+end
+
+# TODO: figure out a way to do the shared specs with helpers instead
+# of spec'ing a method that does not really exist
+describe "BigDecimal#mod_part_of_divmod" do
+ # BigDecimal#divmod[1] behaves exactly like #modulo
+ before :all do
+ class BigDecimal
+ def mod_part_of_divmod(arg)
+ divmod(arg)[1]
+ end
+ end
+ end
+
+ after :all do
+ class BigDecimal
+ undef mod_part_of_divmod
+ end
+ end
+
+ 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("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
+
+describe "BigDecimal#divmod" do
+
+ before :each do
+ @a = BigDecimal("42.00000000000000000001")
+
+ @zero = BigDecimal("0")
+ @zero_pos = BigDecimal("+0")
+ @zero_neg = BigDecimal("-0")
+
+ @one = BigDecimal("1")
+ @mixed = BigDecimal("1.23456789")
+ @pos_int = BigDecimal("2E5555")
+ @neg_int = BigDecimal("-2E5555")
+ @pos_frac = BigDecimal("2E-9999")
+ @neg_frac = BigDecimal("-2E-9999")
+ @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")
+
+ @special_vals = [@infinity, @infinity_minus, @nan]
+ @regular_vals = [
+ @one, @mixed, @pos_int, @neg_int, @pos_frac,
+ @neg_frac, @one_minus, @frac_1, @frac_2]
+ @zeroes = [@zero, @zero_pos, @zero_neg]
+ end
+
+ 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)
+ res[0].should == BigDecimal('0.8E1')
+ res[1].should == BigDecimal('2.00000000000000000001')
+
+ BigDecimal('1').divmod(BigDecimal('2')).should == [0, 1]
+ BigDecimal('2').divmod(BigDecimal('1')).should == [2, 0]
+
+ BigDecimal('1').divmod(BigDecimal('-2')).should == [-1, -1]
+ BigDecimal('2').divmod(BigDecimal('-1')).should == [-2, 0]
+
+ BigDecimal('-1').divmod(BigDecimal('2')).should == [-1, 1]
+ BigDecimal('-2').divmod(BigDecimal('1')).should == [-2, 0]
+ end
+
+ it "can be reversed with * and +" do
+ # Example taken from BigDecimal documentation
+ a = BigDecimal("42")
+ b = BigDecimal("9")
+ q, m = a.divmod(b)
+ c = q * b + m
+ a.should == c
+
+ values = [@one, @one_minus, BigDecimal('2'), BigDecimal('-2'),
+ BigDecimal('5'), BigDecimal('-5'), BigDecimal('10'), BigDecimal('-10'),
+ BigDecimal('20'), BigDecimal('-20'), BigDecimal('100'), BigDecimal('-100'),
+ BigDecimal('1.23456789E10'), BigDecimal('-1.23456789E10')
+ ]
+
+ # TODO: file MRI bug:
+ # BigDecimal('1').divmod(BigDecimal('3E-9'))[0] #=> 0.3E9,
+ # but really should be 0.333333333E9
+ values << BigDecimal('1E-10')
+ values << BigDecimal('-1E-10')
+ values << BigDecimal('2E55')
+ values << BigDecimal('-2E55')
+ values << BigDecimal('2E-5555')
+ values << BigDecimal('-2E-5555')
+
+
+ values_and_zeroes = values + @zeroes
+ values_and_zeroes.each do |val1|
+ values.each do |val2|
+ res = val1.divmod(val2)
+ res[0].should == ((val1/val2).floor)
+ res[1].should == (val1 - res[0] * val2)
+ end
+ end
+ end
+
+ 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|
+ -> { val.divmod(zero) }.should.raise(ZeroDivisionError)
+ end
+ end
+ end
+
+ 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
+
+ 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 dividend is zero" do
+ @zeroes.each do |zero|
+ @regular_vals.each do |val|
+ zero.divmod(val).should == [@zero, @zero]
+ end
+ end
+ end
+
+ it "raises TypeError if the argument cannot be coerced to BigDecimal" do
+ -> {
+ @one.divmod('1')
+ }.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
new file mode 100644
index 0000000000..f742d68f87
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/double_fig_spec.rb
@@ -0,0 +1,9 @@
+require_relative '../../spec_helper'
+require 'bigdecimal'
+
+describe "BigDecimal.double_fig" do
+ # The result depends on the CPU and OS
+ it "returns the number of digits a Float number is allowed to have" do
+ BigDecimal.double_fig.should_not == nil
+ end
+end
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
new file mode 100644
index 0000000000..1be5862751
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/eql_spec.rb
@@ -0,0 +1,6 @@
+require_relative '../../spec_helper'
+require_relative 'shared/eql'
+
+describe "BigDecimal#eql?" do
+ 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
new file mode 100644
index 0000000000..933060eada
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/equal_value_spec.rb
@@ -0,0 +1,7 @@
+require_relative '../../spec_helper'
+require_relative 'shared/eql'
+
+
+describe "BigDecimal#==" do
+ it_behaves_like :bigdecimal_eql, :==
+end
diff --git a/spec/ruby/library/bigdecimal/exponent_spec.rb b/spec/ruby/library/bigdecimal/exponent_spec.rb
new file mode 100644
index 0000000000..8877147955
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/exponent_spec.rb
@@ -0,0 +1,27 @@
+require_relative '../../spec_helper'
+require_relative 'shared/power'
+require 'bigdecimal'
+
+describe "BigDecimal#**" do
+ it_behaves_like :bigdecimal_power, :**
+end
+
+describe "BigDecimal#exponent" do
+
+ it "returns an Integer" do
+ BigDecimal("2E100000000").exponent.kind_of?(Integer).should == true
+ BigDecimal("2E-999").exponent.kind_of?(Integer).should == true
+ end
+
+ it "is n if number can be represented as 0.xxx*10**n" do
+ BigDecimal("2E1000").exponent.should == 1001
+ BigDecimal("1234567E10").exponent.should == 17
+ end
+
+ it "returns 0 if self is 0" do
+ BigDecimal("0").exponent.should == 0
+ BigDecimal("+0").exponent.should == 0
+ BigDecimal("-0").exponent.should == 0
+ end
+
+end
diff --git a/spec/ruby/library/bigdecimal/finite_spec.rb b/spec/ruby/library/bigdecimal/finite_spec.rb
new file mode 100644
index 0000000000..8fc06029bb
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/finite_spec.rb
@@ -0,0 +1,34 @@
+require_relative '../../spec_helper'
+require 'bigdecimal'
+
+describe "BigDecimal#finite?" do
+ before :each do
+ @one = BigDecimal("1")
+ @zero = BigDecimal("0")
+ @zero_pos = BigDecimal("+0")
+ @zero_neg = 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")
+ @big = BigDecimal("2E40001")
+ @finite_vals = [@one, @zero, @zero_pos, @zero_neg, @two,
+ @three, @frac_1, @frac_2, @big, @one_minus]
+ end
+
+ it "is false if Infinity or NaN" do
+ @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.should.finite?
+ end
+ end
+end
diff --git a/spec/ruby/library/bigdecimal/fix_spec.rb b/spec/ruby/library/bigdecimal/fix_spec.rb
new file mode 100644
index 0000000000..dceb2ce867
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/fix_spec.rb
@@ -0,0 +1,57 @@
+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
+
+ it "returns a BigDecimal" do
+ BigDecimal("2E100000000").fix.kind_of?(BigDecimal).should == true
+ BigDecimal("2E-999").kind_of?(BigDecimal).should == true
+ end
+
+ it "returns the integer part of the absolute value" do
+ a = BigDecimal("2E1000")
+ a.fix.should == a
+ b = BigDecimal("-2E1000")
+ b.fix.should == b
+ BigDecimal("0.123456789E5").fix.should == BigDecimal("0.12345E5")
+ BigDecimal("-0.123456789E5").fix.should == BigDecimal("-0.12345E5")
+ end
+
+ it "correctly handles special values" do
+ @infinity.fix.should == @infinity
+ @infinity_neg.fix.should == @infinity_neg
+ @nan.fix.should.nan?
+ end
+
+ it "returns 0 if the absolute value is < 1" do
+ BigDecimal("0.99999").fix.should == 0
+ BigDecimal("-0.99999").fix.should == 0
+ BigDecimal("0.000000001").fix.should == 0
+ BigDecimal("-0.00000001").fix.should == 0
+ BigDecimal("-1000000").fix.should_not == 0
+ @zero.fix.should == 0
+ @zero_pos.fix.should == @zero_pos
+ @zero_neg.fix.should == @zero_neg
+ end
+
+ it "does not allow any arguments" do
+ -> {
+ @mixed.fix(10)
+ }.should.raise(ArgumentError)
+ end
+
+end
diff --git a/spec/ruby/library/bigdecimal/fixtures/classes.rb b/spec/ruby/library/bigdecimal/fixtures/classes.rb
new file mode 100644
index 0000000000..06e4474cf0
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/fixtures/classes.rb
@@ -0,0 +1,17 @@
+module BigDecimalSpecs
+ # helper method to sure that the global limit is reset back
+ def self.with_limit(l)
+ old = BigDecimal.limit(l)
+ yield
+ ensure
+ BigDecimal.limit(old)
+ end
+
+ def self.with_rounding(r)
+ old = BigDecimal.mode(BigDecimal::ROUND_MODE)
+ BigDecimal.mode(BigDecimal::ROUND_MODE, r)
+ yield
+ ensure
+ BigDecimal.mode(BigDecimal::ROUND_MODE, old)
+ end
+end
diff --git a/spec/ruby/library/bigdecimal/floor_spec.rb b/spec/ruby/library/bigdecimal/floor_spec.rb
new file mode 100644
index 0000000000..c0666c668c
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/floor_spec.rb
@@ -0,0 +1,100 @@
+require_relative '../../spec_helper'
+require 'bigdecimal'
+
+describe "BigDecimal#floor" do
+ before :each do
+ @one = BigDecimal("1")
+ @three = BigDecimal("3")
+ @four = BigDecimal("4")
+ @zero = BigDecimal("0")
+ @mixed = BigDecimal("1.23456789")
+ @mixed_big = BigDecimal("1.23456789E100")
+ @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 the greatest integer smaller or equal to self" do
+ @pos_int.floor.should == @pos_int
+ @neg_int.floor.should == @neg_int
+ @pos_frac.floor.should == @zero
+ @neg_frac.floor.should == BigDecimal("-1")
+ @zero.floor.should == 0
+ @zero_pos.floor.should == @zero_pos
+ @zero_neg.floor.should == @zero_neg
+
+ BigDecimal('2.3').floor.should == 2
+ BigDecimal('2.5').floor.should == 2
+ BigDecimal('2.9999').floor.should == 2
+ BigDecimal('-2.3').floor.should == -3
+ BigDecimal('-2.5').floor.should == -3
+ BigDecimal('-2.9999').floor.should == -3
+ BigDecimal('0.8').floor.should == 0
+ BigDecimal('-0.8').floor.should == -1
+ end
+
+ it "raise exception, if self is special value" do
+ -> { @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
+ @mixed.floor(1).should == BigDecimal("1.2")
+ @mixed.floor(5).should == BigDecimal("1.23456")
+
+ BigDecimal("-0.03").floor(1).should == BigDecimal("-0.1")
+ BigDecimal("0.03").floor(1).should == BigDecimal("0")
+
+ BigDecimal("23.45").floor(0).should == BigDecimal('23')
+ BigDecimal("23.45").floor(1).should == BigDecimal('23.4')
+ BigDecimal("23.45").floor(2).should == BigDecimal('23.45')
+
+ BigDecimal("-23.45").floor(0).should == BigDecimal('-24')
+ BigDecimal("-23.45").floor(1).should == BigDecimal('-23.5')
+ BigDecimal("-23.45").floor(2).should == BigDecimal('-23.45')
+
+ BigDecimal("2E-10").floor(0).should == @zero
+ BigDecimal("2E-10").floor(9).should == @zero
+ BigDecimal("2E-10").floor(10).should == BigDecimal('2E-10')
+ BigDecimal("2E-10").floor(11).should == BigDecimal('2E-10')
+
+ (1..10).each do |n|
+ # 0.3, 0.33, 0.333, etc.
+ (@one.div(@three,20)).floor(n).should == BigDecimal("0.#{'3'*n}")
+ # 1.3, 1.33, 1.333, etc.
+ (@four.div(@three,20)).floor(n).should == BigDecimal("1.#{'3'*n}")
+ (BigDecimal('31').div(@three,20)).floor(n).should == BigDecimal("10.#{'3'*n}")
+ end
+ (1..10).each do |n|
+ # -0.4, -0.34, -0.334, etc.
+ (-@one.div(@three,20)).floor(n).should == BigDecimal("-0.#{'3'*(n-1)}4")
+ end
+ (1..10).each do |n|
+ (@three.div(@one,20)).floor(n).should == @three
+ end
+ (1..10).each do |n|
+ (-@three.div(@one,20)).floor(n).should == -@three
+ end
+ end
+
+ it "sets n digits left of the decimal point to 0, if given n < 0" do
+ BigDecimal("13345.234").floor(-2).should == BigDecimal("13300.0")
+ @mixed_big.floor(-99).should == BigDecimal("0.12E101")
+ @mixed_big.floor(-100).should == BigDecimal("0.1E101")
+ @mixed_big.floor(-95).should == BigDecimal("0.123456E101")
+ (1..10).each do |n|
+ BigDecimal('1.8').floor(-n).should == @zero
+ end
+ BigDecimal("1E10").floor(-30).should == @zero
+ BigDecimal("-1E10").floor(-30).should == BigDecimal('-1E30')
+ end
+
+end
diff --git a/spec/ruby/library/bigdecimal/frac_spec.rb b/spec/ruby/library/bigdecimal/frac_spec.rb
new file mode 100644
index 0000000000..11ccf03c2f
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/frac_spec.rb
@@ -0,0 +1,48 @@
+require_relative '../../spec_helper'
+require 'bigdecimal'
+
+describe "BigDecimal#frac" 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
+
+ it "returns a BigDecimal" do
+ @pos_int.frac.kind_of?(BigDecimal).should == true
+ @neg_int.frac.kind_of?(BigDecimal).should == true
+ @pos_frac.kind_of?(BigDecimal).should == true
+ @neg_frac.kind_of?(BigDecimal).should == true
+ end
+
+ it "returns the fractional part of the absolute value" do
+ @mixed.frac.should == BigDecimal("0.23456789")
+ @pos_frac.frac.should == @pos_frac
+ @neg_frac.frac.should == @neg_frac
+ end
+
+ it "returns 0 if the value is 0" do
+ @zero.frac.should == @zero
+ end
+
+ it "returns 0 if the value is an integer" do
+ @pos_int.frac.should == @zero
+ @neg_int.frac.should == @zero
+ end
+
+ it "correctly handles special values" do
+ @infinity.frac.should == @infinity
+ @infinity_neg.frac.should == @infinity_neg
+ @nan.frac.should.nan?
+ end
+
+end
diff --git a/spec/ruby/library/bigdecimal/gt_spec.rb b/spec/ruby/library/bigdecimal/gt_spec.rb
new file mode 100644
index 0000000000..e9c9a60e75
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/gt_spec.rb
@@ -0,0 +1,96 @@
+require_relative '../../spec_helper'
+require 'bigdecimal'
+
+describe "BigDecimal#>" do
+ before :each do
+ @zero = BigDecimal("0")
+ @zero_pos = BigDecimal("+0")
+ @zero_neg = BigDecimal("-0")
+ @mixed = BigDecimal("1.23456789")
+ @pos_int = BigDecimal("2E5555")
+ @neg_int = BigDecimal("-2E5555")
+ @pos_frac = BigDecimal("2E-9999")
+ @neg_frac = BigDecimal("-2E-9999")
+
+ @int_mock = mock('123')
+ class << @int_mock
+ def coerce(other)
+ return [other, BigDecimal('123')]
+ end
+ def >(other)
+ BigDecimal('123') > other
+ end
+ end
+
+ @values = [@mixed, @pos_int, @neg_int, @pos_frac, @neg_frac,
+ -2**32, -2**31, -2**30, -2**16, -2**8, -100, -10, -1,
+ @zero , 1, 2, 10, 10.5, 2**8, 2**16, 2**32, @int_mock, @zero_pos, @zero_neg]
+
+ @infinity = BigDecimal("Infinity")
+ @infinity_neg = BigDecimal("-Infinity")
+
+ @float_infinity = Float::INFINITY
+ @float_infinity_neg = -Float::INFINITY
+
+ @nan = BigDecimal("NaN")
+ end
+
+ it "returns true if a > b" do
+ one = BigDecimal("1")
+ two = BigDecimal("2")
+
+ frac_1 = BigDecimal("1E-99999")
+ frac_2 = BigDecimal("0.9E-99999")
+ (@zero > one).should == false
+ (two > @zero).should == true
+ (frac_2 > frac_1).should == false
+
+ (@neg_int > @pos_int).should == false
+ (@pos_int > @neg_int).should == true
+ (@neg_int > @pos_frac).should == false
+ (@pos_frac > @neg_int).should == true
+ (@zero > @zero_pos).should == false
+ (@zero > @zero_neg).should == false
+ (@zero_neg > @zero_pos).should == false
+ (@zero_pos > @zero_neg).should == false
+ end
+
+ it "properly handles infinity values" do
+ @values.each { |val|
+ (val > @infinity).should == false
+ (@infinity > val).should == true
+ (val > @infinity_neg).should == true
+ (@infinity_neg > val).should == false
+ }
+ (@infinity > @infinity).should == false
+ (@infinity_neg > @infinity_neg).should == false
+ (@infinity > @infinity_neg).should == true
+ (@infinity_neg > @infinity).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
+ @values += [@infinity, @infinity_neg, @nan]
+ @values.each { |val|
+ (@nan > val).should == false
+ (val > @nan).should == false
+ }
+ end
+
+ it "raises an ArgumentError if the argument can't be coerced into a BigDecimal" do
+ -> {@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
new file mode 100644
index 0000000000..548f3efe4c
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/gte_spec.rb
@@ -0,0 +1,100 @@
+require_relative '../../spec_helper'
+require 'bigdecimal'
+
+describe "BigDecimal#>=" do
+ before :each do
+ @zero = BigDecimal("0")
+ @zero_pos = BigDecimal("+0")
+ @zero_neg = BigDecimal("-0")
+ @mixed = BigDecimal("1.23456789")
+ @pos_int = BigDecimal("2E5555")
+ @neg_int = BigDecimal("-2E5555")
+ @pos_frac = BigDecimal("2E-9999")
+ @neg_frac = BigDecimal("-2E-9999")
+
+ @int_mock = mock('123')
+ class << @int_mock
+ def coerce(other)
+ return [other, BigDecimal('123')]
+ end
+ def >=(other)
+ BigDecimal('123') >= other
+ end
+ end
+
+ @values = [@mixed, @pos_int, @neg_int, @pos_frac, @neg_frac,
+ -2**32, -2**31, -2**30, -2**16, -2**8, -100, -10, -1,
+ @zero , 1, 2, 10, 10.5, 2**8, 2**16, 2**32, @int_mock, @zero_pos, @zero_neg]
+
+ @infinity = BigDecimal("Infinity")
+ @infinity_neg = BigDecimal("-Infinity")
+
+ @float_infinity = Float::INFINITY
+ @float_infinity_neg = -Float::INFINITY
+
+ @nan = BigDecimal("NaN")
+ end
+
+ it "returns true if a >= b" do
+ one = BigDecimal("1")
+ two = BigDecimal("2")
+
+ frac_1 = BigDecimal("1E-99999")
+ frac_2 = BigDecimal("0.9E-99999")
+
+ (@zero >= one).should == false
+ (two >= @zero).should == true
+
+ (frac_2 >= frac_1).should == false
+ (two >= two).should == true
+ (frac_1 >= frac_1).should == true
+
+ (@neg_int >= @pos_int).should == false
+ (@pos_int >= @neg_int).should == true
+ (@neg_int >= @pos_frac).should == false
+ (@pos_frac >= @neg_int).should == true
+ (@zero >= @zero_pos).should == true
+ (@zero >= @zero_neg).should == true
+ (@zero_neg >= @zero_pos).should == true
+ (@zero_pos >= @zero_neg).should == true
+ end
+
+ it "properly handles infinity values" do
+ @values.each { |val|
+ (val >= @infinity).should == false
+ (@infinity >= val).should == true
+ (val >= @infinity_neg).should == true
+ (@infinity_neg >= val).should == false
+ }
+ (@infinity >= @infinity).should == true
+ (@infinity_neg >= @infinity_neg).should == true
+ (@infinity >= @infinity_neg).should == true
+ (@infinity_neg >= @infinity).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
+ @values += [@infinity, @infinity_neg, @nan]
+ @values.each { |val|
+ (@nan >= val).should == false
+ (val >= @nan).should == false
+ }
+ end
+
+ it "returns nil if the argument is nil" do
+ -> {@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
new file mode 100644
index 0000000000..025386107f
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/infinite_spec.rb
@@ -0,0 +1,32 @@
+require_relative '../../spec_helper'
+require 'bigdecimal'
+
+describe "BigDecimal#infinite?" do
+
+ it "returns 1 if self is Infinity" do
+ BigDecimal("Infinity").infinite?.should == 1
+ end
+
+ it "returns -1 if self is -Infinity" do
+ BigDecimal("-Infinity").infinite?.should == -1
+ end
+
+ it "returns not true otherwise" do
+ e2_plus = BigDecimal("2E40001")
+ e3_minus = BigDecimal("3E-20001")
+ really_small_zero = BigDecimal("0E-200000000")
+ really_big_zero = BigDecimal("0E200000000000")
+ e3_minus.infinite?.should == nil
+ e2_plus.infinite?.should == nil
+ really_small_zero.infinite?.should == nil
+ really_big_zero.infinite?.should == nil
+ BigDecimal("0.000000000000000000000000").infinite?.should == nil
+ end
+
+ it "returns not true if self is NaN" do
+ # NaN is a special value which is neither finite nor infinite.
+ nan = BigDecimal("NaN")
+ nan.infinite?.should == nil
+ end
+
+end
diff --git a/spec/ruby/library/bigdecimal/inspect_spec.rb b/spec/ruby/library/bigdecimal/inspect_spec.rb
new file mode 100644
index 0000000000..7ce47142b2
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/inspect_spec.rb
@@ -0,0 +1,30 @@
+require_relative '../../spec_helper'
+require 'bigdecimal'
+
+describe "BigDecimal#inspect" do
+
+ before :each do
+ @bigdec = BigDecimal("1234.5678")
+ end
+
+ it "returns String" do
+ @bigdec.inspect.kind_of?(String).should == true
+ end
+
+ it "looks like this" do
+ @bigdec.inspect.should == "0.12345678e4"
+ 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
+
+ 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
new file mode 100644
index 0000000000..75cbc8b55c
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/limit_spec.rb
@@ -0,0 +1,55 @@
+require_relative '../../spec_helper'
+require_relative 'fixtures/classes'
+require 'bigdecimal'
+
+describe "BigDecimal.limit" do
+ it "returns the value before set if the passed argument is nil or is not specified" do
+ old = BigDecimal.limit
+ BigDecimal.limit.should == 0
+ BigDecimal.limit(10).should == 0
+ BigDecimal.limit.should == 10
+ BigDecimal.limit(old)
+ end
+
+ 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
new file mode 100644
index 0000000000..c3f3573247
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/lt_spec.rb
@@ -0,0 +1,94 @@
+require_relative '../../spec_helper'
+require 'bigdecimal'
+
+describe "BigDecimal#<" do
+ before :each do
+ @zero = BigDecimal("0")
+ @zero_pos = BigDecimal("+0")
+ @zero_neg = BigDecimal("-0")
+ @mixed = BigDecimal("1.23456789")
+ @pos_int = BigDecimal("2E5555")
+ @neg_int = BigDecimal("-2E5555")
+ @pos_frac = BigDecimal("2E-9999")
+ @neg_frac = BigDecimal("-2E-9999")
+
+ @int_mock = mock('123')
+ class << @int_mock
+ def coerce(other)
+ return [other, BigDecimal('123')]
+ end
+ def <(other)
+ BigDecimal('123') < other
+ end
+ end
+
+ @values = [@mixed, @pos_int, @neg_int, @pos_frac, @neg_frac,
+ -2**32, -2**31, -2**30, -2**16, -2**8, -100, -10, -1,
+ @zero , 1, 2, 10, 10.5, 2**8, 2**16, 2**32, @int_mock, @zero_pos, @zero_neg]
+
+ @infinity = BigDecimal("Infinity")
+ @infinity_neg = BigDecimal("-Infinity")
+
+ @float_infinity = Float::INFINITY
+ @float_infinity_neg = -Float::INFINITY
+
+ @nan = BigDecimal("NaN")
+ end
+
+ it "returns true if a < b" do
+ one = BigDecimal("1")
+ two = BigDecimal("2")
+ frac_1 = BigDecimal("1E-99999")
+ frac_2 = BigDecimal("0.9E-99999")
+ (@zero < one).should == true
+ (two < @zero).should == false
+ (frac_2 < frac_1).should == true
+ (@neg_int < @pos_int).should == true
+ (@pos_int < @neg_int).should == false
+ (@neg_int < @pos_frac).should == true
+ (@pos_frac < @neg_int).should == false
+ (@zero < @zero_pos).should == false
+ (@zero < @zero_neg).should == false
+ (@zero_neg < @zero_pos).should == false
+ (@zero_pos < @zero_neg).should == false
+ end
+
+ it "properly handles infinity values" do
+ @values.each { |val|
+ (val < @infinity).should == true
+ (@infinity < val).should == false
+ (val < @infinity_neg).should == false
+ (@infinity_neg < val).should == true
+ }
+ (@infinity < @infinity).should == false
+ (@infinity_neg < @infinity_neg).should == false
+ (@infinity < @infinity_neg).should == false
+ (@infinity_neg < @infinity).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
+ @values += [@infinity, @infinity_neg, @nan]
+ @values.each { |val|
+ (@nan < val).should == false
+ (val < @nan).should == false
+ }
+ end
+
+ it "raises an ArgumentError if the argument can't be coerced into a BigDecimal" do
+ -> {@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
new file mode 100644
index 0000000000..7918bde88b
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/lte_spec.rb
@@ -0,0 +1,100 @@
+require_relative '../../spec_helper'
+require 'bigdecimal'
+
+describe "BigDecimal#<=" do
+ before :each do
+ @zero = BigDecimal("0")
+ @zero_pos = BigDecimal("+0")
+ @zero_neg = BigDecimal("-0")
+ @mixed = BigDecimal("1.23456789")
+ @pos_int = BigDecimal("2E5555")
+ @neg_int = BigDecimal("-2E5555")
+ @pos_frac = BigDecimal("2E-9999")
+ @neg_frac = BigDecimal("-2E-9999")
+
+ @int_mock = mock('123')
+ class << @int_mock
+ def coerce(other)
+ return [other, BigDecimal('123')]
+ end
+ def <=(other)
+ BigDecimal('123') <= other
+ end
+ end
+
+ @values = [@mixed, @pos_int, @neg_int, @pos_frac, @neg_frac,
+ -2**32, -2**31, -2**30, -2**16, -2**8, -100, -10, -1,
+ @zero , 1, 2, 10, 10.5, 2**8, 2**16, 2**32, @int_mock, @zero_pos, @zero_neg]
+
+ @infinity = BigDecimal("Infinity")
+ @infinity_neg = BigDecimal("-Infinity")
+
+ @float_infinity = Float::INFINITY
+ @float_infinity_neg = -Float::INFINITY
+
+ @nan = BigDecimal("NaN")
+ end
+
+ it "returns true if a <= b" do
+ one = BigDecimal("1")
+ two = BigDecimal("2")
+
+ frac_1 = BigDecimal("1E-99999")
+ frac_2 = BigDecimal("0.9E-99999")
+
+ (@zero <= one).should == true
+ (two <= @zero).should == false
+
+ (frac_2 <= frac_1).should == true
+ (two <= two).should == true
+ (frac_1 <= frac_1).should == true
+
+ (@neg_int <= @pos_int).should == true
+ (@pos_int <= @neg_int).should == false
+ (@neg_int <= @pos_frac).should == true
+ (@pos_frac <= @neg_int).should == false
+ (@zero <= @zero_pos).should == true
+ (@zero <= @zero_neg).should == true
+ (@zero_neg <= @zero_pos).should == true
+ (@zero_pos <= @zero_neg).should == true
+ end
+
+ it "properly handles infinity values" do
+ @values.each { |val|
+ (val <= @infinity).should == true
+ (@infinity <= val).should == false
+ (val <= @infinity_neg).should == false
+ (@infinity_neg <= val).should == true
+ }
+ (@infinity <= @infinity).should == true
+ (@infinity_neg <= @infinity_neg).should == true
+ (@infinity <= @infinity_neg).should == false
+ (@infinity_neg <= @infinity).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
+ @values += [@infinity, @infinity_neg, @nan]
+ @values.each { |val|
+ (@nan <= val).should == false
+ (val <= @nan).should == false
+ }
+ end
+
+ it "raises an ArgumentError if the argument can't be coerced into a BigDecimal" do
+ -> {@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
new file mode 100644
index 0000000000..bd3c19584b
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/minus_spec.rb
@@ -0,0 +1,66 @@
+require_relative '../../spec_helper'
+require 'bigdecimal'
+
+describe "BigDecimal#-" do
+
+ before :each do
+ @one = BigDecimal("1")
+ @zero = BigDecimal("0")
+ @two = BigDecimal("2")
+ @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")
+ end
+
+ it "returns a - b" do
+ (@two - @one).should == @one
+ (@one - @two).should == @one_minus
+ (@one - @one_minus).should == @two
+ (@frac_2 - @frac_1).should == BigDecimal("-0.1E-99999")
+ (@two - @two).should == @zero
+ (@frac_1 - @frac_1).should == @zero
+ (BigDecimal('1.23456789') - BigDecimal('1.2')).should == BigDecimal("0.03456789")
+ end
+
+ it "returns NaN if NaN is involved" do
+ (@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).should.nan?
+ (@infinity_minus - @infinity_minus).should.nan?
+ end
+
+ it "returns Infinity or -Infinity if these are involved" do
+ (@infinity - @infinity_minus).should == @infinity
+ (@infinity_minus - @infinity).should == @infinity_minus
+
+ (@infinity - @zero).should == @infinity
+ (@infinity - @frac_2).should == @infinity
+ (@infinity - @two).should == @infinity
+ (@infinity - @one_minus).should == @infinity
+
+ (@zero - @infinity).should == @infinity_minus
+ (@frac_2 - @infinity).should == @infinity_minus
+ (@two - @infinity).should == @infinity_minus
+ (@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
new file mode 100644
index 0000000000..26e6d0ea75
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/mode_spec.rb
@@ -0,0 +1,36 @@
+require_relative '../../spec_helper'
+require 'bigdecimal'
+
+describe "BigDecimal.mode" do
+ #the default value of BigDecimal exception constants is false
+ after :each do
+ BigDecimal.mode(BigDecimal::EXCEPTION_NaN, false)
+ BigDecimal.mode(BigDecimal::EXCEPTION_INFINITY, false)
+ BigDecimal.mode(BigDecimal::EXCEPTION_UNDERFLOW, false)
+ BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW, false)
+ BigDecimal.mode(BigDecimal::EXCEPTION_ZERODIVIDE, false)
+ end
+
+ it "returns the appropriate value and continue the computation if the flag is false" do
+ 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
+
+ it "returns Infinity when too big" do
+ BigDecimal("1E11111111111111111111").should == BigDecimal("Infinity")
+ (BigDecimal("1E1000000000000000000")**10).should == BigDecimal("Infinity")
+ end
+
+ it "raise an exception if the flag is true" do
+ BigDecimal.mode(BigDecimal::EXCEPTION_NaN, true)
+ -> { BigDecimal("NaN").add(BigDecimal("1"),0) }.should.raise(FloatDomainError)
+ BigDecimal.mode(BigDecimal::EXCEPTION_INFINITY, true)
+ -> { BigDecimal("0").add(BigDecimal("Infinity"),0) }.should.raise(FloatDomainError)
+ BigDecimal.mode(BigDecimal::EXCEPTION_ZERODIVIDE, true)
+ -> { BigDecimal("1").quo(BigDecimal("0")) }.should.raise(FloatDomainError)
+ BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW, true)
+ -> { 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
new file mode 100644
index 0000000000..035d31bd98
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/modulo_spec.rb
@@ -0,0 +1,12 @@
+require_relative '../../spec_helper'
+require_relative 'shared/modulo'
+
+describe "BigDecimal#%" do
+ 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
+end
diff --git a/spec/ruby/library/bigdecimal/mult_spec.rb b/spec/ruby/library/bigdecimal/mult_spec.rb
new file mode 100644
index 0000000000..2353df9cb8
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/mult_spec.rb
@@ -0,0 +1,24 @@
+require_relative '../../spec_helper'
+require_relative 'shared/mult'
+require 'bigdecimal'
+
+describe "BigDecimal#mult" do
+ it_behaves_like :bigdecimal_mult, :mult, [10]
+end
+
+describe "BigDecimal#mult" do
+ before :each do
+ @one = BigDecimal "1"
+ @e3_minus = BigDecimal("3E-20001")
+ @e3_plus = BigDecimal("3E20001")
+ @e = BigDecimal "1.00000000000000000000123456789"
+ @tolerance = @e.sub @one, 1000
+ @tolerance2 = BigDecimal "30001E-20005"
+
+ end
+
+ it "multiply self with other with (optional) precision" 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
new file mode 100644
index 0000000000..a8ce1da32e
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/multiply_spec.rb
@@ -0,0 +1,41 @@
+require_relative '../../spec_helper'
+require_relative 'shared/mult'
+require 'bigdecimal'
+
+describe "BigDecimal#*" do
+ it_behaves_like :bigdecimal_mult, :*, []
+end
+
+describe "BigDecimal#*" do
+ before :each do
+ @three = BigDecimal("3")
+ @e3_minus = BigDecimal("3E-20001")
+ @e3_plus = BigDecimal("3E20001")
+ @e = BigDecimal("1.00000000000000000000123456789")
+ @one = BigDecimal("1")
+ end
+
+ it "multiply self with other" do
+ (@one * @one).should == @one
+ (@e3_minus * @e3_plus).should == BigDecimal("9")
+ # Can't do this till we implement **
+ # (@e3_minus * @e3_minus).should == @e3_minus ** 2
+ # So let's rewrite it as:
+ (@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
new file mode 100644
index 0000000000..9eaf69b610
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/nan_spec.rb
@@ -0,0 +1,23 @@
+require_relative '../../spec_helper'
+require 'bigdecimal'
+
+describe "BigDecimal#nan?" do
+
+ it "returns true if self is not a number" do
+ BigDecimal("NaN").should.nan?
+ end
+
+ it "returns false if self is not a NaN" do
+ 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/nonzero_spec.rb b/spec/ruby/library/bigdecimal/nonzero_spec.rb
new file mode 100644
index 0000000000..31421ebdf4
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/nonzero_spec.rb
@@ -0,0 +1,29 @@
+require_relative '../../spec_helper'
+require 'bigdecimal'
+
+describe "BigDecimal#nonzero?" do
+
+ it "returns self if self doesn't equal zero" do
+ # documentation says, it returns true. (04/10/08)
+ e2_plus = BigDecimal("2E40001")
+ e3_minus = BigDecimal("3E-20001")
+ 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)
+ end
+
+ it "returns nil otherwise" do
+ # documentation states, it should return false. (04/10/08)
+ really_small_zero = BigDecimal("0E-200000000")
+ really_big_zero = BigDecimal("0E200000000000")
+ really_small_zero.nonzero?.should == nil
+ really_big_zero.nonzero?.should == nil
+ BigDecimal("0.000000000000000000000000").nonzero?.should == nil
+ end
+
+end
diff --git a/spec/ruby/library/bigdecimal/plus_spec.rb b/spec/ruby/library/bigdecimal/plus_spec.rb
new file mode 100644
index 0000000000..d1934841c8
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/plus_spec.rb
@@ -0,0 +1,54 @@
+require_relative '../../spec_helper'
+require 'bigdecimal'
+
+describe "BigDecimal#+" do
+
+ before :each do
+ @one = BigDecimal("1")
+ @zero = BigDecimal("0")
+ @two = BigDecimal("2")
+ @three = BigDecimal("3")
+ @ten = BigDecimal("10")
+ @eleven = BigDecimal("11")
+ @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")
+ end
+
+ it "returns a + b" do
+ (@two + @one).should == @three
+ (@one + @two).should == @three
+ (@one + @one_minus).should == @zero
+ (@zero + @one).should == @one
+ (@ten + @one).should == @eleven
+ (@frac_1 + @frac_2).should == BigDecimal("1.9E-99999")
+ (@frac_2 + @frac_1).should == BigDecimal("1.9E-99999")
+ (@frac_1 + @frac_1).should == BigDecimal("2E-99999")
+ end
+
+ it "returns NaN if NaN is involved" do
+ (@one + @nan).should.nan?
+ (@nan + @one).should.nan?
+ end
+
+ it "returns Infinity or -Infinity if these are involved" do
+ (@zero + @infinity).should == @infinity
+ (@frac_2 + @infinity).should == @infinity
+ (@two + @infinity_minus).should == @infinity_minus
+ end
+
+ it "returns NaN if Infinity + (- Infinity)" do
+ (@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
new file mode 100644
index 0000000000..63a45a1887
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/power_spec.rb
@@ -0,0 +1,6 @@
+require_relative '../../spec_helper'
+require_relative 'shared/power'
+
+describe "BigDecimal#power" do
+ it_behaves_like :bigdecimal_power, :power
+end
diff --git a/spec/ruby/library/bigdecimal/quo_spec.rb b/spec/ruby/library/bigdecimal/quo_spec.rb
new file mode 100644
index 0000000000..65a4330303
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/quo_spec.rb
@@ -0,0 +1,12 @@
+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")).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
new file mode 100644
index 0000000000..27a2986570
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/remainder_spec.rb
@@ -0,0 +1,77 @@
+require_relative '../../spec_helper'
+require 'bigdecimal'
+
+describe "BigDecimal#remainder" do
+
+ before :each do
+ @zero = BigDecimal("0")
+ @one = BigDecimal("1")
+ @three = BigDecimal("3")
+ @mixed = BigDecimal("1.23456789")
+ @pos_int = BigDecimal("2E5555")
+ @neg_int = BigDecimal("-2E5555")
+ @pos_frac = BigDecimal("2E-9999")
+ @neg_frac = BigDecimal("-2E-9999")
+ @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")
+ end
+
+ it "it equals modulo, if both values are of same sign" do
+ BigDecimal('1234567890123456789012345679').remainder(BigDecimal('1')).should == @zero
+ BigDecimal('123456789').remainder(BigDecimal('333333333333333333333333333E-50')).should == BigDecimal('0.12233333333333333333345679E-24')
+
+ @mixed.remainder(@pos_frac).should == @mixed % @pos_frac
+ @pos_int.remainder(@pos_frac).should == @pos_int % @pos_frac
+ @neg_frac.remainder(@neg_int).should == @neg_frac % @neg_int
+ @neg_int.remainder(@neg_frac).should == @neg_int % @neg_frac
+ end
+
+ it "means self-arg*(self/arg).truncate" do
+ @mixed.remainder(@neg_frac).should == @mixed - @neg_frac * (@mixed / @neg_frac).truncate
+ @pos_int.remainder(@neg_frac).should == @pos_int - @neg_frac * (@pos_int / @neg_frac).truncate
+ @neg_frac.remainder(@pos_int).should == @neg_frac - @pos_int * (@neg_frac / @pos_int).truncate
+ @neg_int.remainder(@pos_frac).should == @neg_int - @pos_frac * (@neg_int / @pos_frac).truncate
+ end
+
+ 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
+ @zero.remainder(@mixed).should == @zero
+ end
+
+ it "returns NaN if NaN is involved" do
+ @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
+ @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
+ -> {
+ @one.remainder('2')
+ }.should.raise(TypeError)
+ end
+
+end
diff --git a/spec/ruby/library/bigdecimal/round_spec.rb b/spec/ruby/library/bigdecimal/round_spec.rb
new file mode 100644
index 0000000000..622e129d93
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/round_spec.rb
@@ -0,0 +1,234 @@
+require_relative '../../spec_helper'
+require 'bigdecimal'
+
+describe "BigDecimal#round" do
+ before :each do
+ @one = BigDecimal("1")
+ @two = BigDecimal("2")
+ @three = BigDecimal("3")
+
+ @neg_one = BigDecimal("-1")
+ @neg_two = BigDecimal("-2")
+ @neg_three = BigDecimal("-3")
+
+ @p1_50 = BigDecimal("1.50")
+ @p1_51 = BigDecimal("1.51")
+ @p1_49 = BigDecimal("1.49")
+ @n1_50 = BigDecimal("-1.50")
+ @n1_51 = BigDecimal("-1.51")
+ @n1_49 = BigDecimal("-1.49")
+
+ @p2_50 = BigDecimal("2.50")
+ @p2_51 = BigDecimal("2.51")
+ @p2_49 = BigDecimal("2.49")
+ @n2_50 = BigDecimal("-2.50")
+ @n2_51 = BigDecimal("-2.51")
+ @n2_49 = BigDecimal("-2.49")
+ end
+
+ after :each do
+ BigDecimal.mode(BigDecimal::ROUND_MODE, BigDecimal::ROUND_HALF_UP)
+ end
+
+ it "uses default rounding method unless given" do
+ @p1_50.round(0).should == @two
+ @p1_51.round(0).should == @two
+ @p1_49.round(0).should == @one
+ @n1_50.round(0).should == @neg_two
+ @n1_51.round(0).should == @neg_two
+ @n1_49.round(0).should == @neg_one
+
+ @p2_50.round(0).should == @three
+ @p2_51.round(0).should == @three
+ @p2_49.round(0).should == @two
+ @n2_50.round(0).should == @neg_three
+ @n2_51.round(0).should == @neg_three
+ @n2_49.round(0).should == @neg_two
+
+ BigDecimal.mode(BigDecimal::ROUND_MODE, BigDecimal::ROUND_DOWN)
+
+ @p1_50.round(0).should == @one
+ @p1_51.round(0).should == @one
+ @p1_49.round(0).should == @one
+ @n1_50.round(0).should == @neg_one
+ @n1_51.round(0).should == @neg_one
+ @n1_49.round(0).should == @neg_one
+
+ @p2_50.round(0).should == @two
+ @p2_51.round(0).should == @two
+ @p2_49.round(0).should == @two
+ @n2_50.round(0).should == @neg_two
+ @n2_51.round(0).should == @neg_two
+ @n2_49.round(0).should == @neg_two
+ end
+
+ ["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
+
+ ["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
+
+ ["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
+
+ ["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
+
+ ["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
+
+ ["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
+
+ ["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
+ -> { 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
+ -> { 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
new file mode 100644
index 0000000000..8e3e388bab
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/shared/eql.rb
@@ -0,0 +1,61 @@
+require 'bigdecimal'
+
+describe :bigdecimal_eql, shared: true do
+ before :each do
+ @bg6543_21 = BigDecimal("6543.21")
+ @bg5667_19 = BigDecimal("5667.19")
+ @a = BigDecimal("1.0000000000000000000000000000000000000000005")
+ @b = BigDecimal("1.00000000000000000000000000000000000000000005")
+ @bigint = BigDecimal("1000.0")
+ @nan = BigDecimal("NaN")
+ @infinity = BigDecimal("Infinity")
+ @infinity_minus = BigDecimal("-Infinity")
+ end
+
+ it "tests for equality" do
+ @bg6543_21.send(@method, @bg6543_21).should == true
+ @a.send(@method, @a).should == true
+ @a.send(@method, @b).should == false
+ @bg6543_21.send(@method, @a).should == false
+ @bigint.send(@method, 1000).should == true
+ end
+
+ it "returns false for NaN as it is never equal to any number" do
+ @nan.send(@method, @nan).should == false
+ @a.send(@method, @nan).should == false
+ @nan.send(@method, @a).should == false
+ @nan.send(@method, @infinity).should == false
+ @nan.send(@method, @infinity_minus).should == false
+ @infinity.send(@method, @nan).should == false
+ @infinity_minus.send(@method, @nan).should == false
+ end
+
+ it "returns true for infinity values with the same sign" do
+ @infinity.send(@method, @infinity).should == true
+ @infinity.send(@method, BigDecimal("Infinity")).should == true
+ BigDecimal("Infinity").send(@method, @infinity).should == true
+
+ @infinity_minus.send(@method, @infinity_minus).should == true
+ @infinity_minus.send(@method, BigDecimal("-Infinity")).should == true
+ BigDecimal("-Infinity").send(@method, @infinity_minus).should == true
+ end
+
+ it "returns false for infinity values with different signs" do
+ @infinity.send(@method, @infinity_minus).should == false
+ @infinity_minus.send(@method, @infinity).should == false
+ end
+
+ it "returns false when infinite value compared to finite one" do
+ @infinity.send(@method, @a).should == false
+ @infinity_minus.send(@method, @a).should == false
+
+ @a.send(@method, @infinity).should == false
+ @a.send(@method, @infinity_minus).should == false
+ end
+
+ it "returns false when compared objects that can not be coerced into BigDecimal" do
+ @infinity.send(@method, nil).should == false
+ @bigint.send(@method, nil).should == false
+ @nan.send(@method, nil).should == false
+ end
+end
diff --git a/spec/ruby/library/bigdecimal/shared/modulo.rb b/spec/ruby/library/bigdecimal/shared/modulo.rb
new file mode 100644
index 0000000000..63470d0977
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/shared/modulo.rb
@@ -0,0 +1,131 @@
+require 'bigdecimal'
+
+describe :bigdecimal_modulo, shared: true do
+ before :each do
+ @one = BigDecimal("1")
+ @zero = BigDecimal("0")
+ @zero_pos = BigDecimal("+0")
+ @zero_neg = BigDecimal("-0")
+ @two = BigDecimal("2")
+ @three = BigDecimal("3")
+ @mixed = BigDecimal("1.23456789")
+ @nan = BigDecimal("NaN")
+ @infinity = BigDecimal("Infinity")
+ @infinity_minus = BigDecimal("-Infinity")
+ @one_minus = BigDecimal("-1")
+ @frac_1 = BigDecimal("1E-9999")
+ @frac_2 = BigDecimal("0.9E-9999")
+ end
+
+ it "returns self modulo other" do
+ bd6543 = BigDecimal("6543.21")
+ bd5667 = BigDecimal("5667.19")
+ a = BigDecimal("1.0000000000000000000000000000000000000000005")
+ b = BigDecimal("1.00000000000000000000000000000000000000000005")
+
+ bd6543.send(@method, 137).should == BigDecimal("104.21")
+ bd5667.send(@method, bignum_value).should == 5667.19
+ bd6543.send(@method, BigDecimal("137.24")).should == BigDecimal("92.93")
+ bd6543.send(@method, 137).should be_close(6543.21.%(137), TOLERANCE)
+ bd6543.send(@method, 137).should == bd6543 % 137
+ bd5667.send(@method, bignum_value).should be_close(5667.19.%(0xffffffff), TOLERANCE)
+ bd5667.send(@method, bignum_value).should == bd5667.%(0xffffffff)
+ bd6543.send(@method, 137.24).should be_close(6543.21.%(137.24), TOLERANCE)
+ a.send(@method, b).should == BigDecimal("0.45E-42")
+ @zero.send(@method, @one).should == @zero
+ @zero.send(@method, @one_minus).should == @zero
+ @two.send(@method, @one).should == @zero
+ @one.send(@method, @two).should == @one
+ @frac_1.send(@method, @one).should == @frac_1
+ @frac_2.send(@method, @one).should == @frac_2
+ @one_minus.send(@method, @one_minus).should == @zero
+ @one_minus.send(@method, @one).should == @zero
+ @one_minus.send(@method, @two).should == @one
+ @one.send(@method, -@two).should == -@one
+
+ @one_minus.modulo(BigDecimal('0.9')).should == BigDecimal('0.8')
+ @one.modulo(BigDecimal('-0.9')).should == BigDecimal('-0.8')
+
+ @one_minus.modulo(BigDecimal('0.8')).should == BigDecimal('0.6')
+ @one.modulo(BigDecimal('-0.8')).should == BigDecimal('-0.6')
+
+ @one_minus.modulo(BigDecimal('0.6')).should == BigDecimal('0.2')
+ @one.modulo(BigDecimal('-0.6')).should == BigDecimal('-0.2')
+
+ @one_minus.modulo(BigDecimal('0.5')).should == @zero
+ @one.modulo(BigDecimal('-0.5')).should == @zero
+ @one_minus.modulo(BigDecimal('-0.5')).should == @zero
+
+ @one_minus.modulo(BigDecimal('0.4')).should == BigDecimal('0.2')
+ @one.modulo(BigDecimal('-0.4')).should == BigDecimal('-0.2')
+
+ @one_minus.modulo(BigDecimal('0.3')).should == BigDecimal('0.2')
+ @one_minus.modulo(BigDecimal('0.2')).should == @zero
+ end
+
+ it "returns a [Float value] when the argument is Float" do
+ @two.send(@method, 2.0).should == 0.0
+ @one.send(@method, 2.0).should == 1.0
+ res = @two.send(@method, 5.0)
+ 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).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).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).should.nan?
+ @infinity_minus.send(@method, @one).should.nan?
+
+ @infinity.send(@method, @infinity_minus).should.nan?
+ @infinity_minus.send(@method, @infinity).should.nan?
+ end
+
+ 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
+ -> {
+ @one.send(@method, '2')
+ }.should.raise(TypeError)
+ end
+end
+
+describe :bigdecimal_modulo_zerodivisionerror, shared: true do
+ it "raises ZeroDivisionError if other is zero" do
+ bd5667 = BigDecimal("5667.19")
+
+ -> { 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
new file mode 100644
index 0000000000..c613df04c4
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/shared/mult.rb
@@ -0,0 +1,97 @@
+require 'bigdecimal'
+
+describe :bigdecimal_mult, shared: true do
+ before :each do
+ @zero = BigDecimal "0"
+ @zero_pos = BigDecimal "+0"
+ @zero_neg = BigDecimal "-0"
+
+ @one = BigDecimal "1"
+ @mixed = BigDecimal "1.23456789"
+ @pos_int = BigDecimal "2E5555"
+ @neg_int = BigDecimal "-2E5555"
+ @pos_frac = BigDecimal "2E-9999"
+ @neg_frac = BigDecimal "-2E-9999"
+ @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"
+
+ @e3_minus = BigDecimal "3E-20001"
+ @e = BigDecimal "1.00000000000000000000123456789"
+ @tolerance = @e.sub @one, 1000
+ @tolerance2 = BigDecimal "30001E-20005"
+
+ @special_vals = [@infinity, @infinity_minus, @nan]
+ @regular_vals = [ @one, @mixed, @pos_int, @neg_int,
+ @pos_frac, @neg_frac, @one_minus,
+ @frac_1, @frac_2
+ ]
+ @zeroes = [@zero, @zero_pos, @zero_neg]
+ end
+
+ it "returns zero of appropriate sign if self or argument is zero" do
+ @zero.send(@method, @zero, *@object).sign.should == BigDecimal::SIGN_POSITIVE_ZERO
+ @zero_neg.send(@method, @zero_neg, *@object).sign.should == BigDecimal::SIGN_POSITIVE_ZERO
+ @zero.send(@method, @zero_neg, *@object).sign.should == BigDecimal::SIGN_NEGATIVE_ZERO
+ @zero_neg.send(@method, @zero, *@object).sign.should == BigDecimal::SIGN_NEGATIVE_ZERO
+
+ @one.send(@method, @zero, *@object).sign.should == BigDecimal::SIGN_POSITIVE_ZERO
+ @one.send(@method, @zero_neg, *@object).sign.should == BigDecimal::SIGN_NEGATIVE_ZERO
+
+ @zero.send(@method, @one, *@object).sign.should == BigDecimal::SIGN_POSITIVE_ZERO
+ @zero.send(@method, @one_minus, *@object).sign.should == BigDecimal::SIGN_NEGATIVE_ZERO
+ @zero_neg.send(@method, @one_minus, *@object).sign.should == BigDecimal::SIGN_POSITIVE_ZERO
+ @zero_neg.send(@method, @one, *@object).sign.should == BigDecimal::SIGN_NEGATIVE_ZERO
+ end
+
+ it "returns NaN if NaN is involved" do
+ values = @regular_vals + @zeroes
+
+ values.each do |val|
+ @nan.send(@method, val, *@object).should.nan?
+ val.send(@method, @nan, *@object).should.nan?
+ end
+ end
+
+ it "returns zero if self or argument is zero" do
+ values = @regular_vals + @zeroes
+
+ values.each do |val|
+ @zeroes.each do |zero|
+ zero.send(@method, val, *@object).should == 0
+ zero.send(@method, val, *@object).should.zero?
+ val.send(@method, zero, *@object).should == 0
+ val.send(@method, zero, *@object).should.zero?
+ end
+ end
+ end
+
+ it "returns infinite value if self or argument is infinite" do
+ values = @regular_vals
+ infs = [@infinity, @infinity_minus]
+
+ values.each do |val|
+ infs.each do |inf|
+ inf.send(@method, val, *@object).should_not.finite?
+ val.send(@method, inf, *@object).should_not.finite?
+ end
+ end
+
+ @infinity.send(@method, @infinity, *@object).infinite?.should == 1
+ @infinity_minus.send(@method, @infinity_minus, *@object).infinite?.should == 1
+ @infinity.send(@method, @infinity_minus, *@object).infinite?.should == -1
+ @infinity_minus.send(@method, @infinity, *@object).infinite?.should == -1
+ @infinity.send(@method, @one, *@object).infinite?.should == 1
+ @infinity_minus.send(@method, @one, *@object).infinite?.should == -1
+ end
+
+ it "returns NaN if the result is undefined" do
+ @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
new file mode 100644
index 0000000000..6dafb638e2
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/shared/power.rb
@@ -0,0 +1,72 @@
+require 'bigdecimal'
+
+describe :bigdecimal_power, shared: true do
+ it "powers of self" do
+ e3_minus = BigDecimal("3E-20001")
+ e3_minus_power_2 = BigDecimal("9E-40002")
+ e3_plus = BigDecimal("3E20001")
+ e2_plus = BigDecimal("2E40001")
+ e5_minus = BigDecimal("5E-40002")
+ e = BigDecimal("1.00000000000000000000123456789")
+ one = BigDecimal("1")
+ ten = BigDecimal("10")
+ # 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
+ e3_plus.send(@method, 0).should == 1
+ e3_minus.send(@method, 1).should == e3_minus
+ e2_plus.send(@method, -1).should == e5_minus
+ e2_plus.send(@method, -1).should == e5_minus.power(1)
+ (e2_plus.send(@method, -1) * e5_minus.send(@method, -1)).should == 1
+ e.send(@method, 2).should == e * e
+ e.send(@method, -1).should be_close(one.div(e, 120), tolerance)
+ ten.send(@method, 10000).should == ten_powers
+ pi.send(@method, 10).should be_close(Math::PI ** 10, TOLERANCE)
+ end
+
+ it "powers of 1 equal 1" do
+ one = BigDecimal("1")
+ one.send(@method, 0).should == 1
+ one.send(@method, 1).should == 1
+ one.send(@method, 10).should == 1
+ one.send(@method, -10).should == 1
+ end
+
+ it "0 to power of 0 is 1" do
+ zero = BigDecimal("0")
+ zero.send(@method, 0).should == 1
+ end
+
+ it "0 to powers < 0 is Infinity" do
+ zero = BigDecimal("0")
+ infinity = BigDecimal("Infinity")
+ zero.send(@method, -10).should == infinity
+ zero.send(@method, -1).should == infinity
+ end
+
+ it "other powers of 0 are 0" do
+ zero = BigDecimal("0")
+ zero.send(@method, 1).should == 0
+ zero.send(@method, 10).should == 0
+ end
+
+ it "returns NaN if self is NaN" do
+ 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
+ BigDecimal("Infinity").send(@method, -5).should == 0
+ BigDecimal("-Infinity").send(@method, -5).should == 0
+ end
+
+ it "returns infinite if self is infinite and argument is positive" do
+ infinity = BigDecimal("Infinity")
+ BigDecimal("Infinity").send(@method, 4).should == infinity
+ BigDecimal("-Infinity").send(@method, 4).should == infinity
+ BigDecimal("Infinity").send(@method, 5).should == infinity
+ BigDecimal("-Infinity").send(@method, 5).should == -infinity
+ end
+end
diff --git a/spec/ruby/library/bigdecimal/shared/quo.rb b/spec/ruby/library/bigdecimal/shared/quo.rb
new file mode 100644
index 0000000000..18ff2fe9a5
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/shared/quo.rb
@@ -0,0 +1,68 @@
+require 'bigdecimal'
+
+describe :bigdecimal_quo, shared: true do
+ before :each do
+ @one = BigDecimal("1")
+ @zero = BigDecimal("0")
+ @zero_plus = BigDecimal("+0")
+ @zero_minus = BigDecimal("-0")
+ @two = BigDecimal("2")
+ @three = BigDecimal("3")
+ @eleven = BigDecimal("11")
+ @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")
+ end
+
+ it "returns a / b" do
+ @two.send(@method, @one, *@object).should == @two
+ @one.send(@method, @two, *@object).should == BigDecimal("0.5")
+ @eleven.send(@method, @three, *@object).should be_close(@three + (@two / @three), TOLERANCE)
+ @one.send(@method, @one_minus, *@object).should == @one_minus
+ @one_minus.send(@method, @one_minus, *@object).should == @one
+ @frac_2.send(@method, @frac_1, *@object).should == BigDecimal("0.9")
+ @frac_1.send(@method, @frac_1, *@object).should == @one
+ @one.send(@method, BigDecimal('-2E5555'), *@object).should == BigDecimal('-0.5E-5555')
+ @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
+ end
+
+ it "returns (+|-) Infinity if (+|-) Infinity divided by one" do
+ @infinity_minus.send(@method, @one, *@object).should == @infinity_minus
+ @infinity.send(@method, @one, *@object).should == @infinity
+ @infinity_minus.send(@method, @one_minus, *@object).should == @infinity
+ end
+
+ it "returns NaN if Infinity / ((+|-) Infinity)" do
+ @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
+ @one.send(@method, @zero, *@object).should == @infinity
+ @one.send(@method, @zero_plus, *@object).should == @infinity
+ @one.send(@method, @zero_minus, *@object).should == @infinity_minus
+ end
+
+ it "returns NaN if zero is divided by zero" do
+ @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
new file mode 100644
index 0000000000..3c9f3b4f97
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/shared/to_int.rb
@@ -0,0 +1,16 @@
+require 'bigdecimal'
+
+describe :bigdecimal_to_int, shared: true do
+ it "raises FloatDomainError if BigDecimal is infinity or NaN" do
+ -> { BigDecimal("Infinity").send(@method) }.should.raise(FloatDomainError)
+ -> { BigDecimal("NaN").send(@method) }.should.raise(FloatDomainError)
+ end
+
+ 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
+ BigDecimal("2E10").send(@method).should == 20000000000
+ BigDecimal("3.14159").send(@method).should == 3
+ end
+end
diff --git a/spec/ruby/library/bigdecimal/sign_spec.rb b/spec/ruby/library/bigdecimal/sign_spec.rb
new file mode 100644
index 0000000000..ae2c28e9fd
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/sign_spec.rb
@@ -0,0 +1,46 @@
+require_relative '../../spec_helper'
+require 'bigdecimal'
+
+describe "BigDecimal#sign" do
+
+ it "defines several constants for signs" do
+ # are these really correct?
+ BigDecimal::SIGN_POSITIVE_INFINITE.should == 3
+ BigDecimal::SIGN_NEGATIVE_INFINITE.should == -3
+ BigDecimal::SIGN_POSITIVE_ZERO.should == 1
+ BigDecimal::SIGN_NEGATIVE_ZERO.should == -1
+ BigDecimal::SIGN_POSITIVE_FINITE.should == 2
+ BigDecimal::SIGN_NEGATIVE_FINITE.should == -2
+ end
+
+ it "returns positive value if BigDecimal greater than 0" do
+ BigDecimal("1").sign.should == BigDecimal::SIGN_POSITIVE_FINITE
+ BigDecimal("1E-20000000").sign.should == BigDecimal::SIGN_POSITIVE_FINITE
+ BigDecimal("1E200000000").sign.should == BigDecimal::SIGN_POSITIVE_FINITE
+ BigDecimal("Infinity").sign.should == BigDecimal::SIGN_POSITIVE_INFINITE
+ end
+
+ it "returns negative value if BigDecimal less than 0" do
+ BigDecimal("-1").sign.should == BigDecimal::SIGN_NEGATIVE_FINITE
+ BigDecimal("-1E-9990000").sign.should == BigDecimal::SIGN_NEGATIVE_FINITE
+ BigDecimal("-1E20000000").sign.should == BigDecimal::SIGN_NEGATIVE_FINITE
+ BigDecimal("-Infinity").sign.should == BigDecimal::SIGN_NEGATIVE_INFINITE
+ end
+
+ it "returns positive zero if BigDecimal equals positive zero" do
+ BigDecimal("0").sign.should == BigDecimal::SIGN_POSITIVE_ZERO
+ BigDecimal("0E-200000000").sign.should == BigDecimal::SIGN_POSITIVE_ZERO
+ BigDecimal("0E200000000").sign.should == BigDecimal::SIGN_POSITIVE_ZERO
+ end
+
+ it "returns negative zero if BigDecimal equals negative zero" do
+ BigDecimal("-0").sign.should == BigDecimal::SIGN_NEGATIVE_ZERO
+ BigDecimal("-0E-200000000").sign.should == BigDecimal::SIGN_NEGATIVE_ZERO
+ BigDecimal("-0E200000000").sign.should == BigDecimal::SIGN_NEGATIVE_ZERO
+ end
+
+ it "returns BigDecimal::SIGN_NaN if BigDecimal is NaN" do
+ BigDecimal("NaN").sign.should == BigDecimal::SIGN_NaN
+ end
+
+end
diff --git a/spec/ruby/library/bigdecimal/split_spec.rb b/spec/ruby/library/bigdecimal/split_spec.rb
new file mode 100644
index 0000000000..53b1f649d9
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/split_spec.rb
@@ -0,0 +1,86 @@
+require_relative '../../spec_helper'
+require 'bigdecimal'
+
+describe "BigDecimal#split" do
+
+ before :each do
+ @arr = BigDecimal("0.314159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593014782083152134043E1").split
+ @arr_neg = BigDecimal("-0.314159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593014782083152134043E1").split
+ @digits = "922337203685477580810101333333333333333333333333333"
+ @arr_big = BigDecimal("00#{@digits}000").split
+ @arr_big_neg = BigDecimal("-00#{@digits}000").split
+ @huge = BigDecimal('100000000000000000000000000000000000000000001E90000000').split
+
+ @infinity = BigDecimal("Infinity")
+ @infinity_neg = BigDecimal("-Infinity")
+ @nan = BigDecimal("NaN")
+ @zero = BigDecimal("0")
+ @zero_neg = BigDecimal("-0")
+ end
+
+ it "splits BigDecimal in an array with four values" do
+ @arr.size.should == 4
+ end
+
+ it "first value: 1 for numbers > 0" do
+ @arr[0].should == 1
+ @arr_big[0].should == 1
+ @zero.split[0].should == 1
+ @huge[0].should == 1
+ BigDecimal("+0").split[0].should == 1
+ BigDecimal("1E400").split[0].should == 1
+ @infinity.split[0].should == 1
+ end
+
+ it "first value: -1 for numbers < 0" do
+ @arr_neg[0].should == -1
+ @arr_big_neg[0].should == -1
+ @zero_neg.split[0].should == -1
+ BigDecimal("-1E400").split[0].should == -1
+ @infinity_neg.split[0].should == -1
+ end
+
+ it "first value: 0 if BigDecimal is NaN" do
+ BigDecimal("NaN").split[0].should == 0
+ end
+
+ it "second value: a string with the significant digits" do
+ string = "314159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593014782083152134043"
+ @arr[1].should == string
+ @arr_big[1].should == @digits
+ @arr_big_neg[1].should == @digits
+ @huge[1].should == "100000000000000000000000000000000000000000001"
+ @infinity.split[1].should == @infinity.to_s
+ @nan.split[1].should == @nan.to_s
+ @infinity_neg.split[1].should == @infinity.to_s
+ @zero.split[1].should == "0"
+ BigDecimal("-0").split[1].should == "0"
+ 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
+ end
+
+ it "fourth value: the exponent" do
+ @arr[3].should == 1
+ @arr_neg[3].should == 1
+ @arr_big[3].should == 54
+ @arr_big_neg[3].should == 54
+ @huge[3].should == 90000045
+ @infinity.split[3].should == 0
+ @nan.split[3].should == 0
+ @infinity_neg.split[3].should == 0
+ @zero.split[3].should == 0
+ @zero_neg.split[3].should == 0
+ end
+
+end
diff --git a/spec/ruby/library/bigdecimal/sqrt_spec.rb b/spec/ruby/library/bigdecimal/sqrt_spec.rb
new file mode 100644
index 0000000000..1f3ef9a8c3
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/sqrt_spec.rb
@@ -0,0 +1,114 @@
+require_relative '../../spec_helper'
+require_relative 'fixtures/classes'
+require 'bigdecimal'
+
+describe "BigDecimal#sqrt" do
+ before :each do
+ @one = BigDecimal("1")
+ @zero = BigDecimal("0")
+ @zero_pos = BigDecimal("+0")
+ @zero_neg = BigDecimal("-0")
+ @two = BigDecimal("2.0")
+ @three = BigDecimal("3.0")
+ @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")
+ end
+
+ it "returns square root of 2 with desired precision" do
+ string = "1.41421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157"
+ (1..99).each { |idx|
+ @two.sqrt(idx).should be_close(BigDecimal(string), BigDecimal("1E-#{idx-1}"))
+ }
+ end
+
+ it "returns square root of 3 with desired precision" do
+ sqrt_3 = "1.732050807568877293527446341505872366942805253810380628055806979451933016908800037081146186757248575"
+ (1..99).each { |idx|
+ @three.sqrt(idx).should be_close(BigDecimal(sqrt_3), BigDecimal("1E-#{idx-1}"))
+ }
+ end
+
+ it "returns square root of 121 with desired precision" do
+ BigDecimal('121').sqrt(5).should be_close(11, 0.00001)
+ end
+
+ 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
+ -> {
+ @one.sqrt
+ }.should.raise(ArgumentError)
+ end
+
+ it "raises ArgumentError if a negative number is given" do
+ -> {
+ @one.sqrt(-1)
+ }.should.raise(ArgumentError)
+ end
+
+ it "raises ArgumentError if 2 arguments are given" do
+ -> {
+ @one.sqrt(1, 1)
+ }.should.raise(ArgumentError)
+ end
+
+ it "raises TypeError if nil is given" do
+ -> {
+ @one.sqrt(nil)
+ }.should.raise(TypeError)
+ end
+
+ it "raises TypeError if a string is given" do
+ -> {
+ @one.sqrt("stuff")
+ }.should.raise(TypeError)
+ end
+
+ it "raises TypeError if a plain Object is given" do
+ -> {
+ @one.sqrt(Object.new)
+ }.should.raise(TypeError)
+ end
+
+ it "returns 1 if precision is 0 or 1" do
+ @one.sqrt(1).should == 1
+ @one.sqrt(0).should == 1
+ end
+
+ it "raises FloatDomainError on negative values" do
+ -> {
+ BigDecimal('-1').sqrt(10)
+ }.should.raise(FloatDomainError)
+ end
+
+ it "returns positive infinity for infinity" do
+ @infinity.sqrt(1).should == @infinity
+ end
+
+ it "raises FloatDomainError for negative infinity" do
+ -> {
+ @infinity_minus.sqrt(1)
+ }.should.raise(FloatDomainError)
+ end
+
+ it "raises FloatDomainError for NaN" do
+ -> {
+ @nan.sqrt(1)
+ }.should.raise(FloatDomainError)
+ end
+
+ it "returns 0 for 0, +0.0 and -0.0" do
+ @zero.sqrt(1).should == 0
+ @zero_pos.sqrt(1).should == 0
+ @zero_neg.sqrt(1).should == 0
+ end
+
+end
diff --git a/spec/ruby/library/bigdecimal/sub_spec.rb b/spec/ruby/library/bigdecimal/sub_spec.rb
new file mode 100644
index 0000000000..3b62a0c794
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/sub_spec.rb
@@ -0,0 +1,62 @@
+require_relative '../../spec_helper'
+require 'bigdecimal'
+
+describe "BigDecimal#sub" do
+
+ before :each 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
+ # documentation states, that precision is optional
+ # but implementation raises ArgumentError if not given.
+
+ @two.sub(@one, 1).should == @one
+ @one.sub(@two, 1).should == @one_minus
+ @one.sub(@one_minus, 1).should == @two
+ @frac_2.sub(@frac_1, 1000000).should == BigDecimal("-0.1E-99999")
+ @frac_2.sub(@frac_1, 1).should == BigDecimal("-0.1E-99999")
+ # the above two examples puzzle me.
+ in_arow_one = BigDecimal("1.23456789")
+ in_arow_two = BigDecimal("1.2345678")
+ in_arow_one.sub(in_arow_two, 10).should == BigDecimal("0.9E-7")
+ @two.sub(@two,1).should == @zero
+ @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).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).should.nan?
+ @infinity_minus.sub(@infinity_minus, 1).should.nan?
+ end
+
+ it "returns Infinity or -Infinity if these are involved" do
+ @infinity.sub(@infinity_minus, 1).should == @infinity
+ @infinity_minus.sub(@infinity, 1).should == @infinity_minus
+ @zero.sub(@infinity, 1).should == @infinity_minus
+ @frac_2.sub( @infinity, 1).should == @infinity_minus
+ @two.sub(@infinity, 1).should == @infinity_minus
+ end
+
+end
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
new file mode 100644
index 0000000000..f5220d995a
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/to_f_spec.rb
@@ -0,0 +1,54 @@
+require_relative '../../spec_helper'
+require 'bigdecimal'
+
+describe "BigDecimal#to_f" do
+ before :each do
+ @one = BigDecimal("1")
+ @zero = BigDecimal("0")
+ @zero_pos = BigDecimal("+0")
+ @zero_neg = 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")
+ @vals = [@one, @zero, @two, @three, @frac_1, @frac_2]
+ @spec_vals = [@zero_pos, @zero_neg, @nan, @infinity, @infinity_minus]
+ end
+
+ it "returns number of type float" do
+ 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
+ bigdec = BigDecimal("3.141592653589793238462643383279502884197169399375")
+ bigdec.to_f.should be_close(3.14159265358979, TOLERANCE)
+ @one.to_f.should == 1.0
+ @two.to_f.should == 2.0
+ @three.to_f.should be_close(3.0, TOLERANCE)
+ @one_minus.to_f.should == -1.0
+
+ # regression test for [ruby-talk:338957]
+ BigDecimal("10.03").to_f.should == 10.03
+ end
+
+ it "properly handles special values" do
+ @zero.to_f.should == 0
+ @zero.to_f.to_s.should == "0.0"
+
+ @nan.to_f.should.nan?
+
+ @infinity.to_f.infinite?.should == 1
+ @infinity_minus.to_f.infinite?.should == -1
+ end
+
+ it "remembers negative zero when converted to float" do
+ @zero_neg.to_f.should == 0
+ @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
new file mode 100644
index 0000000000..e5e65c562e
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/to_i_spec.rb
@@ -0,0 +1,7 @@
+require_relative '../../spec_helper'
+require_relative 'shared/to_int'
+require 'bigdecimal'
+
+describe "BigDecimal#to_i" do
+ 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
new file mode 100644
index 0000000000..4df6749845
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/to_int_spec.rb
@@ -0,0 +1,8 @@
+require_relative '../../spec_helper'
+require_relative 'shared/to_int'
+require 'bigdecimal'
+
+
+describe "BigDecimal#to_int" do
+ 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
new file mode 100644
index 0000000000..a112c002ef
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/to_r_spec.rb
@@ -0,0 +1,28 @@
+require_relative '../../spec_helper'
+require 'bigdecimal'
+
+describe "BigDecimal#to_r" do
+
+ it "returns a Rational" do
+ BigDecimal("3.14159").to_r.should.is_a?(Rational)
+ end
+
+ it "returns a Rational with bignum values" do
+ 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
new file mode 100644
index 0000000000..5ec54765b5
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/to_s_spec.rb
@@ -0,0 +1,98 @@
+require_relative '../../spec_helper'
+require 'bigdecimal'
+
+describe "BigDecimal#to_s" do
+
+ before :each do
+ @bigdec_str = "3.14159265358979323846264338327950288419716939937"
+ @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
+ @bigdec.to_s.kind_of?(String).should == true
+ @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]*$/
+ 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
+ -> {@bigdec.to_s("F")}.should_not.raise()
+ end
+
+ it "starts with + if + is supplied and value is positive" do
+ @bigdec.to_s("+").should =~ /^\+.*/
+ @bigneg.to_s("+").should_not =~ /^\+.*/
+ end
+
+ 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
+ @bigdec.to_s(3).should =~ re
+
+ str1 = '-123.45678 90123 45678 9'
+ BigDecimal("-123.45678901234567890").to_s('5F').should == str1
+ # trailing zeroes removed
+ BigDecimal("1.00000000000").to_s('1F').should == "1.0"
+ # 0 is treated as no spaces
+ 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
+ @bigdec.to_s(" F").should =~ /\ .*/
+ @bigneg.to_s(" F").should_not =~ /\ .*/
+ end
+
+ it "removes trailing spaces in floating point notation" do
+ 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
+ @bigdec.to_s("E").should =~ /^0\.[0-9]*E[0-9]*$/i
+ end
+
+ it "can use conventional floating point notation" do
+ %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
+
+ 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
new file mode 100644
index 0000000000..cedc662aeb
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/truncate_spec.rb
@@ -0,0 +1,81 @@
+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')
+ end
+
+ it "returns value of type Integer." do
+ @arr.each do |x|
+ BigDecimal(x).truncate.kind_of?(Integer).should == true
+ end
+ end
+
+ it "returns the integer part as a BigDecimal if no precision given" do
+ BigDecimal(@arr[0]).truncate.should == 3
+ BigDecimal(@arr[1]).truncate.should == 8
+ BigDecimal(@arr[2]).truncate.should == 3
+ BigDecimal('0').truncate.should == 0
+ BigDecimal('0.1').truncate.should == 0
+ BigDecimal('-0.1').truncate.should == 0
+ BigDecimal('1.5').truncate.should == 1
+ BigDecimal('-1.5').truncate.should == -1
+ BigDecimal('1E10').truncate.should == BigDecimal('1E10')
+ BigDecimal('-1E10').truncate.should == BigDecimal('-1E10')
+ BigDecimal('1.8888E10').truncate.should == BigDecimal('1.8888E10')
+ BigDecimal('-1E-1').truncate.should == 0
+ end
+
+ it "returns value of given precision otherwise" do
+ BigDecimal('-1.55').truncate(1).should == BigDecimal('-1.5')
+ BigDecimal('1.55').truncate(1).should == BigDecimal('1.5')
+ BigDecimal(@arr[0]).truncate(2).should == BigDecimal("3.14")
+ BigDecimal('123.456').truncate(2).should == BigDecimal("123.45")
+ BigDecimal('123.456789').truncate(4).should == BigDecimal("123.4567")
+ BigDecimal('0.456789').truncate(10).should == BigDecimal("0.456789")
+ BigDecimal('-1E-1').truncate(1).should == BigDecimal('-0.1')
+ BigDecimal('-1E-1').truncate(2).should == BigDecimal('-0.1E0')
+ BigDecimal('-1E-1').truncate.should == BigDecimal('0')
+ BigDecimal('-1E-1').truncate(0).should == BigDecimal('0')
+ BigDecimal('-1E-1').truncate(-1).should == BigDecimal('0')
+ BigDecimal('-1E-1').truncate(-2).should == BigDecimal('0')
+
+ BigDecimal(@arr[1]).truncate(1).should == BigDecimal("8.7")
+ BigDecimal(@arr[2]).truncate(100).should == BigDecimal(\
+ "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679")
+ end
+
+ it "sets n digits left of the decimal point to 0, if given n < 0" do
+ @big.truncate(-1).should == BigDecimal("123450.0")
+ @big.truncate(-2).should == BigDecimal("123400.0")
+ BigDecimal(@arr[2]).truncate(-1).should == 0
+ end
+
+ it "returns NaN if self is NaN" do
+ @nan.truncate(-1).should.nan?
+ @nan.truncate(+1).should.nan?
+ @nan.truncate(0).should.nan?
+ end
+
+ it "returns Infinity if self is infinite" do
+ @infinity.truncate(-1).should == @infinity
+ @infinity.truncate(+1).should == @infinity
+ @infinity.truncate(0).should == @infinity
+
+ @infinity_negative.truncate(-1).should == @infinity_negative
+ @infinity_negative.truncate(+1).should == @infinity_negative
+ @infinity_negative.truncate(0).should == @infinity_negative
+ end
+
+ it "returns the same value if self is special value" do
+ -> { @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
new file mode 100644
index 0000000000..c780cdfac5
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/uminus_spec.rb
@@ -0,0 +1,58 @@
+require_relative '../../spec_helper'
+require 'bigdecimal'
+
+describe "BigDecimal#-@" do
+ before :each do
+ @one = BigDecimal("1")
+ @zero = BigDecimal("0")
+ @zero_pos = BigDecimal("+0")
+ @zero_neg = BigDecimal("-0")
+ @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")
+ @big = BigDecimal("333E99999")
+ @big_neg = BigDecimal("-333E99999")
+ @values = [@one, @zero, @zero_pos, @zero_neg, @infinity,
+ @infinity_minus, @one_minus, @frac_1, @frac_2, @big, @big_neg]
+ end
+
+ it "negates self" do
+ @one.send(:-@).should == @one_minus
+ @one_minus.send(:-@).should == @one
+ @frac_1.send(:-@).should == BigDecimal("-1E-99999")
+ @frac_2.send(:-@).should == BigDecimal("-0.9E-99999")
+ @big.send(:-@).should == @big_neg
+ @big_neg.send(:-@).should == @big
+ BigDecimal("2.221").send(:-@).should == BigDecimal("-2.221")
+ BigDecimal("2E10000").send(:-@).should == BigDecimal("-2E10000")
+ some_number = BigDecimal("2455999221.5512")
+ some_number_neg = BigDecimal("-2455999221.5512")
+ some_number.send(:-@).should == some_number_neg
+ (-BigDecimal("-5.5")).should == BigDecimal("5.5")
+ another_number = BigDecimal("-8.551551551551551551")
+ another_number_pos = BigDecimal("8.551551551551551551")
+ another_number.send(:-@).should == another_number_pos
+ @values.each do |val|
+ (val.send(:-@).send(:-@)).should == val
+ end
+ end
+
+ it "properly handles special values" do
+ @infinity.send(:-@).should == @infinity_minus
+ @infinity_minus.send(:-@).should == @infinity
+ @infinity.send(:-@).infinite?.should == -1
+ @infinity_minus.send(:-@).infinite?.should == 1
+
+ @zero.send(:-@).should == @zero
+ @zero.send(:-@).sign.should == -1
+ @zero_pos.send(:-@).should == @zero
+ @zero_pos.send(:-@).sign.should == -1
+ @zero_neg.send(:-@).should == @zero
+ @zero_neg.send(:-@).sign.should == 1
+
+ @nan.send(:-@).should.nan?
+ end
+end
diff --git a/spec/ruby/library/bigdecimal/uplus_spec.rb b/spec/ruby/library/bigdecimal/uplus_spec.rb
new file mode 100644
index 0000000000..77483046b7
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/uplus_spec.rb
@@ -0,0 +1,17 @@
+require_relative '../../spec_helper'
+require 'bigdecimal'
+
+describe "BigDecimal#+@" do
+ it "returns the same value with same sign (twos complement)" do
+ first = BigDecimal("34.56")
+ first.send(:+@).should == first
+ second = BigDecimal("-34.56")
+ second.send(:+@).should == second
+ third = BigDecimal("0.0")
+ third.send(:+@).should == third
+ fourth = BigDecimal("2E1000000")
+ fourth.send(:+@).should == fourth
+ fifth = BigDecimal("123456789E-1000000")
+ 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/zero_spec.rb b/spec/ruby/library/bigdecimal/zero_spec.rb
new file mode 100644
index 0000000000..2563210939
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/zero_spec.rb
@@ -0,0 +1,27 @@
+require_relative '../../spec_helper'
+require 'bigdecimal'
+
+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.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").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