Generated on Sat Feb 7 2015 02:01:14 for Gecode by doxygen 1.8.9.1
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  * Last modified:
10  * $Date: 2011-11-03 14:57:30 +0100 (Thu, 03 Nov 2011) $ by $Author: tack $
11  * $Revision: 12455 $
12  *
13  * This file is part of Gecode, the generic constraint
14  * development environment:
15  * http://www.gecode.org
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining
18  * a copy of this software and associated documentation files (the
19  * "Software"), to deal in the Software without restriction, including
20  * without limitation the rights to use, copy, modify, merge, publish,
21  * distribute, sublicense, and/or sell copies of the Software, and to
22  * permit persons to whom the Software is furnished to do so, subject to
23  * the following conditions:
24  *
25  * The above copyright notice and this permission notice shall be
26  * included in all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35  *
36  */
37 
38 #include <gecode/kernel.hh>
39 #include <gecode/int.hh>
40 
41 #include "test/test.hh"
42 
44 #define CHECK_TEST(T,M) \
45 if (opt.log) \
46  olog << ind(3) << "Check: " << (M) << std::endl; \
47 if (!(T)) { \
48  problem = (M); goto failed; \
49 }
50 
52 #define START_TEST(T) \
53  if (opt.log) { \
54  olog.str(""); \
55  olog << ind(2) << "Testing: " << (T) << std::endl; \
56  } \
57  test = (T);
58 
59 namespace Test {
60 
62  namespace Array {
63 
65  static const std::string prefix("Array::Iterator::");
66 
68  class Iterator : public Test::Base {
69  protected:
71  static const int n = 16;
73  Iterator(const std::string& name) : Test::Base(prefix + name) {}
75  template<class Array> bool runTestForArray(Array& a) {
76  // Test/problem information.
77  const char* test = "NONE";
78  const char* problem = "NONE";
79  // Constant reference to the array
80  const Array& const_a = a;
81 
82  START_TEST("Iteration");
83  {
84  typedef typename Array::reference reference;
85  typedef typename Array::pointer pointer;
86  typedef typename Array::iterator iterator;
87  const iterator begin = a.begin(), end = a.end();
88  CHECK_TEST(end-begin==a.size(),"Distance != size");
89  int index = 0;
90  iterator iter = begin;
91  for(; iter != end; ++iter, ++index) {
92  reference ref = *iter;
93  const pointer ptr = &ref;
94  CHECK_TEST(ptr==&a[index],"Iterator points to the wrong element (going forward)");
95  }
96  CHECK_TEST(index==a.size(),"Iteration covered the wrong number of elements (going forward)");
97  for(; iter != begin; --iter, --index) {
98  reference ref = *(iter-1);
99  const pointer ptr = &ref;
100  CHECK_TEST(ptr==&a[index-1],"Iterator points to the wrong element (going backwards)");
101  }
102  CHECK_TEST(index==0,"Iteration covered the wrong number of elements (going backward)");
103  }
104  START_TEST("Read-only iteration");
105  {
106  typedef typename Array::const_reference reference;
107  typedef typename Array::const_pointer pointer;
108  typedef typename Array::const_iterator iterator;
109  const iterator begin = const_a.begin(), end = const_a.end();
110  CHECK_TEST(end-begin==const_a.size(),"Distance != size");
111  int index = 0;
112  iterator iter = begin;
113  for(; iter != end; ++iter, ++index) {
114  reference ref = *iter;
115  const pointer ptr = &ref;
116  CHECK_TEST(ptr==&const_a[index],"Iterator points to the wrong element (going forward)");
117  }
118  CHECK_TEST(index==const_a.size(),"Iteration covered the wrong number of elements (going forward)");
119  for(; iter != begin; --iter, --index) {
120  reference ref = *(iter-1);
121  const pointer ptr = &ref;
122  CHECK_TEST(ptr==&const_a[index-1],"Iterator points to the wrong element (going backwards)");
123  }
124  CHECK_TEST(index==0,"Iteration covered the wrong number of elements (going backward)");
125  }
126 
127  START_TEST("Reverse iteration");
128  {
129  typedef typename Array::reference reference;
130  typedef typename Array::pointer pointer;
131  typedef typename Array::reverse_iterator iterator;
132  const iterator begin = a.rbegin(), end = a.rend();
133  CHECK_TEST(end-begin==a.size(),"Distance != size");
134  int index = a.size();
135  iterator iter = begin;
136  for(; iter != end; ++iter, --index) {
137  reference ref = *iter;
138  const pointer ptr = &ref;
139  CHECK_TEST(ptr==&a[index-1],"Iterator points to the wrong element (going forward)");
140  }
141  CHECK_TEST(index==0,"Iteration covered the wrong number of elements (going forward)");
142  for(; iter != begin; --iter, ++index) {
143  reference ref = *(iter-1);
144  const pointer ptr = &ref;
145  CHECK_TEST(ptr==&a[index],"Iterator points to the wrong element (going backwards)");
146  }
147  CHECK_TEST(index==a.size(),"Iteration covered the wrong number of elements (going backward)");
148  }
149 
150  START_TEST("Reverse read-only iteration");
151  {
152  typedef typename Array::const_reference reference;
153  typedef typename Array::const_pointer pointer;
154  typedef typename Array::const_reverse_iterator iterator;
155  const iterator begin = const_a.rbegin(), end = const_a.rend();
156  CHECK_TEST(end-begin==const_a.size(),"Distance != size");
157  int index = a.size();
158  iterator iter = begin;
159  for(; iter != end; ++iter, --index) {
160  reference ref = *iter;
161  const pointer ptr = &ref;
162  CHECK_TEST(ptr==&const_a[index-1],"Iterator points to the wrong element (going forward)");
163  }
164  CHECK_TEST(index==0,"Iteration covered the wrong number of elements (going forward)");
165  for(; iter != begin; --iter, ++index) {
166  reference ref = *(iter-1);
167  const pointer ptr = &ref;
168  CHECK_TEST(ptr==&const_a[index],"Iterator points to the wrong element (going backwards)");
169  }
170  CHECK_TEST(index==a.size(),"Iteration covered the wrong number of elements (going backward)");
171  }
172 
173  return true;
174  failed:
175  if (opt.log)
176  olog << "FAILURE" << std::endl
177  << ind(1) << "Test: " << test << std::endl
178  << ind(1) << "Problem: " << problem << std::endl;
179  return false;
180  }
181  };
182 
184  class TestSpace : public Gecode::Space {
185  public:
186  TestSpace(void) : Space() {}
187  TestSpace(bool share, TestSpace& s) : Space(share,s) {}
188  virtual Space* copy(bool share) {
189  return new TestSpace(share,*this);
190  }
191  };
192 
194  class VarArrayIterator : public Iterator {
195  protected:
197  static const int n = 16;
200  public:
202  VarArrayIterator(void) : Iterator("VarArray") {}
204  bool run(void) {
205  // Space for the test
206  TestSpace s;
207  // VarArray for the test
208  Array a(s,rand(n));
209  // Run the iterator test
210  return runTestForArray(a);
211  }
213 
215  class VarArgsIterator : public Iterator {
216  protected:
218  static const int n = 16;
221  public:
223  VarArgsIterator(void) : Iterator("VarArgs") {}
225  bool run(void) {
226  // Space for the test
227  TestSpace s;
228  // VarArray for the test
229  Array a(rand(n));
230  // Run the iterator test
231  return runTestForArray(a);
232  }
234 
236  class ViewArrayIterator : public Iterator {
237  protected:
239  static const int n = 16;
242  public:
244  ViewArrayIterator(void) : Iterator("ViewArray") {}
246  bool run(void) {
247  // Space for the test
248  TestSpace s;
249  // VarArray for the test
250  Array a(s,rand(n));
251  // Run the iterator test
252  return runTestForArray(a);
253  }
255 
257  class SharedArrayIterator : public Iterator {
258  protected:
260  static const int n = 16;
263  public:
265  SharedArrayIterator(void) : Iterator("SharedArray") {}
267  bool run(void) {
268  // SharedArray for the test
269  Array a(rand(n));
270  // Run the iterator test
271  return runTestForArray(a);
272  }
274 
275 }}
276 
277 // STATISTICS: test-core
Test space.
Definition: array.cpp:184
const std::string & name(void) const
Return name of test.
Definition: test.hpp:54
static const int n
Maximum array size.
Definition: array.cpp:260
Simple class for describing identation.
Definition: test.hh:70
Test::Array::VarArgsIterator varArgsIteratorTest
static Gecode::Support::RandomGenerator rand
Random number generator.
Definition: test.hh:138
VarArrayIterator(void)
Initialize test.
Definition: array.cpp:202
Gecode::ArgArrayBase< int > Array
Array type being tested.
Definition: array.cpp:220
virtual Space * copy(bool share)
Copying member function.
Definition: array.cpp:188
Gecode::ViewArray< Gecode::IntVar > Array
Array type being tested.
Definition: array.cpp:241
Iterator(const std::string &name)
Initialize test.
Definition: array.cpp:73
VarArgsIterator(void)
Initialize test.
Definition: array.cpp:223
Variable arrays
Definition: array.hpp:52
bool run(void)
Perform actual tests.
Definition: array.cpp:225
Test::Array::SharedArrayIterator sharedArrayIteratorTest
static const int n
Maximum array size.
Definition: array.cpp:197
static const int n
Maximum array size.
Definition: array.cpp:239
Test::Array::ViewArrayIterator viewArrayIteratorTest
Computation spaces.
Definition: core.hpp:1362
SharedArrayIterator(void)
Initialize test.
Definition: array.cpp:265
bool run(void)
Perform actual tests.
Definition: array.cpp:204
Options opt
The options.
Definition: test.cpp:101
ViewArrayIterator(void)
Initialize test.
Definition: array.cpp:244
TestSpace(bool share, TestSpace &s)
Definition: array.cpp:187
Base class for testing iterators
Definition: array.cpp:68
bool run(void)
Perform actual tests.
Definition: array.cpp:246
Gecode::VarArray< Gecode::IntVar > Array
Array type being tested.
Definition: array.cpp:199
Base class for all tests to be run
Definition: test.hh:107
#define CHECK_TEST(T, M)
Check the test result and handle failed test.
Definition: array.cpp:44
bool log
Whether to log the tests.
Definition: test.hh:95
View arrays.
Definition: array.hpp:234
Gecode::SharedArray< int > Array
Array type being tested.
Definition: array.cpp:262
General test support.
Definition: afc.cpp:43
Test::Array::VarArrayIterator varArrayIteratorTest
Space(void)
Default constructor.
Definition: core.cpp:114
Class for testing the VarArray iterator
Definition: array.cpp:194
std::ostringstream olog
Stream used for logging.
Definition: test.cpp:57
bool run(void)
Perform actual tests.
Definition: array.cpp:267
Class for testing the ViewArray iterator
Definition: array.cpp:236
Class for testing the SharedArray iterator
Definition: array.cpp:257
bool runTestForArray(Array &a)
Perform actual tests.
Definition: array.cpp:75
static const int n
Maximum array size.
Definition: array.cpp:218
#define START_TEST(T)
Start new test.
Definition: array.cpp:52
Class for testing the VarArgs iterator
Definition: array.cpp:215
struct Gecode::@518::NNF::@57::@59 a
For atomic nodes.
static const int n
Maximum array size.
Definition: array.cpp:71