# Ruby Treasures 0.1
# Copyright (C) 2001 Paul Brannan <paul@atdesk.com>
# 
# You may distribute this software under the same terms as Ruby (see the file
# COPYING that was distributed with this library).
# 
require 'ds_test_helpers'
require 'iterator/iterator'

class Foo
  def size
    3
  end

  def each
    yield 1
    yield 2
    yield 3
    raise RuntimeError
  end

  include Enumerable
end

def iterator_test(iterator)
  a = [ :a, :b, :c, :d, :e ]
  b = (1..a.size)
  i = iterator.new(a)
  j = iterator.new(b)
  puts "#{i.elem} #{j.elem}"
  (a.size).times do
    i.advance
    j.advance
    puts "#{i.elem} #{j.elem} #{i.finished?} #{j.finished?}"
  end
  puts "#{i.elem} #{j.elem} #{i.finished?} #{j.finished?}"

  puts ""
  f = Foo.new
  i = iterator.new(f)
  while not i.finished?
    puts i.elem
    begin
      i.advance
    rescue Exception
      puts "Got exception correctly"
      raise $!
    end
  end
end

puts "Collect iterator:"
run_block_safe do
  iterator_test(CollectIterator)
end
puts

puts "Continuation iterator:"
run_block_safe do
  iterator_test(ContinuationIterator)
end
puts

puts "Buffered continuation iterator:"
run_block_safe do
  iterator_test(BufferedContinuationIterator)
end
puts
