this repo has no description
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main authors:
4 * Christian Schulte <schulte@gecode.org>
5 *
6 * Copyright:
7 * Christian Schulte, 2005
8 *
9 * This file is part of Gecode, the generic constraint
10 * development environment:
11 * http://www.gecode.org
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining
14 * a copy of this software and associated documentation files (the
15 * "Software"), to deal in the Software without restriction, including
16 * without limitation the rights to use, copy, modify, merge, publish,
17 * distribute, sublicense, and/or sell copies of the Software, and to
18 * permit persons to whom the Software is furnished to do so, subject to
19 * the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be
22 * included in all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 *
32 */
33
34#include "test/int.hh"
35
36namespace Test { namespace Int {
37
38 /// %Tests for count constraints
39 namespace Count {
40
41 /**
42 * \defgroup TaskTestIntCount Count constraints
43 * \ingroup TaskTestInt
44 */
45 //@{
46 /// %Test number of equal integers equal to integer
47 class IntInt : public Test {
48 protected:
49 /// Integer relation type to propagate
50 Gecode::IntRelType irt;
51 public:
52 /// Create and register test
53 IntInt(Gecode::IntRelType irt0)
54 : Test("Count::Int::Int::"+str(irt0),4,-2,2), irt(irt0) {}
55 /// %Test whether \a x is solution
56 virtual bool solution(const Assignment& x) const {
57 int m = 0;
58 for (int i=x.size(); i--; )
59 if (x[i] == 0)
60 m++;
61 return cmp(m,irt,2);
62 }
63 /// Post constraint on \a x
64 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
65 Gecode::count(home, x, 0, irt, 2);
66 }
67 };
68
69 /// %Test number of integers contained in an integer set equal to integer
70 class SetInt : public Test {
71 protected:
72 /// Integer relation type to propagate
73 Gecode::IntRelType irt;
74 public:
75 /// Create and register test
76 SetInt(Gecode::IntRelType irt0)
77 : Test("Count::Set::Int::"+str(irt0),4,-2,2), irt(irt0) {}
78 /// %Test whether \a x is solution
79 virtual bool solution(const Assignment& x) const {
80 int m = 0;
81 for (int i=x.size(); i--; )
82 if ((x[i] >= -1) && (x[i] <= 1))
83 m++;
84 return cmp(m,irt,2);
85 }
86 /// Post constraint on \a x
87 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
88 Gecode::IntSet s(-1,1);
89 Gecode::count(home, x, s, irt, 2);
90 }
91 };
92
93 /// %Test number of equal integers equal to integer with duplicate variables
94 class IntIntDup : public Test {
95 protected:
96 /// Integer relation type to propagate
97 Gecode::IntRelType irt;
98 public:
99 /// Create and register test
100 IntIntDup(Gecode::IntRelType irt0)
101 : Test("Count::Int::Int::Dup::"+str(irt0),4,-2,2), irt(irt0) {}
102 /// %Test whether \a x is solution
103 virtual bool solution(const Assignment& x) const {
104 int m = 0;
105 for (int i=x.size(); i--; )
106 if (x[i] == 0)
107 m += 2;
108 return cmp(m,irt,4);
109 }
110 /// Post constraint on \a x
111 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
112 Gecode::IntVarArgs y(8);
113 for (int i=x.size(); i--; )
114 y[i]=y[4+i]=x[i];
115 Gecode::count(home, y, 0, irt, 4);
116 }
117 };
118
119 /// %Test number of equal integers equal to integer variable
120 class IntVar : public Test {
121 protected:
122 /// Integer relation type to propagate
123 Gecode::IntRelType irt;
124 public:
125 /// Create and register test
126 IntVar(Gecode::IntRelType irt0)
127 : Test("Count::Int::Var::"+str(irt0),5,-2,2), irt(irt0) {}
128 /// %Test whether \a x is solution
129 virtual bool solution(const Assignment& x) const {
130 int m = 0;
131 for (int i=0; i<4; i++)
132 if (x[i] == 0)
133 m++;
134 return cmp(m,irt,x[4]);
135 }
136 /// Post constraint on \a x
137 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
138 Gecode::IntVarArgs y(4);
139 for (int i=0; i<4; i++)
140 y[i]=x[i];
141 Gecode::count(home, y, 0, irt, x[4]);
142 }
143 };
144
145 /// %Test number of integers contained in an integer set equal to integer variable
146 class SetVar : public Test {
147 protected:
148 /// Integer relation type to propagate
149 Gecode::IntRelType irt;
150 public:
151 /// Create and register test
152 SetVar(Gecode::IntRelType irt0)
153 : Test("Count::Set::Var::"+str(irt0),5,-2,2), irt(irt0) {}
154 /// %Test whether \a x is solution
155 virtual bool solution(const Assignment& x) const {
156 int m = 0;
157 for (int i=0; i<4; i++)
158 if ((x[i] >= -1) && (x[i] <= 1))
159 m++;
160 return cmp(m,irt,x[4]);
161 }
162 /// Post constraint on \a x
163 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
164 Gecode::IntVarArgs y(4);
165 for (int i=0; i<4; i++)
166 y[i]=x[i];
167 Gecode::IntSet s(-1,1);
168 Gecode::count(home, y, s, irt, x[4]);
169 }
170 };
171
172 Gecode::IntArgs ints({1,0,3,2});
173
174 /// %Test number of several equal integers equal to integer
175 class IntArrayInt : public Test {
176 protected:
177 /// Integer relation type to propagate
178 Gecode::IntRelType irt;
179 public:
180 /// Create and register test
181 IntArrayInt(Gecode::IntRelType irt0)
182 : Test("Count::IntArray::Int::"+str(irt0),5,-2,2), irt(irt0) {}
183 /// %Test whether \a x is solution
184 virtual bool solution(const Assignment& x) const {
185 int m = 0;
186 for (int i=0; i<4; i++)
187 if (x[i] == ints[i])
188 m++;
189 return cmp(m,irt,2);
190 }
191 /// Post constraint on \a x
192 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
193 Gecode::IntVarArgs y(4);
194 for (int i=0; i<4; i++)
195 y[i]=x[i];
196 Gecode::count(home, y, ints, irt, 2);
197 }
198 };
199
200 /// %Test number of several equal integers equal to integer variable
201 class IntArrayVar : public Test {
202 protected:
203 /// Integer relation type to propagate
204 Gecode::IntRelType irt;
205 public:
206 /// Create and register test
207 IntArrayVar(Gecode::IntRelType irt0)
208 : Test("Count::IntArray::Var::"+str(irt0),5,-2,2), irt(irt0) {}
209 /// %Test whether \a x is solution
210 virtual bool solution(const Assignment& x) const {
211 int m = 0;
212 for (int i=0; i<4; i++)
213 if (x[i] == ints[i])
214 m++;
215 return cmp(m,irt,x[4]);
216 }
217 /// Post constraint on \a x
218 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
219 Gecode::IntVarArgs y(4);
220 for (int i=0; i<4; i++)
221 y[i]=x[i];
222 Gecode::count(home, y, ints, irt, x[4]);
223 }
224 };
225
226 /// %Test number of equal integers equal to integer variable with sharing
227 class IntVarShared : public Test {
228 protected:
229 /// Integer relation type to propagate
230 Gecode::IntRelType irt;
231 public:
232 /// Create and register test
233 IntVarShared(Gecode::IntRelType irt0)
234 : Test("Count::Int::Var::Shared::"+str(irt0),4,-2,2), irt(irt0) {}
235 /// %Test whether \a x is solution
236 virtual bool solution(const Assignment& x) const {
237 int m = 0;
238 for (int i=0; i<4; i++)
239 if (x[i] == 0)
240 m++;
241 return cmp(m,irt,x[2]);
242 }
243 /// Post constraint on \a x
244 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
245 Gecode::count(home, x, 0, irt, x[2]);
246 }
247 };
248
249 /// %Test number of equal variables equal to integer variable
250 class VarVar : public Test {
251 protected:
252 /// Integer relation type to propagate
253 Gecode::IntRelType irt;
254 public:
255 /// Create and register test
256 VarVar(Gecode::IntRelType irt0, Gecode::IntPropLevel ipl)
257 : Test("Count::Var::Var::"+str(irt0)+"::"+str(ipl),5,-2,2,false,ipl),
258 irt(irt0) {
259 contest = CTL_NONE;
260 }
261 /// %Test whether \a x is solution
262 virtual bool solution(const Assignment& x) const {
263 int m = 0;
264 for (int i=0; i<3; i++)
265 if (x[i] == x[3])
266 m++;
267 return cmp(m,irt,x[4]);
268 }
269 /// Post constraint on \a x
270 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
271 Gecode::IntVarArgs y(3);
272 for (int i=0; i<3; i++)
273 y[i]=x[i];
274 Gecode::count(home, y, x[3], irt, x[4], ipl);
275 }
276 };
277
278 /// %Test number of equal variables equal to integer
279 class VarInt : public Test {
280 protected:
281 /// Integer relation type to propagate
282 Gecode::IntRelType irt;
283 public:
284 /// Create and register test
285 VarInt(Gecode::IntRelType irt0, Gecode::IntPropLevel ipl)
286 : Test("Count::Var::Int::"+str(irt0)+"::"+str(ipl),4,-2,2,false,ipl),
287 irt(irt0) {
288 contest = CTL_NONE;
289 }
290 /// %Test whether \a x is solution
291 virtual bool solution(const Assignment& x) const {
292 int m = 0;
293 for (int i=0; i<3; i++)
294 if (x[i] == x[3])
295 m++;
296 return cmp(m,irt,2);
297 }
298 /// Post constraint on \a x
299 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
300 Gecode::IntVarArgs y(3);
301 for (int i=0; i<3; i++)
302 y[i]=x[i];
303 Gecode::count(home, y, x[3], irt, 2, ipl);
304 }
305 };
306
307 /// %Test number of equal variables equal to integer variable with sharing
308 class VarVarSharedA : public Test {
309 protected:
310 /// Integer relation type to propagate
311 Gecode::IntRelType irt;
312 public:
313 /// Create and register test
314 VarVarSharedA(Gecode::IntRelType irt0)
315 : Test("Count::Var::Var::Shared::A::"+str(irt0),5,-2,2), irt(irt0) {}
316 /// %Test whether \a x is solution
317 virtual bool solution(const Assignment& x) const {
318 int m = 0;
319 for (int i=0; i<4; i++)
320 if (x[i] == x[1])
321 m++;
322 return cmp(m,irt,x[4]);
323 }
324 /// Post constraint on \a x
325 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
326 Gecode::IntVarArgs y(4);
327 for (int i=0; i<4; i++)
328 y[i]=x[i];
329 Gecode::count(home, y, x[1], irt, x[4]);
330 }
331 };
332
333 /// %Test number of equal variables equal to integer variable with sharing
334 class VarVarSharedB : public Test {
335 protected:
336 /// Integer relation type to propagate
337 Gecode::IntRelType irt;
338 public:
339 /// Create and register test
340 VarVarSharedB(Gecode::IntRelType irt0)
341 : Test("Count::Var::Var::Shared::B::"+str(irt0),5,-2,2), irt(irt0) {}
342 /// %Test whether \a x is solution
343 virtual bool solution(const Assignment& x) const {
344 int m = 0;
345 for (int i=0; i<4; i++)
346 if (x[i] == x[4])
347 m++;
348 return cmp(m,irt,x[3]);
349 }
350 /// Post constraint on \a x
351 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
352 Gecode::IntVarArgs y(4);
353 for (int i=0; i<4; i++)
354 y[i]=x[i];
355 Gecode::count(home, y, x[4], irt, x[3]);
356 }
357 };
358
359 /// %Test number of equal variables equal to integer variable with sharing
360 class VarVarSharedC : public Test {
361 protected:
362 /// Integer relation type to propagate
363 Gecode::IntRelType irt;
364 public:
365 /// Create and register test
366 VarVarSharedC(Gecode::IntRelType irt0)
367 : Test("Count::Var::Var::Shared::C::"+str(irt0),4,-2,2), irt(irt0) {}
368 /// %Test whether \a x is solution
369 virtual bool solution(const Assignment& x) const {
370 int m = 0;
371 for (int i=0; i<4; i++)
372 if (x[i] == x[1])
373 m++;
374 return cmp(m,irt,x[3]);
375 }
376 /// Post constraint on \a x
377 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
378 Gecode::count(home, x, x[1], irt, x[3]);
379 }
380 };
381
382 /// Help class to create and register tests
383 class Create {
384 public:
385 /// Perform creation and registration
386 Create(void) {
387 for (IntRelTypes irts; irts(); ++irts) {
388 (void) new IntInt(irts.irt());
389 (void) new SetInt(irts.irt());
390 (void) new IntIntDup(irts.irt());
391 (void) new IntVar(irts.irt());
392 (void) new SetVar(irts.irt());
393 (void) new IntArrayInt(irts.irt());
394 (void) new IntArrayVar(irts.irt());
395 (void) new IntVarShared(irts.irt());
396 (void) new VarVar(irts.irt(), Gecode::IPL_BND);
397 (void) new VarVar(irts.irt(), Gecode::IPL_DOM);
398 (void) new VarInt(irts.irt(), Gecode::IPL_BND);
399 (void) new VarInt(irts.irt(), Gecode::IPL_DOM);
400 (void) new VarVarSharedA(irts.irt());
401 (void) new VarVarSharedB(irts.irt());
402 (void) new VarVarSharedC(irts.irt());
403 }
404 }
405 };
406
407 Create c;
408 //@}
409
410 }
411}}
412
413// STATISTICS: test-int
414