Generated on Thu Jan 16 2025 00:00:00 for Gecode by doxygen 1.14.0
afc.cpp
Go to the documentation of this file.
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main authors:
4 * Christian Schulte <schulte@gecode.org>
5 *
6 * Copyright:
7 * Christian Schulte, 2009
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
39namespace Test {
40
42 class AFC : public Test::Base {
43 protected:
45 class TestSpace : public Gecode::Space {
46 protected:
49 public:
51 TestSpace(void) : x(*this,0,10), y(*this,0,10) {}
54 x.update(*this,s.x);
55 y.update(*this,s.y);
56 }
57
58 void post(void) {
59 Gecode::rel(*this, x, Gecode::IRT_LE, y);
60 }
61
62 virtual Space* copy(void) {
63 return new TestSpace(*this);
64 }
65 };
66
67 static const int n_ops = 8 * 1024;
69 static const int n = 16;
71 int space(TestSpace* s[]) {
72 int i = rand(n);
73 while (s[i] == NULL)
74 i = (i+1) % n;
75 return i;
76 }
77
78 int index(void) {
79 return rand(n);
80 }
81 public:
83 AFC(void) : Test::Base("AFC") {}
85 bool run(void) {
86 // Array of spaces for tests
87 TestSpace* s[n];
88 // How many spaces exist in s
89 int n_s = 1;
90
91 for (int i=n; i--; )
92 s[i] = NULL;
93 s[0] = new TestSpace;
94
95 for (int o=n_ops; o--; )
96 switch (rand(3)) {
97 case 0:
98 // clone space
99 {
100 int i = index();
101 if ((s[i] != NULL)) {
102 if (n_s > 1) {
103 delete s[i]; s[i]=NULL; n_s--;
104 } else {
105 break;
106 }
107 }
108 int j = space(s);
109 (void) s[j]->status();
110 s[i] = static_cast<TestSpace*>(s[j]->clone());
111 n_s++;
112 }
113 break;
114 case 1:
115 // delete space
116 if (n_s > 1) {
117 int i = space(s);
118 delete s[i]; s[i]=NULL; n_s--;
119 }
120 break;
121 case 2:
122 // post propagator
123 s[space(s)]->post();
124 break;
125 default:
127 }
128 // Delete all remaining spaces
129 for (int i=n; i--; )
130 delete s[i];
131 return true;
132 }
133 };
134
136
137}
138
139// STATISTICS: test-core
Integer variables.
Definition int.hh:371
Computation spaces.
Definition core.hpp:1744
Test space.
Definition afc.cpp:45
Gecode::IntVar y
Definition afc.cpp:48
void post(void)
Post arbitrary propagator.
Definition afc.cpp:58
TestSpace(TestSpace &s)
Constructor for cloning s.
Definition afc.cpp:53
TestSpace(void)
Constructor for creation.
Definition afc.cpp:51
virtual Space * copy(void)
Copy during cloning.
Definition afc.cpp:62
Gecode::IntVar x
Two integer variables.
Definition afc.cpp:48
Test for AFC infrastructure
Definition afc.cpp:42
AFC(void)
Initialize test.
Definition afc.cpp:83
static const int n
How many spaces to maintain.
Definition afc.cpp:69
bool run(void)
Perform actual tests.
Definition afc.cpp:85
static const int n_ops
How many test operations to be performed.
Definition afc.cpp:67
int index(void)
Return random index.
Definition afc.cpp:78
int space(TestSpace *s[])
Return random index of non-null space.
Definition afc.cpp:71
Base class for all tests to be run
Definition test.hh:103
static Gecode::Support::RandomGenerator rand
Random number generator.
Definition test.hh:134
Base(const std::string &s)
Create and register test with name s.
Definition test.cpp:59
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVar x1)
Post propagator for .
Definition rel.cpp:68
@ IRT_LE
Less ( )
Definition int.hh:944
Space(void)
Default constructor.
Definition core.cpp:115
Space * clone(CloneStatistics &stat=unused_clone) const
Clone space.
Definition core.hpp:3228
General test support.
Definition afc.cpp:39
AFC afc
Definition afc.cpp:135
#define GECODE_NEVER
Assert that this command is never executed.
Definition macros.hpp:56