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
00035
00036
00037
00038 #include <gecode/kernel.hh>
00039 #include <gecode/int.hh>
00040
00041 #include "test/test.hh"
00042
00044 #define CHECK_TEST(T,M) \
00045 if (opt.log) \
00046 olog << ind(3) << "Check: " << (M) << std::endl; \
00047 if (!(T)) { \
00048 problem = (M); goto failed; \
00049 }
00050
00052 #define START_TEST(T) \
00053 if (opt.log) { \
00054 olog.str(""); \
00055 olog << ind(2) << "Testing: " << (T) << std::endl; \
00056 } \
00057 test = (T);
00058
00059 namespace Test { namespace Array { namespace Iterator {
00060
00062 static const std::string prefix("Array::Iterator::");
00063
00065 class Iterator : public Test::Base {
00066 protected:
00068 static const int n = 16;
00070 Iterator(const std::string& name) : Test::Base(prefix + name) {}
00072 template<class Array> bool runTestForArray(Array& a) {
00073
00074 const char* test = "NONE";
00075 const char* problem = "NONE";
00076
00077 const Array& const_a = a;
00078
00079 START_TEST("Iteration");
00080 {
00081 typedef typename Array::reference reference;
00082 typedef typename Array::pointer pointer;
00083 typedef typename Array::iterator iterator;
00084 const iterator begin = a.begin(), end = a.end();
00085 CHECK_TEST(end-begin==a.size(),"Distance != size");
00086 int index = 0;
00087 iterator iter = begin;
00088 for(; iter != end; ++iter, ++index) {
00089 const reference ref = *iter;
00090 const pointer ptr = &ref;
00091 CHECK_TEST(ptr==&a[index],"Iterator points to the wrong element (going forward)");
00092 }
00093 CHECK_TEST(index==a.size(),"Iteration covered the wrong number of elements (going forward)");
00094 for(; iter != begin; --iter, --index) {
00095 const reference ref = *(iter-1);
00096 const pointer ptr = &ref;
00097 CHECK_TEST(ptr==&a[index-1],"Iterator points to the wrong element (going backwards)");
00098 }
00099 CHECK_TEST(index==0,"Iteration covered the wrong number of elements (going backward)");
00100 }
00101 START_TEST("Read-only iteration");
00102 {
00103 typedef typename Array::const_reference reference;
00104 typedef typename Array::const_pointer pointer;
00105 typedef typename Array::const_iterator iterator;
00106 const iterator begin = const_a.begin(), end = const_a.end();
00107 CHECK_TEST(end-begin==const_a.size(),"Distance != size");
00108 int index = 0;
00109 iterator iter = begin;
00110 for(; iter != end; ++iter, ++index) {
00111 const reference ref = *iter;
00112 const pointer ptr = &ref;
00113 CHECK_TEST(ptr==&const_a[index],"Iterator points to the wrong element (going forward)");
00114 }
00115 CHECK_TEST(index==const_a.size(),"Iteration covered the wrong number of elements (going forward)");
00116 for(; iter != begin; --iter, --index) {
00117 const reference ref = *(iter-1);
00118 const pointer ptr = &ref;
00119 CHECK_TEST(ptr==&const_a[index-1],"Iterator points to the wrong element (going backwards)");
00120 }
00121 CHECK_TEST(index==0,"Iteration covered the wrong number of elements (going backward)");
00122 }
00123
00124 START_TEST("Reverse iteration");
00125 {
00126 typedef typename Array::reference reference;
00127 typedef typename Array::pointer pointer;
00128 typedef typename Array::reverse_iterator iterator;
00129 const iterator begin = a.rbegin(), end = a.rend();
00130 CHECK_TEST(end-begin==a.size(),"Distance != size");
00131 int index = a.size();
00132 iterator iter = begin;
00133 for(; iter != end; ++iter, --index) {
00134 const reference ref = *iter;
00135 const pointer ptr = &ref;
00136 CHECK_TEST(ptr==&a[index-1],"Iterator points to the wrong element (going forward)");
00137 }
00138 CHECK_TEST(index==0,"Iteration covered the wrong number of elements (going forward)");
00139 for(; iter != begin; --iter, ++index) {
00140 const reference ref = *(iter-1);
00141 const pointer ptr = &ref;
00142 CHECK_TEST(ptr==&a[index],"Iterator points to the wrong element (going backwards)");
00143 }
00144 CHECK_TEST(index==a.size(),"Iteration covered the wrong number of elements (going backward)");
00145 }
00146
00147 START_TEST("Reverse read-only iteration");
00148 {
00149 typedef typename Array::const_reference reference;
00150 typedef typename Array::const_pointer pointer;
00151 typedef typename Array::const_reverse_iterator iterator;
00152 const iterator begin = const_a.rbegin(), end = const_a.rend();
00153 CHECK_TEST(end-begin==const_a.size(),"Distance != size");
00154 int index = a.size();
00155 iterator iter = begin;
00156 for(; iter != end; ++iter, --index) {
00157 const reference ref = *iter;
00158 const pointer ptr = &ref;
00159 CHECK_TEST(ptr==&const_a[index-1],"Iterator points to the wrong element (going forward)");
00160 }
00161 CHECK_TEST(index==0,"Iteration covered the wrong number of elements (going forward)");
00162 for(; iter != begin; --iter, ++index) {
00163 const reference ref = *(iter-1);
00164 const pointer ptr = &ref;
00165 CHECK_TEST(ptr==&const_a[index],"Iterator points to the wrong element (going backwards)");
00166 }
00167 CHECK_TEST(index==a.size(),"Iteration covered the wrong number of elements (going backward)");
00168 }
00169
00170 return true;
00171 failed:
00172 if (opt.log)
00173 olog << "FAILURE" << std::endl
00174 << ind(1) << "Test: " << test << std::endl
00175 << ind(1) << "Problem: " << problem << std::endl;
00176 return false;
00177 }
00178 };
00179
00181 class TestSpace : public Gecode::Space {
00182 public:
00183 TestSpace(void) : Space() {}
00184 TestSpace(bool share, TestSpace& s) : Space(share,s) {}
00185 virtual Space* copy(bool share) {
00186 return new TestSpace(share,*this);
00187 }
00188 };
00189
00191 class VarArrayIterator : public Iterator {
00192 protected:
00194 static const int n = 16;
00196 typedef Gecode::VarArray<Gecode::IntVar> Array;
00197 public:
00199 VarArrayIterator(void) : Iterator("VarArray") {}
00201 bool run(void) {
00202
00203 TestSpace s;
00204
00205 Array a(s,rand(n));
00206
00207 return runTestForArray(a);
00208 }
00209 } varArrayIteratorTest;
00210
00212 class VarArgsIterator : public Iterator {
00213 protected:
00215 static const int n = 16;
00217 typedef Gecode::ArgArrayBase<int> Array;
00218 public:
00220 VarArgsIterator(void) : Iterator("VarArgs") {}
00222 bool run(void) {
00223
00224 TestSpace s;
00225
00226 Array a(rand(n));
00227
00228 return runTestForArray(a);
00229 }
00230 } varArgsIteratorTest;
00231
00233 class ViewArrayIterator : public Iterator {
00234 protected:
00236 static const int n = 16;
00238 typedef Gecode::ViewArray<Gecode::IntVar> Array;
00239 public:
00241 ViewArrayIterator(void) : Iterator("ViewArray") {}
00243 bool run(void) {
00244
00245 TestSpace s;
00246
00247 Array a(s,rand(n));
00248
00249 return runTestForArray(a);
00250 }
00251 } viewArrayIteratorTest;
00252
00254 class SharedArrayIterator : public Iterator {
00255 protected:
00257 static const int n = 16;
00259 typedef Gecode::SharedArray<int> Array;
00260 public:
00262 SharedArrayIterator(void) : Iterator("SharedArray") {}
00264 bool run(void) {
00265
00266 Array a(rand(n));
00267
00268 return runTestForArray(a);
00269 }
00270 } sharedArrayIteratorTest;
00271
00272 } } }
00273
00274