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