Generated on Sat Apr 10 2021 00:00:00 for Gecode by doxygen 1.9.1
test.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  * Mikael Lagerkvist <lagerkvist@gecode.org>
6  *
7  * Copyright:
8  * Christian Schulte, 2004
9  * Mikael Lagerkvist, 2005
10  *
11  * This file is part of Gecode, the generic constraint
12  * development environment:
13  * http://www.gecode.org
14  *
15  * Permission is hereby granted, free of charge, to any person obtaining
16  * a copy of this software and associated documentation files (the
17  * "Software"), to deal in the Software without restriction, including
18  * without limitation the rights to use, copy, modify, merge, publish,
19  * distribute, sublicense, and/or sell copies of the Software, and to
20  * permit persons to whom the Software is furnished to do so, subject to
21  * the following conditions:
22  *
23  * The above copyright notice and this permission notice shall be
24  * included in all copies or substantial portions of the Software.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33  *
34  */
35 
36 #include "test/test.hh"
37 
38 #ifdef GECODE_HAS_MTRACE
39 #include <mcheck.h>
40 #endif
41 
42 #include <iostream>
43 
44 #include <cstdlib>
45 #include <cstring>
46 #include <ctime>
47 #include <vector>
48 #include <utility>
49 
50 namespace Test {
51 
52  // Log stream
53  std::ostringstream olog;
54 
55  /*
56  * Base class for tests
57  *
58  */
59  Base::Base(const std::string& s)
60  : _name(s), _next(_tests) {
61  _tests = this; _n_tests++;
62  }
63 
64  Base* Base::_tests = NULL;
65  unsigned int Base::_n_tests = 0;
66 
68  class SortByName {
69  public:
70  forceinline bool
72  return x->name() > y->name();
73  }
74  };
75 
76  void
77  Base::sort(void) {
78  Base** b = Gecode::heap.alloc<Base*>(_n_tests);
79  unsigned int i=0;
80  for (Base* t = _tests; t != NULL; t = t->next())
81  b[i++] = t;
82  SortByName sbn;
83  Gecode::Support::quicksort(b,_n_tests,sbn);
84  i=0;
85  _tests = NULL;
86  for ( ; i < _n_tests; i++) {
87  b[i]->next(_tests); _tests = b[i];
88  }
89  Gecode::heap.free(b,_n_tests);
90  }
91 
92  Base::~Base(void) {}
93 
96 
98 
99  void report_error(std::string name) {
100  std::cout << "Options: -seed " << opt.seed;
101  if (opt.fixprob != opt.deffixprob)
102  std::cout << " -fixprob " << opt.fixprob;
103  std::cout << " -test " << name << std::endl;
104  if (opt.log)
105  std::cout << olog.str();
106  }
107 
109  enum MatchType {
110  MT_ANY, //< Positive match anywhere in string
111  MT_NOT, //< Negative match
112  MT_FIRST //< Positive match at beginning
113  };
114 
115  std::vector<std::pair<MatchType, const char*> > testpat;
116  const char* startFrom = NULL;
117  bool list = false;
118 
119  void
120  Options::parse(int argc, char* argv[]) {
121  int i = 1;
122  while (i < argc) {
123  if (!strcmp(argv[i],"-help") || !strcmp(argv[i],"--help")) {
124  std::cerr << "Options for testing:" << std::endl
125  << "\t-seed (unsigned int or \"time\") default: "
126  << seed << std::endl
127  << "\t\tseed for random number generator (unsigned int),"
128  << std::endl
129  << "\t\tor \"time\" for a random seed based on "
130  << "current time" << std::endl
131  << "\t-fixprob (unsigned int) default: "
132  << fixprob << std::endl
133  << "\t\t1/fixprob is the probability of computing a fixpoint"
134  << std::endl
135  << "\t-iter (unsigned int) default: " <<iter<< std::endl
136  << "\t\tthe number of iterations" << std::endl
137  << "\t-test (string) default: (none)" << std::endl
138  << "\t\tsimple pattern for the tests to run" << std::endl
139  << "\t\tprefixing with \"-\" negates the pattern" << std::endl
140  << "\t\tprefixing with \"^\" requires a match at the beginning" << std::endl
141  << "\t\tmultiple pattern-options may be given"
142  << std::endl
143  << "\t-start (string) default: (none)" << std::endl
144  << "\t\tsimple pattern for the first test to run" << std::endl
145  << "\t-log"
146  << std::endl
147  << "\t\tlog execution of tests"
148  << std::endl
149  << "\t\tthe optional argument determines the style of the log"
150  << std::endl
151  << "\t\twith text as the default style"
152  << std::endl
153  << "\t-stop (boolean) default: "
154  << (stop ? "true" : "false") << std::endl
155  << "\t\tstop on first error or continue" << std::endl
156  << "\t-list" << std::endl
157  << "\t\toutput list of all test cases and exit" << std::endl
158  ;
159  exit(EXIT_SUCCESS);
160  } else if (!strcmp(argv[i],"-seed")) {
161  if (++i == argc) goto missing;
162  if (!strcmp(argv[i],"time")) {
163  seed = static_cast<unsigned int>(time(NULL));
164  } else {
165  seed = static_cast<unsigned int>(atoi(argv[i]));
166  }
167  } else if (!strcmp(argv[i],"-iter")) {
168  if (++i == argc) goto missing;
169  iter = static_cast<unsigned int>(atoi(argv[i]));
170  } else if (!strcmp(argv[i],"-fixprob")) {
171  if (++i == argc) goto missing;
172  fixprob = static_cast<unsigned int>(atoi(argv[i]));
173  } else if (!strcmp(argv[i],"-test")) {
174  if (++i == argc) goto missing;
175  if (argv[i][0] == '^')
176  testpat.push_back(std::make_pair(MT_FIRST, argv[i] + 1));
177  else if (argv[i][0] == '-')
178  testpat.push_back(std::make_pair(MT_NOT, argv[i] + 1));
179  else
180  testpat.push_back(std::make_pair(MT_ANY, argv[i]));
181  } else if (!strcmp(argv[i],"-start")) {
182  if (++i == argc) goto missing;
183  startFrom = argv[i];
184  } else if (!strcmp(argv[i],"-log")) {
185  log = true;
186  } else if (!strcmp(argv[i],"-stop")) {
187  if (++i == argc) goto missing;
188  if(argv[i][0] == 't') {
189  stop = true;
190  } else if (argv[i][0] == 'f') {
191  stop = false;
192  }
193  } else if (!strcmp(argv[i],"-list")) {
194  list = true;
195  }
196  i++;
197  }
198  return;
199  missing:
200  std::cerr << "Erroneous argument (" << argv[i-1] << ")" << std::endl
201  << " missing parameter" << std::endl;
202  exit(EXIT_FAILURE);
203  }
204 
205 }
206 
207 int
208 main(int argc, char* argv[]) {
209  using namespace Test;
210 #ifdef GECODE_HAS_MTRACE
211  mtrace();
212 #endif
213 
214  opt.parse(argc, argv);
215 
216  Base::sort();
217 
218  if (list) {
219  for (Base* t = Base::tests() ; t != NULL; t = t->next() ) {
220  std::cout << t->name() << std::endl;
221  }
222  exit(EXIT_SUCCESS);
223  }
224 
226 
227  bool started = startFrom == NULL ? true : false;
228 
229  for (Base* t = Base::tests() ; t != NULL; t = t->next() ) {
230  try {
231  if (!started) {
232  if (t->name().find(startFrom) != std::string::npos)
233  started = true;
234  else
235  goto next;
236  }
237  if (testpat.size() != 0) {
238  bool match_found = false;
239  bool some_positive = false;
240  for (unsigned int i = 0; i < testpat.size(); ++i) {
241  if (testpat[i].first == MT_NOT) { // Negative pattern
242  if (t->name().find(testpat[i].second) != std::string::npos)
243  goto next;
244  } else { // Positive pattern
245  some_positive = true;
246  if (((testpat[i].first == MT_ANY) &&
247  (t->name().find(testpat[i].second) != std::string::npos)) ||
248  ((testpat[i].first == MT_FIRST) &&
249  (t->name().find(testpat[i].second) == 0)))
250  match_found = true;
251  }
252  }
253  if (some_positive && !match_found) goto next;
254  }
255  std::cout << t->name() << " ";
256  std::cout.flush();
257  for (unsigned int i = opt.iter; i--; ) {
258  opt.seed = Base::rand.seed();
259  if (t->run()) {
260  std::cout << '+';
261  std::cout.flush();
262  } else {
263  std::cout << "-" << std::endl;
264  report_error(t->name());
265  if (opt.stop)
266  return 1;
267  }
268  }
269  std::cout << std::endl;
270  } catch (Gecode::Exception e) {
271  std::cout << "Exception in \"Gecode::" << e.what()
272  << "." << std::endl
273  << "Stopping..." << std::endl;
274  report_error(t->name());
275  if (opt.stop)
276  return 1;
277  }
278  next:;
279  }
280  return 0;
281 }
282 
283 std::ostream&
284 operator<<(std::ostream& os, const Test::ind& i) {
285  for (int j=i.l; j--; )
286  os << " ";
287  return os;
288 }
289 
290 // STATISTICS: test-core
struct Gecode::@602::NNF::@65::@66 b
For binary nodes (and, or, eqv)
NodeType t
Type of node.
Definition: bool-expr.cpp:230
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:249
int main(int argc, char *argv[])
Main-function.
Exception: Base-class for exceptions
Definition: exception.hpp:42
virtual const char * what(void) const
Return information.
Definition: exception.cpp:55
std::basic_ostream< Char, Traits > & operator<<(std::basic_ostream< Char, Traits > &os, const FloatView &x)
Print float variable view.
void free(T *b, long unsigned int n)
Delete n objects starting at b.
Definition: heap.hpp:457
T * alloc(long unsigned int n)
Allocate block of n objects of type T from heap.
Definition: heap.hpp:431
Template for linear congruential generators.
Definition: random.hpp:46
void seed(unsigned int s)
Set the current seed to s.
Definition: random.hpp:77
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
static Base * tests(void)
Return all tests.
Definition: test.hpp:54
virtual ~Base(void)
Destructor.
Definition: test.cpp:92
static void sort(void)
Sort tests alphabetically.
Definition: test.cpp:77
Commandline options.
Definition: test.hh:76
void parse(int argc, char *argv[])
Parse commandline arguments.
Definition: test.cpp:120
bool log
Whether to log the tests.
Definition: test.hh:91
unsigned int seed
The random seed to be used.
Definition: test.hh:79
static const unsigned int deffixprob
Default fixpoint probaibility.
Definition: test.hh:87
unsigned int iter
Number of iterations for each test.
Definition: test.hh:81
bool stop
Whether to stop on an error.
Definition: test.hh:89
unsigned int fixprob
The probability for computing a fixpoint.
Definition: test.hh:85
Sort tests by name.
Definition: test.cpp:68
bool operator()(Base *x, Base *y)
Definition: test.cpp:71
Simple class for describing identation.
Definition: test.hh:66
Heap heap
The single global heap.
Definition: heap.cpp:44
LinearCongruentialGenerator< 2147483647, 48271, 44488, 3399 > RandomGenerator
Default values for linear congruential generator.
Definition: random.hpp:120
void quicksort(Type *l, Type *r, Less &less)
Standard quick sort.
Definition: sort.hpp:130
Gecode::IntArgs i({1, 2, 3, 4})
General test support.
Definition: afc.cpp:39
std::ostringstream olog
Stream used for logging.
Definition: test.cpp:53
bool list
Definition: test.cpp:117
MatchType
How to match.
Definition: test.cpp:109
@ MT_FIRST
Definition: test.cpp:112
@ MT_ANY
Definition: test.cpp:110
@ MT_NOT
Definition: test.cpp:111
void report_error(std::string name)
Definition: test.cpp:99
Options opt
The options.
Definition: test.cpp:97
std::vector< std::pair< MatchType, const char * > > testpat
Definition: test.cpp:115
const char * startFrom
Definition: test.cpp:116
#define forceinline
Definition: config.hpp:192