/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* * Main authors: * Christian Schulte * * Copyright: * Christian Schulte, 2005 * * Last modified: * $Date: 2011-10-06 23:38:44 +1100 (Thu, 06 Oct 2011) $ by $Author: schulte $ * $Revision: 12421 $ * * This file is part of Gecode, the generic constraint * development environment: * http://www.gecode.org * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * */ #include "test/int.hh" #include namespace Test { namespace Int { /// %Tests for Boolean constraints namespace Bool { inline int check(int x0, Gecode::BoolOpType op, int x1) { switch (op) { case Gecode::BOT_AND: return x0 & x1; case Gecode::BOT_OR: return x0 | x1; case Gecode::BOT_IMP: return !x0 | x1; case Gecode::BOT_EQV: return x0 == x1; case Gecode::BOT_XOR: return x0 != x1; default: GECODE_NEVER; } GECODE_NEVER; return 0; } /** * \defgroup TaskTestIntBool Boolean constraints * \ingroup TaskTestInt */ //@{ /// %Test for binary Boolean operation class BinXYZ : public Test { protected: /// Boolean operation type for test Gecode::BoolOpType op; public: /// Construct and register test BinXYZ(Gecode::BoolOpType op0) : Test("Bool::Bin::XYZ::"+str(op0),3,0,1), op(op0) {} /// Check whether \a x is solution virtual bool solution(const Assignment& x) const { return check(x[0],op,x[1]) == x[2]; } /// Post constraint virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { using namespace Gecode; rel(home, channel(home,x[0]), op, channel(home,x[1]), channel(home,x[2])); } }; /// %Test for binary Boolean operation with shared variables class BinXXY : public Test { protected: /// Boolean operation type for test Gecode::BoolOpType op; public: /// Construct and register test BinXXY(Gecode::BoolOpType op0) : Test("Bool::Bin::XXY::"+str(op0),2,0,1), op(op0) {} /// Check whether \a x is solution virtual bool solution(const Assignment& x) const { return check(x[0],op,x[0]) == x[1]; } /// Post constraint virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { using namespace Gecode; BoolVar b = channel(home,x[0]); rel(home, b, op, b, channel(home,x[1])); } }; /// %Test for binary Boolean operation with shared variables class BinXYX : public Test { protected: /// Boolean operation type for test Gecode::BoolOpType op; public: /// Construct and register test BinXYX(Gecode::BoolOpType op0) : Test("Bool::Bin::XYX::"+str(op0),2,0,1), op(op0) {} /// Check whether \a x is solution virtual bool solution(const Assignment& x) const { return check(x[0],op,x[1]) == x[0]; } /// Post constraint virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { using namespace Gecode; BoolVar b = channel(home,x[0]); rel(home, b, op, channel(home,x[1]), b); } }; /// %Test for binary Boolean operation with shared variables class BinXYY : public Test { protected: /// Boolean operation type for test Gecode::BoolOpType op; public: /// Construct and register test BinXYY(Gecode::BoolOpType op0) : Test("Bool::Bin::XYY::"+str(op0),2,0,1), op(op0) {} /// Check whether \a x is solution virtual bool solution(const Assignment& x) const { return check(x[0],op,x[1]) == x[1]; } /// Post constraint virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { using namespace Gecode; BoolVar b = channel(home,x[1]); rel(home, channel(home,x[0]), op, b, b); } }; /// %Test for binary Boolean operation with shared variables class BinXXX : public Test { protected: /// Boolean operation type for test Gecode::BoolOpType op; public: /// Construct and register test BinXXX(Gecode::BoolOpType op0) : Test("Bool::Bin::XXX::"+str(op0),1,0,1), op(op0) {} /// Check whether \a x is solution virtual bool solution(const Assignment& x) const { return check(x[0],op,x[0]) == x[0]; } /// Post constraint virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { using namespace Gecode; BoolVar b = channel(home,x[0]); rel(home, b, op, b, b); } }; /// %Test for binary Boolean operation with constant class BinConstXY : public Test { protected: /// Boolean operation type for test Gecode::BoolOpType op; /// Integer constant int c; public: /// Construct and register test BinConstXY(Gecode::BoolOpType op0, int c0) : Test("Bool::Bin::XY::"+str(op0)+"::"+str(c0),2,0,1), op(op0), c(c0) {} /// Check whether \a x is solution virtual bool solution(const Assignment& x) const { return check(x[0],op,x[1]) == c; } /// Post constraint virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { using namespace Gecode; rel(home, channel(home,x[0]), op, channel(home,x[1]), c); } }; /// %Test for binary Boolean operation with shared variables and constant class BinConstXX : public Test { protected: /// Boolean operation type for test Gecode::BoolOpType op; /// Integer constant int c; public: /// Construct and register test BinConstXX(Gecode::BoolOpType op0, int c0) : Test("Bool::Bin::XX::"+str(op0)+"::"+str(c0),1,0,1), op(op0), c(c0) {} /// Check whether \a x is solution virtual bool solution(const Assignment& x) const { return check(x[0],op,x[0]) == c; } /// Post constraint virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { using namespace Gecode; BoolVar b = channel(home,x[0]); rel(home, b, op, b, c); } }; /// %Test for Nary Boolean operation class Nary : public Test { protected: /// Boolean operation type for test Gecode::BoolOpType op; public: /// Construct and register test Nary(Gecode::BoolOpType op0, int n) : Test("Bool::Nary::"+str(op0)+"::"+str(n),n+1,0,1), op(op0) {} /// Check whether \a x is solution virtual bool solution(const Assignment& x) const { int n = x.size()-1; int b = check(x[n-2],op,x[n-1]); for (int i=0; i