array.cpp
Go to the documentation of this file.
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Gregory Crosswhite <gcross@phys.washington.edu>
*
* Copyright:
* Gregory Crosswhite, 2011
*
* This file is part of Gecode, the generic constraint
* development environment:
* http://www.gecode.org
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include <gecode/kernel.hh>
#include <gecode/int.hh>
#include "test/test.hh"
#define CHECK_TEST(T,M) \
if (opt.log) \
olog << ind(3) << "Check: " << (M) << std::endl; \
if (!(T)) { \
problem = (M); goto failed; \
}
#define START_TEST(T) \
if (opt.log) { \
olog.str(""); \
olog << ind(2) << "Testing: " << (T) << std::endl; \
} \
test = (T);
namespace Test {
namespace Array {
static const std::string prefix("Array::Iterator::");
class Iterator : public Test::Base {
protected:
static const int n = 16;
Iterator(const std::string& name) : Test::Base(prefix + name) {}
template<class Array> bool runTestForArray(Array& a) {
// Test/problem information.
const char* test = "NONE";
const char* problem = "NONE";
// Constant reference to the array
const Array& const_a = a;
START_TEST("Iteration");
{
typedef typename Array::reference reference;
typedef typename Array::pointer pointer;
typedef typename Array::iterator iterator;
const iterator begin = a.begin(), end = a.end();
CHECK_TEST(end-begin==a.size(),"Distance != size");
int index = 0;
iterator iter = begin;
for(; iter != end; ++iter, ++index) {
reference ref = *iter;
const pointer ptr = &ref;
CHECK_TEST(ptr==&a[index],"Iterator points to the wrong element (going forward)");
}
CHECK_TEST(index==a.size(),"Iteration covered the wrong number of elements (going forward)");
for(; iter != begin; --iter, --index) {
reference ref = *(iter-1);
const pointer ptr = &ref;
CHECK_TEST(ptr==&a[index-1],"Iterator points to the wrong element (going backwards)");
}
CHECK_TEST(index==0,"Iteration covered the wrong number of elements (going backward)");
}
START_TEST("Read-only iteration");
{
typedef typename Array::const_reference reference;
typedef typename Array::const_pointer pointer;
typedef typename Array::const_iterator iterator;
const iterator begin = const_a.begin(), end = const_a.end();
CHECK_TEST(end-begin==const_a.size(),"Distance != size");
int index = 0;
iterator iter = begin;
for(; iter != end; ++iter, ++index) {
reference ref = *iter;
const pointer ptr = &ref;
CHECK_TEST(ptr==&const_a[index],"Iterator points to the wrong element (going forward)");
}
CHECK_TEST(index==const_a.size(),"Iteration covered the wrong number of elements (going forward)");
for(; iter != begin; --iter, --index) {
reference ref = *(iter-1);
const pointer ptr = &ref;
CHECK_TEST(ptr==&const_a[index-1],"Iterator points to the wrong element (going backwards)");
}
CHECK_TEST(index==0,"Iteration covered the wrong number of elements (going backward)");
}
START_TEST("Reverse iteration");
{
typedef typename Array::reference reference;
typedef typename Array::pointer pointer;
typedef typename Array::reverse_iterator iterator;
const iterator begin = a.rbegin(), end = a.rend();
CHECK_TEST(end-begin==a.size(),"Distance != size");
int index = a.size();
iterator iter = begin;
for(; iter != end; ++iter, --index) {
reference ref = *iter;
const pointer ptr = &ref;
CHECK_TEST(ptr==&a[index-1],"Iterator points to the wrong element (going forward)");
}
CHECK_TEST(index==0,"Iteration covered the wrong number of elements (going forward)");
for(; iter != begin; --iter, ++index) {
reference ref = *(iter-1);
const pointer ptr = &ref;
CHECK_TEST(ptr==&a[index],"Iterator points to the wrong element (going backwards)");
}
CHECK_TEST(index==a.size(),"Iteration covered the wrong number of elements (going backward)");
}
START_TEST("Reverse read-only iteration");
{
typedef typename Array::const_reference reference;
typedef typename Array::const_pointer pointer;
typedef typename Array::const_reverse_iterator iterator;
const iterator begin = const_a.rbegin(), end = const_a.rend();
CHECK_TEST(end-begin==const_a.size(),"Distance != size");
int index = a.size();
iterator iter = begin;
for(; iter != end; ++iter, --index) {
reference ref = *iter;
const pointer ptr = &ref;
CHECK_TEST(ptr==&const_a[index-1],"Iterator points to the wrong element (going forward)");
}
CHECK_TEST(index==0,"Iteration covered the wrong number of elements (going forward)");
for(; iter != begin; --iter, ++index) {
reference ref = *(iter-1);
const pointer ptr = &ref;
CHECK_TEST(ptr==&const_a[index],"Iterator points to the wrong element (going backwards)");
}
CHECK_TEST(index==a.size(),"Iteration covered the wrong number of elements (going backward)");
}
return true;
failed:
if (opt.log)
olog << "FAILURE" << std::endl
<< ind(1) << "Test: " << test << std::endl
<< ind(1) << "Problem: " << problem << std::endl;
return false;
}
};
class TestSpace : public Gecode::Space {
public:
TestSpace(void) : Space() {}
TestSpace(TestSpace& s) : Space(s) {}
virtual Space* copy(void) {
return new TestSpace(*this);
}
};
class VarArrayIterator : public Iterator {
protected:
static const int n = 16;
typedef Gecode::VarArray<Gecode::IntVar> Array;
public:
VarArrayIterator(void) : Iterator("VarArray") {}
bool run(void) {
// Space for the test
TestSpace s;
// VarArray for the test
Array a(s,rand(n));
// Run the iterator test
return runTestForArray(a);
}
} varArrayIteratorTest;
class VarArgsIterator : public Iterator {
protected:
static const int n = 16;
typedef Gecode::ArgArrayBase<int> Array;
public:
VarArgsIterator(void) : Iterator("VarArgs") {}
bool run(void) {
// Space for the test
TestSpace s;
// VarArray for the test
Array a(rand(n));
// Run the iterator test
return runTestForArray(a);
}
} varArgsIteratorTest;
class ViewArrayIterator : public Iterator {
protected:
static const int n = 16;
typedef Gecode::ViewArray<Gecode::IntVar> Array;
public:
ViewArrayIterator(void) : Iterator("ViewArray") {}
bool run(void) {
// Space for the test
TestSpace s;
// VarArray for the test
Array a(s,rand(n));
// Run the iterator test
return runTestForArray(a);
}
} viewArrayIteratorTest;
class SharedArrayIterator : public Iterator {
protected:
static const int n = 16;
typedef Gecode::SharedArray<int> Array;
public:
SharedArrayIterator(void) : Iterator("SharedArray") {}
bool run(void) {
// SharedArray for the test
Array a(rand(n));
// Run the iterator test
return runTestForArray(a);
}
} sharedArrayIteratorTest;
}}
// STATISTICS: test-core
Gecode::ArgArrayBase
Base-class for argument arrays.
Definition array.hpp:537
Gecode::FloatVal::size
FloatNum size(void) const
Return size of float value (distance between maximum and minimum).
Definition val.hpp:78
Gecode::SharedArray
Shared array with arbitrary number of elements.
Definition shared-array.hpp:53
Gecode::Space
Computation spaces.
Definition core.hpp:1742
Gecode::VarArray
Variable arrays
Definition array.hpp:112
Gecode::ViewArray
View arrays.
Definition array.hpp:253
Test::Array::Iterator::runTestForArray
bool runTestForArray(Array &a)
Perform actual tests.
Definition array.cpp:71
Test::Array::Iterator::Iterator
Iterator(const std::string &name)
Initialize test.
Definition array.cpp:69
Test::Array::Iterator::n
static const int n
Maximum array size.
Definition array.cpp:67
Test::Array::SharedArrayIterator::SharedArrayIterator
SharedArrayIterator(void)
Initialize test.
Definition array.cpp:261
Test::Array::SharedArrayIterator::Array
Gecode::SharedArray< int > Array
Array type being tested.
Definition array.cpp:258
Test::Array::SharedArrayIterator::n
static const int n
Maximum array size.
Definition array.cpp:256
Test::Array::SharedArrayIterator::run
bool run(void)
Perform actual tests.
Definition array.cpp:263
Test::Array::TestSpace
Test space.
Definition array.cpp:180
Test::Array::TestSpace::TestSpace
TestSpace(void)
Definition array.cpp:182
Test::Array::TestSpace::TestSpace
TestSpace(TestSpace &s)
Definition array.cpp:183
Test::Array::TestSpace::copy
virtual Space * copy(void)
Copying member function.
Definition array.cpp:184
Test::Array::VarArgsIterator::run
bool run(void)
Perform actual tests.
Definition array.cpp:221
Test::Array::VarArgsIterator::VarArgsIterator
VarArgsIterator(void)
Initialize test.
Definition array.cpp:219
Test::Array::VarArgsIterator::Array
Gecode::ArgArrayBase< int > Array
Array type being tested.
Definition array.cpp:216
Test::Array::VarArgsIterator::n
static const int n
Maximum array size.
Definition array.cpp:214
Test::Array::VarArrayIterator::Array
Gecode::VarArray< Gecode::IntVar > Array
Array type being tested.
Definition array.cpp:195
Test::Array::VarArrayIterator::run
bool run(void)
Perform actual tests.
Definition array.cpp:200
Test::Array::VarArrayIterator::VarArrayIterator
VarArrayIterator(void)
Initialize test.
Definition array.cpp:198
Test::Array::VarArrayIterator::n
static const int n
Maximum array size.
Definition array.cpp:193
Test::Array::ViewArrayIterator::Array
Gecode::ViewArray< Gecode::IntVar > Array
Array type being tested.
Definition array.cpp:237
Test::Array::ViewArrayIterator::ViewArrayIterator
ViewArrayIterator(void)
Initialize test.
Definition array.cpp:240
Test::Array::ViewArrayIterator::run
bool run(void)
Perform actual tests.
Definition array.cpp:242
Test::Array::ViewArrayIterator::n
static const int n
Maximum array size.
Definition array.cpp:235
Test::Base
Base class for all tests to be run
Definition test.hh:103
Test::Base::rand
static Gecode::Support::RandomGenerator rand
Random number generator.
Definition test.hh:134
Test::Base::Base
Base(const std::string &s)
Create and register test with name s.
Definition test.cpp:59
Test::Base::name
const std::string & name(void) const
Return name of test.
Definition test.hpp:50
Test::ind
Simple class for describing identation.
Definition test.hh:66
int.hh
Gecode::Space::Space
Space(void)
Default constructor.
Definition core.cpp:115
kernel.hh
Test::Array
Tests for arrays.
Definition array.cpp:58
Test::Array::viewArrayIteratorTest
Test::Array::ViewArrayIterator viewArrayIteratorTest
Test::Array::varArrayIteratorTest
Test::Array::VarArrayIterator varArrayIteratorTest
Test::Array::varArgsIteratorTest
Test::Array::VarArgsIterator varArgsIteratorTest
Test::Array::sharedArrayIteratorTest
Test::Array::SharedArrayIterator sharedArrayIteratorTest
Test
General test support.
Definition afc.cpp:39
Test::olog
std::ostringstream olog
Stream used for logging.
Definition test.cpp:53
Test::opt
Options opt
The options.
Definition test.cpp:97
START_TEST
#define START_TEST(T)
Start new test.
Definition array.cpp:48
CHECK_TEST
#define CHECK_TEST(T, M)
Check the test result and handle failed test.
Definition array.cpp:40
test.hh