/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* * Main authors: * Mikael Lagerkvist * Linnea Ingmar * Christian Schulte * * Copyright: * Linnea Ingmar, 2017 * Mikael Lagerkvist, 2007 * Christian Schulte, 2005 * * 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 #include namespace Test { namespace Int { /// %Tests for extensional (relation) constraints namespace Extensional { /** * \defgroup TaskTestIntExtensional Extensional (relation) constraints * \ingroup TaskTestInt */ //@{ /// %Test with simple regular expression class RegSimpleA : public Test { public: /// Create and register test RegSimpleA(void) : Test("Extensional::Reg::Simple::A",4,2,2) {} /// %Test whether \a x is solution virtual bool solution(const Assignment& x) const { return (((x[0] == 0) || (x[0] == 2)) && ((x[1] == -1) || (x[1] == 1)) && ((x[2] == 0) || (x[2] == 1)) && ((x[3] == 0) || (x[3] == 1))); } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { using namespace Gecode; extensional(home, x, (REG(0) | REG(2)) + (REG(-1) | REG(1)) + (REG(7) | REG(0) | REG(1)) + (REG(0) | REG(1))); } }; /// %Test with simple regular expression class RegSimpleB : public Test { public: /// Create and register test RegSimpleB(void) : Test("Extensional::Reg::Simple::B",4,2,2) {} /// %Test whether \a x is solution virtual bool solution(const Assignment& x) const { return (x[0] s) return false; for (int i = 0; i < 2; ++i, ++pos) if (x[pos] != 1) return false; if (pos + 2 > s) return false; for (int i = 0; i < 1; ++i, ++pos) if (x[pos] != 0) return false; while (pos < s && x[pos] == 0) ++pos; if (pos + 1 > s) return false; for (int i = 0; i < 1; ++i, ++pos) if (x[pos] != 1) return false; while (pos < s) if (x[pos++] != 0) return false; return true; } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { using namespace Gecode; extensional(home, x, *REG(0) + REG(1)(2,2) + +REG(0) + REG(1)(1,1) + *REG(0)); } }; /// %Test with regular expression for distinct constraint class RegDistinct : public Test { public: /// Create and register test RegDistinct(void) : Test("Extensional::Reg::Distinct",4,-1,4) {} /// %Test whether \a x is solution virtual bool solution(const Assignment& x) const { for (int i=0; i 3)) return false; for (int j=i+1; j 1) && (x[n-2] == 0)) || ((n > 0) && (x[n-1] == 0)); } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { using namespace Gecode; REG r0(0), r1(1); REG r01 = r0 | r1; extensional(home, x, *r01 + r0 + r01(0,1)); } }; /// %Test with simple regular expression and shared variables (uses unsharing) class RegSharedA : public Test { public: /// Create and register test RegSharedA(void) : Test("Extensional::Reg::Shared::A",4,2,2) {} /// %Test whether \a x is solution virtual bool solution(const Assignment& x) const { return (((x[0] == 0) || (x[0] == 2)) && ((x[1] == -1) || (x[1] == 1)) && ((x[2] == 0) || (x[2] == 1)) && ((x[3] == 0) || (x[3] == 1))); } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { using namespace Gecode; IntVarArgs y(8); for (int i=0; i<4; i++) y[i]=y[i+4]=x[i]; unshare(home,y); extensional(home, y, ((REG(0) | REG(2)) + (REG(-1) | REG(1)) + (REG(7) | REG(0) | REG(1)) + (REG(0) | REG(1)))(2,2)); } }; /// %Test with simple regular expression and shared variables (uses unsharing) class RegSharedB : public Test { public: /// Create and register test RegSharedB(void) : Test("Extensional::Reg::Shared::B",4,2,2) {} /// %Test whether \a x is solution virtual bool solution(const Assignment& x) const { return (((x[0] == 0) || (x[0] == 2)) && ((x[1] == -1) || (x[1] == 1)) && ((x[2] == 0) || (x[2] == 1)) && ((x[3] == 0) || (x[3] == 1))); } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { using namespace Gecode; IntVarArgs y(12); for (int i=0; i<4; i++) y[i]=y[i+4]=y[i+8]=x[i]; unshare(home,y); extensional(home, y, ((REG(0) | REG(2)) + (REG(-1) | REG(1)) + (REG(7) | REG(0) | REG(1)) + (REG(0) | REG(1)))(3,3)); } }; /// %Test with simple regular expression and shared variables (uses unsharing) class RegSharedC : public Test { public: /// Create and register test RegSharedC(void) : Test("Extensional::Reg::Shared::C",4,0,1) {} /// %Test whether \a x is solution virtual bool solution(const Assignment& x) const { return (x[1]==1) && (x[2]==0) && (x[3]==1); } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { using namespace Gecode; Gecode::BoolVarArgs y(8); for (int i=0; i<4; i++) y[i]=y[i+4]=channel(home,x[i]); unshare(home,y); extensional(home,y, ((REG(0) | REG(1)) + REG(1) + REG(0) + REG(1))(2,2)); } }; /// %Test with simple regular expression and shared variables (uses unsharing) class RegSharedD : public Test { public: /// Create and register test RegSharedD(void) : Test("Extensional::Reg::Shared::D",4,0,1) {} /// %Test whether \a x is solution virtual bool solution(const Assignment& x) const { return (x[1]==1) && (x[2]==0) && (x[3]==1); } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { using namespace Gecode; Gecode::BoolVarArgs y(12); for (int i=0; i<4; i++) y[i]=y[i+4]=y[i+8]=channel(home,x[i]); unshare(home, y); extensional(home, y, ((REG(0) | REG(1)) + REG(1) + REG(0) + REG(1))(3,3)); } }; /// %Test for empty DFA class RegEmptyDFA : public Test { public: /// Create and register test RegEmptyDFA(void) : Test("Extensional::Reg::Empty::DFA",1,0,0) { testsearch = false; } /// %Test whether \a x is solution virtual bool solution(const Assignment& x) const { (void)x; return false; } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { Gecode::DFA d; Gecode::extensional(home, x, d); } }; /// %Test for empty regular expression class RegEmptyREG : public Test { public: /// Create and register test RegEmptyREG(void) : Test("Extensional::Reg::Empty::REG",1,0,0) { testsearch = false; } /// %Test whether \a x is solution virtual bool solution(const Assignment& x) const { (void)x; return false; } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { Gecode::REG r; Gecode::extensional(home, x, r); } }; /// %Test for optimizations class RegOpt : public Test { protected: /// DFA size characteristic int n; public: /// Create and register test RegOpt(int n0) : Test("Extensional::Reg::Opt::"+str(n0),1,0,15), n(n0) {} /// %Test whether \a x is solution virtual bool solution(const Assignment& x) const { return (x[0] < n) && ((x[0] & 1) == 0); } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { using namespace Gecode; DFA::Transition* t = new DFA::Transition[n+1]; DFA::Transition* ti = t; int* f = new int[n+1]; int* fi = f; for (int i=0; ii_state = 0; ti->symbol = i; ti->o_state = i+1; ti++; if ((i & 1) == 0) { *fi = i+1; fi++; } } ti->i_state = -1; *fi = -1; DFA d(0, t, f, false); delete [] t; delete [] f; extensional(home, x, d); } }; ///% Transform a TupleSet into a DFA Gecode::DFA tupleset2dfa(Gecode::TupleSet ts) { using namespace Gecode; REG expression; for (int i = 0; i(UCHAR_MAX-1)); RegOpt ro3(static_cast(UCHAR_MAX)); RegOpt ro4(SHRT_MAX-1); RegOpt ro5(SHRT_MAX); RegOpt ro6(static_cast(USHRT_MAX-1)); RegOpt ro7(static_cast(USHRT_MAX)); //@} } }} // STATISTICS: test-int