1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  *  Main authors:
4  *     Christian Schulte <schulte@gecode.org>
5  *     Mikael Lagerkvist <lagerkvist@gecode.org>
6  *
7  *  Copyright:
8  *     Christian Schulte, 2005
9  *     Mikael Lagerkvist, 2006
10  *
11  *  This file is part of Gecode, the generic constraint
12  *  development environment:
13  *     http://www.gecode.org
14  *
15  *  Permission is hereby granted, free of charge, to any person obtaining
16  *  a copy of this software and associated documentation files (the
17  *  "Software"), to deal in the Software without restriction, including
18  *  without limitation the rights to use, copy, modify, merge, publish,
19  *  distribute, sublicense, and/or sell copies of the Software, and to
20  *  permit persons to whom the Software is furnished to do so, subject to
21  *  the following conditions:
22  *
23  *  The above copyright notice and this permission notice shall be
24  *  included in all copies or substantial portions of the Software.
25  *
26  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33  *
34  */
35 
36 namespace Test { namespace Int {
37 
38   /*
39    * Assignments
40    *
41    */
42   inline
Assignment(int n0,const Gecode::IntSet & d0)43   Assignment::Assignment(int n0, const Gecode::IntSet& d0)
44     : n(n0), d(d0) {}
45   inline int
size(void) const46   Assignment::size(void) const {
47     return n;
48   }
49   inline
~Assignment(void)50   Assignment::~Assignment(void) {}
51 
52 
53   inline
CpltAssignment(int n,const Gecode::IntSet & d)54   CpltAssignment::CpltAssignment(int n, const Gecode::IntSet& d)
55     : Assignment(n,d),
56       dsv(new Gecode::IntSetValues[static_cast<unsigned int>(n)]) {
57     for (int i=n; i--; )
58       dsv[i].init(d);
59   }
60   inline bool
has_more(void) const61   CpltAssignment::has_more(void) const {
62     return dsv[0]();
63   }
64   inline int
operator [](int i) const65   CpltAssignment::operator[](int i) const {
66     assert((i>=0) && (i<n));
67     return dsv[i].val();
68   }
69   inline
~CpltAssignment(void)70   CpltAssignment::~CpltAssignment(void) {
71     delete [] dsv;
72   }
73 
74 
75   forceinline int
randval(Gecode::Support::RandomGenerator & rand)76   RandomAssignment::randval(Gecode::Support::RandomGenerator& rand) {
77     unsigned int skip = rand(d.size());
78     for (Gecode::IntSetRanges it(d); true; ++it) {
79       if (it.width() > skip)
80         return it.min() + static_cast<int>(skip);
81       skip -= it.width();
82     }
83     GECODE_NEVER;
84     return 0;
85   }
86 
87   inline
RandomAssignment(int n,const Gecode::IntSet & d,int a0,Gecode::Support::RandomGenerator & rand)88   RandomAssignment::RandomAssignment(int n, const Gecode::IntSet& d, int a0, Gecode::Support::RandomGenerator& rand)
89     : Assignment(n,d), vals(new int[static_cast<size_t>(n)]), a(a0) {
90     for (int i=n; i--; )
91       vals[i] = randval(rand);
92   }
93 
94   inline bool
has_more(void) const95   RandomAssignment::has_more(void) const {
96     return a>0;
97   }
98   inline int
operator [](int i) const99   RandomAssignment::operator[](int i) const {
100     assert((i>=0) && (i<n));
101     return vals[i];
102   }
103   inline
~RandomAssignment(void)104   RandomAssignment::~RandomAssignment(void) {
105     delete [] vals;
106   }
107 
108   forceinline int
randval(const Gecode::IntSet & d,Gecode::Support::RandomGenerator & rand)109   RandomMixAssignment::randval(const Gecode::IntSet& d, Gecode::Support::RandomGenerator& rand) {
110     unsigned int skip = rand(d.size());
111     for (Gecode::IntSetRanges it(d); true; ++it) {
112       if (it.width() > skip)
113         return it.min() + static_cast<int>(skip);
114       skip -= it.width();
115     }
116     GECODE_NEVER;
117     return 0;
118   }
119 
120   inline
RandomMixAssignment(int n0,const Gecode::IntSet & d0,int n1,const Gecode::IntSet & d1,int a0,Gecode::Support::RandomGenerator & rand)121   RandomMixAssignment::RandomMixAssignment(int n0, const Gecode::IntSet& d0, int n1, const Gecode::IntSet& d1, int a0,
122                                            Gecode::Support::RandomGenerator& rand)
123     : Assignment(n0+n1,d0),vals(new int[static_cast<size_t>(n0+n1)]),
124       a(a0),_n1(n1),_d1(d1) {
125     for (int i=n0; i--; )
126       vals[i] = randval(d, rand);
127     for (int i=n1; i--; )
128       vals[n0+i] = randval(_d1, rand);
129   }
130 
131   inline bool
has_more(void) const132   RandomMixAssignment::has_more(void) const {
133     return a>0;
134   }
135 
136   inline int
operator [](int i) const137   RandomMixAssignment::operator[](int i) const {
138     assert((i>=0) && (i<n));
139     return vals[i];
140   }
141 
142   inline
~RandomMixAssignment(void)143   RandomMixAssignment::~RandomMixAssignment(void) {
144     delete [] vals;
145   }
146 
147   /*
148    * Tests with integer constraints
149    *
150    */
151   forceinline bool
eqv(void) const152   Test::eqv(void) const {
153     return reified && ((rms & (1 << Gecode::RM_EQV)) != 0);
154   }
155   forceinline bool
imp(void) const156   Test::imp(void) const {
157     return reified && ((rms & (1 << Gecode::RM_IMP)) != 0);
158   }
159   forceinline bool
pmi(void) const160   Test::pmi(void) const {
161     return reified && ((rms & (1 << Gecode::RM_PMI)) != 0);
162   }
163   inline
Test(const std::string & p,const std::string & s,int a,const Gecode::IntSet & d,bool r,Gecode::IntPropLevel i)164   Test::Test(const std::string& p, const std::string& s,
165              int a, const Gecode::IntSet& d, bool r,
166              Gecode::IntPropLevel i)
167     : Base(p+s), arity(a), dom(d),
168       reified(r), rms((1 << Gecode::RM_EQV) |
169                       (1 << Gecode::RM_IMP) |
170                       (1 << Gecode::RM_PMI)),
171       ipl(i), contest(ipl == Gecode::IPL_DOM ? CTL_DOMAIN : CTL_NONE),
172       testsearch(true), testfix(true) {}
173 
174   inline
Test(const std::string & s,int a,const Gecode::IntSet & d,bool r,Gecode::IntPropLevel i)175   Test::Test(const std::string& s,
176              int a, const Gecode::IntSet& d, bool r,
177              Gecode::IntPropLevel i)
178     : Base("Int::"+s), arity(a), dom(d),
179       reified(r), rms((1 << Gecode::RM_EQV) |
180                       (1 << Gecode::RM_IMP) |
181                       (1 << Gecode::RM_PMI)),
182       ipl(i), contest(ipl == Gecode::IPL_DOM ? CTL_DOMAIN : CTL_NONE),
183       testsearch(true), testfix(true) {}
184 
185   inline
Test(const std::string & p,const std::string & s,int a,int min,int max,bool r,Gecode::IntPropLevel i)186   Test::Test(const std::string& p, const std::string& s,
187              int a, int min, int max, bool r,
188              Gecode::IntPropLevel i)
189     : Base(p+s), arity(a), dom(min,max),
190       reified(r), rms((1 << Gecode::RM_EQV) |
191                       (1 << Gecode::RM_IMP) |
192                       (1 << Gecode::RM_PMI)),
193       ipl(i), contest(ipl == Gecode::IPL_DOM ? CTL_DOMAIN : CTL_NONE),
194       testsearch(true), testfix(true) {}
195 
196   inline
Test(const std::string & s,int a,int min,int max,bool r,Gecode::IntPropLevel i)197   Test::Test(const std::string& s,
198              int a, int min, int max, bool r, Gecode::IntPropLevel i)
199     : Base("Int::"+s), arity(a), dom(min,max),
200       reified(r), rms((1 << Gecode::RM_EQV) |
201                       (1 << Gecode::RM_IMP) |
202                       (1 << Gecode::RM_PMI)),
203       ipl(i), contest(ipl == Gecode::IPL_DOM ? CTL_DOMAIN : CTL_NONE),
204       testsearch(true), testfix(true) {}
205 
206   inline
207   std::string
str(Gecode::IntPropLevel ipl)208   Test::str(Gecode::IntPropLevel ipl) {
209     using namespace Gecode;
210     std::stringstream s;
211     switch (vbd(ipl)) {
212     case IPL_VAL: s << "Val"; break;
213     case IPL_BND: s << "Bnd"; break;
214     case IPL_DOM: s << "Dom"; break;
215     default: s << "Def"; break;
216     }
217     if (ipl & IPL_BASIC) s << "+B";
218     if (ipl & IPL_ADVANCED) s << "+A";
219     return s.str();
220   }
221 
222   inline
223   std::string
str(Gecode::IntRelType irt)224   Test::str(Gecode::IntRelType irt) {
225     using namespace Gecode;
226     switch (irt) {
227     case IRT_LQ: return "Lq";
228     case IRT_LE: return "Le";
229     case IRT_GQ: return "Gq";
230     case IRT_GR: return "Gr";
231     case IRT_EQ: return "Eq";
232     case IRT_NQ: return "Nq";
233     default: ;
234     }
235     GECODE_NEVER;
236     return "NONE";
237   }
238 
239   inline std::string
str(Gecode::BoolOpType bot)240   Test::str(Gecode::BoolOpType bot) {
241     using namespace Gecode;
242     switch (bot) {
243     case BOT_AND: return "And";
244     case BOT_OR:  return "Or";
245     case BOT_IMP: return "Imp";
246     case BOT_EQV: return "Eqv";
247     case BOT_XOR: return "Xor";
248     default: GECODE_NEVER;
249     }
250     GECODE_NEVER;
251     return "NONE";
252   }
253 
254   inline std::string
str(bool b)255   Test::str(bool b) {
256     return Base::str(b);
257   }
258 
259   inline std::string
str(int i)260   Test::str(int i) {
261     return Base::str(i);
262   }
263 
264   inline std::string
str(const Gecode::IntArgs & x)265   Test::str(const Gecode::IntArgs& x) {
266     return Base::str(x);
267   }
268 
269 
270 
271   template<class T>
272   inline bool
cmp(T x,Gecode::IntRelType r,T y)273   Test::cmp(T x, Gecode::IntRelType r, T y) {
274     using namespace Gecode;
275     switch (r) {
276     case IRT_EQ: return x == y;
277     case IRT_NQ: return x != y;
278     case IRT_LQ: return x <= y;
279     case IRT_LE: return x < y;
280     case IRT_GR: return x > y;
281     case IRT_GQ: return x >= y;
282     default: ;
283     }
284     return false;
285   }
286 
287 
288 
289   inline
IntPropLevels(void)290   IntPropLevels::IntPropLevels(void)
291     : i(sizeof(ipls)/sizeof(Gecode::IntPropLevel)-1) {}
292   inline bool
operator ()(void) const293   IntPropLevels::operator()(void) const {
294     return i>=0;
295   }
296   inline void
operator ++(void)297   IntPropLevels::operator++(void) {
298     i--;
299   }
300   inline Gecode::IntPropLevel
ipl(void) const301   IntPropLevels::ipl(void) const {
302     return ipls[i];
303   }
304 
305 
306   inline
IntPropBasicAdvanced(void)307   IntPropBasicAdvanced::IntPropBasicAdvanced(void)
308     : i(sizeof(ipls)/sizeof(Gecode::IntPropLevel)-1) {}
309   inline bool
operator ()(void) const310   IntPropBasicAdvanced::operator()(void) const {
311     return i>=0;
312   }
313   inline void
operator ++(void)314   IntPropBasicAdvanced::operator++(void) {
315     i--;
316   }
317   inline Gecode::IntPropLevel
ipl(void) const318   IntPropBasicAdvanced::ipl(void) const {
319     return ipls[i];
320   }
321 
322 
323   inline
IntRelTypes(void)324   IntRelTypes::IntRelTypes(void)
325     : i(sizeof(irts)/sizeof(Gecode::IntRelType)-1) {}
326   inline void
reset(void)327   IntRelTypes::reset(void) {
328     i = sizeof(irts)/sizeof(Gecode::IntRelType)-1;
329   }
330   inline bool
operator ()(void) const331   IntRelTypes::operator()(void) const {
332     return i>=0;
333   }
334   inline void
operator ++(void)335   IntRelTypes::operator++(void) {
336     i--;
337   }
338   inline Gecode::IntRelType
irt(void) const339   IntRelTypes::irt(void) const {
340     return irts[i];
341   }
342 
343   inline
BoolOpTypes(void)344   BoolOpTypes::BoolOpTypes(void)
345     : i(sizeof(bots)/sizeof(Gecode::BoolOpType)-1) {}
346   inline bool
operator ()(void) const347   BoolOpTypes::operator()(void) const {
348     return i>=0;
349   }
350   inline void
operator ++(void)351   BoolOpTypes::operator++(void) {
352     i--;
353   }
354   inline Gecode::BoolOpType
bot(void) const355   BoolOpTypes::bot(void) const {
356     return bots[i];
357   }
358 
359 }}
360 
361 // STATISTICS: test-int
362 
363