summaryrefslogtreecommitdiff
path: root/test/ruby/test_enumerator.rb
blob: 6568cbff18f6004acacd9e5470909182a514b66e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
# frozen_string_literal: false
require 'test/unit'

class TestEnumerator < Test::Unit::TestCase
  def setup
    @obj = Object.new
    class << @obj
      include Enumerable
      def foo(*a)
        a.each {|x| yield x }
      end
    end
    @sized = @obj.clone
    def @sized.size
      42
    end
  end

  def enum_test obj
    obj.map{|e|
      e
    }.sort
  end

  def test_iterators
    assert_equal [0, 1, 2], enum_test(3.times)
    assert_equal [:x, :y, :z], enum_test([:x, :y, :z].each)
    assert_equal [[:x, 1], [:y, 2]], enum_test({:x=>1, :y=>2}.each)
  end

  ## Enumerator as Iterator

  def test_next
    e = 3.times
    3.times{|i|
      assert_equal i, e.next
    }
    assert_raise(StopIteration){e.next}
  end

  def test_loop
    e = 3.times
    i = 0
    loop{
      assert_equal(i, e.next)
      i += 1
    }
  end

  def test_loop_return_value
    assert_equal nil, loop { break }
    assert_equal 42,  loop { break 42 }

    e = Enumerator.new { |y| y << 1; y << 2; :stopped }
    assert_equal :stopped, loop { e.next while true }
  end

  def test_nested_iteration
    def (o = Object.new).each
      yield :ok1
      yield [:ok2, :x].each.next
    end
    e = o.to_enum
    assert_equal :ok1, e.next
    assert_equal :ok2, e.next
    assert_raise(StopIteration){e.next}
  end


  def test_initialize
    assert_equal([1, 2, 3], @obj.to_enum(:foo, 1, 2, 3).to_a)
    _, err = capture_io do
      assert_equal([1, 2, 3], Enumerator.new(@obj, :foo, 1, 2, 3).to_a)
    end
    assert_match 'Enumerator.new without a block is deprecated', err
    assert_equal([1, 2, 3], Enumerator.new { |y| i = 0; loop { y << (i += 1) } }.take(3))
    assert_raise(ArgumentError) { Enumerator.new }

    enum = @obj.to_enum
    assert_raise(NoMethodError) { enum.each {} }
    enum.freeze
    assert_raise(FrozenError) {
      capture_io do
        # warning: Enumerator.new without a block is deprecated; use Object#to_enum
        enum.__send__(:initialize, @obj, :foo)
      end
    }
  end

  def test_initialize_copy
    assert_equal([1, 2, 3], @obj.to_enum(:foo, 1, 2, 3).dup.to_a)
    e = @obj.to_enum(:foo, 1, 2, 3)
    assert_nothing_raised { assert_equal(1, e.next) }
    assert_raise(TypeError) { e.dup }

    e = Enumerator.new { |y| i = 0; loop { y << (i += 1) } }.dup
    assert_nothing_raised { assert_equal(1, e.next) }
    assert_raise(TypeError) { e.dup }
  end

  def test_gc
    assert_nothing_raised do
      1.times do
        foo = [1,2,3].to_enum
        GC.start
        foo
      end
      GC.start
    end
  end

  def test_slice
    assert_equal([[1,2,3],[4,5,6],[7,8,9],[10]], (1..10).each_slice(3).to_a)
  end

  def test_cons
    a = [[1,2,3], [2,3,4], [3,4,5], [4,5,6], [5,6,7], [6,7,8], [7,8,9], [8,9,10]]
    assert_equal(a, (1..10).each_cons(3).to_a)
  end

  def test_with_index
    assert_equal([[1,0],[2,1],[3,2]], @obj.to_enum(:foo, 1, 2, 3).with_index.to_a)
    assert_equal([[1,5],[2,6],[3,7]], @obj.to_enum(:foo, 1, 2, 3).with_index(5).to_a)
  end

  def test_with_index_large_offset
    bug8010 = '[ruby-dev:47131] [Bug #8010]'
    s = 1 << (8*1.size-2)
    assert_equal([[1,s],[2,s+1],[3,s+2]], @obj.to_enum(:foo, 1, 2, 3).with_index(s).to_a, bug8010)
    s <<= 1
    assert_equal([[1,s],[2,s+1],[3,s+2]], @obj.to_enum(:foo, 1, 2, 3).with_index(s).to_a, bug8010)
  end

  def test_with_index_nonnum_offset
    bug8010 = '[ruby-dev:47131] [Bug #8010]'
    s = Object.new
    def s.to_int; 1 end
    assert_equal([[1,1],[2,2],[3,3]], @obj.to_enum(:foo, 1, 2, 3).with_index(s).to_a, bug8010)
  end

  def test_with_index_string_offset
    bug8010 = '[ruby-dev:47131] [Bug #8010]'
    assert_raise(TypeError, bug8010){ @obj.to_enum(:foo, 1, 2, 3).with_index('1').to_a }
  end

  def test_with_index_dangling_memo
    bug9178 = '[ruby-core:58692] [Bug #9178]'
    assert_separately([], <<-"end;")
    bug = "#{bug9178}"
    e = [1].to_enum(:chunk).with_index {|c,i| i == 5}
    assert_kind_of(Enumerator, e)
    assert_equal([false, [1]], e.to_a[0], bug)
    end;
  end

  def test_with_object
    obj = [0, 1]
    ret = (1..10).each.with_object(obj) {|i, memo|
      memo[0] += i
      memo[1] *= i
    }
    assert_same(obj, ret)
    assert_equal([55, 3628800], ret)

    a = [2,5,2,1,5,3,4,2,1,0]
    obj = {}
    ret = a.delete_if.with_object(obj) {|i, seen|
      if seen.key?(i)
        true
      else
        seen[i] = true
        false
      end
    }
    assert_same(obj, ret)
    assert_equal([2, 5, 1, 3, 4, 0], a)
  end

  def test_next_rewind
    e = @obj.to_enum(:foo, 1, 2, 3)
    assert_equal(1, e.next)
    assert_equal(2, e.next)
    e.rewind
    assert_equal(1, e.next)
    assert_equal(2, e.next)
    assert_equal(3, e.next)
    assert_raise(StopIteration) { e.next }
  end

  def test_peek
    a = [1]
    e = a.each
    assert_equal(1, e.peek)
    assert_equal(1, e.peek)
    assert_equal(1, e.next)
    assert_raise(StopIteration) { e.peek }
    assert_raise(StopIteration) { e.peek }
  end

  def test_peek_modify
    o = Object.new
    def o.each
      yield 1,2
    end
    e = o.to_enum
    a = e.peek
    a << 3
    assert_equal([1,2], e.peek)
  end

  def test_peek_values_modify
    o = Object.new
    def o.each
      yield 1,2
    end
    e = o.to_enum
    a = e.peek_values
    a << 3
    assert_equal([1,2], e.peek)
  end

  def test_next_after_stopiteration
    a = [1]
    e = a.each
    assert_equal(1, e.next)
    assert_raise(StopIteration) { e.next }
    assert_raise(StopIteration) { e.next }
    e.rewind
    assert_equal(1, e.next)
    assert_raise(StopIteration) { e.next }
    assert_raise(StopIteration) { e.next }
  end

  def test_stop_result
    a = [1]
    res = a.each {}
    e = a.each
    assert_equal(1, e.next)
    exc = assert_raise(StopIteration) { e.next }
    assert_equal(res, exc.result)
  end

  def test_next_values
    o = Object.new
    def o.each
      yield
      yield 1
      yield 1, 2
    end
    e = o.to_enum
    assert_equal(nil, e.next)
    assert_equal(1, e.next)
    assert_equal([1,2], e.next)
    e = o.to_enum
    assert_equal([], e.next_values)
    assert_equal([1], e.next_values)
    assert_equal([1,2], e.next_values)
  end

  def test_peek_values
    o = Object.new
    def o.each
      yield
      yield 1
      yield 1, 2
    end
    e = o.to_enum
    assert_equal(nil, e.peek)
    assert_equal(nil, e.next)
    assert_equal(1, e.peek)
    assert_equal(1, e.next)
    assert_equal([1,2], e.peek)
    assert_equal([1,2], e.next)
    e = o.to_enum
    assert_equal([], e.peek_values)
    assert_equal([], e.next_values)
    assert_equal([1], e.peek_values)
    assert_equal([1], e.next_values)
    assert_equal([1,2], e.peek_values)
    assert_equal([1,2], e.next_values)
    e = o.to_enum
    assert_equal([], e.peek_values)
    assert_equal(nil, e.next)
    assert_equal([1], e.peek_values)
    assert_equal(1, e.next)
    assert_equal([1,2], e.peek_values)
    assert_equal([1,2], e.next)
    e = o.to_enum
    assert_equal(nil, e.peek)
    assert_equal([], e.next_values)
    assert_equal(1, e.peek)
    assert_equal([1], e.next_values)
    assert_equal([1,2], e.peek)
    assert_equal([1,2], e.next_values)
  end

  def test_each_arg
    o = Object.new
    def o.each(ary)
      ary << 1
      yield
    end
    ary = []
    e = o.to_enum.each(ary)
    e.next
    assert_equal([1], ary)
  end

  def test_feed
    o = Object.new
    def o.each(ary)
      ary << yield
      ary << yield
      ary << yield
    end
    ary = []
    e = o.to_enum(:each, ary)
    e.next
    e.feed 1
    e.next
    e.feed 2
    e.next
    e.feed 3
    assert_raise(StopIteration) { e.next }
    assert_equal([1,2,3], ary)
  end

  def test_feed_mixed
    o = Object.new
    def o.each(ary)
      ary << yield
      ary << yield
      ary << yield
    end
    ary = []
    e = o.to_enum(:each, ary)
    e.next
    e.feed 1
    e.next
    e.next
    e.feed 3
    assert_raise(StopIteration) { e.next }
    assert_equal([1,nil,3], ary)
  end

  def test_feed_twice
    o = Object.new
    def o.each(ary)
      ary << yield
      ary << yield
      ary << yield
    end
    ary = []
    e = o.to_enum(:each, ary)
    e.feed 1
    assert_raise(TypeError) { e.feed 2 }
  end

  def test_feed_before_first_next
    o = Object.new
    def o.each(ary)
      ary << yield
      ary << yield
      ary << yield
    end
    ary = []
    e = o.to_enum(:each, ary)
    e.feed 1
    e.next
    e.next
    assert_equal([1], ary)
  end

  def test_rewind_clear_feed
    o = Object.new
    def o.each(ary)
      ary << yield
      ary << yield
      ary << yield
    end
    ary = []
    e = o.to_enum(:each, ary)
    e.next
    e.feed 1
    e.next
    e.feed 2
    e.rewind
    e.next
    e.next
    assert_equal([1,nil], ary)
  end

  def test_feed_yielder
    x = nil
    e = Enumerator.new {|y| x = y.yield; 10 }
    e.next
    e.feed 100
    exc = assert_raise(StopIteration) { e.next }
    assert_equal(100, x)
    assert_equal(10, exc.result)
  end

  def test_inspect
    e = (0..10).each_cons(2)
    assert_equal("#<Enumerator: 0..10:each_cons(2)>", e.inspect)

    e = Enumerator.new {|y| y.yield; 10 }
    assert_match(/\A#<Enumerator: .*:each>/, e.inspect)

    a = []
    e = a.each_with_object(a)
    a << e
    assert_equal("#<Enumerator: [#<Enumerator: ...>]:each_with_object([#<Enumerator: ...>])>",
		e.inspect)
  end

  def test_inspect_verbose
    bug6214 = '[ruby-dev:45449]'
    assert_warning("", bug6214) { "".bytes.inspect }
    assert_warning("", bug6214) { [].lazy.inspect }
  end

  def test_inspect_encoding
    c = Class.new{define_method("\u{3042}"){}}
    e = c.new.enum_for("\u{3042}")
    s = assert_nothing_raised(Encoding::CompatibilityError) {break e.inspect}
    assert_equal(Encoding::UTF_8, s.encoding)
    assert_match(/\A#<Enumerator: .*:\u{3042}>\z/, s)
  end

  def test_generator
    # note: Enumerator::Generator is a class just for internal
    g = Enumerator::Generator.new {|y| y << 1 << 2 << 3; :foo }
    g2 = g.dup
    a = []
    assert_equal(:foo, g.each {|x| a << x })
    assert_equal([1, 2, 3], a)
    a = []
    assert_equal(:foo, g2.each {|x| a << x })
    assert_equal([1, 2, 3], a)

    g.freeze
    assert_raise(FrozenError) {
      g.__send__ :initialize, proc { |y| y << 4 << 5 }
    }

    g = Enumerator::Generator.new(proc {|y| y << 4 << 5; :foo })
    a = []
    assert_equal(:foo, g.each {|x| a << x })
    assert_equal([4, 5], a)

    assert_raise(LocalJumpError) {Enumerator::Generator.new}
    assert_raise(TypeError) {Enumerator::Generator.new(1)}
    obj = eval("class C\u{1f5ff}; self; end").new
    assert_raise_with_message(TypeError, /C\u{1f5ff}/) {
      Enumerator::Generator.new(obj)
    }
  end

  def test_generator_args
    g = Enumerator::Generator.new {|y, x| y << 1 << 2 << 3; x }
    a = []
    assert_equal(:bar, g.each(:bar) {|x| a << x })
    assert_equal([1, 2, 3], a)
  end

  def test_yielder
    # note: Enumerator::Yielder is a class just for internal
    a = []
    y = Enumerator::Yielder.new {|x| a << x }
    assert_equal(y, y << 1 << 2 << 3)
    assert_equal([1, 2, 3], a)

    a = []
    y = Enumerator::Yielder.new {|x| a << x }
    assert_equal([1], y.yield(1))
    assert_equal([1, 2], y.yield(2))
    assert_equal([1, 2, 3], y.yield(3))

    assert_raise(LocalJumpError) { Enumerator::Yielder.new }
  end

  def test_size
    assert_equal nil, Enumerator.new{}.size
    assert_equal 42, Enumerator.new(->{42}){}.size
    obj = Object.new
    def obj.call; 42; end
    assert_equal 42, Enumerator.new(obj){}.size
    assert_equal 42, Enumerator.new(42){}.size
    assert_equal 1 << 70, Enumerator.new(1 << 70){}.size
    assert_equal Float::INFINITY, Enumerator.new(Float::INFINITY){}.size
    assert_equal nil, Enumerator.new(nil){}.size
    assert_raise(TypeError) { Enumerator.new("42"){} }

    assert_equal nil, @obj.to_enum(:foo, 0, 1).size
    assert_equal 2, @obj.to_enum(:foo, 0, 1){ 2 }.size
  end

  def test_size_for_enum_created_by_enumerators
    enum = to_enum{ 42 }
    assert_equal 42, enum.with_index.size
    assert_equal 42, enum.with_object(:foo).size
  end

  def test_size_for_enum_created_from_array
    arr = %w[hello world]
    %i[each each_with_index reverse_each sort_by! sort_by map map!
      keep_if reject! reject select! select filter! filter delete_if].each do |method|
      assert_equal arr.size, arr.send(method).size
    end
  end

  def test_size_for_enum_created_from_enumerable
    %i[find_all reject map flat_map partition group_by sort_by min_by max_by
      minmax_by each_with_index reverse_each each_entry].each do |method|
      assert_equal nil, @obj.send(method).size
      assert_equal 42, @sized.send(method).size
    end
    assert_equal nil, @obj.each_with_object(nil).size
    assert_equal 42, @sized.each_with_object(nil).size
  end

  def test_size_for_enum_created_from_hash
    h = {a: 1, b: 2, c: 3}
    methods = %i[delete_if reject reject! select select! filter filter! keep_if each each_key each_pair]
    enums = methods.map {|method| h.send(method)}
    s = enums.group_by(&:size)
    assert_equal([3], s.keys, ->{s.reject!{|k| k==3}.inspect})
    h[:d] = 4
    s = enums.group_by(&:size)
    assert_equal([4], s.keys, ->{s.reject!{|k| k==4}.inspect})
  end

  def test_size_for_enum_created_from_env
    %i[each_pair reject! delete_if select select! filter filter! keep_if].each do |method|
      assert_equal ENV.size, ENV.send(method).size
    end
  end

  def test_size_for_enum_created_from_struct
    s = Struct.new(:foo, :bar, :baz).new(1, 2)
    %i[each each_pair select].each do |method|
      assert_equal 3, s.send(method).size
    end
  end

  def check_consistency_for_combinatorics(method)
    [ [], [:a, :b, :c, :d, :e] ].product([-2, 0, 2, 5, 6]) do |array, arg|
      assert_equal array.send(method, arg).to_a.size, array.send(method, arg).size,
        "inconsistent size for #{array}.#{method}(#{arg})"
    end
  end

  def test_size_for_array_combinatorics
    check_consistency_for_combinatorics(:permutation)
    assert_equal 24, [0, 1, 2, 4].permutation.size
    assert_equal 2933197128679486453788761052665610240000000,
      (1..42).to_a.permutation(30).size # 1.upto(42).inject(:*) / 1.upto(12).inject(:*)

    check_consistency_for_combinatorics(:combination)
    assert_equal 28258808871162574166368460400,
      (1..100).to_a.combination(42).size
      # 1.upto(100).inject(:*) / 1.upto(42).inject(:*) / 1.upto(58).inject(:*)

    check_consistency_for_combinatorics(:repeated_permutation)
    assert_equal 291733167875766667063796853374976,
      (1..42).to_a.repeated_permutation(20).size # 42 ** 20

    check_consistency_for_combinatorics(:repeated_combination)
    assert_equal 28258808871162574166368460400,
      (1..59).to_a.repeated_combination(42).size
      # 1.upto(100).inject(:*) / 1.upto(42).inject(:*) / 1.upto(58).inject(:*)
  end

  def test_size_for_cycle
    assert_equal Float::INFINITY, [:foo].cycle.size
    assert_equal 10, [:foo, :bar].cycle(5).size
    assert_equal 0,  [:foo, :bar].cycle(-10).size
    assert_equal Float::INFINITY, {foo: 1}.cycle.size
    assert_equal 10, {foo: 1, bar: 2}.cycle(5).size
    assert_equal 0,  {foo: 1, bar: 2}.cycle(-10).size
    assert_equal 0,  [].cycle.size
    assert_equal 0,  [].cycle(5).size
    assert_equal 0,  {}.cycle.size
    assert_equal 0,  {}.cycle(5).size

    assert_equal nil, @obj.cycle.size
    assert_equal nil, @obj.cycle(5).size
    assert_equal Float::INFINITY, @sized.cycle.size
    assert_equal 126, @sized.cycle(3).size
    assert_equal Float::INFINITY, [].to_enum { 42 }.cycle.size
    assert_equal 0, [].to_enum { 0 }.cycle.size

    assert_raise(TypeError) {[].to_enum { 0 }.cycle("").size}
  end

  def test_size_for_loops
    assert_equal Float::INFINITY, loop.size
    assert_equal 42, 42.times.size
  end

  def test_size_for_each_slice
    assert_equal nil, @obj.each_slice(3).size
    assert_equal 6, @sized.each_slice(7).size
    assert_equal 5, @sized.each_slice(10).size
    assert_equal 1, @sized.each_slice(70).size
    assert_raise(ArgumentError){ @obj.each_slice(0).size }
  end

  def test_size_for_each_cons
    assert_equal nil, @obj.each_cons(3).size
    assert_equal 33, @sized.each_cons(10).size
    assert_equal 0, @sized.each_cons(70).size
    assert_raise(ArgumentError){ @obj.each_cons(0).size }
  end

  def test_size_for_step
    assert_equal 42, 5.step(46).size
    assert_equal 4, 1.step(10, 3).size
    assert_equal 3, 1.step(9, 3).size
    assert_equal 0, 1.step(-11).size
    assert_equal 0, 1.step(-11, 2).size
    assert_equal 7, 1.step(-11, -2).size
    assert_equal 7, 1.step(-11.1, -2).size
    assert_equal 0, 42.step(Float::INFINITY, -2).size
    assert_equal 1, 42.step(55, Float::INFINITY).size
    assert_equal 1, 42.step(Float::INFINITY, Float::INFINITY).size
    assert_equal 14, 0.1.step(4.2, 0.3).size
    assert_equal Float::INFINITY, 42.step(Float::INFINITY, 2).size

    assert_equal 10, (1..10).step.size
    assert_equal 4, (1..10).step(3).size
    assert_equal 3, (1...10).step(3).size
    assert_equal Float::INFINITY, (42..Float::INFINITY).step(2).size
    assert_raise(ArgumentError){ (1..10).step(-2).size }
  end

  def test_size_for_downup_to
    assert_equal 0, 1.upto(-100).size
    assert_equal 102, 1.downto(-100).size
    assert_equal Float::INFINITY, 42.upto(Float::INFINITY).size
  end

  def test_size_for_string
    assert_equal 5, 'hello'.each_byte.size
    assert_equal 5, 'hello'.each_char.size
    assert_equal 5, 'hello'.each_codepoint.size
  end

  def test_peek_for_enumerator_objects
    e = 2.times
    assert_equal(0, e.peek)
    e.next
    assert_equal(1, e.peek)
    e.next
    assert_raise(StopIteration) { e.peek }
  end

  def test_uniq
    u = [0, 1, 0, 1].to_enum.lazy.uniq
    assert_equal([0, 1], u.force)
    assert_equal([0, 1], u.force)
  end
end