Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 2 additions & 38 deletions library/bigdecimal/BigDecimal_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,12 @@
end

it "accepts significant digits >= given precision" do
suppress_warning do
BigDecimal("3.1415923", 10).precs[1].should >= 10
end
BigDecimal("3.1415923", 10).should == BigDecimal("3.1415923")
end

it "determines precision from initial value" do
pi_string = "3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593014782083152134043"
suppress_warning {
BigDecimal(pi_string).precs[1]
}.should >= pi_string.size-1
BigDecimal(pi_string).precision.should == pi_string.size-1
end

it "ignores leading and trailing whitespace" do
Expand Down Expand Up @@ -208,14 +204,6 @@ def to_s; "cheese"; end
Float(@b).to_s.should == "166.66666666666666"
end

it "has the expected precision on the LHS" do
suppress_warning { @a.precs[0] }.should == 18
end

it "has the expected maximum precision on the LHS" do
suppress_warning { @a.precs[1] }.should == 27
end

it "produces the expected result when done via Float" do
(Float(@a) - Float(@b)).to_s.should == "-6.666596163995564e-10"
end
Expand All @@ -226,34 +214,10 @@ def to_s; "cheese"; end

# Check underlying methods work as we understand

it "BigDecimal precision is the number of digits rounded up to a multiple of nine" do
1.upto(100) do |n|
b = BigDecimal('4' * n)
precs, _ = suppress_warning { b.precs }
(precs >= 9).should be_true
(precs >= n).should be_true
(precs % 9).should == 0
end
suppress_warning { BigDecimal('NaN').precs[0] }.should == 9
end

it "BigDecimal maximum precision is nine more than precision except for abnormals" do
1.upto(100) do |n|
b = BigDecimal('4' * n)
precs, max = suppress_warning { b.precs }
max.should == precs + 9
end
suppress_warning { BigDecimal('NaN').precs[1] }.should == 9
end

it "BigDecimal(Rational, 18) produces the result we expect" do
BigDecimal(@b, 18).to_s.should == "0.166666666666666667e3"
end

it "BigDecimal(Rational, BigDecimal.precs[0]) produces the result we expect" do
BigDecimal(@b, suppress_warning { @a.precs[0] }).to_s.should == "0.166666666666666667e3"
end

# Check the top-level expression works as we expect

it "produces a BigDecimal" do
Expand Down
61 changes: 43 additions & 18 deletions library/bigdecimal/divmod_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@ class BigDecimal
end
end

it_behaves_like :bigdecimal_modulo, :mod_part_of_divmod
version_is BigDecimal::VERSION, ""..."4.0.0" do
it_behaves_like :bigdecimal_modulo, :mod_part_of_divmod
end

it "raises ZeroDivisionError if other is zero" do
bd5667 = BigDecimal("5667.19")

zero = BigDecimal("0")
-> { bd5667.mod_part_of_divmod(0) }.should raise_error(ZeroDivisionError)
-> { bd5667.mod_part_of_divmod(BigDecimal("0")) }.should raise_error(ZeroDivisionError)
-> { @zero.mod_part_of_divmod(@zero) }.should raise_error(ZeroDivisionError)
-> { zero.mod_part_of_divmod(zero) }.should raise_error(ZeroDivisionError)
end
end

Expand Down Expand Up @@ -73,14 +75,25 @@ class BigDecimal
@zeroes = [@zero, @zero_pos, @zero_neg]
end

it "divides value, returns an array" do
res = @a.divmod(5)
res.kind_of?(Array).should == true
version_is BigDecimal::VERSION, ""..."4.0.0" do
it "divides value, returns [BigDecimal, BigDecimal]" do
res = @a.divmod(5)
res.kind_of?(Array).should == true
DivmodSpecs.check_both_bigdecimal(res)
end
end

version_is BigDecimal::VERSION, "4.0.0" do
it "divides value, returns [Integer, BigDecimal]" do
res = @a.divmod(5)
res.kind_of?(Array).should == true
res[0].kind_of?(Integer).should == true
res[1].kind_of?(BigDecimal).should == true
end
end

it "array contains quotient and modulus as BigDecimal" do
res = @a.divmod(5)
DivmodSpecs.check_both_bigdecimal(res)
res[0].should == BigDecimal('0.8E1')
res[1].should == BigDecimal('2.00000000000000000001')

Expand Down Expand Up @@ -123,17 +136,27 @@ class BigDecimal
values_and_zeroes.each do |val1|
values.each do |val2|
res = val1.divmod(val2)
DivmodSpecs.check_both_bigdecimal(res)
res[0].should == ((val1/val2).floor)
res[1].should == (val1 - res[0] * val2)
end
end
end

it "returns an array of two NaNs if NaN is involved" do
(@special_vals + @regular_vals + @zeroes).each do |val|
DivmodSpecs.check_both_nan(val.divmod(@nan))
DivmodSpecs.check_both_nan(@nan.divmod(val))
version_is BigDecimal::VERSION, "4.0.0" do
it "raise FloatDomainError error if NaN is involved" do
(@special_vals + @regular_vals + @zeroes).each do |val|
-> { val.divmod(@nan) }.should raise_error(FloatDomainError)
-> { @nan.divmod(val) }.should raise_error(FloatDomainError)
end
end
end

version_is BigDecimal::VERSION, ""..."4.0.0" do
it "returns an array of two NaNs if NaN is involved" do
(@special_vals + @regular_vals + @zeroes).each do |val|
DivmodSpecs.check_both_nan(val.divmod(@nan))
DivmodSpecs.check_both_nan(@nan.divmod(val))
end
end
end

Expand All @@ -145,12 +168,14 @@ class BigDecimal
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].should.nan?
version_is BigDecimal::VERSION, ""..."4.0.0" do
it "returns an array of Infinity and NaN if the dividend is Infinity" do
@regular_vals.each do |val|
array = @infinity.divmod(val)
array.length.should == 2
array[0].infinite?.should == (val > 0 ? 1 : -1)
array[1].should.nan?
end
end
end

Expand Down
55 changes: 0 additions & 55 deletions library/bigdecimal/precs_spec.rb

This file was deleted.