#!/usr/bin/ruby $:.unshift File.join(File.dirname(__FILE__), "..", "lib") require 'test/unit' require 'BinaryConstraint' require 'Variable' require 'Domain' module ConstraintSolver class BinaryConstraintTest < Test::Unit::TestCase def setup @lhs = Variable.new("lhs", Domain.new([ 1, 2 ].to_set)) @rhs = Variable.new("rhs", Domain.new([ 1, 2 ].to_set)) @rel = BinaryRelation.new("==") @constraint = BinaryConstraint.new(@lhs, @rhs, @rel) end def testConstructor assert_raise(ArgumentError) { BinaryConstraint.new } assert_raise(ArgumentError) { BinaryConstraint.new(@lhs, @rhs, Proc.new { |a| a }) } assert_nothing_raised { BinaryConstraint.new(@lhs, @rhs, @rel) } assert_nothing_raised { BinaryConstraint.new(@lhs, @rhs, @rel, 0) } end def testAssigned assert_equal(false, @constraint.allAssigned?) @rhs.value = 1 assert_equal(false, @constraint.allAssigned?) @lhs.value = 1 assert_equal(true, @constraint.allAssigned?) @rhs.reset @lhs.reset end def testInclude assert_equal(true, @constraint.include?(@lhs)) assert_equal(true, @constraint.include?(@rhs)) assert_equal(false, @constraint.include?(Variable.new("2", [ 3 ].to_set))) end def testHolds assert_equal(true, @constraint.holds?) assert_nothing_raised { @lhs.value = 1 } assert_nothing_raised { @rhs.value = 2 } assert_equal(false, @constraint.holds?) assert_nothing_raised { @rhs.value = 1 } assert_equal(true, @constraint.holds?) @lhs.reset @rhs.reset end def testEquals assert_equal(BinaryConstraint.new(@lhs, @rhs, @rel), @constraint) assert_not_equal(BinaryConstraint.new(@lhs, @rhs, BinaryRelation.new("<")), @constraint) assert_not_equal(BinaryConstraint.new(Variable.new("foo", [ 1 ].to_set), @rhs, @rel), @constraint) assert_not_equal(BinaryConstraint.new(@lhs, Variable.new("foo", [ 1 ].to_set), @rel), @constraint) end def testRevise lhs = Variable.new("x", Domain.new([ 1 ].to_set)) rhs = Variable.new("y", Domain.new([ 1, 2, 3, 4 ].to_set)) rel = BinaryRelation.new("<") constraint = BinaryConstraint.new(lhs, rhs, rel) revisedVariables, checks, wipeout = constraint.revise assert_equal(false, wipeout) assert_equal([ rhs ], revisedVariables) assert_equal(4, checks) assert_equal([ 2, 3, 4 ].to_set, rhs.values) lhs = Variable.new("x", Domain.new([ 1 ].to_set), 1) rhs = Variable.new("y", Domain.new([ 1, 2, 3, 4 ].to_set)) rel = BinaryRelation.new("<") constraint = BinaryConstraint.new(lhs, rhs, rel) revisedVariables, checks, wipeout = constraint.revise assert_equal(false, wipeout) assert_equal([ rhs ], revisedVariables) assert_equal(4, checks) assert_equal([ 2, 3, 4 ].to_set, rhs.values) lhs = Variable.new("x", Domain.new([ 1 ].to_set), 1) rhs = Variable.new("y", Domain.new([ 1, 2, 3, 4 ].to_set), 4) rel = BinaryRelation.new("<") constraint = BinaryConstraint.new(lhs, rhs, rel) revisedVariables, checks, wipeout = constraint.revise assert_equal(false, wipeout) assert_equal([], revisedVariables) assert_equal(0, checks) end def testReviseNothing lhs = Variable.new("x", Domain.new([ 1, 2, 3 ].to_set)) rhs = Variable.new("y", Domain.new([ 1, 2, 3 ].to_set)) rel = BinaryRelation.new("!=") constraint = BinaryConstraint.new(lhs, rhs, rel) revisedVariables, checks = constraint.revise assert_equal([ ], revisedVariables) assert_equal(9, checks) end end class BinaryRelationTest < Test::Unit::TestCase def testConstructor assert_raise(ArgumentError) { BinaryRelation.new } assert_nothing_raised { BinaryRelation.new("<") } end end end