diff options
author | Jeremy Evans <[email protected]> | 2021-04-29 12:51:05 -0700 |
---|---|---|
committer | Jeremy Evans <[email protected]> | 2021-05-29 08:56:15 -0700 |
commit | f516379853f36d143d820c55d5eeaa9fc410ef52 (patch) | |
tree | e5a9e14896dee5767d0d33834613bd56c590ce66 /test/ruby | |
parent | e56ba6231f77dd0aa88a1ce737a342baafc884c7 (diff) |
Fix Enumerator::ArithmeticSequence handling of float ranges
Depending on the float range, there could be an off-by-one error,
where the last result that should be in the range was missed. Fix
this by checking if the computed value for the expected value
outside the range is still inside the range, and if so, increment
the step size.
Fixes [Bug #16612]
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/4434
Diffstat (limited to 'test/ruby')
-rw-r--r-- | test/ruby/test_float.rb | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/test/ruby/test_float.rb b/test/ruby/test_float.rb index fbf0d87f8e..ca616456f1 100644 --- a/test/ruby/test_float.rb +++ b/test/ruby/test_float.rb @@ -882,6 +882,11 @@ class TestFloat < Test::Unit::TestCase end assert_equal([5.0, 4.0, 3.0, 2.0], 5.0.step(1.5, -1).to_a) + + assert_equal(11, ((0.24901079128550474)..(340.2500808898068)).step(34.00010700985213).to_a.size) + assert_equal(11, ((0.24901079128550474)..(340.25008088980684)).step(34.00010700985213).to_a.size) + assert_equal(11, ((-0.24901079128550474)..(-340.2500808898068)).step(-34.00010700985213).to_a.size) + assert_equal(11, ((-0.24901079128550474)..(-340.25008088980684)).step(-34.00010700985213).to_a.size) end def test_step2 @@ -893,6 +898,7 @@ class TestFloat < Test::Unit::TestCase a = rand b = a+rand*1000 s = (b - a) / 10 + b = a + s*10 seq = (a...b).step(s) assert_equal(10, seq.to_a.length, seq.inspect) end @@ -903,6 +909,11 @@ class TestFloat < Test::Unit::TestCase (1.0 ... e).step(1E-16) do |n| assert_operator(n, :<=, e) end + + assert_equal(10, ((0.24901079128550474)...(340.2500808898068)).step(34.00010700985213).to_a.size) + assert_equal(11, ((0.24901079128550474)...(340.25008088980684)).step(34.00010700985213).to_a.size) + assert_equal(10, ((-0.24901079128550474)...(-340.2500808898068)).step(-34.00010700985213).to_a.size) + assert_equal(11, ((-0.24901079128550474)...(-340.25008088980684)).step(-34.00010700985213).to_a.size) end def test_singleton_method |