afc.cpp
Go to the documentation of this file.
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Christian Schulte <schulte@gecode.org>
*
* Copyright:
* Christian Schulte, 2009
*
* 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"
namespace Test {
class AFC : public Test::Base {
protected:
class TestSpace : public Gecode::Space {
protected:
Gecode::IntVar x, y;
public:
TestSpace(void) : x(*this,0,10), y(*this,0,10) {}
TestSpace(TestSpace& s) : Space(s) {
x.update(*this,s.x);
y.update(*this,s.y);
}
void post(void) {
Gecode::rel(*this, x, Gecode::IRT_LE, y);
}
virtual Space* copy(void) {
return new TestSpace(*this);
}
};
static const int n_ops = 8 * 1024;
static const int n = 16;
int space(TestSpace* s[]) {
int i = rand(n);
while (s[i] == NULL)
i = (i+1) % n;
return i;
}
int index(void) {
return rand(n);
}
public:
AFC(void) : Test::Base("AFC") {}
bool run(void) {
// Array of spaces for tests
TestSpace* s[n];
// How many spaces exist in s
int n_s = 1;
for (int i=n; i--; )
s[i] = NULL;
s[0] = new TestSpace;
for (int o=n_ops; o--; )
switch (rand(3)) {
case 0:
// clone space
{
int i = index();
if ((s[i] != NULL)) {
if (n_s > 1) {
delete s[i]; s[i]=NULL; n_s--;
} else {
break;
}
}
int j = space(s);
(void) s[j]->status();
s[i] = static_cast<TestSpace*>(s[j]->clone());
n_s++;
}
break;
case 1:
// delete space
if (n_s > 1) {
int i = space(s);
delete s[i]; s[i]=NULL; n_s--;
}
break;
case 2:
// post propagator
s[space(s)]->post();
break;
default:
GECODE_NEVER;
}
// Delete all remaining spaces
for (int i=n; i--; )
delete s[i];
return true;
}
};
AFC afc;
}
// STATISTICS: test-core
Gecode::IntVar
Integer variables.
Definition int.hh:371
Gecode::Space
Computation spaces.
Definition core.hpp:1742
Test::AFC::TestSpace
Test space.
Definition afc.cpp:45
Test::AFC::TestSpace::y
Gecode::IntVar y
Definition afc.cpp:48
Test::AFC::TestSpace::post
void post(void)
Post arbitrary propagator.
Definition afc.cpp:58
Test::AFC::TestSpace::TestSpace
TestSpace(TestSpace &s)
Constructor for cloning s.
Definition afc.cpp:53
Test::AFC::TestSpace::TestSpace
TestSpace(void)
Constructor for creation.
Definition afc.cpp:51
Test::AFC::TestSpace::copy
virtual Space * copy(void)
Copy during cloning.
Definition afc.cpp:62
Test::AFC::TestSpace::x
Gecode::IntVar x
Two integer variables.
Definition afc.cpp:48
Test::AFC
Test for AFC infrastructure
Definition afc.cpp:42
Test::AFC::AFC
AFC(void)
Initialize test.
Definition afc.cpp:83
Test::AFC::n
static const int n
How many spaces to maintain.
Definition afc.cpp:69
Test::AFC::run
bool run(void)
Perform actual tests.
Definition afc.cpp:85
Test::AFC::n_ops
static const int n_ops
How many test operations to be performed.
Definition afc.cpp:67
Test::AFC::index
int index(void)
Return random index.
Definition afc.cpp:78
Test::AFC::space
int space(TestSpace *s[])
Return random index of non-null space.
Definition afc.cpp:71
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
int.hh
Gecode::rel
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVar x1)
Post propagator for .
Definition rel.cpp:68
Gecode::IRT_LE
@ IRT_LE
Less ( ).
Definition int.hh:929
Gecode::Space::Space
Space(void)
Default constructor.
Definition core.cpp:115
Gecode::Space::clone
Space * clone(CloneStatistics &stat=unused_clone) const
Clone space.
Definition core.hpp:3224
kernel.hh
Test
General test support.
Definition afc.cpp:39
Test::afc
AFC afc
Definition afc.cpp:135
GECODE_NEVER
#define GECODE_NEVER
Assert that this command is never executed.
Definition macros.hpp:56
test.hh