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/abs_spec.rb50
-rw-r--r--spec/ruby/library/bigdecimal/add_spec.rb179
-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/coerce_spec.rb26
-rw-r--r--spec/ruby/library/bigdecimal/comparison_spec.rb81
-rw-r--r--spec/ruby/library/bigdecimal/div_spec.rb102
-rw-r--r--spec/ruby/library/bigdecimal/divide_spec.rb7
-rw-r--r--spec/ruby/library/bigdecimal/divmod_spec.rb180
-rw-r--r--spec/ruby/library/bigdecimal/double_fig_spec.rb9
-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.rb38
-rw-r--r--spec/ruby/library/bigdecimal/finite_spec.rb35
-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.rb98
-rw-r--r--spec/ruby/library/bigdecimal/gte_spec.rb102
-rw-r--r--spec/ruby/library/bigdecimal/infinite_spec.rb32
-rw-r--r--spec/ruby/library/bigdecimal/inspect_spec.rb47
-rw-r--r--spec/ruby/library/bigdecimal/limit_spec.rb30
-rw-r--r--spec/ruby/library/bigdecimal/lt_spec.rb96
-rw-r--r--spec/ruby/library/bigdecimal/lte_spec.rb102
-rw-r--r--spec/ruby/library/bigdecimal/minus_spec.rb58
-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.rb26
-rw-r--r--spec/ruby/library/bigdecimal/nan_spec.rb23
-rw-r--r--spec/ruby/library/bigdecimal/new_spec.rb109
-rw-r--r--spec/ruby/library/bigdecimal/nonzero_spec.rb29
-rw-r--r--spec/ruby/library/bigdecimal/plus_spec.rb47
-rw-r--r--spec/ruby/library/bigdecimal/power_spec.rb6
-rw-r--r--spec/ruby/library/bigdecimal/precs_spec.rb49
-rw-r--r--spec/ruby/library/bigdecimal/quo_spec.rb13
-rw-r--r--spec/ruby/library/bigdecimal/remainder_spec.rb84
-rw-r--r--spec/ruby/library/bigdecimal/round_spec.rb202
-rw-r--r--spec/ruby/library/bigdecimal/shared/eql.rb61
-rw-r--r--spec/ruby/library/bigdecimal/shared/modulo.rb116
-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.rb59
-rw-r--r--spec/ruby/library/bigdecimal/shared/to_int.rb16
-rw-r--r--spec/ruby/library/bigdecimal/sign_spec.rb47
-rw-r--r--spec/ruby/library/bigdecimal/split_spec.rb88
-rw-r--r--spec/ruby/library/bigdecimal/sqrt_spec.rb112
-rw-r--r--spec/ruby/library/bigdecimal/sub_spec.rb53
-rw-r--r--spec/ruby/library/bigdecimal/to_f_spec.rb55
-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.rb16
-rw-r--r--spec/ruby/library/bigdecimal/to_s_spec.rb73
-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.rb20
-rw-r--r--spec/ruby/library/bigdecimal/ver_spec.rb11
-rw-r--r--spec/ruby/library/bigdecimal/zero_spec.rb28
59 files changed, 3356 insertions, 0 deletions
diff --git a/spec/ruby/library/bigdecimal/abs_spec.rb b/spec/ruby/library/bigdecimal/abs_spec.rb
new file mode 100644
index 0000000000..9027abfb47
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/abs_spec.rb
@@ -0,0 +1,50 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+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.nan?.should == true # 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..6136c9dccb
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/add_spec.rb
@@ -0,0 +1,179 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../fixtures/classes', __FILE__)
+
+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 ist 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:
+# http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/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
+
+ 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).nan?.should == true
+ @nan.add(@one, 1).nan?.should == true
+ 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).nan?.should == true
+ @infinity_minus.add(@infinity, 10000).nan?.should == true
+ end
+
+ it "raises TypeError when adds nil" do
+ lambda {
+ @one.add(nil, 10)
+ }.should raise_error(TypeError)
+ lambda {
+ @one.add(nil, 0)
+ }.should raise_error(TypeError)
+ end
+
+ it "raises TypeError when precision parameter is nil" do
+ lambda {
+ @one.add(@one, nil)
+ }.should raise_error(TypeError)
+ end
+
+ it "raises ArgumentError when precision parameter is negative" do
+ lambda {
+ @one.add(@one, -10)
+ }.should raise_error(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..dcbde80e85
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/case_compare_spec.rb
@@ -0,0 +1,7 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../shared/eql.rb', __FILE__)
+
+
+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..d8829411b9
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/ceil_spec.rb
@@ -0,0 +1,104 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+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
+ lambda { @infinity.ceil }.should raise_error(FloatDomainError)
+ lambda { @infinity_neg.ceil }.should raise_error(FloatDomainError)
+ lambda { @nan.ceil }.should raise_error(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/coerce_spec.rb b/spec/ruby/library/bigdecimal/coerce_spec.rb
new file mode 100644
index 0000000000..1c63ddf2bc
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/coerce_spec.rb
@@ -0,0 +1,26 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+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..c4de8348b2
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/comparison_spec.rb
@@ -0,0 +1,81 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+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/div_spec.rb b/spec/ruby/library/bigdecimal/div_spec.rb
new file mode 100644
index 0000000000..f8f8ac4e5e
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/div_spec.rb
@@ -0,0 +1,102 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../shared/quo', __FILE__)
+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
+
+ it "raises FloatDomainError if NaN is involved" do
+ lambda { @one.div(@nan) }.should raise_error(FloatDomainError)
+ lambda { @nan.div(@one) }.should raise_error(FloatDomainError)
+ lambda { @nan.div(@nan) }.should raise_error(FloatDomainError)
+ 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
+ lambda { @one.div(@zero) }.should raise_error(ZeroDivisionError)
+ lambda { @one.div(@zero_plus) }.should raise_error(ZeroDivisionError)
+ lambda { @one.div(@zero_minus) }.should raise_error(ZeroDivisionError)
+
+ lambda { @zero.div(@zero) }.should raise_error(ZeroDivisionError)
+ lambda { @zero_minus.div(@zero_plus) }.should raise_error(ZeroDivisionError)
+ lambda { @zero_minus.div(@zero_minus) }.should raise_error(ZeroDivisionError)
+ lambda { @zero_plus.div(@zero_minus) }.should raise_error(ZeroDivisionError)
+ end
+
+ it "returns NaN if zero is divided by zero" do
+ @zero.div(@zero, 0).nan?.should == true
+ @zero_minus.div(@zero_plus, 0).nan?.should == true
+ @zero_plus.div(@zero_minus, 0).nan?.should == true
+
+ @zero.div(@zero, 10).nan?.should == true
+ @zero_minus.div(@zero_plus, 10).nan?.should == true
+ @zero_plus.div(@zero_minus, 10).nan?.should == true
+ end
+
+ it "raises FloatDomainError if (+|-) Infinity divided by 1 and no precision given" do
+ lambda { @infinity_minus.div(@one) }.should raise_error(FloatDomainError)
+ lambda { @infinity.div(@one) }.should raise_error(FloatDomainError)
+ lambda { @infinity_minus.div(@one_minus) }.should raise_error(FloatDomainError)
+ 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).nan?.should == true
+ @infinity_minus.div(@infinity, 1).nan?.should == true
+ 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..4dfe2702bb
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/divide_spec.rb
@@ -0,0 +1,7 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../shared/quo', __FILE__)
+require 'bigdecimal'
+
+describe "BigDecimal#/" do
+ it_behaves_like :bigdecimal_quo, :/, []
+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..839a289ab6
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/divmod_spec.rb
@@ -0,0 +1,180 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../shared/modulo', __FILE__)
+require 'bigdecimal'
+
+module DivmodSpecs
+ def self.check_both_nan(array)
+ array.length.should == 2
+ array[0].nan?.should == true
+ array[1].nan?.should == true
+ 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
+
+ it_behaves_like :bigdecimal_modulo, :mod_part_of_divmod
+
+ it "raises ZeroDivisionError if other is zero" do
+ bd5667 = BigDecimal.new("5667.19")
+
+ lambda { bd5667.send(@method, 0) }.should raise_error(ZeroDivisionError)
+ lambda { bd5667.send(@method, BigDecimal("0")) }.should raise_error(ZeroDivisionError)
+ lambda { @zero.send(@method, @zero) }.should raise_error(ZeroDivisionError)
+ 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
+
+ it "divides value, returns an array" do
+ res = @a.divmod(5)
+ res.kind_of?(Array).should == true
+ end
+
+ it "array contains quotient and modulus as BigDecimal" do
+ res = @a.divmod(5)
+ DivmodSpecs.check_both_bigdecimal(res)
+ res[0].should == BigDecimal('0.8E1')
+ res[1].should == BigDecimal('2.00000000000000000001')
+
+ 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.new("42")
+ b = BigDecimal.new("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)
+ DivmodSpecs.check_both_bigdecimal(res)
+ res[0].should == ((val1/val2).floor)
+ res[1].should == (val1 - res[0] * val2)
+ end
+ end
+ end
+
+ it "returns an array of two NaNs if NaN is involved" do
+ (@special_vals + @regular_vals + @zeroes).each do |val|
+ DivmodSpecs.check_both_nan(val.divmod(@nan))
+ DivmodSpecs.check_both_nan(@nan.divmod(val))
+ end
+ end
+
+ it "raises ZeroDivisionError if the divisor is zero" do
+ (@special_vals + @regular_vals + @zeroes - [@nan]).each do |val|
+ @zeroes.each do |zero|
+ lambda { val.divmod(zero) }.should raise_error(ZeroDivisionError)
+ end
+ end
+ end
+
+ it "returns an array of Infinity and NaN if the dividend is Infinity" do
+ @regular_vals.each do |val|
+ array = @infinity.divmod(val)
+ array.length.should == 2
+ array[0].infinite?.should == (val > 0 ? 1 : -1)
+ array[1].nan?.should == true
+ end
+ end
+
+ it "returns an array of zero and the dividend if the divisor is Infinity" do
+ @regular_vals.each do |val|
+ array = val.divmod(@infinity)
+ array.length.should == 2
+ array[0].should == @zero
+ array[1].should == val
+ end
+ end
+
+ it "returns an array of two zero if the diviend 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
+ lambda {
+ @one.divmod('1')
+ }.should raise_error(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..3ab5c2f207
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/double_fig_spec.rb
@@ -0,0 +1,9 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+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/eql_spec.rb b/spec/ruby/library/bigdecimal/eql_spec.rb
new file mode 100644
index 0000000000..f3f525a7ba
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/eql_spec.rb
@@ -0,0 +1,6 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../shared/eql.rb', __FILE__)
+
+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..bd07217b98
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/equal_value_spec.rb
@@ -0,0 +1,7 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../shared/eql.rb', __FILE__)
+
+
+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..6bb678d4ed
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/exponent_spec.rb
@@ -0,0 +1,38 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../shared/power', __FILE__)
+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
+
+# commenting this spec out after discussion with Defiler, since it seems to be an MRI bug, not a real feature
+=begin
+ platform_is wordsize: 32 do
+ # TODO: write specs for both 32 and 64 bit
+ it "returns 0 if exponent can't be represented as Fixnum" do
+ BigDecimal("2E1000000000000000").exponent.should == 0
+ BigDecimal("-5E-999999999999999").exponent.should == 0
+ end
+ end
+=end
+
+ it "returns 0 if self is 0" do
+ BigDecimal("0").exponent.should == 0
+ BigDecimal("+0").exponent.should == 0
+ 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..6d6d8398fa
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/finite_spec.rb
@@ -0,0 +1,35 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+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.finite?.should == false
+ @infinity_minus.finite?.should == false
+ @nan.finite?.should == false
+ end
+
+ it "returns true for finite values" do
+ @finite_vals.each do |val|
+ val.finite?.should == true
+ 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..0a4a98c241
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/fix_spec.rb
@@ -0,0 +1,57 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+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.nan?.should == true
+ 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
+ lambda {
+ @mixed.fix(10)
+ }.should raise_error(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..0bae6e4ab0
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/floor_spec.rb
@@ -0,0 +1,100 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+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
+ lambda { @infinity.floor }.should raise_error(FloatDomainError)
+ lambda { @infinity_neg.floor }.should raise_error(FloatDomainError)
+ lambda { @nan.floor }.should raise_error(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..9a727a70dd
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/frac_spec.rb
@@ -0,0 +1,48 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+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.nan?.should == true
+ 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..2f9ea4fd68
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/gt_spec.rb
@@ -0,0 +1,98 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+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
+
+ ruby_bug "#13674", ""..."2.4" do
+ it "properly handles Float infinity values" do
+ @values.each { |val|
+ (val > @float_infinity).should == false
+ (@float_infinity > val).should == true
+ (val > @float_infinity_neg).should == true
+ (@float_infinity_neg > val).should == false
+ }
+ end
+ 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
+ lambda {@zero > nil }.should raise_error(ArgumentError)
+ lambda {@infinity > nil }.should raise_error(ArgumentError)
+ lambda {@infinity_neg > nil }.should raise_error(ArgumentError)
+ lambda {@mixed > nil }.should raise_error(ArgumentError)
+ lambda {@pos_int > nil }.should raise_error(ArgumentError)
+ lambda {@neg_frac > nil }.should raise_error(ArgumentError)
+ 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..aab5338ad6
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/gte_spec.rb
@@ -0,0 +1,102 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+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
+
+ ruby_bug "#13674", ""..."2.4" do
+ it "properly handles Float infinity values" do
+ @values.each { |val|
+ (val >= @float_infinity).should == false
+ (@float_infinity >= val).should == true
+ (val >= @float_infinity_neg).should == true
+ (@float_infinity_neg >= val).should == false
+ }
+ end
+ 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
+ lambda {@zero >= nil }.should raise_error(ArgumentError)
+ lambda {@infinity >= nil }.should raise_error(ArgumentError)
+ lambda {@infinity_neg >= nil }.should raise_error(ArgumentError)
+ lambda {@mixed >= nil }.should raise_error(ArgumentError)
+ lambda {@pos_int >= nil }.should raise_error(ArgumentError)
+ lambda {@neg_frac >= nil }.should raise_error(ArgumentError)
+ 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..b218ee371c
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/infinite_spec.rb
@@ -0,0 +1,32 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+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..9831b2be8e
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/inspect_spec.rb
@@ -0,0 +1,47 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require 'bigdecimal'
+
+describe "BigDecimal#inspect" do
+
+ before :each do
+ @bigdec = BigDecimal.new("1234.5678")
+ end
+
+ it "returns String" do
+ @bigdec.inspect.kind_of?(String).should == true
+ end
+
+ ruby_version_is ""..."2.4" do
+ it "returns String starting with #" do
+ @bigdec.inspect[0].should == ?#
+ end
+
+ it "encloses information in angle brackets" do
+ @bigdec.inspect.should =~ /^.<.*>$/
+ end
+
+ it "is comma separated list of three items" do
+ @bigdec.inspect.should =~ /...*,.*,.*/
+ end
+
+ it "value after first comma is value as string" do
+ @bigdec.inspect.split(",")[1].should == "\'0.12345678E4\'"
+ end
+
+ it "last part is number of significant digits" do
+ signific_string = "#{@bigdec.precs[0]}(#{@bigdec.precs[1]})"
+ @bigdec.inspect.split(",")[2].should == signific_string + ">"
+ end
+
+ it "looks like this" do
+ regex = /^\#\<BigDecimal\:.*,'0\.12345678E4',[0-9]+\([0-9]+\)>$/
+ @bigdec.inspect.should =~ regex
+ end
+ end
+
+ ruby_version_is "2.4" do
+ it "looks like this" do
+ @bigdec.inspect.should == "0.12345678e4"
+ end
+ 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..41308abcd8
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/limit_spec.rb
@@ -0,0 +1,30 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../fixtures/classes', __FILE__)
+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 "use 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('3')).should == BigDecimal('2.664')
+ end
+
+ BigDecimalSpecs.with_limit(1) do
+ (BigDecimal('0.888') + BigDecimal('0')).should == BigDecimal('0.9')
+ (BigDecimal('0.888') * BigDecimal('3')).should == BigDecimal('3')
+ end
+
+ BigDecimalSpecs.with_limit(2) do
+ (BigDecimal('0.888') + BigDecimal('0')).should == BigDecimal('0.89')
+ (BigDecimal('0.888') * BigDecimal('3')).should == BigDecimal('2.7')
+ 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..089e7aef73
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/lt_spec.rb
@@ -0,0 +1,96 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+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
+
+ ruby_bug "#13674", ""..."2.4" do
+ it "properly handles Float infinity values" do
+ @values.each { |val|
+ (val < @float_infinity).should == true
+ (@float_infinity < val).should == false
+ (val < @float_infinity_neg).should == false
+ (@float_infinity_neg < val).should == true
+ }
+ end
+ 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
+ lambda {@zero < nil }.should raise_error(ArgumentError)
+ lambda {@infinity < nil }.should raise_error(ArgumentError)
+ lambda {@infinity_neg < nil }.should raise_error(ArgumentError)
+ lambda {@mixed < nil }.should raise_error(ArgumentError)
+ lambda {@pos_int < nil }.should raise_error(ArgumentError)
+ lambda {@neg_frac < nil }.should raise_error(ArgumentError)
+ 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..5cda9842bd
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/lte_spec.rb
@@ -0,0 +1,102 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+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
+
+ ruby_bug "#13674", ""..."2.4" do
+ it "properly handles Float infinity values" do
+ @values.each { |val|
+ (val <= @float_infinity).should == true
+ (@float_infinity <= val).should == false
+ (val <= @float_infinity_neg).should == false
+ (@float_infinity_neg <= val).should == true
+ }
+ end
+ 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
+ lambda {@zero <= nil }.should raise_error(ArgumentError)
+ lambda {@infinity <= nil }.should raise_error(ArgumentError)
+ lambda {@infinity_neg <= nil }.should raise_error(ArgumentError)
+ lambda {@mixed <= nil }.should raise_error(ArgumentError)
+ lambda {@pos_int <= nil }.should raise_error(ArgumentError)
+ lambda {@neg_frac <= nil }.should raise_error(ArgumentError)
+ 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..bdd8478465
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/minus_spec.rb
@@ -0,0 +1,58 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+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).nan?.should == true
+ (@nan - @one).nan?.should == true
+ (@nan - @nan).nan?.should == true
+ (@nan - @infinity).nan?.should == true
+ (@nan - @infinity_minus).nan?.should == true
+ (@infinity - @nan).nan?.should == true
+ (@infinity_minus - @nan).nan?.should == true
+ end
+
+ it "returns NaN both operands are infinite with the same sign" do
+ (@infinity - @infinity).nan?.should == true
+ (@infinity_minus - @infinity_minus).nan?.should == true
+ 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
+
+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..d88ac61aaf
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/mode_spec.rb
@@ -0,0 +1,36 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+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).nan?.should == true
+ 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)
+ lambda { BigDecimal("NaN").add(BigDecimal("1"),0) }.should raise_error(FloatDomainError)
+ BigDecimal.mode(BigDecimal::EXCEPTION_INFINITY, true)
+ lambda { BigDecimal("0").add(BigDecimal("Infinity"),0) }.should raise_error(FloatDomainError)
+ BigDecimal.mode(BigDecimal::EXCEPTION_ZERODIVIDE, true)
+ lambda { BigDecimal("1").quo(BigDecimal("0")) }.should raise_error(FloatDomainError)
+ BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW, true)
+ lambda { BigDecimal("1E11111111111111111111") }.should raise_error(FloatDomainError)
+ lambda { (BigDecimal("1E1000000000000000000")**10) }.should raise_error(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..6feeb685eb
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/modulo_spec.rb
@@ -0,0 +1,12 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../shared/modulo', __FILE__)
+
+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..a140cf899a
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/mult_spec.rb
@@ -0,0 +1,24 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../shared/mult', __FILE__)
+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"
+ @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..842e22eaa2
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/multiply_spec.rb
@@ -0,0 +1,26 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../shared/mult', __FILE__)
+require 'bigdecimal'
+
+describe "BigDecimal#*" do
+ it_behaves_like :bigdecimal_mult, :*, []
+end
+
+describe "BigDecimal#*" do
+ before :each do
+ @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
+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..5c291629b3
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/nan_spec.rb
@@ -0,0 +1,23 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require 'bigdecimal'
+
+describe "BigDecimal#nan?" do
+
+ it "returns true if self is not a number" do
+ BigDecimal("NaN").nan?.should == true
+ end
+
+ it "returns false if self is not a NaN" do
+ BigDecimal("Infinity").nan?.should == false
+ BigDecimal("-Infinity").nan?.should == false
+ BigDecimal("0").nan?.should == false
+ BigDecimal("+0").nan?.should == false
+ BigDecimal("-0").nan?.should == false
+ BigDecimal("2E40001").nan?.should == false
+ BigDecimal("3E-20001").nan?.should == false
+ BigDecimal("0E-200000000").nan?.should == false
+ BigDecimal("0E200000000000").nan?.should == false
+ BigDecimal("0.000000000000000000000000").nan?.should == false
+ end
+
+end
diff --git a/spec/ruby/library/bigdecimal/new_spec.rb b/spec/ruby/library/bigdecimal/new_spec.rb
new file mode 100644
index 0000000000..de3aa195ca
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/new_spec.rb
@@ -0,0 +1,109 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require 'bigdecimal'
+
+describe "BigDecimal.new" do
+
+ it "creates a new object of class BigDecimal" do
+ BigDecimal.new("3.14159").should be_kind_of(BigDecimal)
+ (0..9).each {|i|
+ BigDecimal.new("1#{i}").should == 10 + i
+ BigDecimal.new("-1#{i}").should == -10 - i
+ BigDecimal.new("1E#{i}").should == 10**i
+ BigDecimal.new("1000000E-#{i}").should == 10**(6-i).to_f
+ # ^ to_f to avoid Rational type
+ }
+ (1..9).each {|i|
+ BigDecimal.new("100.#{i}").to_s.should =~ /\A0\.100#{i}E3\z/i
+ BigDecimal.new("-100.#{i}").to_s.should =~ /\A-0\.100#{i}E3\z/i
+ }
+ end
+
+ it "accepts significant digits >= given precision" do
+ BigDecimal.new("3.1415923", 10).precs[1].should >= 10
+ end
+
+ it "determines precision from initial value" do
+ pi_string = "3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593014782083152134043"
+ BigDecimal.new(pi_string).precs[1].should >= pi_string.size-1
+ end
+
+ it "ignores leading whitespace" do
+ BigDecimal.new(" \t\n \r1234").should == BigDecimal.new("1234")
+ BigDecimal.new(" \t\n \rNaN \n").nan?.should == true
+ BigDecimal.new(" \t\n \rInfinity \n").infinite?.should == 1
+ BigDecimal.new(" \t\n \r-Infinity \n").infinite?.should == -1
+ end
+
+ it "ignores trailing garbage" do
+ BigDecimal.new("123E45ruby").should == BigDecimal.new("123E45")
+ BigDecimal.new("123x45").should == BigDecimal.new("123")
+ BigDecimal.new("123.4%E5").should == BigDecimal.new("123.4")
+ BigDecimal.new("1E2E3E4E5E").should == BigDecimal.new("100")
+ end
+
+ ruby_version_is ""..."2.4" do
+ it "treats invalid strings as 0.0" do
+ BigDecimal.new("ruby").should == BigDecimal.new("0.0")
+ BigDecimal.new(" \t\n \r-\t\t\tInfinity \n").should == BigDecimal.new("0.0")
+ end
+ end
+
+ ruby_version_is "2.4" do
+ it "raises ArgumentError for invalid strings" do
+ lambda { BigDecimal.new("ruby") }.should raise_error(ArgumentError)
+ lambda { BigDecimal.new(" \t\n \r-\t\t\tInfinity \n") }.should raise_error(ArgumentError)
+ end
+ end
+
+ it "allows omitting the integer part" do
+ BigDecimal.new(".123").should == BigDecimal.new("0.123")
+ end
+
+ it "allows for underscores in all parts" do
+ reference = BigDecimal.new("12345.67E89")
+
+ BigDecimal.new("12_345.67E89").should == reference
+ BigDecimal.new("1_2_3_4_5_._6____7_E89").should == reference
+ BigDecimal.new("12345_.67E_8__9_").should == reference
+ end
+
+ it "accepts NaN and [+-]Infinity" do
+ BigDecimal.new("NaN").nan?.should == true
+
+ pos_inf = BigDecimal.new("Infinity")
+ pos_inf.finite?.should == false
+ pos_inf.should > 0
+ pos_inf.should == BigDecimal.new("+Infinity")
+
+ neg_inf = BigDecimal.new("-Infinity")
+ neg_inf.finite?.should == false
+ neg_inf.should < 0
+ end
+
+ it "allows for [eEdD] as exponent separator" do
+ reference = BigDecimal.new("12345.67E89")
+
+ BigDecimal.new("12345.67e89").should == reference
+ BigDecimal.new("12345.67E89").should == reference
+ BigDecimal.new("12345.67d89").should == reference
+ BigDecimal.new("12345.67D89").should == reference
+ end
+
+ it "allows for varying signs" do
+ reference = BigDecimal.new("123.456E1")
+
+ BigDecimal.new("+123.456E1").should == reference
+ BigDecimal.new("-123.456E1").should == -reference
+ BigDecimal.new("123.456E+1").should == reference
+ BigDecimal.new("12345.6E-1").should == reference
+ BigDecimal.new("+123.456E+1").should == reference
+ BigDecimal.new("+12345.6E-1").should == reference
+ BigDecimal.new("-123.456E+1").should == -reference
+ BigDecimal.new("-12345.6E-1").should == -reference
+ end
+
+ it 'raises ArgumentError when Float is used without precision' do
+ lambda { BigDecimal(1.0) }.should raise_error(ArgumentError)
+ end
+
+end
diff --git a/spec/ruby/library/bigdecimal/nonzero_spec.rb b/spec/ruby/library/bigdecimal/nonzero_spec.rb
new file mode 100644
index 0000000000..336c72a6b2
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/nonzero_spec.rb
@@ -0,0 +1,29 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+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..72ec282075
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/plus_spec.rb
@@ -0,0 +1,47 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+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).nan?.should == true
+ (@nan + @one).nan?.should == true
+ 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).nan?.should == true
+ 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..f2b1a9b5c8
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/power_spec.rb
@@ -0,0 +1,6 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../shared/power', __FILE__)
+
+describe "BigDecimal#power" do
+ it_behaves_like(:bigdecimal_power, :power)
+end
diff --git a/spec/ruby/library/bigdecimal/precs_spec.rb b/spec/ruby/library/bigdecimal/precs_spec.rb
new file mode 100644
index 0000000000..c7700a3819
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/precs_spec.rb
@@ -0,0 +1,49 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require 'bigdecimal'
+
+describe "BigDecimal#precs" do
+
+ before :each do
+ @infinity = BigDecimal("Infinity")
+ @infinity_neg = BigDecimal("-Infinity")
+ @nan = BigDecimal("NaN")
+ @zero = BigDecimal("0")
+ @zero_neg = BigDecimal("-0")
+
+ @arr = [BigDecimal("2E40001"), BigDecimal("3E-20001"),\
+ @infinity, @infinity_neg, @nan, @zero, @zero_neg]
+ @precision = BigDecimal::BASE.to_s.length - 1
+ end
+
+ it "returns array of two values" do
+ @arr.each do |x|
+ x.precs.kind_of?(Array).should == true
+ x.precs.size.should == 2
+ end
+ end
+
+ it "returns Integers as array values" do
+ @arr.each do |x|
+ x.precs[0].kind_of?(Integer).should == true
+ x.precs[1].kind_of?(Integer).should == true
+ end
+ end
+
+ it "returns the current value of significant digits as the first value" do
+ BigDecimal("3.14159").precs[0].should >= 6
+ BigDecimal('1').precs[0].should == BigDecimal('1' + '0' * 100).precs[0]
+ [@infinity, @infinity_neg, @nan, @zero, @zero_neg].each do |value|
+ value.precs[0].should <= @precision
+ end
+ end
+
+ it "returns the maximum number of significant digits as the second value" do
+ BigDecimal("3.14159").precs[1].should >= 6
+ BigDecimal('1').precs[1].should >= 1
+ BigDecimal('1' + '0' * 100).precs[1] >= 101
+ [@infinity, @infinity_neg, @nan, @zero, @zero_neg].each do |value|
+ value.precs[1].should >= 1
+ end
+ end
+end
+
diff --git a/spec/ruby/library/bigdecimal/quo_spec.rb b/spec/ruby/library/bigdecimal/quo_spec.rb
new file mode 100644
index 0000000000..bc27fd0f5e
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/quo_spec.rb
@@ -0,0 +1,13 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../shared/quo', __FILE__)
+require 'bigdecimal'
+
+describe "BigDecimal#quo" do
+ it_behaves_like :bigdecimal_quo, :quo, []
+
+ it "returns NaN if NaN is involved" do
+ BigDecimal("1").quo(BigDecimal("NaN")).nan?.should == true
+ BigDecimal("NaN").quo(BigDecimal("1")).nan?.should == true
+ 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..796522c5c8
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/remainder_spec.rb
@@ -0,0 +1,84 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require 'bigdecimal'
+
+describe "BigDecimal#remainder" do
+
+ before :each do
+ @zero = BigDecimal("0")
+ @one = BigDecimal("0")
+ @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
+
+ it "returns NaN used with zero" do
+ @mixed.remainder(@zero).nan?.should == true
+ @zero.remainder(@zero).nan?.should == true
+ 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).nan?.should == true
+ @nan.remainder(@one).nan?.should == true
+ @one.remainder(@nan).nan?.should == true
+ @infinity.remainder(@nan).nan?.should == true
+ @nan.remainder(@infinity).nan?.should == true
+ end
+
+ it "returns NaN if Infinity is involved" do
+ @infinity.remainder(@infinity).nan?.should == true
+ @infinity.remainder(@one).nan?.should == true
+ @infinity.remainder(@mixed).nan?.should == true
+ @infinity.remainder(@one_minus).nan?.should == true
+ @infinity.remainder(@frac_1).nan?.should == true
+ @one.remainder(@infinity).nan?.should == true
+
+ @infinity_minus.remainder(@infinity_minus).nan?.should == true
+ @infinity_minus.remainder(@one).nan?.should == true
+ @one.remainder(@infinity_minus).nan?.should == true
+ @frac_2.remainder(@infinity_minus).nan?.should == true
+
+ @infinity.remainder(@infinity_minus).nan?.should == true
+ @infinity_minus.remainder(@infinity).nan?.should == true
+ end
+
+ it "coerces arguments to BigDecimal if possible" do
+ @one.remainder(2).should == @one
+ end
+
+
+ it "raises TypeError if the argument cannot be coerced to BigDecimal" do
+ lambda {
+ @one.remainder('2')
+ }.should raise_error(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..6c1987c5d8
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/round_spec.rb
@@ -0,0 +1,202 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+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
+
+ describe "BigDecimal::ROUND_UP" do
+ it "rounds values away from zero" do
+ @p1_50.round(0, BigDecimal::ROUND_UP).should == @two
+ @p1_51.round(0, BigDecimal::ROUND_UP).should == @two
+ @p1_49.round(0, BigDecimal::ROUND_UP).should == @two
+ @n1_50.round(0, BigDecimal::ROUND_UP).should == @neg_two
+ @n1_51.round(0, BigDecimal::ROUND_UP).should == @neg_two
+ @n1_49.round(0, BigDecimal::ROUND_UP).should == @neg_two
+
+ @p2_50.round(0, BigDecimal::ROUND_UP).should == @three
+ @p2_51.round(0, BigDecimal::ROUND_UP).should == @three
+ @p2_49.round(0, BigDecimal::ROUND_UP).should == @three
+ @n2_50.round(0, BigDecimal::ROUND_UP).should == @neg_three
+ @n2_51.round(0, BigDecimal::ROUND_UP).should == @neg_three
+ @n2_49.round(0, BigDecimal::ROUND_UP).should == @neg_three
+ end
+ end
+
+ describe "BigDecimal::ROUND_DOWN" do
+ it "rounds values towards zero" do
+ @p1_50.round(0, BigDecimal::ROUND_DOWN).should == @one
+ @p1_51.round(0, BigDecimal::ROUND_DOWN).should == @one
+ @p1_49.round(0, BigDecimal::ROUND_DOWN).should == @one
+ @n1_50.round(0, BigDecimal::ROUND_DOWN).should == @neg_one
+ @n1_51.round(0, BigDecimal::ROUND_DOWN).should == @neg_one
+ @n1_49.round(0, BigDecimal::ROUND_DOWN).should == @neg_one
+
+ @p2_50.round(0, BigDecimal::ROUND_DOWN).should == @two
+ @p2_51.round(0, BigDecimal::ROUND_DOWN).should == @two
+ @p2_49.round(0, BigDecimal::ROUND_DOWN).should == @two
+ @n2_50.round(0, BigDecimal::ROUND_DOWN).should == @neg_two
+ @n2_51.round(0, BigDecimal::ROUND_DOWN).should == @neg_two
+ @n2_49.round(0, BigDecimal::ROUND_DOWN).should == @neg_two
+ end
+ end
+
+ describe "BigDecimal::ROUND_HALF_UP" do
+ it "rounds values >= 5 up, otherwise down" do
+ @p1_50.round(0, BigDecimal::ROUND_HALF_UP).should == @two
+ @p1_51.round(0, BigDecimal::ROUND_HALF_UP).should == @two
+ @p1_49.round(0, BigDecimal::ROUND_HALF_UP).should == @one
+ @n1_50.round(0, BigDecimal::ROUND_HALF_UP).should == @neg_two
+ @n1_51.round(0, BigDecimal::ROUND_HALF_UP).should == @neg_two
+ @n1_49.round(0, BigDecimal::ROUND_HALF_UP).should == @neg_one
+
+ @p2_50.round(0, BigDecimal::ROUND_HALF_UP).should == @three
+ @p2_51.round(0, BigDecimal::ROUND_HALF_UP).should == @three
+ @p2_49.round(0, BigDecimal::ROUND_HALF_UP).should == @two
+ @n2_50.round(0, BigDecimal::ROUND_HALF_UP).should == @neg_three
+ @n2_51.round(0, BigDecimal::ROUND_HALF_UP).should == @neg_three
+ @n2_49.round(0, BigDecimal::ROUND_HALF_UP).should == @neg_two
+ end
+ end
+
+ describe "BigDecimal::ROUND_HALF_DOWN" do
+ it "rounds values > 5 up, otherwise down" do
+ @p1_50.round(0, BigDecimal::ROUND_HALF_DOWN).should == @one
+ @p1_51.round(0, BigDecimal::ROUND_HALF_DOWN).should == @two
+ @p1_49.round(0, BigDecimal::ROUND_HALF_DOWN).should == @one
+ @n1_50.round(0, BigDecimal::ROUND_HALF_DOWN).should == @neg_one
+ @n1_51.round(0, BigDecimal::ROUND_HALF_DOWN).should == @neg_two
+ @n1_49.round(0, BigDecimal::ROUND_HALF_DOWN).should == @neg_one
+
+ @p2_50.round(0, BigDecimal::ROUND_HALF_DOWN).should == @two
+ @p2_51.round(0, BigDecimal::ROUND_HALF_DOWN).should == @three
+ @p2_49.round(0, BigDecimal::ROUND_HALF_DOWN).should == @two
+ @n2_50.round(0, BigDecimal::ROUND_HALF_DOWN).should == @neg_two
+ @n2_51.round(0, BigDecimal::ROUND_HALF_DOWN).should == @neg_three
+ @n2_49.round(0, BigDecimal::ROUND_HALF_DOWN).should == @neg_two
+ end
+ end
+
+ describe "BigDecimal::ROUND_CEILING" do
+ it "rounds values towards +infinity" do
+ @p1_50.round(0, BigDecimal::ROUND_CEILING).should == @two
+ @p1_51.round(0, BigDecimal::ROUND_CEILING).should == @two
+ @p1_49.round(0, BigDecimal::ROUND_CEILING).should == @two
+ @n1_50.round(0, BigDecimal::ROUND_CEILING).should == @neg_one
+ @n1_51.round(0, BigDecimal::ROUND_CEILING).should == @neg_one
+ @n1_49.round(0, BigDecimal::ROUND_CEILING).should == @neg_one
+
+ @p2_50.round(0, BigDecimal::ROUND_CEILING).should == @three
+ @p2_51.round(0, BigDecimal::ROUND_CEILING).should == @three
+ @p2_49.round(0, BigDecimal::ROUND_CEILING).should == @three
+ @n2_50.round(0, BigDecimal::ROUND_CEILING).should == @neg_two
+ @n2_51.round(0, BigDecimal::ROUND_CEILING).should == @neg_two
+ @n2_49.round(0, BigDecimal::ROUND_CEILING).should == @neg_two
+ end
+ end
+
+ describe "BigDecimal::ROUND_FLOOR" do
+ it "rounds values towards -infinity" do
+ @p1_50.round(0, BigDecimal::ROUND_FLOOR).should == @one
+ @p1_51.round(0, BigDecimal::ROUND_FLOOR).should == @one
+ @p1_49.round(0, BigDecimal::ROUND_FLOOR).should == @one
+ @n1_50.round(0, BigDecimal::ROUND_FLOOR).should == @neg_two
+ @n1_51.round(0, BigDecimal::ROUND_FLOOR).should == @neg_two
+ @n1_49.round(0, BigDecimal::ROUND_FLOOR).should == @neg_two
+
+ @p2_50.round(0, BigDecimal::ROUND_FLOOR).should == @two
+ @p2_51.round(0, BigDecimal::ROUND_FLOOR).should == @two
+ @p2_49.round(0, BigDecimal::ROUND_FLOOR).should == @two
+ @n2_50.round(0, BigDecimal::ROUND_FLOOR).should == @neg_three
+ @n2_51.round(0, BigDecimal::ROUND_FLOOR).should == @neg_three
+ @n2_49.round(0, BigDecimal::ROUND_FLOOR).should == @neg_three
+ end
+ end
+
+ describe "BigDecimal::ROUND_HALF_EVEN" do
+ it "rounds values > 5 up, < 5 down and == 5 towards even neighbor" do
+ @p1_50.round(0, BigDecimal::ROUND_HALF_EVEN).should == @two
+ @p1_51.round(0, BigDecimal::ROUND_HALF_EVEN).should == @two
+ @p1_49.round(0, BigDecimal::ROUND_HALF_EVEN).should == @one
+ @n1_50.round(0, BigDecimal::ROUND_HALF_EVEN).should == @neg_two
+ @n1_51.round(0, BigDecimal::ROUND_HALF_EVEN).should == @neg_two
+ @n1_49.round(0, BigDecimal::ROUND_HALF_EVEN).should == @neg_one
+
+ @p2_50.round(0, BigDecimal::ROUND_HALF_EVEN).should == @two
+ @p2_51.round(0, BigDecimal::ROUND_HALF_EVEN).should == @three
+ @p2_49.round(0, BigDecimal::ROUND_HALF_EVEN).should == @two
+ @n2_50.round(0, BigDecimal::ROUND_HALF_EVEN).should == @neg_two
+ @n2_51.round(0, BigDecimal::ROUND_HALF_EVEN).should == @neg_three
+ @n2_49.round(0, BigDecimal::ROUND_HALF_EVEN).should == @neg_two
+ end
+ end
+
+ it 'raise exception, if self is special value' do
+ lambda { BigDecimal('NaN').round }.should raise_error(FloatDomainError)
+ lambda { BigDecimal('Infinity').round }.should raise_error(FloatDomainError)
+ lambda { BigDecimal('-Infinity').round }.should raise_error(FloatDomainError)
+ end
+
+ it 'do not raise exception, if self is special value and precision is given' do
+ lambda { BigDecimal('NaN').round(2) }.should_not raise_error(FloatDomainError)
+ lambda { BigDecimal('Infinity').round(2) }.should_not raise_error(FloatDomainError)
+ lambda { BigDecimal('-Infinity').round(2) }.should_not raise_error(FloatDomainError)
+ 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..eaad272e9a
--- /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.new("6543.21")
+ @bg5667_19 = BigDecimal.new("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..78ebe8360d
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/shared/modulo.rb
@@ -0,0 +1,116 @@
+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.new("6543.21")
+ bd5667 = BigDecimal.new("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
+
+ it "returns NaN if NaN is involved" do
+ @nan.send(@method, @nan).nan?.should == true
+ @nan.send(@method, @one).nan?.should == true
+ @one.send(@method, @nan).nan?.should == true
+ @infinity.send(@method, @nan).nan?.should == true
+ @nan.send(@method, @infinity).nan?.should == true
+ end
+
+ it "returns NaN if the dividend is Infinity" do
+ @infinity.send(@method, @infinity).nan?.should == true
+ @infinity.send(@method, @one).nan?.should == true
+ @infinity.send(@method, @mixed).nan?.should == true
+ @infinity.send(@method, @one_minus).nan?.should == true
+ @infinity.send(@method, @frac_1).nan?.should == true
+
+ @infinity_minus.send(@method, @infinity_minus).nan?.should == true
+ @infinity_minus.send(@method, @one).nan?.should == true
+
+ @infinity.send(@method, @infinity_minus).nan?.should == true
+ @infinity_minus.send(@method, @infinity).nan?.should == true
+ end
+
+ it "returns the dividend if the divisor is Infinity" do
+ @one.send(@method, @infinity).should == @one
+ @one.send(@method, @infinity_minus).should == @one
+ @frac_2.send(@method, @infinity_minus).should == @frac_2
+ end
+
+ it "raises TypeError if the argument cannot be coerced to BigDecimal" do
+ lambda {
+ @one.send(@method, '2')
+ }.should raise_error(TypeError)
+ end
+end
+
+describe :bigdecimal_modulo_zerodivisionerror, shared: true do
+ it "raises ZeroDivisionError if other is zero" do
+ bd5667 = BigDecimal.new("5667.19")
+
+ lambda { bd5667.send(@method, 0) }.should raise_error(ZeroDivisionError)
+ lambda { bd5667.send(@method, BigDecimal("0")) }.should raise_error(ZeroDivisionError)
+ lambda { @zero.send(@method, @zero) }.should raise_error(ZeroDivisionError)
+ 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..b94c9385de
--- /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).nan?.should == true
+ val.send(@method, @nan, *@object).nan?.should == true
+ 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).zero?.should == true
+ val.send(@method, zero, *@object).should == 0
+ val.send(@method, zero, *@object).zero?.should == true
+ 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).finite?.should == false
+ val.send(@method, inf, *@object).finite?.should == false
+ 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).nan?.should == true
+ @zero.send(@method, @infinity_minus, *@object).nan?.should == true
+ @infinity.send(@method, @zero, *@object).nan?.should == true
+ @infinity_minus.send(@method, @zero, *@object).nan?.should == true
+ 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..a4848fb2e2
--- /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")
+ # The tolerance is dependent upon the size of BASE_FIG
+ tolerance = BigDecimal("1E-70")
+ 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).nan?.should == true
+ BigDecimal("NaN").send(@method, 5).nan?.should == true
+ 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..cb51c10d71
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/shared/quo.rb
@@ -0,0 +1,59 @@
+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
+
+ 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).nan?.should == true
+ @infinity_minus.send(@method, @infinity, *@object).nan?.should == true
+ 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).nan?.should == true
+ @zero_minus.send(@method, @zero_plus, *@object).nan?.should == true
+ @zero_plus.send(@method, @zero_minus, *@object).nan?.should == true
+ 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..729a25f511
--- /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
+ lambda { BigDecimal("Infinity").send(@method) }.should raise_error(FloatDomainError)
+ lambda { BigDecimal("NaN").send(@method) }.should raise_error(FloatDomainError)
+ end
+
+ it "returns Integer or Bignum 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..0d722987b5
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/sign_spec.rb
@@ -0,0 +1,47 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+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 positve 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..e2ba955a3b
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/split_spec.rb
@@ -0,0 +1,88 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+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..f677192b33
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/sqrt_spec.rb
@@ -0,0 +1,112 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../fixtures/classes', __FILE__)
+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
+
+ 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
+
+ it "raises ArgumentError when no argument is given" do
+ lambda {
+ @one.sqrt
+ }.should raise_error(ArgumentError)
+ end
+
+ it "raises ArgumentError if a negative number is given" do
+ lambda {
+ @one.sqrt(-1)
+ }.should raise_error(ArgumentError)
+ end
+
+ it "raises ArgumentError if 2 arguments are given" do
+ lambda {
+ @one.sqrt(1, 1)
+ }.should raise_error(ArgumentError)
+ end
+
+ it "raises TypeError if nil is given" do
+ lambda {
+ @one.sqrt(nil)
+ }.should raise_error(TypeError)
+ end
+
+ it "raises TypeError if a string is given" do
+ lambda {
+ @one.sqrt("stuff")
+ }.should raise_error(TypeError)
+ end
+
+ it "raises TypeError if a plain Object is given" do
+ lambda {
+ @one.sqrt(Object.new)
+ }.should raise_error(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
+ lambda {
+ BigDecimal('-1').sqrt(10)
+ }.should raise_error(FloatDomainError)
+ end
+
+ it "returns positive infitinity for infinity" do
+ @infinity.sqrt(1).should == @infinity
+ end
+
+ it "raises FloatDomainError for negative infinity" do
+ lambda {
+ @infinity_minus.sqrt(1)
+ }.should raise_error(FloatDomainError)
+ end
+
+ it "raises FloatDomainError for NaN" do
+ lambda {
+ @nan.sqrt(1)
+ }.should raise_error(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..06ad3b79d2
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/sub_spec.rb
@@ -0,0 +1,53 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require 'bigdecimal'
+
+describe "BigDecimal#sub" 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 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
+
+ it "returns NaN if NaN is involved" do
+ @one.sub(@nan, 1).nan?.should == true
+ @nan.sub(@one, 1).nan?.should == true
+ end
+
+ it "returns NaN if both values are infinite with the same signs" do
+ @infinity.sub(@infinity, 1).nan?.should == true
+ @infinity_minus.sub(@infinity_minus, 1).nan?.should == true
+ 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_f_spec.rb b/spec/ruby/library/bigdecimal/to_f_spec.rb
new file mode 100644
index 0000000000..b490fce4d9
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/to_f_spec.rb
@@ -0,0 +1,55 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+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 be_kind_of(Float)
+ @vals.each { |val| val.to_f.should be_kind_of(Float) }
+ @spec_vals.each { |val| val.to_f.should be_kind_of(Float) }
+ 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.nan?.should == true
+
+ @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..8db69003c5
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/to_i_spec.rb
@@ -0,0 +1,7 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../shared/to_int', __FILE__)
+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..56a60d4a03
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/to_int_spec.rb
@@ -0,0 +1,8 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../shared/to_int', __FILE__)
+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..6f6e5a1bea
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/to_r_spec.rb
@@ -0,0 +1,16 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require 'bigdecimal'
+
+describe "BigDecimal#to_r" do
+
+ it "returns a Rational" do
+ BigDecimal("3.14159").to_r.should be_kind_of(Rational)
+ end
+
+ it "returns a Rational with bignum values" do
+ r = BigDecimal.new("3.141592653589793238462643").to_r
+ r.numerator.should eql(3141592653589793238462643)
+ r.denominator.should eql(1000000000000000000000000)
+ 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..b97311c800
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/to_s_spec.rb
@@ -0,0 +1,73 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+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)
+ 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]*$/i
+ end
+
+ it "takes an optional argument" do
+ lambda {@bigdec.to_s("F")}.should_not raise_error()
+ 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, 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.new("-123.45678901234567890").to_s('5F').should == str1
+ # trailing zeroes removed
+ BigDecimal.new("1.00000000000").to_s('1F').should == "1.0"
+ # 0 is treated as no spaces
+ BigDecimal.new("1.2345").to_s('0F').should == "1.2345"
+ 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.new('-123.45678901234567890').to_s('F').should == "-123.4567890123456789"
+ BigDecimal.new('1.2500').to_s('F').should == "1.25"
+ BigDecimal.new('0000.00000').to_s('F').should == "0.0"
+ BigDecimal.new('-00.000010000').to_s('F').should == "-0.00001"
+ BigDecimal.new("5.00000E-2").to_s("F").should == "0.05"
+
+ BigDecimal.new("500000").to_s("F").should == "500000.0"
+ BigDecimal.new("5E2").to_s("F").should == "500.0"
+ BigDecimal.new("-5E100").to_s("F").should == "-5" + "0" * 100 + ".0"
+ 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
+ @bigdec.to_s("F").should == @bigdec_str
+ @bigneg.to_s("F").should == @bigneg_str
+ str2 = "+123.45678901 23456789"
+ BigDecimal.new('123.45678901234567890').to_s('+8F').should == str2
+ 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..41f0afc8fa
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/truncate_spec.rb
@@ -0,0 +1,81 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+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).nan?.should == true
+ @nan.truncate(+1).nan?.should == true
+ @nan.truncate(0).nan?.should == true
+ 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
+ lambda { @nan.truncate }.should raise_error(FloatDomainError)
+ lambda { @infinity.truncate }.should raise_error(FloatDomainError)
+ lambda { @infinity_negative.truncate }.should raise_error(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..901f9f59ce
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/uminus_spec.rb
@@ -0,0 +1,58 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+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(:-@).nan?.should == true
+ 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..00aadc723c
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/uplus_spec.rb
@@ -0,0 +1,20 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+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/ver_spec.rb b/spec/ruby/library/bigdecimal/ver_spec.rb
new file mode 100644
index 0000000000..61b073d8f1
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/ver_spec.rb
@@ -0,0 +1,11 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require 'bigdecimal'
+
+describe "BigDecimal.ver" do
+
+ it "returns the Version number" do
+ lambda {BigDecimal.ver }.should_not raise_error()
+ BigDecimal.ver.should_not == nil
+ end
+
+end
diff --git a/spec/ruby/library/bigdecimal/zero_spec.rb b/spec/ruby/library/bigdecimal/zero_spec.rb
new file mode 100644
index 0000000000..7dbb0fa7f0
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/zero_spec.rb
@@ -0,0 +1,28 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+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.zero?.should == true
+ really_big_zero.zero?.should == true
+ BigDecimal("0.000000000000000000000000").zero?.should == true
+ BigDecimal("0").zero?.should == true
+ BigDecimal("0E0").zero?.should == true
+ BigDecimal("+0").zero?.should == true
+ BigDecimal("-0").zero?.should == true
+ end
+
+ it "returns false otherwise" do
+ BigDecimal("0000000001").zero?.should == false
+ BigDecimal("2E40001").zero?.should == false
+ BigDecimal("3E-20001").zero?.should == false
+ BigDecimal("Infinity").zero?.should == false
+ BigDecimal("-Infinity").zero?.should == false
+ BigDecimal("NaN").zero?.should == false
+ end
+
+end
+