diff options
author | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2009-09-22 01:35:53 +0000 |
---|---|---|
committer | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2009-09-22 01:35:53 +0000 |
commit | 3abf99a3e91618bf4454e45106ce9f0c749e7338 (patch) | |
tree | 6e7b5fa6f38615d11a5e30f97a36d4508528f389 /test | |
parent | c869363a52a72794902dd714cec5cd41571c7f6c (diff) | |
download | ruby-3abf99a3e91618bf4454e45106ce9f0c749e7338.tar.gz ruby-3abf99a3e91618bf4454e45106ce9f0c749e7338.tar.xz ruby-3abf99a3e91618bf4454e45106ce9f0c749e7338.zip |
* enum.c (enum_chunk): new method Enumerable#chunk.
* enum.c (enum_slice_before): new method Enumerable#slice_before.
[ruby-dev:38392] [ruby-dev:39240]
git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@25032 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test')
-rw-r--r-- | test/ruby/test_enum.rb | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/test/ruby/test_enum.rb b/test/ruby/test_enum.rb index 10695ccba..2a5bd5eaa 100644 --- a/test/ruby/test_enum.rb +++ b/test/ruby/test_enum.rb @@ -315,4 +315,74 @@ class TestEnumerable < Test::Unit::TestCase ensure $, = ofs end + + def test_chunk + e = [].chunk {|elt| true } + assert_equal([], e.to_a) + + e = @obj.chunk {|elt| elt & 2 == 0 ? false : true } + assert_equal([[false, [1]], [true, [2, 3]], [false, [1]], [true, [2]]], e.to_a) + + e = @obj.chunk(acc: 0) {|elt, h| h[:acc] += elt; h[:acc].even? } + assert_equal([[false, [1,2]], [true, [3]], [false, [1,2]]], e.to_a) + assert_equal([[false, [1,2]], [true, [3]], [false, [1,2]]], e.to_a) # this tests h is duplicated. + + hs = [{}] + e = [:foo].chunk(hs[0]) {|elt, h| + hs << h + true + } + assert_equal([[true, [:foo]]], e.to_a) + assert_equal([[true, [:foo]]], e.to_a) + assert_equal([{}, {}, {}], hs) + assert_not_same(hs[0], hs[1]) + assert_not_same(hs[0], hs[2]) + assert_not_same(hs[1], hs[2]) + + e = @obj.chunk {|elt| elt < 3 ? :_alone : true } + assert_equal([[:_alone, [1]], + [:_alone, [2]], + [true, [3]], + [:_alone, [1]], + [:_alone, [2]]], e.to_a) + + e = @obj.chunk {|elt| elt == 3 ? :_separator : true } + assert_equal([[true, [1, 2]], + [true, [1, 2]]], e.to_a) + + e = @obj.chunk {|elt| elt == 3 ? nil : true } + assert_equal([[true, [1, 2]], + [true, [1, 2]]], e.to_a) + + e = @obj.chunk {|elt| :_foo } + assert_raise(RuntimeError) { e.to_a } + end + + def test_slice_before + e = [].slice_before {|elt| true } + assert_equal([], e.to_a) + + e = @obj.slice_before {|elt| elt.even? } + assert_equal([[1], [2,3,1], [2]], e.to_a) + + e = @obj.slice_before {|elt| elt.odd? } + assert_equal([[1,2], [3], [1,2]], e.to_a) + + e = @obj.slice_before(acc: 0) {|elt, h| h[:acc] += elt; h[:acc].even? } + assert_equal([[1,2], [3,1,2]], e.to_a) + assert_equal([[1,2], [3,1,2]], e.to_a) # this tests h is duplicated. + + hs = [{}] + e = [:foo].slice_before(hs[0]) {|elt, h| + hs << h + true + } + assert_equal([[:foo]], e.to_a) + assert_equal([[:foo]], e.to_a) + assert_equal([{}, {}, {}], hs) + assert_not_same(hs[0], hs[1]) + assert_not_same(hs[0], hs[2]) + assert_not_same(hs[1], hs[2]) + end + end |