From c8860b504e0a31b72725f130a2c43670908cb1b8 Mon Sep 17 00:00:00 2001 From: shugo Date: Wed, 14 Mar 2012 13:04:18 +0000 Subject: * enumerator.c (lazy_init_iterator): break when Qundef is returned to make obj.drop(3).take(2) work properly. * enumerator.c (lazy_take_while): add Enumerable::Lazy#take_while. * enumerator.c (lazy_drop): add Enumerable::Lazy#drop. * enumerator.c (lazy_drop_while): add Enumerable::Lazy#drop_while. * enumerator.c (InitVM_Enumerator): add Enumerable::Lazy#force as an alias of to_a. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35019 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/ruby/test_lazy_enumerator.rb | 44 +++++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) (limited to 'test/ruby/test_lazy_enumerator.rb') diff --git a/test/ruby/test_lazy_enumerator.rb b/test/ruby/test_lazy_enumerator.rb index 3adaaa32f3..6b4d49a041 100644 --- a/test/ruby/test_lazy_enumerator.rb +++ b/test/ruby/test_lazy_enumerator.rb @@ -145,10 +145,46 @@ class TestLazyEnumerator < Test::Unit::TestCase end def test_take - a = Step.new(1..3) - assert_equal(1, a.take(2).first) - assert_equal(2, a.current) - assert_equal(1, a.lazy.take(2).first) + a = Step.new(1..10) + assert_equal(1, a.take(5).first) + assert_equal(5, a.current) + assert_equal(1, a.lazy.take(5).first) + assert_equal(1, a.current) + assert_equal((1..5).to_a, a.lazy.take(5).to_a) + end + + def test_take_while + a = Step.new(1..10) + assert_equal(1, a.take_while {|i| i < 5}.first) + assert_equal(5, a.current) + assert_equal(1, a.lazy.take_while {|i| i < 5}.first) assert_equal(1, a.current) + assert_equal((1..4).to_a, a.lazy.take_while {|i| i < 5}.to_a) + end + + def test_drop + a = Step.new(1..10) + assert_equal(6, a.drop(5).first) + assert_equal(10, a.current) + assert_equal(6, a.lazy.drop(5).first) + assert_equal(6, a.current) + assert_equal((6..10).to_a, a.lazy.drop(5).to_a) + end + + def test_drop_while + a = Step.new(1..10) + assert_equal(5, a.drop_while {|i| i < 5}.first) + assert_equal(10, a.current) + assert_equal(5, a.lazy.drop_while {|i| i < 5}.first) + assert_equal(5, a.current) + assert_equal((5..10).to_a, a.lazy.drop_while {|i| i < 5}.to_a) + end + + def test_drop_and_take + assert_equal([4, 5], (1..Float::INFINITY).lazy.drop(3).take(2).to_a) + end + + def test_force + assert_equal([1, 2, 3], (1..Float::INFINITY).lazy.take(3).force) end end -- cgit v1.2.3