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, 2011
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 <gecode/minimodel.hh>
35#include "test/int.hh"
36
37namespace Test { namespace Int {
38
39 /// %Tests for membership constraints
40 namespace Member {
41
42 /**
43 * \defgroup TaskTestIntMember Membership constraints
44 * \ingroup TaskTestInt
45 */
46 //@{
47 /// %Test membership for integer variables
48 class Int : public Test {
49 public:
50 /// Create and register test
51 Int(int n) : Test("Member::Int::"+str(n),n+1,0,n,true) {}
52 /// %Test whether \a x is solution
53 virtual bool solution(const Assignment& x) const {
54 int n = x.size() - 1;
55 for (int i=n; i--; )
56 if (x[i] == x[n])
57 return true;
58 return false;
59 }
60 /// Post constraint on \a x
61 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
62 using namespace Gecode;
63 int n = x.size() - 1;
64 IntVarArgs y(n);
65 for (int i=n; i--; )
66 y[i] = x[i];
67 member(home, y, x[n]);
68 }
69 /// Post reified constraint on \a x for \a r
70 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x,
71 Gecode::Reify r) {
72 using namespace Gecode;
73 int n = x.size() - 1;
74 IntVarArgs y(n);
75 for (int i=n; i--; )
76 y[i] = x[i];
77 member(home, y, x[n], r);
78 }
79 };
80
81 /// %Test membership for Boolean variables
82 class Bool : public Test {
83 public:
84 /// Create and register test
85 Bool(int n) : Test("Member::Bool::"+str(n),n+1,0,1,true) {}
86 /// %Test whether \a x is solution
87 virtual bool solution(const Assignment& x) const {
88 int n = x.size() - 1;
89 for (int i=n; i--; )
90 if (x[i] == x[n])
91 return true;
92 return false;
93 }
94 /// Post constraint on \a x
95 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
96 using namespace Gecode;
97 int n = x.size() - 1;
98 BoolVarArgs y(n);
99 for (int i=n; i--; )
100 y[i] = channel(home,x[i]);
101 member(home, y, channel(home,x[n]));
102 }
103 /// Post reified constraint on \a x for \a r
104 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x,
105 Gecode::Reify r) {
106 using namespace Gecode;
107 int n = x.size() - 1;
108 BoolVarArgs y(n);
109 for (int i=n; i--; )
110 y[i] = channel(home,x[i]);
111 member(home, y, channel(home,x[n]), r);
112 }
113 };
114
115 /// Help class to create and register tests
116 class Create {
117 public:
118 /// Perform creation and registration
119 Create(void) {
120 for (int i=0; i<=4; i++) {
121 (void) new Int(i);
122 (void) new Bool(i);
123 }
124 }
125 };
126
127 Create c;
128 //@}
129
130 }
131}}
132
133// STATISTICS: test-int
134