summaryrefslogtreecommitdiff
path: root/test/bigdecimal/test_bigdecimal.rb
diff options
context:
space:
mode:
authorKenta Murata <mrkn@mrkn.jp>2020-12-19 04:24:15 +0900
committerKenta Murata <mrkn@mrkn.jp>2020-12-19 22:13:58 +0900
commitff9e40811c67b1090966b655f7adc0684fc58abe (patch)
tree5ade13fabd69840d3ad5f90d089c2044068fd45a /test/bigdecimal/test_bigdecimal.rb
parent928a06723d1ede495b7c3b42f3ca48e370ccec77 (diff)
[bigdecimal] Add BigDecimal#precision
https://github.com/ruby/bigdecimal/commit/458eb66c49
Diffstat (limited to 'test/bigdecimal/test_bigdecimal.rb')
-rw-r--r--test/bigdecimal/test_bigdecimal.rb49
1 files changed, 49 insertions, 0 deletions
diff --git a/test/bigdecimal/test_bigdecimal.rb b/test/bigdecimal/test_bigdecimal.rb
index a2af553..59726ad 100644
--- a/test/bigdecimal/test_bigdecimal.rb
+++ b/test/bigdecimal/test_bigdecimal.rb
@@ -1903,6 +1903,55 @@ class TestBigDecimal < Test::Unit::TestCase
EOS
end
+ def test_precision_only_integer
+ assert_equal(0, BigDecimal(0).precision)
+ assert_equal(1, BigDecimal(1).precision)
+ assert_equal(1, BigDecimal(-1).precision)
+ assert_equal(2, BigDecimal(10).precision)
+ assert_equal(2, BigDecimal(-10).precision)
+ assert_equal(21, BigDecimal(100_000_000_000_000_000_000).precision)
+ assert_equal(21, BigDecimal(-100_000_000_000_000_000_000).precision)
+ assert_equal(103, BigDecimal("111e100").precision)
+ assert_equal(103, BigDecimal("-111e100").precision)
+ end
+
+ def test_precision_only_fraction
+ assert_equal(1, BigDecimal("0.1").precision)
+ assert_equal(1, BigDecimal("-0.1").precision)
+ assert_equal(1, BigDecimal("0.01").precision)
+ assert_equal(1, BigDecimal("-0.01").precision)
+ assert_equal(2, BigDecimal("0.11").precision)
+ assert_equal(2, BigDecimal("-0.11").precision)
+ assert_equal(21, BigDecimal("0.000_000_000_000_000_000_001").precision)
+ assert_equal(21, BigDecimal("-0.000_000_000_000_000_000_001").precision)
+ assert_equal(100, BigDecimal("111e-100").precision)
+ assert_equal(100, BigDecimal("-111e-100").precision)
+ end
+
+ def test_precision_full
+ assert_equal(1, BigDecimal("0.1").precision)
+ assert_equal(1, BigDecimal("-0.1").precision)
+ assert_equal(1, BigDecimal("0.01").precision)
+ assert_equal(1, BigDecimal("-0.01").precision)
+ assert_equal(2, BigDecimal("0.11").precision)
+ assert_equal(2, BigDecimal("-0.11").precision)
+ assert_equal(5, BigDecimal("11111e-2").precision)
+ assert_equal(5, BigDecimal("-11111e-2").precision)
+ assert_equal(21, BigDecimal("100.000_000_000_000_000_001").precision)
+ assert_equal(21, BigDecimal("-100.000_000_000_000_000_001").precision)
+ end
+
+ def test_precision_special
+ BigDecimal.save_exception_mode do
+ BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW, false)
+ BigDecimal.mode(BigDecimal::EXCEPTION_NaN, false)
+
+ assert_equal(0, BigDecimal("Infinity").precision)
+ assert_equal(0, BigDecimal("-Infinity").precision)
+ assert_equal(0, BigDecimal("NaN").precision)
+ end
+ end
+
def test_initialize_copy_dup_clone_frozen_error
bd = BigDecimal(1)
bd2 = BigDecimal(2)