Generated on for Gecode by doxygen 1.15.0
array.cpp
Go to the documentation of this file.
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main authors:
4 * Gregory Crosswhite <gcross@phys.washington.edu>
5 *
6 * Copyright:
7 * Gregory Crosswhite, 2011
8 *
9 * This file is part of Gecode, the generic constraint
10 * development environment:
11 * http://www.gecode.dev
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/kernel.hh>
35#include <gecode/int.hh>
36
37#include <vector>
38
39#include "test/test.hh"
40
42#define CHECK_TEST(T,M) \
43do { \
44if (opt.log) \
45 olog << ind(3) << "Check: " << (M) << std::endl; \
46if (!(T)) { \
47 problem = (M); goto failed; \
48} \
49} while (false)
50
52#define START_TEST(T) \
53do { \
54 if (opt.log) { \
55 olog.str(""); \
56 olog << ind(2) << "Testing: " << (T) << std::endl; \
57 } \
58 test = (T); \
59} while (false)
60
61namespace Test {
62
64 namespace Array {
65
67 static const std::string prefix("Array::Iterator::");
68
70 class Iterator : public Test::Base {
71 protected:
73 static const int n = 16;
75 Iterator(const std::string& name) : Test::Base(prefix + name) {}
77 template<class Array> bool runTestForArray(Array& a) {
78 // Test/problem information.
79 const char* test = "NONE";
80 const char* problem = "NONE";
81 // Constant reference to the array
82 const Array& const_a = a;
83
84 START_TEST("Iteration");
85 {
86 typedef typename Array::reference reference;
87 typedef typename Array::pointer pointer;
88 typedef typename Array::iterator iterator;
89 const iterator begin = a.begin(), end = a.end();
90 CHECK_TEST(end-begin==a.size(),"Distance != size");
91 int index = 0;
92 iterator iter = begin;
93 for(; iter != end; ++iter, ++index) {
94 reference ref = *iter;
95 const pointer ptr = &ref;
96 CHECK_TEST(ptr==&a[index],"Iterator points to the wrong element (going forward)");
97 }
98 CHECK_TEST(index==a.size(),"Iteration covered the wrong number of elements (going forward)");
99 for(; iter != begin; --iter, --index) {
100 reference ref = *(iter-1);
101 const pointer ptr = &ref;
102 CHECK_TEST(ptr==&a[index-1],"Iterator points to the wrong element (going backwards)");
103 }
104 CHECK_TEST(index==0,"Iteration covered the wrong number of elements (going backward)");
105 }
106 START_TEST("Read-only iteration");
107 {
108 typedef typename Array::const_reference reference;
109 typedef typename Array::const_pointer pointer;
110 typedef typename Array::const_iterator iterator;
111 const iterator begin = const_a.begin(), end = const_a.end();
112 CHECK_TEST(end-begin==const_a.size(),"Distance != size");
113 int index = 0;
114 iterator iter = begin;
115 for(; iter != end; ++iter, ++index) {
116 reference ref = *iter;
117 const pointer ptr = &ref;
118 CHECK_TEST(ptr==&const_a[index],"Iterator points to the wrong element (going forward)");
119 }
120 CHECK_TEST(index==const_a.size(),"Iteration covered the wrong number of elements (going forward)");
121 for(; iter != begin; --iter, --index) {
122 reference ref = *(iter-1);
123 const pointer ptr = &ref;
124 CHECK_TEST(ptr==&const_a[index-1],"Iterator points to the wrong element (going backwards)");
125 }
126 CHECK_TEST(index==0,"Iteration covered the wrong number of elements (going backward)");
127 }
128
129 START_TEST("Reverse iteration");
130 {
131 typedef typename Array::reference reference;
132 typedef typename Array::pointer pointer;
133 typedef typename Array::reverse_iterator iterator;
134 const iterator begin = a.rbegin(), end = a.rend();
135 CHECK_TEST(end-begin==a.size(),"Distance != size");
136 int index = a.size();
137 iterator iter = begin;
138 for(; iter != end; ++iter, --index) {
139 reference ref = *iter;
140 const pointer ptr = &ref;
141 CHECK_TEST(ptr==&a[index-1],"Iterator points to the wrong element (going forward)");
142 }
143 CHECK_TEST(index==0,"Iteration covered the wrong number of elements (going forward)");
144 for(; iter != begin; --iter, ++index) {
145 reference ref = *(iter-1);
146 const pointer ptr = &ref;
147 CHECK_TEST(ptr==&a[index],"Iterator points to the wrong element (going backwards)");
148 }
149 CHECK_TEST(index==a.size(),"Iteration covered the wrong number of elements (going backward)");
150 }
151
152 START_TEST("Reverse read-only iteration");
153 {
154 typedef typename Array::const_reference reference;
155 typedef typename Array::const_pointer pointer;
156 typedef typename Array::const_reverse_iterator iterator;
157 const iterator begin = const_a.rbegin(), end = const_a.rend();
158 CHECK_TEST(end-begin==const_a.size(),"Distance != size");
159 int index = a.size();
160 iterator iter = begin;
161 for(; iter != end; ++iter, --index) {
162 reference ref = *iter;
163 const pointer ptr = &ref;
164 CHECK_TEST(ptr==&const_a[index-1],"Iterator points to the wrong element (going forward)");
165 }
166 CHECK_TEST(index==0,"Iteration covered the wrong number of elements (going forward)");
167 for(; iter != begin; --iter, ++index) {
168 reference ref = *(iter-1);
169 const pointer ptr = &ref;
170 CHECK_TEST(ptr==&const_a[index],"Iterator points to the wrong element (going backwards)");
171 }
172 CHECK_TEST(index==a.size(),"Iteration covered the wrong number of elements (going backward)");
173 }
174
175 return true;
176 failed:
177 if (opt.log)
178 olog << "FAILURE" << std::endl
179 << ind(1) << "Test: " << test << std::endl
180 << ind(1) << "Problem: " << problem << std::endl;
181 return false;
182 }
183 };
184
186 class TestSpace : public Gecode::Space {
187 public:
188 TestSpace(void) : Space() {}
190 virtual Space* copy(void) {
191 return new TestSpace(*this);
192 }
193 };
194
196 static const std::string vectorPrefix("Array::Vector::");
197
199 class EmptyVector : public Test::Base {
200 public:
202 EmptyVector(void) : Test::Base(vectorPrefix + "Empty") {}
204 bool run(void) {
205 std::vector<int> ints;
206 Gecode::IntArgs ia(ints);
207 if (ia.size() != 0)
208 return false;
209
210 std::vector<Gecode::IntVar> intVars;
211 Gecode::IntVarArgs iva(intVars);
212 if (iva.size() != 0)
213 return false;
214
215 std::vector<Gecode::BoolVar> boolVars;
216 Gecode::BoolVarArgs bva(boolVars);
217 return bva.size() == 0;
218 }
220
222 class VarArrayIterator : public Iterator {
223 protected:
225 static const int n = 16;
228 public:
230 VarArrayIterator(void) : Iterator("VarArray") {}
232 bool run(void) {
233 // Space for the test
234 TestSpace s;
235 // VarArray for the test
236 Array a(s,_rand(n));
237 // Run the iterator test
238 return runTestForArray(a);
239 }
241
243 class VarArgsIterator : public Iterator {
244 protected:
246 static const int n = 16;
249 public:
251 VarArgsIterator(void) : Iterator("VarArgs") {}
253 bool run(void) {
254 // Space for the test
255 TestSpace s;
256 // VarArray for the test
257 Array a(_rand(n));
258 // Run the iterator test
259 return runTestForArray(a);
260 }
262
265 protected:
267 static const int n = 16;
270 public:
272 ViewArrayIterator(void) : Iterator("ViewArray") {}
274 bool run(void) {
275 // Space for the test
276 TestSpace s;
277 // VarArray for the test
278 Array a(s,_rand(n));
279 // Run the iterator test
280 return runTestForArray(a);
281 }
283
286 protected:
288 static const int n = 16;
291 public:
293 SharedArrayIterator(void) : Iterator("SharedArray") {}
295 bool run(void) {
296 // SharedArray for the test
297 Array a(_rand(n));
298 // Run the iterator test
299 return runTestForArray(a);
300 }
302
303}}
304
305// STATISTICS: test-core
Base-class for argument arrays.
Definition array.hpp:541
int size(void) const
Return size of array (number of elements).
Definition array.hpp:1597
Passing Boolean variables.
Definition int.hh:738
FloatNum size(void) const
Return size of float value (distance between maximum and minimum).
Definition val.hpp:78
Passing integer arguments.
Definition int.hh:652
Passing integer variables.
Definition int.hh:680
Shared array with arbitrary number of elements.
Computation spaces.
Definition core.hpp:1775
Variable arrays
Definition array.hpp:112
View arrays.
Definition array.hpp:255
EmptyVector(void)
Initialize test.
Definition array.cpp:202
bool run(void)
Perform actual tests.
Definition array.cpp:204
bool runTestForArray(Array &a)
Perform actual tests.
Definition array.cpp:77
Iterator(const std::string &name)
Initialize test.
Definition array.cpp:75
static const int n
Maximum array size.
Definition array.cpp:73
SharedArrayIterator(void)
Initialize test.
Definition array.cpp:293
Gecode::SharedArray< int > Array
Array type being tested.
Definition array.cpp:290
static const int n
Maximum array size.
Definition array.cpp:288
bool run(void)
Perform actual tests.
Definition array.cpp:295
TestSpace(TestSpace &s)
Definition array.cpp:189
virtual Space * copy(void)
Copying member function.
Definition array.cpp:190
bool run(void)
Perform actual tests.
Definition array.cpp:253
VarArgsIterator(void)
Initialize test.
Definition array.cpp:251
Gecode::ArgArrayBase< int > Array
Array type being tested.
Definition array.cpp:248
static const int n
Maximum array size.
Definition array.cpp:246
Gecode::VarArray< Gecode::IntVar > Array
Array type being tested.
Definition array.cpp:227
bool run(void)
Perform actual tests.
Definition array.cpp:232
VarArrayIterator(void)
Initialize test.
Definition array.cpp:230
static const int n
Maximum array size.
Definition array.cpp:225
Gecode::ViewArray< Gecode::IntVar > Array
Array type being tested.
Definition array.cpp:269
ViewArrayIterator(void)
Initialize test.
Definition array.cpp:272
bool run(void)
Perform actual tests.
Definition array.cpp:274
static const int n
Maximum array size.
Definition array.cpp:267
Base class for all tests to be run
Definition test.hh:121
Gecode::Support::RandomGenerator _rand
Random number generator.
Definition test.hh:164
Base(std::string s)
Create and register test with name s.
Definition test.cpp:60
const std::string & name(void) const
Return name of test.
Definition test.hpp:51
Simple class for describing indentation.
Definition test.hh:67
Space(void)
Default constructor.
Definition core.cpp:121
Tests for arrays.
Definition array.cpp:64
Test::Array::ViewArrayIterator viewArrayIteratorTest
Test::Array::EmptyVector emptyVectorTest
Test::Array::VarArrayIterator varArrayIteratorTest
Test::Array::VarArgsIterator varArgsIteratorTest
Test::Array::SharedArrayIterator sharedArrayIteratorTest
General test support.
Definition afc.cpp:39
std::ostringstream olog
Stream used for logging.
Definition test.cpp:54
Options opt
The options.
Definition test.cpp:95
#define START_TEST(T)
Start new test.
Definition array.cpp:52
#define CHECK_TEST(T, M)
Check the test result and handle failed test.
Definition array.cpp:42