#!/usr/local/bin/ruby

# $Id$
# $Source$

require 'test/unit'
require '../intspan'

class TestSetIntspan < Test::Unit::TestCase
	# def setup
	# end

	# def teardown
	# end

	def test_empty_set
		set = Set::IntSpan.new()
		assert(set.empty?)
		assert_equal("", set.to_s)
		set.set_neg_inf(true)
		assert_equal(false, set.empty?)
		set = Set::IntSpan.new()
		set.set_pos_inf(true)
		assert_equal(false, set.empty?)
		set = Set::IntSpan.new("1")
		assert_equal(false, set.empty?)
	end

	def test_neg_inf
		set = Set::IntSpan.new()
		set.set_neg_inf(true)
		assert(set.neg_inf?)
		set =  Set::IntSpan.new("(-1,3,5")
		assert_equal(true, set.neg_inf?)
		set = Set::IntSpan.new("1-3,5-)")
		assert_equal(false, set.neg_inf?)
		set = Set::IntSpan.new("(-1,3,5-)")
		assert_equal(true, set.neg_inf?)
	end

	def test_pos_inf
		set = Set::IntSpan.new()
		set.set_pos_inf(true)
		assert(set.pos_inf?)
	end

	def test_to_s
		assert_equal("", Set::IntSpan.new().to_s)
		assert_equal("1", Set::IntSpan.new("1").to_s)
		assert_equal("1-3", Set::IntSpan.new("1-3").to_s)
		assert_equal("1-3,5", Set::IntSpan.new("1-3,5").to_s)
		assert_equal("(-1,3,5", Set::IntSpan.new("(-1,3,5").to_s)
		assert_equal("1-3,5-)", Set::IntSpan.new("1-3,5-)").to_s)
		assert_equal("(-1,3,5-)", Set::IntSpan.new("(-1,3,5-)").to_s)
	end

	def test_array_init
		assert_equal("", Set::IntSpan.new([]).to_s)
		assert_equal("1", Set::IntSpan.new([1]).to_s)
		assert_equal("1-3", Set::IntSpan.new([1,2,3]).to_s)
		assert_equal("1-3,5", Set::IntSpan.new([1,2,3,5]).to_s)
	end

	def test_set_init
		set = Set::IntSpan.new("1-3,5")
		set2 = Set::IntSpan.new(set)
		assert_equal("1-3,5", set2.to_s)
		set = Set::IntSpan.new("(-1,3,5-)")
		set2 = Set::IntSpan.new(set)
		assert_equal("(-1,3,5-)", set2.to_s)
	end

	def test_insert
		set = Set::IntSpan.new()
		assert_equal("", set.to_s)
		set.insert(4)
		assert_equal("4", set.to_s)
		set.insert(5)
		assert_equal("4-5", set.to_s)
		set.insert(7)
		assert_equal("4-5,7", set.to_s)
		set.insert(6)
		assert_equal("4-7", set.to_s)
		set.insert(16)
		assert_equal("4-7,16", set.to_s)
		set.insert(10)
		assert_equal("4-7,10,16", set.to_s)
		set.insert(3)
		assert_equal("3-7,10,16", set.to_s)
		set.insert(1)
		assert_equal("1,3-7,10,16", set.to_s)
		set.insert(17)
		assert_equal("1,3-7,10,16-17", set.to_s)
		set.insert(14974324)
		assert_equal("1,3-7,10,16-17,14974324", set.to_s)
	end

	def test_finite
		assert_equal(true, Set::IntSpan.new("1-3,5").finite?)
		assert_equal(false, Set::IntSpan.new("(-1,3,5").finite?)
		assert_equal(false, Set::IntSpan.new("1-3,5-)").finite?)
		assert_equal(false, Set::IntSpan.new("(-1,3,5-)").finite?)
		assert_equal(false, Set::IntSpan.new("1-3,5").infinite?)
		assert_equal(true, Set::IntSpan.new("(-1,3,5").infinite?)
		assert_equal(true, Set::IntSpan.new("1-3,5-)").infinite?)
		assert_equal(true, Set::IntSpan.new("(-1,3,5-)").infinite?)
	end

	def test_remove
		set = Set::IntSpan.new("1-3,5")
		set.remove!(3)
		assert_equal("1-2,5", set.to_s)
		assert_equal("1-2,5", Set::IntSpan.new("1-3,5").remove!(3).to_s)
		assert_equal("(-1,5", Set::IntSpan.new("(-1,3,5").remove!(3).to_s)
		assert_equal("1-2,5-)", Set::IntSpan.new("1-3,5-)").remove!(3).to_s)
		assert_equal("(-1,5-)", Set::IntSpan.new("(-1,3,5-)").remove!(3).to_s)
	end

	def test_member
		assert_equal(true, Set::IntSpan.new("1-3,5").member?(1))
		assert_equal(true, Set::IntSpan.new("1-3,5").member?(3))
		assert_equal(false, Set::IntSpan.new("1-3,5").member?(4))
		assert_equal(false, Set::IntSpan.new("1-3,5").member?(6))
		assert_equal(false, Set::IntSpan.new("1-3,5").member?(7))
		assert_equal(true, Set::IntSpan.new("1-3,5-)").member?(7))
		assert_equal(true, Set::IntSpan.new("(-3,5").member?(-10))
	end
end
