summaryrefslogtreecommitdiff
path: root/test/bigdecimal/test_bigdecimal.rb
diff options
context:
space:
mode:
authorKenta Murata <mrkn@users.noreply.github.com>2019-10-08 09:06:28 +0900
committerGitHub <noreply@github.com>2019-10-08 09:06:28 +0900
commitdd0c75fdc2104a6ba38b68d4431a572504a3bbc2 (patch)
treecea82d0d8ea56e8c7d03158f35654adb974d6bd0 /test/bigdecimal/test_bigdecimal.rb
parent59c3b1c9c843fcd2d30393791fe224e5789d1677 (diff)
Import changes from ruby/bigdecimal (#2531)
Sync to ruby/bigdecimal@92356ba71c6bd325b0ab618c634a7aecf8cdc767
Notes
Notes: Merged-By: mrkn <mrkn@ruby-lang.org>
Diffstat (limited to 'test/bigdecimal/test_bigdecimal.rb')
-rw-r--r--test/bigdecimal/test_bigdecimal.rb62
1 files changed, 41 insertions, 21 deletions
diff --git a/test/bigdecimal/test_bigdecimal.rb b/test/bigdecimal/test_bigdecimal.rb
index 63db005..f6ef88e 100644
--- a/test/bigdecimal/test_bigdecimal.rb
+++ b/test/bigdecimal/test_bigdecimal.rb
@@ -77,6 +77,14 @@ class TestBigDecimal < Test::Unit::TestCase
end
end
+ def test_BigDecimal_bug7522
+ bd = BigDecimal("1.12", 1)
+ assert_same(bd, BigDecimal(bd))
+ assert_same(bd, BigDecimal(bd, exception: false))
+ assert_not_same(bd, BigDecimal(bd, 1))
+ assert_not_same(bd, BigDecimal(bd, 1, exception: false))
+ end
+
def test_BigDecimal_with_invalid_string
[
'', '.', 'e1', 'd1', '.e', '.d', '1.e', '1.d', '.1e', '.1d',
@@ -222,16 +230,30 @@ class TestBigDecimal < Test::Unit::TestCase
end
def test_s_new
- # TODO: BigDecimal.new will be removed on 1.5
- # assert_raise_with_message(NoMethodError, /undefined method `new'/) { BigDecimal.new("1") }
- verbose, $VERBOSE = $VERBOSE, nil
- assert_equal(BigDecimal(1), BigDecimal.new(1))
- assert_raise(ArgumentError) { BigDecimal.new(',', exception: true) }
- assert_nothing_raised { assert_equal(nil, BigDecimal.new(',', exception: false)) }
- assert_raise(TypeError) { BigDecimal.new(nil, exception: true) }
- assert_nothing_raised { assert_equal(nil, BigDecimal.new(nil, exception: false)) }
- ensure
- $VERBOSE = verbose
+ assert_raise_with_message(NoMethodError, /undefined method `new'/) { BigDecimal.new("1") }
+ end
+
+ def test_s_interpret_loosely
+ assert_equal(BigDecimal('1'), BigDecimal.interpret_loosely("1__1_1"))
+ assert_equal(BigDecimal('2.5'), BigDecimal.interpret_loosely("2.5"))
+ assert_equal(BigDecimal('2.5'), BigDecimal.interpret_loosely("2.5 degrees"))
+ assert_equal(BigDecimal('2.5e1'), BigDecimal.interpret_loosely("2.5e1 degrees"))
+ assert_equal(BigDecimal('0'), BigDecimal.interpret_loosely("degrees 100.0"))
+ assert_equal(BigDecimal('0.125'), BigDecimal.interpret_loosely("0.1_2_5"))
+ assert_equal(BigDecimal('0.125'), BigDecimal.interpret_loosely("0.1_2_5__"))
+ assert_equal(BigDecimal('1'), BigDecimal.interpret_loosely("1_.125"))
+ assert_equal(BigDecimal('1'), BigDecimal.interpret_loosely("1._125"))
+ assert_equal(BigDecimal('0.1'), BigDecimal.interpret_loosely("0.1__2_5"))
+ assert_equal(BigDecimal('0.1'), BigDecimal.interpret_loosely("0.1_e10"))
+ assert_equal(BigDecimal('0.1'), BigDecimal.interpret_loosely("0.1e_10"))
+ assert_equal(BigDecimal('1'), BigDecimal.interpret_loosely("0.1e1__0"))
+ assert_equal(BigDecimal('1.2'), BigDecimal.interpret_loosely("1.2.3"))
+ assert_equal(BigDecimal('1'), BigDecimal.interpret_loosely("1."))
+ assert_equal(BigDecimal('1'), BigDecimal.interpret_loosely("1e"))
+
+ assert_equal(BigDecimal('0.0'), BigDecimal.interpret_loosely("invalid"))
+
+ assert(BigDecimal.interpret_loosely("2.5").frozen?)
end
def _test_mode(type)
@@ -1597,6 +1619,13 @@ class TestBigDecimal < Test::Unit::TestCase
end
end
+ def test_exp_with_negative
+ x = BigDecimal(-1)
+ y = BigMath.exp(x, 20)
+ assert_equal(y, BigMath.exp(-1, 20))
+ assert_equal(BigDecimal(-1), x)
+ end
+
def test_exp_with_negative_infinite
BigDecimal.save_exception_mode do
BigDecimal.mode(BigDecimal::EXCEPTION_INFINITY, false)
@@ -1833,18 +1862,9 @@ class TestBigDecimal < Test::Unit::TestCase
end
end
- def test_dup_subclass
+ def test_new_subclass
c = Class.new(BigDecimal)
- # TODO: BigDecimal.new will be removed on 1.5
- # assert_raise_with_message(NoMethodError, /undefined method `new'/) { c.new(1) }
- verbose, $VERBOSE = $VERBOSE, nil
- assert_equal(BigDecimal(1), c.new(1))
- assert_raise(ArgumentError) { c.new(',', exception: true) }
- assert_nothing_raised { assert_equal(nil, c.new(',', exception: false)) }
- assert_raise(TypeError) { c.new(nil, exception: true) }
- assert_nothing_raised { assert_equal(nil, c.new(nil, exception: false)) }
- ensure
- $VERBOSE = verbose
+ assert_raise_with_message(NoMethodError, /undefined method `new'/) { c.new(1) }
end
def test_to_d