Generated on Sat Feb 7 2015 02:01:27 for Gecode by doxygen 1.8.9.1
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  * Last modified:
10  * $Date: 2011-11-03 14:57:30 +0100 (Thu, 03 Nov 2011) $ by $Author: tack $
11  * $Revision: 12455 $
12  *
13  * This file is part of Gecode, the generic constraint
14  * development environment:
15  * http://www.gecode.org
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining
18  * a copy of this software and associated documentation files (the
19  * "Software"), to deal in the Software without restriction, including
20  * without limitation the rights to use, copy, modify, merge, publish,
21  * distribute, sublicense, and/or sell copies of the Software, and to
22  * permit persons to whom the Software is furnished to do so, subject to
23  * the following conditions:
24  *
25  * The above copyright notice and this permission notice shall be
26  * included in all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35  *
36  */
37 
38 #include <gecode/kernel.hh>
39 #include <gecode/int.hh>
40 
41 #include "test/test.hh"
42 
43 namespace Test {
44 
46  class AFC : public Test::Base {
47  protected:
49  class TestSpace : public Gecode::Space {
50  protected:
53  public:
55  TestSpace(void) : x(*this,0,10), y(*this,0,10) {}
57  TestSpace(bool share, TestSpace& s) : Space(share,s) {
58  x.update(*this,share,s.x);
59  y.update(*this,share,s.y);
60  }
62  void post(void) {
63  Gecode::rel(*this, x, Gecode::IRT_LE, y);
64  }
66  virtual Space* copy(bool share) {
67  return new TestSpace(share,*this);
68  }
69  };
71  static const int n_ops = 8 * 1024;
73  static const int n = 16;
75  int space(TestSpace* s[]) {
76  int i = rand(n);
77  while (s[i] == NULL)
78  i = (i+1) % n;
79  return i;
80  }
82  int index(void) {
83  return rand(n);
84  }
85  public:
87  AFC(void) : Test::Base("AFC") {}
89  bool run(void) {
90  // Array of spaces for tests
91  TestSpace* s[n];
92  // How many spaces exist in s
93  int n_s = 1;
94 
95  for (int i=n; i--; )
96  s[i] = NULL;
97  s[0] = new TestSpace;
98 
99  for (int o=n_ops; o--; )
100  switch (rand(3)) {
101  case 0:
102  // clone space
103  {
104  int i = index();
105  if ((s[i] != NULL)) {
106  if (n_s > 1) {
107  delete s[i]; s[i]=NULL; n_s--;
108  } else {
109  break;
110  }
111  }
112  int j = space(s);
113  (void) s[j]->status();
114  s[i] = static_cast<TestSpace*>(s[j]->clone());
115  n_s++;
116  }
117  break;
118  case 1:
119  // delete space
120  if (n_s > 1) {
121  int i = space(s);
122  delete s[i]; s[i]=NULL; n_s--;
123  }
124  break;
125  case 2:
126  // post propagator
127  s[space(s)]->post();
128  break;
129  default:
130  GECODE_NEVER;
131  }
132  // Delete all remaining spaces
133  for (int i=n; i--; )
134  delete s[i];
135  return true;
136  }
137  };
138 
140 
141 }
142 
143 // STATISTICS: test-core
void update(Space &home, bool share, VarImpVar< VarImp > &y)
Update this variable to be a clone of variable y.
Definition: var.hpp:128
static Gecode::Support::RandomGenerator rand
Random number generator.
Definition: test.hh:138
Space * clone(bool share=true, CloneStatistics &stat=unused_clone) const
Clone space.
Definition: core.hpp:2854
AFC afc
Definition: afc.cpp:139
TestSpace(bool share, TestSpace &s)
Constructor for cloning s.
Definition: afc.cpp:57
void post(void)
Post arbitrary propagator.
Definition: afc.cpp:62
Computation spaces.
Definition: core.hpp:1362
static const int n_ops
How many test operations to be performed.
Definition: afc.cpp:71
Test for AFC infrastructure
Definition: afc.cpp:46
TestSpace(void)
Constructor for creation.
Definition: afc.cpp:55
Gecode::IntArgs i(4, 1, 2, 3, 4)
Gecode::IntVar y
Definition: afc.cpp:52
Base class for all tests to be run
Definition: test.hh:107
Less ( )
Definition: int.hh:907
AFC(void)
Initialize test.
Definition: afc.cpp:87
General test support.
Definition: afc.cpp:43
virtual Space * copy(bool share)
Copy during cloning.
Definition: afc.cpp:66
Space(void)
Default constructor.
Definition: core.cpp:114
Integer variables.
Definition: int.hh:350
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVal n)
Propagates .
Definition: rel.cpp:47
Test space.
Definition: afc.cpp:49
Gecode::IntVar x
Two integer variables.
Definition: afc.cpp:52
static const int n
How many spaces to maintain.
Definition: afc.cpp:73
bool run(void)
Perform actual tests.
Definition: afc.cpp:89
#define GECODE_NEVER
Assert that this command is never executed.
Definition: macros.hpp:60
int space(TestSpace *s[])
Return random index of non-null space.
Definition: afc.cpp:75
int index(void)
Return random index.
Definition: afc.cpp:82