summaryrefslogtreecommitdiff
path: root/spec/ruby/library/bigdecimal/BigDecimal_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/library/bigdecimal/BigDecimal_spec.rb')
-rw-r--r--spec/ruby/library/bigdecimal/BigDecimal_spec.rb99
1 files changed, 97 insertions, 2 deletions
diff --git a/spec/ruby/library/bigdecimal/BigDecimal_spec.rb b/spec/ruby/library/bigdecimal/BigDecimal_spec.rb
index e1e6fe9fb1..179bde1aed 100644
--- a/spec/ruby/library/bigdecimal/BigDecimal_spec.rb
+++ b/spec/ruby/library/bigdecimal/BigDecimal_spec.rb
@@ -24,6 +24,12 @@ describe "Kernel#BigDecimal" do
}
end
+ it "BigDecimal(Rational) with bigger-than-double numerator" do
+ rational = 99999999999999999999/100r
+ rational.numerator.should > 2**64
+ BigDecimal(rational, 100).to_s.should == "0.99999999999999999999e18"
+ end
+
it "accepts significant digits >= given precision" do
BigDecimal("3.1415923", 10).precs[1].should >= 10
end
@@ -33,8 +39,8 @@ describe "Kernel#BigDecimal" do
BigDecimal(pi_string).precs[1].should >= pi_string.size-1
end
- it "ignores leading whitespace" do
- BigDecimal(" \t\n \r1234").should == BigDecimal("1234")
+ it "ignores leading and trailing whitespace" do
+ BigDecimal(" \t\n \r1234\t\r\n ").should == BigDecimal("1234")
BigDecimal(" \t\n \rNaN \n").nan?.should == true
BigDecimal(" \t\n \rInfinity \n").infinite?.should == 1
BigDecimal(" \t\n \r-Infinity \n").infinite?.should == -1
@@ -159,4 +165,93 @@ describe "Kernel#BigDecimal" do
BigDecimal(0.0, Float::DIG).sign.should == 1
end
+ it "pre-coerces long integers" do
+ BigDecimal(3).add(1 << 50, 3).should == BigDecimal('0.113e16')
+ end
+
+ describe "when interacting with Rational" do
+ before :each do
+ @a = BigDecimal('166.666666666')
+ @b = Rational(500, 3)
+ @c = @a - @b
+ end
+
+ # Check the input is as we understand it
+
+ it "has the LHS print as expected" do
+ @a.to_s.should == "0.166666666666e3"
+ @a.to_f.to_s.should == "166.666666666"
+ Float(@a).to_s.should == "166.666666666"
+ end
+
+ it "has the RHS print as expected" do
+ @b.to_s.should == "500/3"
+ @b.to_f.to_s.should == "166.66666666666666"
+ Float(@b).to_s.should == "166.66666666666666"
+ end
+
+ it "has the expected precision on the LHS" do
+ @a.precs[0].should == 18
+ end
+
+ it "has the expected maximum precision on the LHS" do
+ @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
+
+ it "produces the expected result when done via to_f" do
+ (@a.to_f - @b.to_f).to_s.should == "-6.666596163995564e-10"
+ 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, _ = b.precs
+ (precs >= 9).should be_true
+ (precs >= n).should be_true
+ (precs % 9).should == 0
+ end
+ 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 = b.precs
+ max.should == precs + 9
+ end
+ 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, @a.precs[0]).to_s.should == "0.166666666666666667e3"
+ end
+
+ # Check the top-level expression works as we expect
+
+ it "produces a BigDecimal" do
+ @c.class.should == BigDecimal
+ end
+
+ it "produces the expected result" do
+ @c.should == BigDecimal("-0.666667e-9")
+ @c.to_s.should == "-0.666667e-9"
+ end
+
+ it "produces the correct class for other arithmetic operators" do
+ (@a + @b).class.should == BigDecimal
+ (@a * @b).class.should == BigDecimal
+ (@a / @b).class.should == BigDecimal
+ (@a % @b).class.should == BigDecimal
+ end
+ end
end