1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* enum.c (rb_enum_values_pack): rename from enum_values_pack, and

remove static.

* enumerator.c (lazy_init_iterator, lazy_init_yielder,
  lazy_select_func, lazy_reject_func, lazy_grep_func): handle
  multiple values correctly.

* enumerator.c (lazy_grep): change the behavior when a block is
  given, to be consistent with Enumerable#grep.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35043 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
shugo 2012-03-15 14:20:27 +00:00
parent d135138f9b
commit 2baeb78294
5 changed files with 120 additions and 41 deletions

View file

@ -72,6 +72,18 @@ class TestLazyEnumerator < Test::Unit::TestCase
assert_equal("word", a.current)
end
def test_select_multiple_values
e = Enumerator.new { |yielder|
for i in 1..5
yielder.yield(i, i.to_s)
end
}
assert_equal([[2, "2"], [4, "4"]],
e.select {|x| x[0] % 2 == 0})
assert_equal([[2, "2"], [4, "4"]],
e.lazy.select {|x| x[0] % 2 == 0}.force)
end
def test_map
a = Step.new(1..3)
assert_equal(2, a.map {|x| x * 2}.first)
@ -112,6 +124,18 @@ class TestLazyEnumerator < Test::Unit::TestCase
assert_equal(nil, a.current)
end
def test_reject_multiple_values
e = Enumerator.new { |yielder|
for i in 1..5
yielder.yield(i, i.to_s)
end
}
assert_equal([[2, "2"], [4, "4"]],
e.reject {|x| x[0] % 2 != 0})
assert_equal([[2, "2"], [4, "4"]],
e.lazy.reject {|x| x[0] % 2 != 0}.force)
end
def test_grep
a = Step.new('a'..'f')
assert_equal('c', a.grep(/c/).first)
@ -122,6 +146,24 @@ class TestLazyEnumerator < Test::Unit::TestCase
assert_equal(%w[a e], a.lazy.grep(proc {|x| /[aeiou]/ =~ x}).to_a)
end
def test_grep_with_block
a = Step.new('a'..'f')
assert_equal('C', a.grep(/c/) {|i| i.upcase}.first)
assert_equal('C', a.lazy.grep(/c/) {|i| i.upcase}.first)
end
def test_grep_multiple_values
e = Enumerator.new { |yielder|
3.times { |i|
yielder.yield(i, i.to_s)
}
}
assert_equal([[2, "2"]], e.grep(proc {|x| x == [2, "2"]}))
assert_equal([[2, "2"]], e.lazy.grep(proc {|x| x == [2, "2"]}).force)
assert_equal(["22"],
e.lazy.grep(proc {|x| x == [2, "2"]}, &:join).force)
end
def test_zip
a = Step.new(1..3)
assert_equal([1, "a"], a.zip("a".."c").first)