diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2014-04-04 10:33:17 +0900 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2014-04-04 10:33:17 +0900 |
| commit | 7a9630a5bb9534ab266aa225d728935ad648b861 (patch) | |
| tree | 9c25558f3b4369461e4fd1a6a5c78452c918d2f5 /mrbgems/mruby-enum-lazy/test | |
| parent | 4e5ec1993adb7f156edb08b9077734f8f9ef8b48 (diff) | |
| download | mruby-7a9630a5bb9534ab266aa225d728935ad648b861.tar.gz mruby-7a9630a5bb9534ab266aa225d728935ad648b861.zip | |
add mruby-enum-lazy mrbgem for Enumerable::Lazy
Diffstat (limited to 'mrbgems/mruby-enum-lazy/test')
| -rw-r--r-- | mrbgems/mruby-enum-lazy/test/lazy.rb | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/mrbgems/mruby-enum-lazy/test/lazy.rb b/mrbgems/mruby-enum-lazy/test/lazy.rb new file mode 100644 index 000000000..ca009d34c --- /dev/null +++ b/mrbgems/mruby-enum-lazy/test/lazy.rb @@ -0,0 +1,47 @@ +assert("Enumerable::Lazy") do + a = [1, 2] + assert_equal Enumerable::Lazy, a.lazy.class +end + +assert("Enumerable::Lazy laziness") do + a = Object.new + def a.each + return to_enum :each unless block_given? + self.b << 10 + yield 1 + self.b << 20 + yield 2 + self.b << 30 + yield 3 + self.b << 40 + yield 4 + self.b << 50 + yield 5 + end + def a.b(b=nil) + @b = b if b + @b + end + + a.b([]) + assert_equal [1,2], a.each.lazy.take(2).force + assert_equal [10,20], a.b + + a.b([]) + assert_equal [2,4], a.each.lazy.select{|x|x%2==0}.take(2).force + assert_equal [10,20,30,40], a.b + + a.b([]) + assert_equal [1], a.each.lazy.take_while{|x|x<2}.take(1).force + assert_equal [10], a.b + + a.b([]) + assert_equal [1], a.each.lazy.take_while{|x|x<2}.take(4).force + assert_equal [10,20], a.b +end + +assert("Enumerable::Lazy#zip with cycle") do + e1 = [1, 2, 3].cycle + e2 = [:a, :b].cycle + assert_equal [[1,:a],[2,:b],[3,:a]], e1.lazy.zip(e2).first(3) +end |
