mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* 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
This commit is contained in:
parent
0b35e9e5dd
commit
c8860b504e
3 changed files with 130 additions and 9 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue