summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormarcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-11-05 22:14:25 (GMT)
committermarcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-11-05 22:14:25 (GMT)
commitdcb8a22f2670626198ecf1c805b53bd13350d899 (patch)
tree81e00f65c2fb3f233816186246e5deaeef6d2ee9
parent3280f54168069d207834d9d15b0fb2ffa17168ce (diff)
Fix size on Enumerable#cycle when the size is 0 [Bug #14082].
Patch by Kenichi Kamiya git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60666 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--enum.c1
-rw-r--r--test/ruby/test_enumerator.rb7
2 files changed, 8 insertions, 0 deletions
diff --git a/enum.c b/enum.c
index 4613ab7..2b2806a 100644
--- a/enum.c
+++ b/enum.c
@@ -2845,6 +2845,7 @@ enum_cycle_size(VALUE self, VALUE args, VALUE eobj)
VALUE size = enum_size(self, args, 0);
if (size == Qnil) return Qnil;
+ if (FIXNUM_ZERO_P(size)) return size;
if (args && (RARRAY_LEN(args) > 0)) {
n = RARRAY_AREF(args, 0);
diff --git a/test/ruby/test_enumerator.rb b/test/ruby/test_enumerator.rb
index bd6b7fa..0ee11da 100644
--- a/test/ruby/test_enumerator.rb
+++ b/test/ruby/test_enumerator.rb
@@ -576,13 +576,20 @@ class TestEnumerator < Test::Unit::TestCase
assert_equal Float::INFINITY, [:foo].cycle.size
assert_equal 10, [:foo, :bar].cycle(5).size
assert_equal 0, [:foo, :bar].cycle(-10).size
+ assert_equal Float::INFINITY, {foo: 1}.cycle.size
+ assert_equal 10, {foo: 1, bar: 2}.cycle(5).size
+ assert_equal 0, {foo: 1, bar: 2}.cycle(-10).size
assert_equal 0, [].cycle.size
assert_equal 0, [].cycle(5).size
+ assert_equal 0, {}.cycle.size
+ assert_equal 0, {}.cycle(5).size
assert_equal nil, @obj.cycle.size
assert_equal nil, @obj.cycle(5).size
assert_equal Float::INFINITY, @sized.cycle.size
assert_equal 126, @sized.cycle(3).size
+ assert_equal Float::INFINITY, [].to_enum { 42 }.cycle.size
+ assert_equal 0, [].to_enum { 0 }.cycle.size
end
def test_size_for_loops