Skip to content

Commit 8bb046b

Browse files
authored
Fix for bigdecimal v4.0.0 (#1312)
1 parent ffc54df commit 8bb046b

File tree

3 files changed

+45
-111
lines changed

3 files changed

+45
-111
lines changed

library/bigdecimal/BigDecimal_spec.rb

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,12 @@
3131
end
3232

3333
it "accepts significant digits >= given precision" do
34-
suppress_warning do
35-
BigDecimal("3.1415923", 10).precs[1].should >= 10
36-
end
34+
BigDecimal("3.1415923", 10).should == BigDecimal("3.1415923")
3735
end
3836

3937
it "determines precision from initial value" do
4038
pi_string = "3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593014782083152134043"
41-
suppress_warning {
42-
BigDecimal(pi_string).precs[1]
43-
}.should >= pi_string.size-1
39+
BigDecimal(pi_string).precision.should == pi_string.size-1
4440
end
4541

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

211-
it "has the expected precision on the LHS" do
212-
suppress_warning { @a.precs[0] }.should == 18
213-
end
214-
215-
it "has the expected maximum precision on the LHS" do
216-
suppress_warning { @a.precs[1] }.should == 27
217-
end
218-
219207
it "produces the expected result when done via Float" do
220208
(Float(@a) - Float(@b)).to_s.should == "-6.666596163995564e-10"
221209
end
@@ -226,34 +214,10 @@ def to_s; "cheese"; end
226214

227215
# Check underlying methods work as we understand
228216

229-
it "BigDecimal precision is the number of digits rounded up to a multiple of nine" do
230-
1.upto(100) do |n|
231-
b = BigDecimal('4' * n)
232-
precs, _ = suppress_warning { b.precs }
233-
(precs >= 9).should be_true
234-
(precs >= n).should be_true
235-
(precs % 9).should == 0
236-
end
237-
suppress_warning { BigDecimal('NaN').precs[0] }.should == 9
238-
end
239-
240-
it "BigDecimal maximum precision is nine more than precision except for abnormals" do
241-
1.upto(100) do |n|
242-
b = BigDecimal('4' * n)
243-
precs, max = suppress_warning { b.precs }
244-
max.should == precs + 9
245-
end
246-
suppress_warning { BigDecimal('NaN').precs[1] }.should == 9
247-
end
248-
249217
it "BigDecimal(Rational, 18) produces the result we expect" do
250218
BigDecimal(@b, 18).to_s.should == "0.166666666666666667e3"
251219
end
252220

253-
it "BigDecimal(Rational, BigDecimal.precs[0]) produces the result we expect" do
254-
BigDecimal(@b, suppress_warning { @a.precs[0] }).to_s.should == "0.166666666666666667e3"
255-
end
256-
257221
# Check the top-level expression works as we expect
258222

259223
it "produces a BigDecimal" do

library/bigdecimal/divmod_spec.rb

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,16 @@ class BigDecimal
3333
end
3434
end
3535

36-
it_behaves_like :bigdecimal_modulo, :mod_part_of_divmod
36+
version_is BigDecimal::VERSION, ""..."4.0.0" do
37+
it_behaves_like :bigdecimal_modulo, :mod_part_of_divmod
38+
end
3739

3840
it "raises ZeroDivisionError if other is zero" do
3941
bd5667 = BigDecimal("5667.19")
40-
42+
zero = BigDecimal("0")
4143
-> { bd5667.mod_part_of_divmod(0) }.should raise_error(ZeroDivisionError)
4244
-> { bd5667.mod_part_of_divmod(BigDecimal("0")) }.should raise_error(ZeroDivisionError)
43-
-> { @zero.mod_part_of_divmod(@zero) }.should raise_error(ZeroDivisionError)
45+
-> { zero.mod_part_of_divmod(zero) }.should raise_error(ZeroDivisionError)
4446
end
4547
end
4648

@@ -73,14 +75,25 @@ class BigDecimal
7375
@zeroes = [@zero, @zero_pos, @zero_neg]
7476
end
7577

76-
it "divides value, returns an array" do
77-
res = @a.divmod(5)
78-
res.kind_of?(Array).should == true
78+
version_is BigDecimal::VERSION, ""..."4.0.0" do
79+
it "divides value, returns [BigDecimal, BigDecimal]" do
80+
res = @a.divmod(5)
81+
res.kind_of?(Array).should == true
82+
DivmodSpecs.check_both_bigdecimal(res)
83+
end
84+
end
85+
86+
version_is BigDecimal::VERSION, "4.0.0" do
87+
it "divides value, returns [Integer, BigDecimal]" do
88+
res = @a.divmod(5)
89+
res.kind_of?(Array).should == true
90+
res[0].kind_of?(Integer).should == true
91+
res[1].kind_of?(BigDecimal).should == true
92+
end
7993
end
8094

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

@@ -123,17 +136,27 @@ class BigDecimal
123136
values_and_zeroes.each do |val1|
124137
values.each do |val2|
125138
res = val1.divmod(val2)
126-
DivmodSpecs.check_both_bigdecimal(res)
127139
res[0].should == ((val1/val2).floor)
128140
res[1].should == (val1 - res[0] * val2)
129141
end
130142
end
131143
end
132144

133-
it "returns an array of two NaNs if NaN is involved" do
134-
(@special_vals + @regular_vals + @zeroes).each do |val|
135-
DivmodSpecs.check_both_nan(val.divmod(@nan))
136-
DivmodSpecs.check_both_nan(@nan.divmod(val))
145+
version_is BigDecimal::VERSION, "4.0.0" do
146+
it "raise FloatDomainError error if NaN is involved" do
147+
(@special_vals + @regular_vals + @zeroes).each do |val|
148+
-> { val.divmod(@nan) }.should raise_error(FloatDomainError)
149+
-> { @nan.divmod(val) }.should raise_error(FloatDomainError)
150+
end
151+
end
152+
end
153+
154+
version_is BigDecimal::VERSION, ""..."4.0.0" do
155+
it "returns an array of two NaNs if NaN is involved" do
156+
(@special_vals + @regular_vals + @zeroes).each do |val|
157+
DivmodSpecs.check_both_nan(val.divmod(@nan))
158+
DivmodSpecs.check_both_nan(@nan.divmod(val))
159+
end
137160
end
138161
end
139162

@@ -145,12 +168,14 @@ class BigDecimal
145168
end
146169
end
147170

148-
it "returns an array of Infinity and NaN if the dividend is Infinity" do
149-
@regular_vals.each do |val|
150-
array = @infinity.divmod(val)
151-
array.length.should == 2
152-
array[0].infinite?.should == (val > 0 ? 1 : -1)
153-
array[1].should.nan?
171+
version_is BigDecimal::VERSION, ""..."4.0.0" do
172+
it "returns an array of Infinity and NaN if the dividend is Infinity" do
173+
@regular_vals.each do |val|
174+
array = @infinity.divmod(val)
175+
array.length.should == 2
176+
array[0].infinite?.should == (val > 0 ? 1 : -1)
177+
array[1].should.nan?
178+
end
154179
end
155180
end
156181

library/bigdecimal/precs_spec.rb

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)