Skip to content

Commit 0833f22

Browse files
committed
Fix bigdecimal spec
Fix for new feature BigDecimal(float). Fix assertion of BigMath.log not to expect incorrect random digits to be appended. Fix assertion of too-strict precision and assertion that expecting precision to be too loose. Fix divmod, modulo and remainder assertion which was expecting a bug inconsistent with Float as a specification. Remove coerce expectation to add/sub/mult which was dropped because of a bug, and never implemented correctly in BigDecmal.
1 parent 9739d33 commit 0833f22

File tree

9 files changed

+34
-41
lines changed

9 files changed

+34
-41
lines changed

library/bigdecimal/BigDecimal_spec.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,9 @@
156156
BigDecimal("-12345.6E-1").should == -reference
157157
end
158158

159-
it "raises ArgumentError when Float is used without precision" do
160-
-> { BigDecimal(1.0) }.should raise_error(ArgumentError)
159+
it "allows Float without precision" do
160+
skip if BigDecimal::VERSION < "3.3.0"
161+
BigDecimal(1.2).should == BigDecimal("1.2")
161162
end
162163

163164
it "returns appropriate BigDecimal zero for signed zero" do
@@ -259,8 +260,8 @@ def to_s; "cheese"; end
259260
end
260261

261262
it "produces the expected result" do
262-
@c.should == BigDecimal("-0.666667e-9")
263-
@c.to_s.should == "-0.666667e-9"
263+
@c.round(15).should == BigDecimal("-0.666667e-9")
264+
@c.round(15).to_s.should == "-0.666667e-9"
264265
end
265266

266267
it "produces the correct class for other arithmetic operators" do

library/bigdecimal/add_spec.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,6 @@
7373
# BigDecimal("0.88").add(0.0, 1).should == BigDecimal("0.9")
7474
# end
7575

76-
describe "with Object" do
77-
it "tries to coerce the other operand to self" do
78-
object = mock("Object")
79-
object.should_receive(:coerce).with(@frac_3).and_return([@frac_3, @frac_4])
80-
@frac_3.add(object, 1).should == BigDecimal("0.1E16")
81-
end
82-
end
83-
8476
describe "with Rational" do
8577
it "produces a BigDecimal" do
8678
(@three + Rational(500, 2)).should == BigDecimal("0.253e3")

library/bigdecimal/core_spec.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@
2020

2121
describe "BigDecimal#log" do
2222
it "handles high-precision Rational arguments" do
23-
result = BigDecimal('0.22314354220170971436137296411949880462556361100856391620766259404746040597133837784E0')
23+
# log(BigDecimal(r, 50), 50)
24+
result1 = BigDecimal('0.22314354220170971436137296411949880462556361100856e0')
25+
# log(BigDecimal(r, 1000), 50)
26+
result2 = BigDecimal('0.22314354220170971436137296411949880462556361100853e0')
2427
r = Rational(1_234_567_890, 987_654_321)
25-
BigMath.log(r, 50).should == result
28+
[result1, result2].should include(BigMath.log(r, 50).mult(1, 50))
2629
end
2730
end
2831

library/bigdecimal/divmod_spec.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,18 @@ class BigDecimal
154154
end
155155
end
156156

157-
it "returns an array of zero and the dividend if the divisor is Infinity" do
157+
it "returns an array of zero and the dividend or minus one and Infinity if the divisor is Infinity" do
158+
skip if BigDecimal::VERSION < "3.3.0"
158159
@regular_vals.each do |val|
159160
array = val.divmod(@infinity)
160161
array.length.should == 2
161-
array[0].should == @zero
162-
array[1].should == val
162+
if val >= 0
163+
array[0].should == @zero
164+
array[1].should == val
165+
else
166+
array[0].should == @one_minus
167+
array[1].should == @infinity
168+
end
163169
end
164170
end
165171

library/bigdecimal/mult_spec.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,4 @@
2121
@e.mult(@one, 1).should be_close(@one, @tolerance)
2222
@e3_minus.mult(@one, 1).should be_close(0, @tolerance2)
2323
end
24-
25-
describe "with Object" do
26-
it "tries to coerce the other operand to self" do
27-
object = mock("Object")
28-
object.should_receive(:coerce).with(@e3_minus).and_return([@e3_minus, @e3_plus])
29-
@e3_minus.mult(object, 1).should == BigDecimal("9")
30-
end
31-
end
3224
end

library/bigdecimal/remainder_spec.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@
3737
@neg_int.remainder(@pos_frac).should == @neg_int - @pos_frac * (@neg_int / @pos_frac).truncate
3838
end
3939

40-
it "returns NaN used with zero" do
41-
@mixed.remainder(@zero).should.nan?
42-
@zero.remainder(@zero).should.nan?
40+
it "raises ZeroDivisionError used with zero" do
41+
skip if BigDecimal::VERSION < "3.3.0"
42+
-> { @mixed.remainder(@zero) }.should raise_error(ZeroDivisionError)
43+
-> { @zero.remainder(@zero) }.should raise_error(ZeroDivisionError)
4344
end
4445

4546
it "returns zero if used on zero" do

library/bigdecimal/shared/modulo.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,16 @@
101101
@infinity_minus.send(@method, @infinity).should.nan?
102102
end
103103

104-
it "returns the dividend if the divisor is Infinity" do
104+
it "returns the dividend if the divisor is Infinity and signs are same" do
105+
skip if BigDecimal::VERSION < "3.3.0"
105106
@one.send(@method, @infinity).should == @one
106-
@one.send(@method, @infinity_minus).should == @one
107-
@frac_2.send(@method, @infinity_minus).should == @frac_2
107+
(-@frac_2).send(@method, @infinity_minus).should == -@frac_2
108+
end
109+
110+
it "returns the divisor if the divisor is Infinity and signs are different" do
111+
skip if BigDecimal::VERSION < "3.3.0"
112+
(-@one).send(@method, @infinity).should == @infinity
113+
@frac_2.send(@method, @infinity_minus).should == @infinity_minus
108114
end
109115

110116
it "raises TypeError if the argument cannot be coerced to BigDecimal" do

library/bigdecimal/shared/power.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
e = BigDecimal("1.00000000000000000000123456789")
1111
one = BigDecimal("1")
1212
ten = BigDecimal("10")
13-
# The tolerance is dependent upon the size of BASE_FIG
14-
tolerance = BigDecimal("1E-70")
13+
# Accuracy is at least ndigits(== 30) + DOUBLE_FIG(== 16)
14+
tolerance = BigDecimal("1E-46")
1515
ten_powers = BigDecimal("1E10000")
1616
pi = BigDecimal("3.14159265358979")
1717
e3_minus.send(@method, 2).should == e3_minus_power_2

library/bigdecimal/sub_spec.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,6 @@
3535
@frac_1.sub(@frac_1, 1000000).should == @zero
3636
end
3737

38-
describe "with Object" do
39-
it "tries to coerce the other operand to self" do
40-
object = mock("Object")
41-
object.should_receive(:coerce).with(@frac_3).and_return([@frac_3, @frac_4])
42-
@frac_3.sub(object, 1).should == BigDecimal("-0.9E15")
43-
end
44-
end
45-
4638
describe "with Rational" do
4739
it "produces a BigDecimal" do
4840
(@three - Rational(500, 2)).should == BigDecimal('-0.247e3')

0 commit comments

Comments
 (0)