require "rbtree"

class Test
	def Test::addtotree tree, value
		tree << value
		printtree tree
		puts "################################"
	end

	def Test::printtree tree
		tree.traverse {|child, depth|
			unless child.nil?
			  	p = "-"
				unless child.parent.nil?
				  print "<"*depth if child == child.parent.left
				  print ">"*depth if child == child.parent.right
				end
				print " #{child.value} (#{child.color})"
				print " [#{child.parent.value}]" unless child.parent.nil?
			end
			puts ""
		}
	end

	def Test::shuffle array
		size = array.size
		(size/2).times {
			ind = rand(size)
			ind2 = rand(size)
			array[ind], array[ind2] = array[ind2], array[ind]
		}
	end

	def Test::simpletest
		puts "########### Simple test"
		tree = RedBlackTree.new
		array = (1..5).to_a
		shuffle array
		array.each { |i| addtotree tree, i }
	end

	def Test::bigtest
		puts "########### Big test"
		depth = 1000
		arry = (1..depth).to_a
		shuffle arry
		tree = RedBlackTree.new
		t_start = Time.new
		depth.times{ |i| tree << arry[i] }
		t_end = Time.new
		puts "#{depth} inserts took #{t_end-t_start} seconds."
		# printtree tree
		puts "Left depth: #{tree.max_depth_of tree.root.left}"
		puts "Right depth: #{tree.max_depth_of tree.root.right}"

		t_start = Time.new
		1000.times{
			search_item = rand(depth)
			tree.search search_item
		}
		t_end = Time.new
		puts "1000 searches on tree took #{t_end-t_start} seconds"
		t_start = Time.new
		1000.times{
			search_item = rand(depth)
			arry.find{|x| x == search_item}
		}
		t_end = Time.new
		puts "1000 finds on array took #{t_end-t_start} seconds"
	end

	def Test::duplicatetest
		puts "########### Duplicates test"
		tree = RedBlackTree.new
		100.times { tree << 5 }
		#printtree tree
		puts "Left depth: #{tree.max_depth_of tree.root.left}"
		puts "Right depth: #{tree.max_depth_of tree.root.right}"
	end

	def Test::alphatest
		puts "########### Duplicates test"
		tree = RedBlackTree.new
		arry = %w{ once upon a midnight dreary while I pondered weak and weary over many a quaint and curious volume of forgotten lore }
		shuffle arry
		arry.each {|item| tree << item }
		printtree tree
		puts "Left depth: #{tree.max_depth_of tree.root.left}"
		puts "Right depth: #{tree.max_depth_of tree.root.right}"
	end
end

Test::simpletest
Test::bigtest
Test::duplicatetest
Test::alphatest
