Generated on Thu Mar 22 10:39:33 2012 for Gecode by doxygen 1.6.3

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-05-25 16:56:41 +0200 (Wed, 25 May 2011) $ by $Author: schulte $
00011  *     $Revision: 12022 $
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 { 
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       // Test/problem information.
00077       const char* test    = "NONE";
00078       const char* problem = "NONE";
00079       // Constant reference to the array
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       // Space for the test
00206       TestSpace s;
00207       // VarArray for the test
00208       Array a(s,rand(n));
00209       // Run the iterator test
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       // Space for the test
00227       TestSpace s;
00228       // VarArray for the test
00229       Array a(rand(n));
00230       // Run the iterator test
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       // Space for the test
00248       TestSpace s;
00249       // VarArray for the test
00250       Array a(s,rand(n));
00251       // Run the iterator test
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       // SharedArray for the test
00269       Array a(rand(n));
00270       // Run the iterator test
00271       return runTestForArray(a);
00272     }
00273   } sharedArrayIteratorTest;
00274 
00275 }}
00276 
00277 // STATISTICS: test-kernel