File tree Expand file tree Collapse file tree 8 files changed +70
-3
lines changed
spec/ruby/library/bigdecimal Expand file tree Collapse file tree 8 files changed +70
-3
lines changed Original file line number Diff line number Diff line change 73
73
# BigDecimal("0.88").add(0.0, 1).should == BigDecimal("0.9")
74
74
# end
75
75
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
+
76
84
it "favors the precision specified in the second argument over the global limit" do
77
85
BigDecimalSpecs . with_limit ( 1 ) do
78
86
BigDecimal ( '0.888' ) . add ( @zero , 3 ) . should == BigDecimal ( '0.888' )
Original file line number Diff line number Diff line change 42
42
}
43
43
end
44
44
45
+ describe "with Object" do
46
+ it "tries to coerce the other operand to self" do
47
+ object = mock ( "Object" )
48
+ object . should_receive ( :coerce ) . with ( @one ) . and_return ( [ @one , @two ] )
49
+ @one . div ( object ) . should == @zero
50
+ end
51
+ end
52
+
45
53
it "raises FloatDomainError if NaN is involved" do
46
54
lambda { @one . div ( @nan ) } . should raise_error ( FloatDomainError )
47
55
lambda { @nan . div ( @one ) } . should raise_error ( FloatDomainError )
Original file line number Diff line number Diff line change 9
9
describe "BigDecimal#mult" do
10
10
before :each do
11
11
@one = BigDecimal "1"
12
- @e3_minus = BigDecimal "3E-20001"
12
+ @e3_minus = BigDecimal ( "3E-20001" )
13
+ @e3_plus = BigDecimal ( "3E20001" )
13
14
@e = BigDecimal "1.00000000000000000000123456789"
14
15
@tolerance = @e . sub @one , 1000
15
16
@tolerance2 = BigDecimal "30001E-20005"
21
22
@e3_minus . mult ( @one , 1 ) . should be_close ( 0 , @tolerance2 )
22
23
end
23
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
24
32
end
Original file line number Diff line number Diff line change 23
23
( @e3_minus * @e3_minus ) . should == BigDecimal ( "9E-40002" )
24
24
( @e * @one ) . should == @e
25
25
end
26
+
27
+ describe "with Object" do
28
+ it "tries to coerce the other operand to self" do
29
+ object = mock ( "Object" )
30
+ object . should_receive ( :coerce ) . with ( @e3_minus ) . and_return ( [ @e3_minus , @e3_plus ] )
31
+ ( @e3_minus * object ) . should == BigDecimal ( "9" )
32
+ end
33
+ end
26
34
end
Original file line number Diff line number Diff line change 5
5
6
6
before :each do
7
7
@zero = BigDecimal ( "0" )
8
- @one = BigDecimal ( "0" )
8
+ @one = BigDecimal ( "1" )
9
+ @three = BigDecimal ( "3" )
9
10
@mixed = BigDecimal ( "1.23456789" )
10
11
@pos_int = BigDecimal ( "2E5555" )
11
12
@neg_int = BigDecimal ( "-2E5555" )
71
72
end
72
73
73
74
it "coerces arguments to BigDecimal if possible" do
74
- @one . remainder ( 2 ) . should == @one
75
+ @three . remainder ( 2 ) . should == @one
75
76
end
76
77
78
+ describe "with Object" do
79
+ it "tries to coerce the other operand to self" do
80
+ object = mock ( "Object" )
81
+ object . should_receive ( :coerce ) . with ( @three ) . and_return ( [ @three , 2 ] )
82
+ @three . remainder ( object ) . should == @one
83
+ end
84
+ end
77
85
78
86
it "raises TypeError if the argument cannot be coerced to BigDecimal" do
79
87
lambda {
Original file line number Diff line number Diff line change 70
70
res . kind_of? ( BigDecimal ) . should == true
71
71
end
72
72
73
+ describe "with Object" do
74
+ it "tries to coerce the other operand to self" do
75
+ bd6543 = BigDecimal ( "6543.21" )
76
+ object = mock ( "Object" )
77
+ object . should_receive ( :coerce ) . with ( bd6543 ) . and_return ( [ bd6543 , 137 ] )
78
+ bd6543 . send ( @method , object , *@object ) . should == BigDecimal ( "104.21" )
79
+ end
80
+ end
81
+
73
82
it "returns NaN if NaN is involved" do
74
83
@nan . send ( @method , @nan ) . nan? . should == true
75
84
@nan . send ( @method , @one ) . nan? . should == true
Original file line number Diff line number Diff line change 29
29
@one . send ( @method , BigDecimal ( '2E-5555' ) , *@object ) . should == BigDecimal ( '0.5E5555' )
30
30
end
31
31
32
+ describe "with Object" do
33
+ it "tries to coerce the other operand to self" do
34
+ object = mock ( "Object" )
35
+ object . should_receive ( :coerce ) . with ( @one ) . and_return ( [ @one , @two ] )
36
+ @one . send ( @method , object , *@object ) . should == BigDecimal ( "0.5" )
37
+ end
38
+ end
39
+
32
40
it "returns 0 if divided by Infinity" do
33
41
@zero . send ( @method , @infinity , *@object ) . should == 0
34
42
@frac_2 . send ( @method , @infinity , *@object ) . should == 0
Original file line number Diff line number Diff line change 13
13
@one_minus = BigDecimal ( "-1" )
14
14
@frac_1 = BigDecimal ( "1E-99999" )
15
15
@frac_2 = BigDecimal ( "0.9E-99999" )
16
+ @frac_3 = BigDecimal ( "12345E10" )
17
+ @frac_4 = BigDecimal ( "98765E10" )
16
18
end
17
19
18
20
it "returns a - b with given precision" do
32
34
@frac_1 . sub ( @frac_1 , 1000000 ) . should == @zero
33
35
end
34
36
37
+ describe "with Object" do
38
+ it "tries to coerce the other operand to self" do
39
+ object = mock ( "Object" )
40
+ object . should_receive ( :coerce ) . with ( @frac_3 ) . and_return ( [ @frac_3 , @frac_4 ] )
41
+ @frac_3 . sub ( object , 1 ) . should == BigDecimal ( "-0.9E15" )
42
+ end
43
+ end
44
+
35
45
it "returns NaN if NaN is involved" do
36
46
@one . sub ( @nan , 1 ) . nan? . should == true
37
47
@nan . sub ( @one , 1 ) . nan? . should == true
You can’t perform that action at this time.
0 commit comments