Unverified Commit fd4e3740 authored by Yukihiro "Matz" Matsumoto's avatar Yukihiro "Matz" Matsumoto Committed by GitHub

Merge pull request #4557 from shuujii/fix-Numeric-step-to-infinity

Fix `Numeric#step` to infinity; ref. #4555
parents 519f3963 32c2aa10
......@@ -105,14 +105,14 @@ module Integral
return to_enum(:step, num, step) unless block
i = __coerce_step_counter(num, step)
if num == nil
if num == self || step.infinite?
block.call(i) if step > 0 && i <= (num||i) || step < 0 && i >= (num||-i)
elsif num == nil
while true
block.call(i)
i += step
end
return self
end
if step > 0
elsif step > 0
while i <= num
block.call(i)
i += step
......
##
# Numeric ISO Test
def assert_step(exp, receiver, args, inf: false)
act = []
ret = receiver.step(*args) do |i|
act << i
break if inf && exp.size == act.size
end
expr = "#{receiver.inspect}.step(#{args.map(&:inspect).join(', ')})"
assert do
assert_true(exp.eql?(act), "#{expr}: counters", assertion_diff(exp, act))
assert_same(receiver, ret, "#{expr}: return value") unless inf
end
end
assert('Numeric', '15.2.7') do
assert_equal(Class, Numeric.class)
end
......@@ -42,31 +55,60 @@ assert('Numeric#**') do
end
assert('Numeric#step') do
assert_step = ->(exp, receiver, args) do
inf = !args[0]
act = []
ret = receiver.step(*args) do |i|
act << i
break if inf && exp.size == act.size
end
expr = "#{receiver.inspect}.step(#{args.map(&:inspect).join(', ')})"
assert_true(exp.eql?(act), "#{expr}: counters", assertion_diff(exp, act))
assert_same(receiver, ret, "#{expr}: return value") unless inf
end
assert_raise(ArgumentError) { 1.step(2, 0) { break } }
assert_step.([2, 3, 4], 2, [4])
assert_step.([10, 8, 6, 4, 2], 10, [1, -2])
assert_step.([], 2, [1, 3])
assert_step.([], -2, [-1, -3])
assert_step.([10, 11, 12, 13], 10, [])
assert_step.([10, 7, 4], 10, [nil, -3])
assert_step([2, 3, 4], 2, [4])
assert_step([10, 8, 6, 4, 2], 10, [1, -2])
assert_step([], 2, [1, 3])
assert_step([], -2, [-1, -3])
assert_step([10, 11, 12, 13], 10, [], inf: true)
assert_step([10, 7, 4], 10, [nil, -3], inf: true)
skip unless Object.const_defined?(:Float)
inf = Float::INFINITY
assert_raise(ArgumentError) { 1.step(2, 0.0) { break } }
assert_step.([2.0, 3.0, 4.0], 2, [4.0])
assert_step.([7.0, 4.0, 1.0, -2.0], 7, [-4, -3.0])
assert_step.([2.0, 3.0, 4.0], 2.0, [4])
assert_step.([10.0, 11.0, 12.0, 13.0], 10.0, [])
assert_step.([10.0, 7.0, 4.0], 10, [nil, -3.0])
assert_step([2.0, 3.0, 4.0], 2, [4.0])
assert_step([7.0, 4.0, 1.0, -2.0], 7, [-4, -3.0])
assert_step([2.0, 3.0, 4.0], 2.0, [4])
assert_step([10.0, 11.0, 12.0, 13.0], 10.0, [], inf: true)
assert_step([10.0, 7.0, 4.0], 10, [nil, -3.0], inf: true)
assert_step([1.0], 1, [nil, inf])
assert_step([1.0], 1, [nil, -inf])
assert_step([1.0], 1, [3, inf])
assert_step([], 1, [-3, inf])
assert_step([], 1, [3, -inf])
assert_step([1.0], 1, [-3, -inf])
assert_step([1.0], 1, [inf, inf])
assert_step([], 1, [inf, -inf])
assert_step([], 1, [-inf, inf])
assert_step([1.0], 1, [-inf, -inf])
assert_step([], inf, [2])
assert_step([], inf, [-2])
assert_step([], inf, [2, 3])
assert_step([inf, inf, inf], inf, [2, -3], inf: true)
assert_step([], inf, [2, inf])
assert_step([inf], inf, [2, -inf])
assert_step([], inf, [-2, inf])
assert_step([inf], inf, [-2, -inf])
assert_step([], inf, [-2, 3])
assert_step([inf, inf, inf], inf, [-2, -3], inf: true)
assert_step([inf], inf, [inf])
assert_step([], inf, [-inf])
assert_step([inf], inf, [inf, inf])
assert_step([inf], inf, [inf, -inf])
assert_step([inf], inf, [-inf, -inf])
assert_step([-inf, -inf, -inf], -inf, [2], inf: true)
assert_step([-inf, -inf, -inf], -inf, [-2], inf: true)
assert_step([-inf, -inf, -inf], -inf, [2, 3], inf: true)
assert_step([], -inf, [2, -3])
assert_step([-inf], -inf, [2, inf])
assert_step([], -inf, [2, -inf])
assert_step([-inf], -inf, [-2, inf])
assert_step([], -inf, [-2, -inf])
assert_step([-inf, -inf, -inf], -inf, [-2, 3], inf: true)
assert_step([], -inf, [-2, -3])
assert_step([-inf, -inf, -inf], -inf, [inf], inf: true)
assert_step([-inf], -inf, [-inf])
assert_step([-inf], -inf, [inf, inf])
assert_step([], -inf, [inf, -inf])
assert_step([-inf], -inf, [-inf, -inf])
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment