# 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).
# 
tmp = ['../lib']
Dir.foreach('../ext') do |file|
  # TODO: This is definitely a hack
  if FileTest.directory?("../ext/#{file}") then
    tmp << "../ext/#{file}"
  end
end
tmp.push(*$:)
$:.clear
$:.push(*tmp)

srand(1)

require 'loaders'
require 'iterator/iterator'

$VERBOSE = false
require 'runit/testcase'
$VERBOSE = true

requirelocal 'ds_test_case'
requirelocal 'generate_random'

DEBUG = true

def random_string(n)
  str = ''
  n.times do
    str << ?A + rand(26)
  end
  return str
end

def assert(bool)
  puts "Assertion failed at #{caller[0]}" if not bool
end

def run_block_safe(*args)
  begin
    yield(*args)
  rescue Exception
    puts $!
    puts $!.backtrace
  end
end

def time_block(*args)
  start_time = Time.now
  yield(*args)
  end_time = Time.now
  puts "Total time: #{end_time - start_time}"
end

def format_container(a)
  case a
  when Array
    a.inspect
  else
    a.to_s
  end
end

def display_container(a)
  puts format_container(a)
end

def assert_containers_equal(l1, l2, method=:each)
  assert_equal l1.size, l2.size
  if method == :each then
    it1 = GenericIterator(l1)
    it2 = GenericIterator(l2)
  else
    it1 = ContinuationIterator.new(l1, method)
    it2 = ContinuationIterator.new(l2, method)
  end
  while not it1.finished? and not it2.finished? do
    assert_equal it1.elem, it2.elem
    it1.advance
    it2.advance
  end
end

def run_test(test, klass=nil)
  require 'runit/cui/testrunner'
  suite = test.suite
  testresult = RUNIT::CUI::TestRunner.run(suite)
  retval = (
      testresult.error_size   == 0 &&
      testresult.failure_size == 0)
  if klass != nil then
    # retval =
      test.assert_all_public_methods_checked(klass) # && retval
  end
  return retval ? 0 : 1
end

