source: EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-enum-lazy/test/lazy.rb@ 439

Last change on this file since 439 was 439, checked in by coas-nagasima, 4 years ago

mrubyを2.1.1に更新

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-ruby;charset=UTF-8
File size: 1.2 KB
Line 
1assert("Enumerator::Lazy") do
2 a = [1, 2]
3 assert_equal Enumerator::Lazy, a.lazy.class
4end
5
6assert("Enumerator::Lazy laziness") do
7 a = Object.new
8 def a.each
9 return to_enum :each unless block_given?
10 self.b << 10
11 yield 1
12 self.b << 20
13 yield 2
14 self.b << 30
15 yield 3
16 self.b << 40
17 yield 4
18 self.b << 50
19 yield 5
20 end
21 def a.b(b=nil)
22 @b = b if b
23 @b
24 end
25
26 a.b([])
27 assert_equal [1,2], a.each.lazy.take(2).force
28 assert_equal [10,20], a.b
29
30 a.b([])
31 assert_equal [2,4], a.each.lazy.select{|x|x%2==0}.take(2).force
32 assert_equal [10,20,30,40], a.b
33
34 a.b([])
35 assert_equal [1], a.each.lazy.take_while{|x|x<2}.take(1).force
36 assert_equal [10], a.b
37
38 a.b([])
39 assert_equal [1], a.each.lazy.take_while{|x|x<2}.take(4).force
40 assert_equal [10,20], a.b
41end
42
43assert("Enumerator::Lazy#to_enum") do
44 lazy_enum = (0..Float::INFINITY).lazy.to_enum(:each_slice, 2)
45 assert_kind_of Enumerator::Lazy, lazy_enum
46 assert_equal [0*1, 2*3, 4*5, 6*7], lazy_enum.map { |a| a.first * a.last }.first(4)
47end
48
49assert("Enumerator::Lazy#zip with cycle") do
50 e1 = [1, 2, 3].cycle
51 e2 = [:a, :b].cycle
52 assert_equal [[1,:a],[2,:b],[3,:a]], e1.lazy.zip(e2).first(3)
53end
Note: See TracBrowser for help on using the repository browser.