summaryrefslogtreecommitdiff
path: root/spec/ruby/library/bigdecimal
diff options
context:
space:
mode:
authorBenoit Daloze <[email protected]>2019-05-28 22:41:48 +0200
committerBenoit Daloze <[email protected]>2019-05-28 22:41:48 +0200
commita66bc2c01194a9c017c874a30db5b3b6bd95e966 (patch)
tree598d6375b44fd86f90c3477c73086f6fcf08d76c /spec/ruby/library/bigdecimal
parentd070523e7be4b95914adeef9a10401fba7718c5a (diff)
Update to ruby/spec@9a501a8
Diffstat (limited to 'spec/ruby/library/bigdecimal')
-rw-r--r--spec/ruby/library/bigdecimal/BigDecimal_spec.rb28
-rw-r--r--spec/ruby/library/bigdecimal/clone_spec.rb6
-rw-r--r--spec/ruby/library/bigdecimal/constants_spec.rb76
-rw-r--r--spec/ruby/library/bigdecimal/dup_spec.rb6
-rw-r--r--spec/ruby/library/bigdecimal/hash_spec.rb30
-rw-r--r--spec/ruby/library/bigdecimal/inspect_spec.rb7
-rw-r--r--spec/ruby/library/bigdecimal/shared/clone.rb24
-rw-r--r--spec/ruby/library/bigdecimal/to_d_spec.rb11
8 files changed, 188 insertions, 0 deletions
diff --git a/spec/ruby/library/bigdecimal/BigDecimal_spec.rb b/spec/ruby/library/bigdecimal/BigDecimal_spec.rb
index 2312a4e910..98b3f47703 100644
--- a/spec/ruby/library/bigdecimal/BigDecimal_spec.rb
+++ b/spec/ruby/library/bigdecimal/BigDecimal_spec.rb
@@ -100,6 +100,34 @@ describe "Kernel#BigDecimal" do
neg_inf.should < 0
end
+ describe "accepts NaN and [+-]Infinity as Float values" do
+ it "works without an explicit precision" do
+ BigDecimal(Float::NAN).nan?.should == true
+
+ pos_inf = BigDecimal(Float::INFINITY)
+ pos_inf.finite?.should == false
+ pos_inf.should > 0
+ pos_inf.should == BigDecimal("+Infinity")
+
+ neg_inf = BigDecimal(-Float::INFINITY)
+ neg_inf.finite?.should == false
+ neg_inf.should < 0
+ end
+
+ it "works with an explicit precision" do
+ BigDecimal(Float::NAN, Float::DIG).nan?.should == true
+
+ pos_inf = BigDecimal(Float::INFINITY, Float::DIG)
+ pos_inf.finite?.should == false
+ pos_inf.should > 0
+ pos_inf.should == BigDecimal("+Infinity")
+
+ neg_inf = BigDecimal(-Float::INFINITY, Float::DIG)
+ neg_inf.finite?.should == false
+ neg_inf.should < 0
+ end
+ end
+
it "allows for [eEdD] as exponent separator" do
reference = BigDecimal("12345.67E89")
diff --git a/spec/ruby/library/bigdecimal/clone_spec.rb b/spec/ruby/library/bigdecimal/clone_spec.rb
new file mode 100644
index 0000000000..b3a1c61d6a
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/clone_spec.rb
@@ -0,0 +1,6 @@
+require_relative '../../spec_helper'
+require_relative 'shared/clone'
+
+describe "BigDecimal#dup" do
+ it_behaves_like :bigdecimal_clone, :clone
+end
diff --git a/spec/ruby/library/bigdecimal/constants_spec.rb b/spec/ruby/library/bigdecimal/constants_spec.rb
new file mode 100644
index 0000000000..1e08c6779b
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/constants_spec.rb
@@ -0,0 +1,76 @@
+require_relative '../../spec_helper'
+require 'bigdecimal'
+
+describe "BigDecimal constants" do
+ ruby_version_is "2.5" do
+ it "defines a VERSION value" do
+ BigDecimal.const_defined?(:VERSION).should be_true
+ end
+ end
+
+ it "has a BASE value" do
+ platform_is wordsize: 64 do
+ BigDecimal::BASE.should == 1000000000
+ end
+
+ platform_is wordsize: 32 do
+ BigDecimal::BASE.should == 10000
+ end
+ end
+
+ it "has a NaN value" do
+ BigDecimal::NAN.nan?.should be_true
+ end
+
+ it "has an INFINITY value" do
+ BigDecimal::INFINITY.infinite?.should == 1
+ end
+
+ describe "exception-related constants" do
+ [
+ [:EXCEPTION_ALL, 0xff],
+ [:EXCEPTION_INFINITY, 0x01],
+ [:EXCEPTION_NaN, 0x02],
+ [:EXCEPTION_UNDERFLOW, 0x04],
+ [:EXCEPTION_OVERFLOW, 0x01],
+ [:EXCEPTION_ZERODIVIDE, 0x10]
+ ].each do |const, value|
+ it "has a #{const} value" do
+ BigDecimal.const_get(const).should == value
+ end
+ end
+ end
+
+ describe "rounding-related constants" do
+ [
+ [:ROUND_MODE, 0x100],
+ [:ROUND_UP, 1],
+ [:ROUND_DOWN, 2],
+ [:ROUND_HALF_UP, 3],
+ [:ROUND_HALF_DOWN, 4],
+ [:ROUND_CEILING, 5],
+ [:ROUND_FLOOR, 6],
+ [:ROUND_HALF_EVEN, 7]
+ ].each do |const, value|
+ it "has a #{const} value" do
+ BigDecimal.const_get(const).should == value
+ end
+ end
+ end
+
+ describe "sign-related constants" do
+ [
+ [:SIGN_NaN, 0],
+ [:SIGN_POSITIVE_ZERO, 1],
+ [:SIGN_NEGATIVE_ZERO, -1],
+ [:SIGN_POSITIVE_FINITE, 2],
+ [:SIGN_NEGATIVE_FINITE, -2],
+ [:SIGN_POSITIVE_INFINITE, 3],
+ [:SIGN_NEGATIVE_INFINITE, -3]
+ ].each do |const, value|
+ it "has a #{const} value" do
+ BigDecimal.const_get(const).should == value
+ end
+ end
+ end
+end
diff --git a/spec/ruby/library/bigdecimal/dup_spec.rb b/spec/ruby/library/bigdecimal/dup_spec.rb
new file mode 100644
index 0000000000..bfabaf6e8b
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/dup_spec.rb
@@ -0,0 +1,6 @@
+require_relative '../../spec_helper'
+require_relative 'shared/clone'
+
+describe "BigDecimal#dup" do
+ it_behaves_like :bigdecimal_clone, :dup
+end
diff --git a/spec/ruby/library/bigdecimal/hash_spec.rb b/spec/ruby/library/bigdecimal/hash_spec.rb
new file mode 100644
index 0000000000..7581c90f68
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/hash_spec.rb
@@ -0,0 +1,30 @@
+require_relative '../../spec_helper'
+require 'bigdecimal'
+
+describe "BidDecimal#hash" do
+ describe "two BigDecimal objects with the same value" do
+ it "should have the same hash for ordinary values" do
+ BigDecimal('1.2920').hash.should == BigDecimal('1.2920').hash
+ end
+
+ it "should have the same hash for infinite values" do
+ BigDecimal("+Infinity").hash.should == BigDecimal("+Infinity").hash
+ BigDecimal("-Infinity").hash.should == BigDecimal("-Infinity").hash
+ end
+
+ it "should have the same hash for NaNs" do
+ BigDecimal("NaN").hash.should == BigDecimal("NaN").hash
+ end
+
+ it "should have the same hash for zero values" do
+ BigDecimal("+0").hash.should == BigDecimal("+0").hash
+ BigDecimal("-0").hash.should == BigDecimal("-0").hash
+ end
+ end
+
+ describe "two BigDecimal objects with numerically equal values" do
+ it "should have the same hash value" do
+ BigDecimal("1.2920").hash.should == BigDecimal("1.2920000").hash
+ end
+ end
+end
diff --git a/spec/ruby/library/bigdecimal/inspect_spec.rb b/spec/ruby/library/bigdecimal/inspect_spec.rb
index cd2f1a3cd4..18e9ca9a0c 100644
--- a/spec/ruby/library/bigdecimal/inspect_spec.rb
+++ b/spec/ruby/library/bigdecimal/inspect_spec.rb
@@ -14,4 +14,11 @@ describe "BigDecimal#inspect" do
it "looks like this" do
@bigdec.inspect.should == "0.12345678e4"
end
+
+ it "properly cases non-finite values" do
+ BigDecimal("NaN").inspect.should == "NaN"
+ BigDecimal("Infinity").inspect.should == "Infinity"
+ BigDecimal("+Infinity").inspect.should == "Infinity"
+ BigDecimal("-Infinity").inspect.should == "-Infinity"
+ end
end
diff --git a/spec/ruby/library/bigdecimal/shared/clone.rb b/spec/ruby/library/bigdecimal/shared/clone.rb
new file mode 100644
index 0000000000..e58df3a94b
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/shared/clone.rb
@@ -0,0 +1,24 @@
+require 'bigdecimal'
+
+describe :bigdecimal_clone, shared: true do
+ before :each do
+ @obj = BigDecimal("1.2345")
+ end
+
+ ruby_version_is "" ... "2.5" do
+ it "copies the BigDecimal's value to a newly allocated object" do
+ copy = @obj.public_send(@method)
+
+ copy.should_not equal(@obj)
+ copy.should == @obj
+ end
+ end
+
+ ruby_version_is "2.5" do
+ it "returns self" do
+ copy = @obj.public_send(@method)
+
+ copy.should equal(@obj)
+ end
+ end
+end
diff --git a/spec/ruby/library/bigdecimal/to_d_spec.rb b/spec/ruby/library/bigdecimal/to_d_spec.rb
new file mode 100644
index 0000000000..8e20901fd9
--- /dev/null
+++ b/spec/ruby/library/bigdecimal/to_d_spec.rb
@@ -0,0 +1,11 @@
+require_relative '../../spec_helper'
+require 'bigdecimal'
+require 'bigdecimal/util'
+
+
+describe "Float#to_d" do
+ it "returns appropriate BigDecimal zero for signed zero" do
+ -0.0.to_d.sign.should == -1
+ 0.0.to_d.sign.should == 1
+ end
+end