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