array.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include <gecode/kernel.hh>
00035 #include <gecode/int.hh>
00036
00037 #include "test/test.hh"
00038
00040 #define CHECK_TEST(T,M) \
00041 if (opt.log) \
00042 olog << ind(3) << "Check: " << (M) << std::endl; \
00043 if (!(T)) { \
00044 problem = (M); goto failed; \
00045 }
00046
00048 #define START_TEST(T) \
00049 if (opt.log) { \
00050 olog.str(""); \
00051 olog << ind(2) << "Testing: " << (T) << std::endl; \
00052 } \
00053 test = (T);
00054
00055 namespace Test {
00056
00058 namespace Array {
00059
00061 static const std::string prefix("Array::Iterator::");
00062
00064 class Iterator : public Test::Base {
00065 protected:
00067 static const int n = 16;
00069 Iterator(const std::string& name) : Test::Base(prefix + name) {}
00071 template<class Array> bool runTestForArray(Array& a) {
00072
00073 const char* test = "NONE";
00074 const char* problem = "NONE";
00075
00076 const Array& const_a = a;
00077
00078 START_TEST("Iteration");
00079 {
00080 typedef typename Array::reference reference;
00081 typedef typename Array::pointer pointer;
00082 typedef typename Array::iterator iterator;
00083 const iterator begin = a.begin(), end = a.end();
00084 CHECK_TEST(end-begin==a.size(),"Distance != size");
00085 int index = 0;
00086 iterator iter = begin;
00087 for(; iter != end; ++iter, ++index) {
00088 reference ref = *iter;
00089 const pointer ptr = &ref;
00090 CHECK_TEST(ptr==&a[index],"Iterator points to the wrong element (going forward)");
00091 }
00092 CHECK_TEST(index==a.size(),"Iteration covered the wrong number of elements (going forward)");
00093 for(; iter != begin; --iter, --index) {
00094 reference ref = *(iter-1);
00095 const pointer ptr = &ref;
00096 CHECK_TEST(ptr==&a[index-1],"Iterator points to the wrong element (going backwards)");
00097 }
00098 CHECK_TEST(index==0,"Iteration covered the wrong number of elements (going backward)");
00099 }
00100 START_TEST("Read-only iteration");
00101 {
00102 typedef typename Array::const_reference reference;
00103 typedef typename Array::const_pointer pointer;
00104 typedef typename Array::const_iterator iterator;
00105 const iterator begin = const_a.begin(), end = const_a.end();
00106 CHECK_TEST(end-begin==const_a.size(),"Distance != size");
00107 int index = 0;
00108 iterator iter = begin;
00109 for(; iter != end; ++iter, ++index) {
00110 reference ref = *iter;
00111 const pointer ptr = &ref;
00112 CHECK_TEST(ptr==&const_a[index],"Iterator points to the wrong element (going forward)");
00113 }
00114 CHECK_TEST(index==const_a.size(),"Iteration covered the wrong number of elements (going forward)");
00115 for(; iter != begin; --iter, --index) {
00116 reference ref = *(iter-1);
00117 const pointer ptr = &ref;
00118 CHECK_TEST(ptr==&const_a[index-1],"Iterator points to the wrong element (going backwards)");
00119 }
00120 CHECK_TEST(index==0,"Iteration covered the wrong number of elements (going backward)");
00121 }
00122
00123 START_TEST("Reverse iteration");
00124 {
00125 typedef typename Array::reference reference;
00126 typedef typename Array::pointer pointer;
00127 typedef typename Array::reverse_iterator iterator;
00128 const iterator begin = a.rbegin(), end = a.rend();
00129 CHECK_TEST(end-begin==a.size(),"Distance != size");
00130 int index = a.size();
00131 iterator iter = begin;
00132 for(; iter != end; ++iter, --index) {
00133 reference ref = *iter;
00134 const pointer ptr = &ref;
00135 CHECK_TEST(ptr==&a[index-1],"Iterator points to the wrong element (going forward)");
00136 }
00137 CHECK_TEST(index==0,"Iteration covered the wrong number of elements (going forward)");
00138 for(; iter != begin; --iter, ++index) {
00139 reference ref = *(iter-1);
00140 const pointer ptr = &ref;
00141 CHECK_TEST(ptr==&a[index],"Iterator points to the wrong element (going backwards)");
00142 }
00143 CHECK_TEST(index==a.size(),"Iteration covered the wrong number of elements (going backward)");
00144 }
00145
00146 START_TEST("Reverse read-only iteration");
00147 {
00148 typedef typename Array::const_reference reference;
00149 typedef typename Array::const_pointer pointer;
00150 typedef typename Array::const_reverse_iterator iterator;
00151 const iterator begin = const_a.rbegin(), end = const_a.rend();
00152 CHECK_TEST(end-begin==const_a.size(),"Distance != size");
00153 int index = a.size();
00154 iterator iter = begin;
00155 for(; iter != end; ++iter, --index) {
00156 reference ref = *iter;
00157 const pointer ptr = &ref;
00158 CHECK_TEST(ptr==&const_a[index-1],"Iterator points to the wrong element (going forward)");
00159 }
00160 CHECK_TEST(index==0,"Iteration covered the wrong number of elements (going forward)");
00161 for(; iter != begin; --iter, ++index) {
00162 reference ref = *(iter-1);
00163 const pointer ptr = &ref;
00164 CHECK_TEST(ptr==&const_a[index],"Iterator points to the wrong element (going backwards)");
00165 }
00166 CHECK_TEST(index==a.size(),"Iteration covered the wrong number of elements (going backward)");
00167 }
00168
00169 return true;
00170 failed:
00171 if (opt.log)
00172 olog << "FAILURE" << std::endl
00173 << ind(1) << "Test: " << test << std::endl
00174 << ind(1) << "Problem: " << problem << std::endl;
00175 return false;
00176 }
00177 };
00178
00180 class TestSpace : public Gecode::Space {
00181 public:
00182 TestSpace(void) : Space() {}
00183 TestSpace(TestSpace& s) : Space(s) {}
00184 virtual Space* copy(void) {
00185 return new TestSpace(*this);
00186 }
00187 };
00188
00190 class VarArrayIterator : public Iterator {
00191 protected:
00193 static const int n = 16;
00195 typedef Gecode::VarArray<Gecode::IntVar> Array;
00196 public:
00198 VarArrayIterator(void) : Iterator("VarArray") {}
00200 bool run(void) {
00201
00202 TestSpace s;
00203
00204 Array a(s,rand(n));
00205
00206 return runTestForArray(a);
00207 }
00208 } varArrayIteratorTest;
00209
00211 class VarArgsIterator : public Iterator {
00212 protected:
00214 static const int n = 16;
00216 typedef Gecode::ArgArrayBase<int> Array;
00217 public:
00219 VarArgsIterator(void) : Iterator("VarArgs") {}
00221 bool run(void) {
00222
00223 TestSpace s;
00224
00225 Array a(rand(n));
00226
00227 return runTestForArray(a);
00228 }
00229 } varArgsIteratorTest;
00230
00232 class ViewArrayIterator : public Iterator {
00233 protected:
00235 static const int n = 16;
00237 typedef Gecode::ViewArray<Gecode::IntVar> Array;
00238 public:
00240 ViewArrayIterator(void) : Iterator("ViewArray") {}
00242 bool run(void) {
00243
00244 TestSpace s;
00245
00246 Array a(s,rand(n));
00247
00248 return runTestForArray(a);
00249 }
00250 } viewArrayIteratorTest;
00251
00253 class SharedArrayIterator : public Iterator {
00254 protected:
00256 static const int n = 16;
00258 typedef Gecode::SharedArray<int> Array;
00259 public:
00261 SharedArrayIterator(void) : Iterator("SharedArray") {}
00263 bool run(void) {
00264
00265 Array a(rand(n));
00266
00267 return runTestForArray(a);
00268 }
00269 } sharedArrayIteratorTest;
00270
00271 }}
00272
00273