array.cpp
Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 00002 /* 00003 * Main authors: 00004 * Gregory Crosswhite <gcross@phys.washington.edu> 00005 * 00006 * Copyright: 00007 * Gregory Crosswhite, 2011 00008 * 00009 * Last modified: 00010 * $Date: 2011-01-27 13:03:05 +0100 (Thu, 27 Jan 2011) $ by $Author: schulte $ 00011 * $Revision: 11570 $ 00012 * 00013 * This file is part of Gecode, the generic constraint 00014 * development environment: 00015 * http://www.gecode.org 00016 * 00017 * Permission is hereby granted, free of charge, to any person obtaining 00018 * a copy of this software and associated documentation files (the 00019 * "Software"), to deal in the Software without restriction, including 00020 * without limitation the rights to use, copy, modify, merge, publish, 00021 * distribute, sublicense, and/or sell copies of the Software, and to 00022 * permit persons to whom the Software is furnished to do so, subject to 00023 * the following conditions: 00024 * 00025 * The above copyright notice and this permission notice shall be 00026 * included in all copies or substantial portions of the Software. 00027 * 00028 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00029 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00030 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00031 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 00032 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 00033 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 00034 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 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 // Test/problem information. 00074 const char* test = "NONE"; 00075 const char* problem = "NONE"; 00076 // Constant reference to the array 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 // Space for the test 00203 TestSpace s; 00204 // VarArray for the test 00205 Array a(s,rand(n)); 00206 // Run the iterator test 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 // Space for the test 00224 TestSpace s; 00225 // VarArray for the test 00226 Array a(rand(n)); 00227 // Run the iterator test 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 // Space for the test 00245 TestSpace s; 00246 // VarArray for the test 00247 Array a(s,rand(n)); 00248 // Run the iterator test 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 // SharedArray for the test 00266 Array a(rand(n)); 00267 // Run the iterator test 00268 return runTestForArray(a); 00269 } 00270 } sharedArrayIteratorTest; 00271 00272 } } } 00273 00274 // STATISTICS: test-kernel