Generated on Fri Jan 28 2022 04:43:06 for Gecode by doxygen 1.8.13
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.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/kernel.hh>
35 #include <gecode/int.hh>
36 
37 #include "test/test.hh"
38 
40 #define CHECK_TEST(T,M) \
41 if (opt.log) \
42  olog << ind(3) << "Check: " << (M) << std::endl; \
43 if (!(T)) { \
44  problem = (M); goto failed; \
45 }
46 
48 #define START_TEST(T) \
49  if (opt.log) { \
50  olog.str(""); \
51  olog << ind(2) << "Testing: " << (T) << std::endl; \
52  } \
53  test = (T);
54 
55 namespace Test {
56 
58  namespace Array {
59 
61  static const std::string prefix("Array::Iterator::");
62 
64  class Iterator : public Test::Base {
65  protected:
67  static const int n = 16;
69  Iterator(const std::string& name) : Test::Base(prefix + name) {}
71  template<class Array> bool runTestForArray(Array& a) {
72  // Test/problem information.
73  const char* test = "NONE";
74  const char* problem = "NONE";
75  // Constant reference to the array
76  const Array& const_a = a;
77 
78  START_TEST("Iteration");
79  {
80  typedef typename Array::reference reference;
81  typedef typename Array::pointer pointer;
82  typedef typename Array::iterator iterator;
83  const iterator begin = a.begin(), end = a.end();
84  CHECK_TEST(end-begin==a.size(),"Distance != size");
85  int index = 0;
86  iterator iter = begin;
87  for(; iter != end; ++iter, ++index) {
88  reference ref = *iter;
89  const pointer ptr = &ref;
90  CHECK_TEST(ptr==&a[index],"Iterator points to the wrong element (going forward)");
91  }
92  CHECK_TEST(index==a.size(),"Iteration covered the wrong number of elements (going forward)");
93  for(; iter != begin; --iter, --index) {
94  reference ref = *(iter-1);
95  const pointer ptr = &ref;
96  CHECK_TEST(ptr==&a[index-1],"Iterator points to the wrong element (going backwards)");
97  }
98  CHECK_TEST(index==0,"Iteration covered the wrong number of elements (going backward)");
99  }
100  START_TEST("Read-only iteration");
101  {
102  typedef typename Array::const_reference reference;
103  typedef typename Array::const_pointer pointer;
104  typedef typename Array::const_iterator iterator;
105  const iterator begin = const_a.begin(), end = const_a.end();
106  CHECK_TEST(end-begin==const_a.size(),"Distance != size");
107  int index = 0;
108  iterator iter = begin;
109  for(; iter != end; ++iter, ++index) {
110  reference ref = *iter;
111  const pointer ptr = &ref;
112  CHECK_TEST(ptr==&const_a[index],"Iterator points to the wrong element (going forward)");
113  }
114  CHECK_TEST(index==const_a.size(),"Iteration covered the wrong number of elements (going forward)");
115  for(; iter != begin; --iter, --index) {
116  reference ref = *(iter-1);
117  const pointer ptr = &ref;
118  CHECK_TEST(ptr==&const_a[index-1],"Iterator points to the wrong element (going backwards)");
119  }
120  CHECK_TEST(index==0,"Iteration covered the wrong number of elements (going backward)");
121  }
122 
123  START_TEST("Reverse iteration");
124  {
125  typedef typename Array::reference reference;
126  typedef typename Array::pointer pointer;
127  typedef typename Array::reverse_iterator iterator;
128  const iterator begin = a.rbegin(), end = a.rend();
129  CHECK_TEST(end-begin==a.size(),"Distance != size");
130  int index = a.size();
131  iterator iter = begin;
132  for(; iter != end; ++iter, --index) {
133  reference ref = *iter;
134  const pointer ptr = &ref;
135  CHECK_TEST(ptr==&a[index-1],"Iterator points to the wrong element (going forward)");
136  }
137  CHECK_TEST(index==0,"Iteration covered the wrong number of elements (going forward)");
138  for(; iter != begin; --iter, ++index) {
139  reference ref = *(iter-1);
140  const pointer ptr = &ref;
141  CHECK_TEST(ptr==&a[index],"Iterator points to the wrong element (going backwards)");
142  }
143  CHECK_TEST(index==a.size(),"Iteration covered the wrong number of elements (going backward)");
144  }
145 
146  START_TEST("Reverse read-only iteration");
147  {
148  typedef typename Array::const_reference reference;
149  typedef typename Array::const_pointer pointer;
150  typedef typename Array::const_reverse_iterator iterator;
151  const iterator begin = const_a.rbegin(), end = const_a.rend();
152  CHECK_TEST(end-begin==const_a.size(),"Distance != size");
153  int index = a.size();
154  iterator iter = begin;
155  for(; iter != end; ++iter, --index) {
156  reference ref = *iter;
157  const pointer ptr = &ref;
158  CHECK_TEST(ptr==&const_a[index-1],"Iterator points to the wrong element (going forward)");
159  }
160  CHECK_TEST(index==0,"Iteration covered the wrong number of elements (going forward)");
161  for(; iter != begin; --iter, ++index) {
162  reference ref = *(iter-1);
163  const pointer ptr = &ref;
164  CHECK_TEST(ptr==&const_a[index],"Iterator points to the wrong element (going backwards)");
165  }
166  CHECK_TEST(index==a.size(),"Iteration covered the wrong number of elements (going backward)");
167  }
168 
169  return true;
170  failed:
171  if (opt.log)
172  olog << "FAILURE" << std::endl
173  << ind(1) << "Test: " << test << std::endl
174  << ind(1) << "Problem: " << problem << std::endl;
175  return false;
176  }
177  };
178 
180  class TestSpace : public Gecode::Space {
181  public:
182  TestSpace(void) : Space() {}
183  TestSpace(TestSpace& s) : Space(s) {}
184  virtual Space* copy(void) {
185  return new TestSpace(*this);
186  }
187  };
188 
190  class VarArrayIterator : public Iterator {
191  protected:
193  static const int n = 16;
196  public:
198  VarArrayIterator(void) : Iterator("VarArray") {}
200  bool run(void) {
201  // Space for the test
202  TestSpace s;
203  // VarArray for the test
204  Array a(s,rand(n));
205  // Run the iterator test
206  return runTestForArray(a);
207  }
209 
211  class VarArgsIterator : public Iterator {
212  protected:
214  static const int n = 16;
217  public:
219  VarArgsIterator(void) : Iterator("VarArgs") {}
221  bool run(void) {
222  // Space for the test
223  TestSpace s;
224  // VarArray for the test
225  Array a(rand(n));
226  // Run the iterator test
227  return runTestForArray(a);
228  }
230 
232  class ViewArrayIterator : public Iterator {
233  protected:
235  static const int n = 16;
238  public:
240  ViewArrayIterator(void) : Iterator("ViewArray") {}
242  bool run(void) {
243  // Space for the test
244  TestSpace s;
245  // VarArray for the test
246  Array a(s,rand(n));
247  // Run the iterator test
248  return runTestForArray(a);
249  }
251 
253  class SharedArrayIterator : public Iterator {
254  protected:
256  static const int n = 16;
259  public:
261  SharedArrayIterator(void) : Iterator("SharedArray") {}
263  bool run(void) {
264  // SharedArray for the test
265  Array a(rand(n));
266  // Run the iterator test
267  return runTestForArray(a);
268  }
270 
271 }}
272 
273 // STATISTICS: test-core
Test space.
Definition: array.cpp:180
Simple class for describing identation.
Definition: test.hh:66
Test::Array::VarArgsIterator varArgsIteratorTest
const std::string & name(void) const
Return name of test.
Definition: test.hpp:50
virtual Space * copy(void)
Copying member function.
Definition: array.cpp:184
static Gecode::Support::RandomGenerator rand
Random number generator.
Definition: test.hh:134
VarArrayIterator(void)
Initialize test.
Definition: array.cpp:198
Gecode::ArgArrayBase< int > Array
Array type being tested.
Definition: array.cpp:216
Gecode::ViewArray< Gecode::IntVar > Array
Array type being tested.
Definition: array.cpp:237
Iterator(const std::string &name)
Initialize test.
Definition: array.cpp:69
VarArgsIterator(void)
Initialize test.
Definition: array.cpp:219
Variable arrays
Definition: array.hpp:60
bool run(void)
Perform actual tests.
Definition: array.cpp:221
Test::Array::SharedArrayIterator sharedArrayIteratorTest
Test::Array::ViewArrayIterator viewArrayIteratorTest
Computation spaces.
Definition: core.hpp:1701
SharedArrayIterator(void)
Initialize test.
Definition: array.cpp:261
bool run(void)
Perform actual tests.
Definition: array.cpp:200
Options opt
The options.
Definition: test.cpp:97
ViewArrayIterator(void)
Initialize test.
Definition: array.cpp:240
Base class for testing iterators
Definition: array.cpp:64
bool run(void)
Perform actual tests.
Definition: array.cpp:242
Gecode::VarArray< Gecode::IntVar > Array
Array type being tested.
Definition: array.cpp:195
Base class for all tests to be run
Definition: test.hh:103
#define CHECK_TEST(T, M)
Check the test result and handle failed test.
Definition: array.cpp:40
TestSpace(TestSpace &s)
Definition: array.cpp:183
bool log
Whether to log the tests.
Definition: test.hh:91
View arrays.
Definition: array.hpp:235
Gecode::SharedArray< int > Array
Array type being tested.
Definition: array.cpp:258
General test support.
Definition: afc.cpp:39
Test::Array::VarArrayIterator varArrayIteratorTest
Class for testing the VarArray iterator
Definition: array.cpp:190
std::ostringstream olog
Stream used for logging.
Definition: test.cpp:53
bool run(void)
Perform actual tests.
Definition: array.cpp:263
Class for testing the ViewArray iterator
Definition: array.cpp:232
Class for testing the SharedArray iterator
Definition: array.cpp:253
bool runTestForArray(Array &a)
Perform actual tests.
Definition: array.cpp:71
#define START_TEST(T)
Start new test.
Definition: array.cpp:48
Class for testing the VarArgs iterator
Definition: array.cpp:211
struct Gecode::@593::NNF::@62::@64 a
For atomic nodes.
static const int n
Maximum array size.
Definition: array.cpp:67