Generated on Sat Feb 7 2015 02:01:33 for Gecode by doxygen 1.8.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  * Last modified:
12  * $Date: 2009-02-04 13:25:42 +0100 (Wed, 04 Feb 2009) $ by $Author: schulte $
13  * $Revision: 8138 $
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 
112  std::vector<std::pair<bool, const char*> > testpat;
113  const char* startFrom = NULL;
114  bool list = false;
115 
116  void
117  Options::parse(int argc, char* argv[]) {
118  int i = 1;
119  while (i < argc) {
120  if (!strcmp(argv[i],"-help") || !strcmp(argv[i],"--help")) {
121  std::cerr << "Options for testing:" << std::endl
122  << "\t-seed (unsigned int or \"time\") default: "
123  << seed << std::endl
124  << "\t\tseed for random number generator (unsigned int),"
125  << std::endl
126  << "\t\tor \"time\" for a random seed based on "
127  << "current time" << std::endl
128  << "\t-fixprob (unsigned int) default: "
129  << fixprob << std::endl
130  << "\t\t1/fixprob is the probability of computing a fixpoint"
131  << std::endl
132  << "\t-iter (unsigned int) default: " <<iter<< std::endl
133  << "\t\tthe number of iterations" << std::endl
134  << "\t-test (string) default: (none)" << std::endl
135  << "\t\tsimple pattern for the tests to run" << std::endl
136  << "\t\tprefixing the pattern with \"-\" negates the pattern"
137  << std::endl
138  << "\t\tmultiple pattern-options may be given" << std::endl
139  << "\t-start (string) default: (none)" << std::endl
140  << "\t\tsimple pattern for the first test to run" << std::endl
141  << "\t-log"
142  << std::endl
143  << "\t\tlog execution of tests"
144  << std::endl
145  << "\t\tthe optional argument determines the style of the log"
146  << std::endl
147  << "\t\twith text as the default style"
148  << std::endl
149  << "\t-stop (boolean) default: "
150  << (stop ? "true" : "false") << std::endl
151  << "\t\tstop on first error or continue" << std::endl
152  << "\t-list" << std::endl
153  << "\t\toutput list of all test cases and exit" << std::endl
154  ;
155  exit(EXIT_SUCCESS);
156  } else if (!strcmp(argv[i],"-seed")) {
157  if (++i == argc) goto missing;
158  if (!strcmp(argv[i],"time")) {
159  seed = static_cast<unsigned int>(time(NULL));
160  } else {
161  seed = static_cast<unsigned int>(atoi(argv[i]));
162  }
163  } else if (!strcmp(argv[i],"-iter")) {
164  if (++i == argc) goto missing;
165  iter = static_cast<unsigned int>(atoi(argv[i]));
166  } else if (!strcmp(argv[i],"-fixprob")) {
167  if (++i == argc) goto missing;
168  fixprob = static_cast<unsigned int>(atoi(argv[i]));
169  } else if (!strcmp(argv[i],"-test")) {
170  if (++i == argc) goto missing;
171  if (argv[i][0] == '-')
172  testpat.push_back(std::make_pair(true, argv[i] + 1));
173  else
174  testpat.push_back(std::make_pair(false, argv[i]));
175  } else if (!strcmp(argv[i],"-start")) {
176  if (++i == argc) goto missing;
177  startFrom = argv[i];
178  } else if (!strcmp(argv[i],"-log")) {
179  log = true;
180  } else if (!strcmp(argv[i],"-stop")) {
181  if (++i == argc) goto missing;
182  if(argv[i][0] == 't') {
183  stop = true;
184  } else if (argv[i][0] == 'f') {
185  stop = false;
186  }
187  } else if (!strcmp(argv[i],"-list")) {
188  list = true;
189  }
190  i++;
191  }
192  return;
193  missing:
194  std::cerr << "Erroneous argument (" << argv[i-1] << ")" << std::endl
195  << " missing parameter" << std::endl;
196  exit(EXIT_FAILURE);
197  }
198 
199 }
200 
201 int
202 main(int argc, char* argv[]) {
203  using namespace Test;
204 #ifdef GECODE_HAS_MTRACE
205  mtrace();
206 #endif
207 
208  opt.parse(argc, argv);
209 
210  Base::sort();
211 
212  if (list) {
213  for (Base* t = Base::tests() ; t != NULL; t = t->next() ) {
214  std::cout << t->name() << std::endl;
215  }
216  exit(EXIT_SUCCESS);
217  }
218 
220 
221  bool started = startFrom == NULL ? true : false;
222 
223  for (Base* t = Base::tests() ; t != NULL; t = t->next() ) {
224  try {
225  if (!started) {
226  if (t->name().find(startFrom) != std::string::npos)
227  started = true;
228  else
229  goto next;
230  }
231  if (testpat.size() != 0) {
232  bool match_found = false;
233  bool some_positive = false;
234  for (unsigned int i = 0; i < testpat.size(); ++i) {
235  if (testpat[i].first) { // Negative pattern
236  if (t->name().find(testpat[i].second) != std::string::npos)
237  goto next;
238  } else { // Positive pattern
239  some_positive = true;
240  if (t->name().find(testpat[i].second) != std::string::npos)
241  match_found = true;
242  }
243  }
244  if (some_positive && !match_found) goto next;
245  }
246  std::cout << t->name() << " ";
247  std::cout.flush();
248  for (unsigned int i = opt.iter; i--; ) {
249  opt.seed = Base::rand.seed();
250  if (t->run()) {
251  std::cout << '+';
252  std::cout.flush();
253  } else {
254  std::cout << "-" << std::endl;
255  report_error(t->name());
256  if (opt.stop)
257  return 1;
258  }
259  }
260  std::cout << std::endl;
261  } catch (Gecode::Exception e) {
262  std::cout << "Exception in \"Gecode::" << e.what()
263  << "." << std::endl
264  << "Stopping..." << std::endl;
265  report_error(t->name());
266  if (opt.stop)
267  return 1;
268  }
269  next:;
270  }
271  return 0;
272 }
273 
274 std::ostream&
275 operator<<(std::ostream& os, const Test::ind& i) {
276  for (int j=i.l; j--; )
277  os << " ";
278  return os;
279 }
280 
281 // STATISTICS: test-core
const std::string & name(void) const
Return name of test.
Definition: test.hpp:54
NodeType t
Type of node.
Definition: bool-expr.cpp:234
std::vector< std::pair< bool, const char * > > testpat
Definition: test.cpp:112
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
Base * next(void) const
Return next test.
Definition: test.hpp:62
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
virtual const char * what(void) const
Return information.
Definition: exception.cpp:59
bool list
Definition: test.cpp:114
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
Heap heap
The single global heap.
Definition: heap.cpp:49
T * alloc(long unsigned int n)
Allocate block of n objects of type T from heap.
Definition: heap.hpp:400
unsigned int iter
Number of iterations for each test.
Definition: test.hh:85
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
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
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:426
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:117
#define forceinline
Definition: config.hpp:132
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
struct Gecode::@518::NNF::@57::@58 b
For binary nodes (and, or, eqv)
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:113
Sort tests by name.
Definition: test.cpp:72
bool stop
Whether to stop on an error.
Definition: test.hh:93
int l
Which indentation level.
Definition: test.hh:73