#!/usr/bin/ruby $:.unshift File.join(File.dirname(__FILE__), "..", "lib") require 'test/unit' require 'OneOfEqualsConstraint' require 'Variable' require 'Domain' module ConstraintSolver class OneOfEqualsConstraintTest < Test::Unit::TestCase def setup @x = Variable.new("x", Domain.new([ 1, 2 ].to_set)) @y = Variable.new("y", Domain.new([ 1, 2 ].to_set)) @constraint = OneOfEqualsConstraint.new([ @x, @y ], 2) end def testConstructor assert_raise(ArgumentError) { OneOfEqualsConstraint.new } assert_raise(ArgumentError) { OneOfEqualsConstraint.new([], 1) } assert_nothing_raised { OneOfEqualsConstraint.new([ @x, @y ], 2) } assert_nothing_raised { OneOfEqualsConstraint.new([ @x, @y ], 2, 0) } end def testAssigned assert_equal(false, @constraint.allAssigned?) @x.value = 1 assert_equal(false, @constraint.allAssigned?) @y.value = 1 assert_equal(true, @constraint.allAssigned?) @x.reset @y.reset end def testInclude assert_equal(true, @constraint.include?(@x)) assert_equal(true, @constraint.include?(@y)) assert_equal(false, @constraint.include?(Variable.new("2", [ 3 ].to_set))) end def testHolds assert_equal(true, @constraint.holds?) @x.value = 1 @y.value = 1 assert_equal(false, @constraint.holds?) @x.value = 2 @y.value = 2 assert_equal(true, @constraint.holds?) @x.value = 1 @y.value = 2 assert_equal(true, @constraint.holds?) @x.value = 2 @y.value = 1 assert_equal(true, @constraint.holds?) end def testEquals assert_equal(OneOfEqualsConstraint.new([ @x, @y ], 2), @constraint) assert_not_equal(OneOfEqualsConstraint.new([ Variable.new("z", Domain.new([ 1, 2 ].to_set)), @y ], 2), @constraint) assert_not_equal(OneOfEqualsConstraint.new([ @x, @y ], 1), @constraint) end def testRevise assert_equal([ [], 0, false ], @constraint.revise) @y.value = 2 assert_equal([ [], 0, false ], @constraint.revise) @y.value = 1 assert_equal([ [ @x ], 0, false ], @constraint.revise) assert_equal([ 2 ].to_set, @x.domain.values) @y.reset @x.domain.undoPruning end def testFaulty x = Variable.new("x", Domain.new([ 1, 2 ].to_set)) y = Variable.new("y", Domain.new([ 1, 2 ].to_set)) constraint = OneOfEqualsConstraint.new([ x, y ], 3) y.value = 1 assert_equal([ [ x ], 0, true ], constraint.revise) end end end