diff options
| author | Yusuke Endoh <mame@ruby-lang.org> | 2024-11-08 14:46:35 +0900 |
|---|---|---|
| committer | Yusuke Endoh <mame@ruby-lang.org> | 2024-11-08 19:48:56 +0900 |
| commit | 45cd4a8296814f3b082dfb906cdef29974726731 (patch) | |
| tree | f0bb06b401fd95c09e9b44281473fafe5621c67b /test/ruby | |
| parent | f7b334e002eba25e386917337771b65bed5297f8 (diff) | |
Do not round `a**b` to infinity
... instead, just calculate the value unless it is too big.
Also, this change raises an ArgumentError if it is expected to exceed
16 GB in a 64-bit environment.
(It is possible to calculate it straightforward, but it would likely be
out-of-memory, so I didn't think it would make sense.)
[Feature #20811]
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/12033
Diffstat (limited to 'test/ruby')
| -rw-r--r-- | test/ruby/test_bignum.rb | 4 | ||||
| -rw-r--r-- | test/ruby/test_integer.rb | 9 | ||||
| -rw-r--r-- | test/ruby/test_rational.rb | 9 |
3 files changed, 10 insertions, 12 deletions
diff --git a/test/ruby/test_bignum.rb b/test/ruby/test_bignum.rb index 1858793952..beef33e2a6 100644 --- a/test/ruby/test_bignum.rb +++ b/test/ruby/test_bignum.rb @@ -476,8 +476,8 @@ class TestBignum < Test::Unit::TestCase def test_pow assert_equal(1.0, T32 ** 0.0) assert_equal(1.0 / T32, T32 ** -1) - assert_equal(1, assert_warning(/may be too big/) {T32 ** T32}.infinite?) - assert_equal(1, assert_warning(/may be too big/) {T32 ** (2**30-1)}.infinite?) + assert_raise(ArgumentError) { T32 ** T32 } + assert_raise(ArgumentError) { T32 ** (2**30-1) } ### rational changes the behavior of Bignum#** #assert_raise(TypeError) { T32**"foo" } diff --git a/test/ruby/test_integer.rb b/test/ruby/test_integer.rb index c2cad36aa4..7819e03e28 100644 --- a/test/ruby/test_integer.rb +++ b/test/ruby/test_integer.rb @@ -57,20 +57,19 @@ class TestInteger < Test::Unit::TestCase nil end, "[ruby-dev:32084] [ruby-dev:34547]") - x = EnvUtil.suppress_warning {2 ** -0x4000000000000000} - assert_in_delta(0.0, (x / 2), Float::EPSILON) + assert_raise(ArgumentError) {2 ** -0x4000000000000000} <<~EXPRS.each_line.with_index(__LINE__+1) do |expr, line| crash01: 111r+11**-11111161111111 crash02: 1118111111111**-1111111111111111**1+1==11111 - crash03: -1111111**-1111*11 - -1111111** -111111111 + crash03: -1111111**-1111*11 - -11** -11111111 crash04: 1118111111111** -1111111111111111**1+11111111111**1 ===111 crash05: 11** -111155555555555555 -55 !=5-555 crash07: 1 + 111111111**-1111811111 crash08: 18111111111**-1111111111111111**1 + 1111111111**-1111**1 crash10: -7 - -1111111** -1111**11 crash12: 1118111111111** -1111111111111111**1 + 1111 - -1111111** -1111*111111111119 - crash13: 1.0i - -1111111** -111111111 + crash13: 1.0i - -11** -11111111 crash14: 11111**111111111**111111 * -11111111111111111111**-111111111111 crash15: ~1**1111 + -~1**~1**111 crash17: 11** -1111111**1111 /11i @@ -80,7 +79,7 @@ class TestInteger < Test::Unit::TestCase crash21: 11**-10111111119-1i -1r EXPRS name, expr = expr.split(':', 2) - assert_ruby_status(%w"-W0", expr, name) + assert_ruby_status(%w"-W0", "begin; #{ expr }; rescue ArgumentError; end", name) end end diff --git a/test/ruby/test_rational.rb b/test/ruby/test_rational.rb index a51ce3dc99..89bb7b20a8 100644 --- a/test/ruby/test_rational.rb +++ b/test/ruby/test_rational.rb @@ -1066,11 +1066,10 @@ class Rational_Test < Test::Unit::TestCase end def test_power_overflow - bug = '[ruby-core:79686] [Bug #13242]: Infinity due to overflow' - x = EnvUtil.suppress_warning {4r**40000000} - assert_predicate x, :infinite?, bug - x = EnvUtil.suppress_warning {(1/4r)**40000000} - assert_equal 0, x, bug + assert_raise(ArgumentError) { 4r**400000000000000000000 } + exp = 4**40000000 + assert_equal exp, 4r**40000000 + assert_equal 1r/exp, (1/4r)**40000000 end def test_positive_p |
