mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Make size on an infinite each_slice enumerator return Infinity
Fixes [Bug #15889]
This commit is contained in:
parent
f48aad7ba2
commit
17af8bfce6
2 changed files with 10 additions and 0 deletions
5
enum.c
5
enum.c
|
@ -2527,10 +2527,15 @@ enum_each_slice_size(VALUE obj, VALUE args, VALUE eobj)
|
|||
{
|
||||
VALUE n, size;
|
||||
long slice_size = NUM2LONG(RARRAY_AREF(args, 0));
|
||||
ID infinite_p;
|
||||
CONST_ID(infinite_p, "infinite?");
|
||||
if (slice_size <= 0) rb_raise(rb_eArgError, "invalid slice size");
|
||||
|
||||
size = enum_size(obj, 0, 0);
|
||||
if (size == Qnil) return Qnil;
|
||||
if (RB_FLOAT_TYPE_P(size) && RTEST(rb_funcall(size, infinite_p, 0))) {
|
||||
return size;
|
||||
}
|
||||
|
||||
n = add_int(size, slice_size-1);
|
||||
return div_int(n, slice_size);
|
||||
|
|
|
@ -113,6 +113,11 @@ class TestEnumerator < Test::Unit::TestCase
|
|||
assert_equal([[1,2,3],[4,5,6],[7,8,9],[10]], (1..10).each_slice(3).to_a)
|
||||
end
|
||||
|
||||
def test_each_slice_size
|
||||
assert_equal(4, (1..10).each_slice(3).size)
|
||||
assert_equal(Float::INFINITY, 1.step.each_slice(3).size)
|
||||
end
|
||||
|
||||
def test_cons
|
||||
a = [[1,2,3], [2,3,4], [3,4,5], [4,5,6], [5,6,7], [6,7,8], [7,8,9], [8,9,10]]
|
||||
assert_equal(a, (1..10).each_cons(3).to_a)
|
||||
|
|
Loading…
Add table
Reference in a new issue