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) \ 41do { \ 42if (opt.log) \ 43 olog << ind(3) << "Check: " << (M) << std::endl; \ 44if (!(T)) { \ 45 problem = (M); goto failed; \ 46} \ 47} while (false) 48 49/// Start new test 50#define START_TEST(T) \ 51do { \ 52 if (opt.log) { \ 53 olog.str(""); \ 54 olog << ind(2) << "Testing: " << (T) << std::endl; \ 55 } \ 56 test = (T); \ 57} while (false) 58 59namespace Test { 60 61 /// Tests for arrays 62 namespace Array { 63 64 /// Test name prefix 65 static const std::string prefix("Array::Iterator::"); 66 67 /// %Base class for testing iterators 68 class Iterator : public Test::Base { 69 protected: 70 /// Maximum array size 71 static const int n = 16; 72 /// Initialize test 73 Iterator(const std::string& name) : Test::Base(prefix + name) {} 74 /// Perform actual tests 75 template<class Array> bool runTestForArray(Array& a) { 76 // Test/problem information. 77 const char* test = "NONE"; 78 const char* problem = "NONE"; 79 // Constant reference to the array 80 const Array& const_a = a; 81 82 START_TEST("Iteration"); 83 { 84 typedef typename Array::reference reference; 85 typedef typename Array::pointer pointer; 86 typedef typename Array::iterator iterator; 87 const iterator begin = a.begin(), end = a.end(); 88 CHECK_TEST(end-begin==a.size(),"Distance != size"); 89 int index = 0; 90 iterator iter = begin; 91 for(; iter != end; ++iter, ++index) { 92 reference ref = *iter; 93 const pointer ptr = &ref; 94 CHECK_TEST(ptr==&a[index],"Iterator points to the wrong element (going forward)"); 95 } 96 CHECK_TEST(index==a.size(),"Iteration covered the wrong number of elements (going forward)"); 97 for(; iter != begin; --iter, --index) { 98 reference ref = *(iter-1); 99 const pointer ptr = &ref; 100 CHECK_TEST(ptr==&a[index-1],"Iterator points to the wrong element (going backwards)"); 101 } 102 CHECK_TEST(index==0,"Iteration covered the wrong number of elements (going backward)"); 103 } 104 START_TEST("Read-only iteration"); 105 { 106 typedef typename Array::const_reference reference; 107 typedef typename Array::const_pointer pointer; 108 typedef typename Array::const_iterator iterator; 109 const iterator begin = const_a.begin(), end = const_a.end(); 110 CHECK_TEST(end-begin==const_a.size(),"Distance != size"); 111 int index = 0; 112 iterator iter = begin; 113 for(; iter != end; ++iter, ++index) { 114 reference ref = *iter; 115 const pointer ptr = &ref; 116 CHECK_TEST(ptr==&const_a[index],"Iterator points to the wrong element (going forward)"); 117 } 118 CHECK_TEST(index==const_a.size(),"Iteration covered the wrong number of elements (going forward)"); 119 for(; iter != begin; --iter, --index) { 120 reference ref = *(iter-1); 121 const pointer ptr = &ref; 122 CHECK_TEST(ptr==&const_a[index-1],"Iterator points to the wrong element (going backwards)"); 123 } 124 CHECK_TEST(index==0,"Iteration covered the wrong number of elements (going backward)"); 125 } 126 127 START_TEST("Reverse iteration"); 128 { 129 typedef typename Array::reference reference; 130 typedef typename Array::pointer pointer; 131 typedef typename Array::reverse_iterator iterator; 132 const iterator begin = a.rbegin(), end = a.rend(); 133 CHECK_TEST(end-begin==a.size(),"Distance != size"); 134 int index = a.size(); 135 iterator iter = begin; 136 for(; iter != end; ++iter, --index) { 137 reference ref = *iter; 138 const pointer ptr = &ref; 139 CHECK_TEST(ptr==&a[index-1],"Iterator points to the wrong element (going forward)"); 140 } 141 CHECK_TEST(index==0,"Iteration covered the wrong number of elements (going forward)"); 142 for(; iter != begin; --iter, ++index) { 143 reference ref = *(iter-1); 144 const pointer ptr = &ref; 145 CHECK_TEST(ptr==&a[index],"Iterator points to the wrong element (going backwards)"); 146 } 147 CHECK_TEST(index==a.size(),"Iteration covered the wrong number of elements (going backward)"); 148 } 149 150 START_TEST("Reverse read-only iteration"); 151 { 152 typedef typename Array::const_reference reference; 153 typedef typename Array::const_pointer pointer; 154 typedef typename Array::const_reverse_iterator iterator; 155 const iterator begin = const_a.rbegin(), end = const_a.rend(); 156 CHECK_TEST(end-begin==const_a.size(),"Distance != size"); 157 int index = a.size(); 158 iterator iter = begin; 159 for(; iter != end; ++iter, --index) { 160 reference ref = *iter; 161 const pointer ptr = &ref; 162 CHECK_TEST(ptr==&const_a[index-1],"Iterator points to the wrong element (going forward)"); 163 } 164 CHECK_TEST(index==0,"Iteration covered the wrong number of elements (going forward)"); 165 for(; iter != begin; --iter, ++index) { 166 reference ref = *(iter-1); 167 const pointer ptr = &ref; 168 CHECK_TEST(ptr==&const_a[index],"Iterator points to the wrong element (going backwards)"); 169 } 170 CHECK_TEST(index==a.size(),"Iteration covered the wrong number of elements (going backward)"); 171 } 172 173 return true; 174 failed: 175 if (opt.log) 176 olog << "FAILURE" << std::endl 177 << ind(1) << "Test: " << test << std::endl 178 << ind(1) << "Problem: " << problem << std::endl; 179 return false; 180 } 181 }; 182 183 /// Test space 184 class TestSpace : public Gecode::Space { 185 public: 186 TestSpace(void) : Space() {} 187 TestSpace(TestSpace& s) : Space(s) {} 188 virtual Space* copy(void) { 189 return new TestSpace(*this); 190 } 191 }; 192 193 /// %Class for testing the VarArray iterator 194 class VarArrayIterator : public Iterator { 195 protected: 196 /// Maximum array size 197 static const int n = 16; 198 /// Array type being tested 199 typedef Gecode::VarArray<Gecode::IntVar> Array; 200 public: 201 /// Initialize test 202 VarArrayIterator(void) : Iterator("VarArray") {} 203 /// Perform actual tests 204 bool run(void) { 205 // Space for the test 206 TestSpace s; 207 // VarArray for the test 208 Array a(s,_rand(n)); 209 // Run the iterator test 210 return runTestForArray(a); 211 } 212 } varArrayIteratorTest; 213 214 /// %Class for testing the VarArgs iterator 215 class VarArgsIterator : public Iterator { 216 protected: 217 /// Maximum array size 218 static const int n = 16; 219 /// Array type being tested 220 typedef Gecode::ArgArrayBase<int> Array; 221 public: 222 /// Initialize test 223 VarArgsIterator(void) : Iterator("VarArgs") {} 224 /// Perform actual tests 225 bool run(void) { 226 // Space for the test 227 TestSpace s; 228 // VarArray for the test 229 Array a(_rand(n)); 230 // Run the iterator test 231 return runTestForArray(a); 232 } 233 } varArgsIteratorTest; 234 235 /// %Class for testing the ViewArray iterator 236 class ViewArrayIterator : public Iterator { 237 protected: 238 /// Maximum array size 239 static const int n = 16; 240 /// Array type being tested 241 typedef Gecode::ViewArray<Gecode::IntVar> Array; 242 public: 243 /// Initialize test 244 ViewArrayIterator(void) : Iterator("ViewArray") {} 245 /// Perform actual tests 246 bool run(void) { 247 // Space for the test 248 TestSpace s; 249 // VarArray for the test 250 Array a(s,_rand(n)); 251 // Run the iterator test 252 return runTestForArray(a); 253 } 254 } viewArrayIteratorTest; 255 256 /// %Class for testing the SharedArray iterator 257 class SharedArrayIterator : public Iterator { 258 protected: 259 /// Maximum array size 260 static const int n = 16; 261 /// Array type being tested 262 typedef Gecode::SharedArray<int> Array; 263 public: 264 /// Initialize test 265 SharedArrayIterator(void) : Iterator("SharedArray") {} 266 /// Perform actual tests 267 bool run(void) { 268 // SharedArray for the test 269 Array a(_rand(n)); 270 // Run the iterator test 271 return runTestForArray(a); 272 } 273 } sharedArrayIteratorTest; 274 275}} 276 277// STATISTICS: test-core