From f516379853f36d143d820c55d5eeaa9fc410ef52 Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Thu, 29 Apr 2021 12:51:05 -0700 Subject: 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] --- test/ruby/test_float.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'test/ruby') 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 -- cgit v1.2.3