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