diff options
author | mrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2018-12-05 11:30:24 +0000 |
---|---|---|
committer | mrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2018-12-05 11:30:24 +0000 |
commit | 2810c12a99fed7297d558d8b1aaaf755eb8b8a2d (patch) | |
tree | 03ded624e1b6eab7bcf9059c5193c25f10ee26ae /test | |
parent | ae88d2fc93e887f22d025b4738ebbb00f6ae53e9 (diff) | |
download | ruby-2810c12a99fed7297d558d8b1aaaf755eb8b8a2d.tar.gz |
Import bigdecimal 1.4.0.pre.20181205a
* https://github.com/ruby/bigdecimal/compare/74d25ef..v1.4.0.pre.20181205a
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66222 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test')
-rw-r--r-- | test/bigdecimal/test_bigdecimal.rb | 185 | ||||
-rw-r--r-- | test/bigdecimal/test_bigdecimal_util.rb | 32 |
2 files changed, 133 insertions, 84 deletions
diff --git a/test/bigdecimal/test_bigdecimal.rb b/test/bigdecimal/test_bigdecimal.rb index bb9ec92481..ff295b4270 100644 --- a/test/bigdecimal/test_bigdecimal.rb +++ b/test/bigdecimal/test_bigdecimal.rb @@ -44,49 +44,71 @@ class TestBigDecimal < Test::Unit::TestCase end def test_not_equal - assert_not_equal BigDecimal("1"), BigDecimal.allocate + assert_not_equal BigDecimal("1"), BigDecimal("2") end - def test_global_new + def test_BigDecimal assert_equal(1, BigDecimal("1")) assert_equal(1, BigDecimal("1", 1)) + assert_equal(1, BigDecimal(" 1 ")) + assert_equal(111, BigDecimal("1_1_1_")) + assert_equal(10**(-1), BigDecimal("1E-1"), '#4825') + assert_equal(1234, BigDecimal(" \t\n\r \r1234 \t\n\r \r")) + assert_raise(ArgumentError) { BigDecimal("1", -1) } + assert_raise(ArgumentError, /"1__1_1"/) { BigDecimal("1__1_1") } + assert_raise(ArgumentError, /"_1_1_1"/) { BigDecimal("_1_1_1") } BigDecimal.save_exception_mode do BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW, false) BigDecimal.mode(BigDecimal::EXCEPTION_NaN, false) - assert_equal(1234, BigDecimal(" \t\n\r \r1234 \t\n\r \r")) + assert_positive_infinite(BigDecimal("Infinity")) + assert_positive_infinite(BigDecimal("1E1111111111111111111")) assert_positive_infinite(BigDecimal(" \t\n\r \rInfinity \t\n\r \r")) + assert_negative_infinite(BigDecimal("-Infinity")) assert_negative_infinite(BigDecimal(" \t\n\r \r-Infinity \t\n\r \r")) + assert_nan(BigDecimal("NaN")) assert_nan(BigDecimal(" \t\n\r \rNaN \t\n\r \r")) end end - def test_global_new_with_invalid_string + def test_BigDecimal_with_invalid_string [ '', '.', 'e1', 'd1', '.e', '.d', '1.e', '1.d', '.1e', '.1d', - 'invlaid value' + '2,30', '19,000.0', '-2,30', '-19,000.0', '+2,30', '+19,000.0', + '2.3,0', '19.000,0', '-2.3,0', '-19.000,0', '+2.3,0', '+19.000,0', + '2.3.0', '19.000.0', '-2.3.0', '-19.000.0', '+2.3.0', '+19.000.0', + 'invlaid value', '123 xyz' ].each do |invalid_string| assert_raise_with_message(ArgumentError, %Q[invalid value for BigDecimal(): "#{invalid_string}"]) do BigDecimal(invalid_string) end end + + BigDecimal.save_exception_mode do + BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW, false) + BigDecimal.mode(BigDecimal::EXCEPTION_NaN, false) + assert_raise(ArgumentError, /"Infinity_"/) { BigDecimal("Infinity_") } + assert_raise(ArgumentError, /"+Infinity_"/) { BigDecimal("+Infinity_") } + assert_raise(ArgumentError, /"-Infinity_"/) { BigDecimal("-Infinity_") } + assert_raise(ArgumentError, /"NaN_"/) { BigDecimal("NaN_") } + end end - def test_global_new_with_integer + def test_BigDecimal_with_integer assert_equal(BigDecimal("1"), BigDecimal(1)) assert_equal(BigDecimal("-1"), BigDecimal(-1)) assert_equal(BigDecimal((2**100).to_s), BigDecimal(2**100)) assert_equal(BigDecimal((-2**100).to_s), BigDecimal(-2**100)) end - def test_global_new_with_rational + def test_BigDecimal_with_rational assert_equal(BigDecimal("0.333333333333333333333"), BigDecimal(1.quo(3), 21)) assert_equal(BigDecimal("-0.333333333333333333333"), BigDecimal(-1.quo(3), 21)) assert_raise_with_message(ArgumentError, "can't omit precision for a Rational.") { BigDecimal(42.quo(7)) } end - def test_global_new_with_float + def test_BigDecimal_with_float assert_equal(BigDecimal("0.1235"), BigDecimal(0.1234567, 4)) assert_equal(BigDecimal("-0.1235"), BigDecimal(-0.1234567, 4)) assert_raise_with_message(ArgumentError, "can't omit precision for a Float.") { BigDecimal(4.2) } @@ -107,7 +129,7 @@ class TestBigDecimal < Test::Unit::TestCase end end - def test_global_new_with_big_decimal + def test_BigDecimal_with_big_decimal assert_equal(BigDecimal(1), BigDecimal(BigDecimal(1))) assert_equal(BigDecimal('+0'), BigDecimal(BigDecimal('+0'))) assert_equal(BigDecimal('-0'), BigDecimal(BigDecimal('-0'))) @@ -120,7 +142,7 @@ class TestBigDecimal < Test::Unit::TestCase end end - def test_global_new_with_tainted_string + def test_BigDecimal_with_tainted_string Thread.new { $SAFE = 1 BigDecimal('1'.taint) @@ -129,76 +151,73 @@ class TestBigDecimal < Test::Unit::TestCase $SAFE = 0 end - def test_s_ver - assert_warning(/BigDecimal\.ver is deprecated; use BigDecimal::VERSION instead/) do - BigDecimal.ver - end - end - - def test_s_new - assert_warning(/BigDecimal.new is deprecated/) do - BigDecimal.new("1") - end - end - - def test_new - assert_equal(1, BigDecimal("1")) - assert_equal(1, BigDecimal("1", 1)) - assert_equal(1, BigDecimal(" 1 ")) - assert_equal(111, BigDecimal("1_1_1_")) - assert_equal(10**(-1), BigDecimal("1E-1"), '#4825') - - assert_raise(ArgumentError, /"_1_1_1"/) { BigDecimal("_1_1_1") } - - BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW, false) - BigDecimal.mode(BigDecimal::EXCEPTION_NaN, false) - assert_positive_infinite(BigDecimal("Infinity")) - assert_negative_infinite(BigDecimal("-Infinity")) - assert_nan(BigDecimal("NaN")) - assert_positive_infinite(BigDecimal("1E1111111111111111111")) - end - - def test_new_with_integer - assert_equal(BigDecimal("1"), BigDecimal(1)) - assert_equal(BigDecimal("-1"), BigDecimal(-1)) - assert_equal(BigDecimal((2**100).to_s), BigDecimal(2**100)) - assert_equal(BigDecimal((-2**100).to_s), BigDecimal(-2**100)) - end - - def test_new_with_rational - assert_equal(BigDecimal("0.333333333333333333333"), BigDecimal(1.quo(3), 21)) - assert_equal(BigDecimal("-0.333333333333333333333"), BigDecimal(-1.quo(3), 21)) - assert_raise(ArgumentError) { BigDecimal(1.quo(3)) } + def test_BigDecimal_with_exception_keyword + assert_raise(ArgumentError) { + BigDecimal('.', exception: true) + } + assert_nothing_raised(ArgumentError) { + assert_equal(nil, BigDecimal(".", exception: false)) + } + assert_raise(ArgumentError) { + BigDecimal("1", -1, exception: true) + } + assert_nothing_raised(ArgumentError) { + assert_equal(nil, BigDecimal("1", -1, exception: false)) + } + assert_raise(ArgumentError) { + BigDecimal(42.quo(7), exception: true) + } + assert_nothing_raised(ArgumentError) { + assert_equal(nil, BigDecimal(42.quo(7), exception: false)) + } + assert_raise(ArgumentError) { + BigDecimal(4.2, exception: true) + } + assert_nothing_raised(ArgumentError) { + assert_equal(nil, BigDecimal(4.2, exception: false)) + } + # TODO: support conversion from complex + # assert_raise(RangeError) { + # BigDecimal(1i, exception: true) + # } + # assert_nothing_raised(RangeError) { + # assert_equal(nil, BigDecimal(1i, exception: false)) + # } + assert_raise(TypeError) { + BigDecimal(nil, exception: true) + } + assert_nothing_raised(TypeError) { + assert_equal(nil, BigDecimal(nil, exception: false)) + } + assert_nothing_raised(TypeError) { + assert_equal(nil, BigDecimal(:test, exception: false)) + } + assert_nothing_raised(TypeError) { + assert_equal(nil, BigDecimal(Object.new, exception: false)) + } + # TODO: support to_d + # assert_nothing_raised(TypeError) { + # o = Object.new + # def o.to_d; 3.14; end + # assert_equal(3.14, BigDecimal(o, exception: false)) + # } + # assert_nothing_raised(RuntimeError) { + # o = Object.new + # def o.to_d; raise; end + # assert_equal(nil, BigDecimal(o, exception: false)) + # } end - def test_new_with_float - assert_equal(BigDecimal("0.1235"), BigDecimal(0.1234567, 4)) - assert_equal(BigDecimal("-0.1235"), BigDecimal(-0.1234567, 4)) - assert_raise(ArgumentError) { BigDecimal(0.1) } - assert_raise(ArgumentError) { BigDecimal(0.1, Float::DIG + 2) } - assert_nothing_raised { BigDecimal(0.1, Float::DIG + 1) } + def test_s_ver + assert_raise(NoMethodError, /undefined method `ver`/) { BigDecimal.ver } end - def test_new_with_big_decimal - assert_equal(BigDecimal(1), BigDecimal(BigDecimal(1))) - assert_equal(BigDecimal('+0'), BigDecimal(BigDecimal('+0'))) - assert_equal(BigDecimal('-0'), BigDecimal(BigDecimal('-0'))) - BigDecimal.save_exception_mode do - BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW, false) - BigDecimal.mode(BigDecimal::EXCEPTION_NaN, false) - assert_positive_infinite(BigDecimal(BigDecimal('Infinity'))) - assert_negative_infinite(BigDecimal(BigDecimal('-Infinity'))) - assert_nan(BigDecimal(BigDecimal('NaN'))) - end + def test_s_allocate + assert_raise(NoMethodError, /undefined method `allocate`/) { BigDecimal.allocate } end - def test_new_with_tainted_string - Thread.new { - $SAFE = 1 - BigDecimal('1'.taint) - }.join - ensure - $SAFE = 0 + def test_s_new + assert_raise(NoMethodError, /undefined method `new`/) { BigDecimal.new("1") } end def _test_mode(type) @@ -1778,6 +1797,12 @@ class TestBigDecimal < Test::Unit::TestCase EOS end + def test_frozen_p + x = BigDecimal(1) + assert(x.frozen?) + assert((x + x).frozen?) + end + def test_clone assert_warning(/^$/) do x = BigDecimal(0) @@ -1795,14 +1820,8 @@ class TestBigDecimal < Test::Unit::TestCase end def test_dup_subclass - assert_warning(/BigDecimal\.new is deprecated/) do - c = Class.new(BigDecimal) - x = c.new(1) - y = x.dup - assert_same(x, y) - assert_equal(1, y) - assert_kind_of(c, y) - end + c = Class.new(BigDecimal) + assert_raise(NoMethodError, /undefined method `new`/) { c.new(1) } end def test_to_d @@ -1834,7 +1853,7 @@ class TestBigDecimal < Test::Unit::TestCase assert_no_memory_leak("BigDecimal()") end - def test_no_memory_leak_global_new + def test_no_memory_leak_BigDecimal assert_no_memory_leak("BigDecimal('10')") assert_no_memory_leak("BigDecimal(b)") end diff --git a/test/bigdecimal/test_bigdecimal_util.rb b/test/bigdecimal/test_bigdecimal_util.rb index fd457f6bf5..04c8eb2b46 100644 --- a/test/bigdecimal/test_bigdecimal_util.rb +++ b/test/bigdecimal/test_bigdecimal_util.rb @@ -12,6 +12,8 @@ class TestBigDecimalUtil < Test::Unit::TestCase def test_Integer_to_d assert_equal(BigDecimal(1), 1.to_d) assert_equal(BigDecimal(2<<100), (2<<100).to_d) + + assert(1.to_d.frozen?) end def test_Float_to_d_without_precision @@ -22,6 +24,11 @@ class TestBigDecimalUtil < Test::Unit::TestCase bug9214 = '[ruby-core:58858]' assert_equal((-0.0).to_d.sign, -1, bug9214) + + assert_raise(TypeError) { 0.3.to_d(nil) } + assert_raise(TypeError) { 0.3.to_d(false) } + + assert(1.1.to_d.frozen?) end def test_Float_to_d_with_precision @@ -32,6 +39,8 @@ class TestBigDecimalUtil < Test::Unit::TestCase bug9214 = '[ruby-core:58858]' assert_equal((-0.0).to_d(digits).sign, -1, bug9214) + + assert(1.1.to_d(digits).frozen?) end def test_Rational_to_d @@ -39,6 +48,8 @@ class TestBigDecimalUtil < Test::Unit::TestCase delta = 1.0/10**(digits) assert_in_delta(BigDecimal(1.quo(2), digits), 1.quo(2).to_d(digits), delta) assert_in_delta(BigDecimal(355.quo(113), digits), 355.quo(113).to_d(digits), delta) + + assert(355.quo(113).to_d(digits).frozen?) end def test_Rational_to_d_with_zero_precision @@ -50,11 +61,30 @@ class TestBigDecimalUtil < Test::Unit::TestCase end def test_String_to_d - assert_equal("2.5".to_d, BigDecimal('2.5')) + assert_equal(BigDecimal('1'), "1__1_1".to_d) + assert_equal(BigDecimal('2.5'), "2.5".to_d) + assert_equal(BigDecimal('2.5'), "2.5 degrees".to_d) + assert_equal(BigDecimal('2.5e1'), "2.5e1 degrees".to_d) + assert_equal(BigDecimal('0'), "degrees 100.0".to_d) + assert_equal(BigDecimal('0.125'), "0.1_2_5".to_d) + assert_equal(BigDecimal('0.125'), "0.1_2_5__".to_d) + assert_equal(BigDecimal('1'), "1_.125".to_d) + assert_equal(BigDecimal('1'), "1._125".to_d) + assert_equal(BigDecimal('0.1'), "0.1__2_5".to_d) + assert_equal(BigDecimal('0.1'), "0.1_e10".to_d) + assert_equal(BigDecimal('0.1'), "0.1e_10".to_d) + assert_equal(BigDecimal('1'), "0.1e1__0".to_d) + + assert("2.5".to_d.frozen?) end def test_invalid_String_to_d assert_equal("invalid".to_d, BigDecimal('0.0')) end + def test_Nil_to_d + assert_equal(nil.to_d, BigDecimal('0.0')) + + assert(nil.to_d) + end end |