Debian Codegraph @

Osi/src/OsiCommonTest/OsiUnitTests.hpp

raw symbols SCIP
1 // Copyright (C) 2010
2 // All Rights Reserved.
3 // This code is licensed under the terms of the Eclipse Public License (EPL).
4
5 /*! \file OsiUnitTests.hpp
6
7 Utility methods for OSI unit tests.
8 */
9
10 #ifndef OSISOLVERINTERFACETEST_HPP_
11 #define OSISOLVERINTERFACETEST_HPP_
12
13 #include <cstdio>
14 #include <cstdlib>
15 #include <iostream>
16 #include <string>
17 #include <sstream>
18 #include <vector>
19 #include <list>
20 #include <map>
21
22 class OsiSolverInterface;
23 class CoinPackedVectorBase;
24
25 /** A function that tests that a lot of problems given in MPS files (mostly the NETLIB problems) solve properly with all the specified solvers.
26 *
27 * The routine creates a vector of NetLib problems (problem name, objective,
28 * various other characteristics), and a vector of solvers to be tested.
29 *
30 * Each solver is run on each problem. The run is deemed successful if the
31 * solver reports the correct problem size after loading and returns the
32 * correct objective value after optimization.
33
34 * If multiple solvers are available, the results are compared pairwise against
35 * the results reported by adjacent solvers in the solver vector. Due to
36 * limitations of the volume solver, it must be the last solver in vecEmptySiP.
37 */
38 void OsiSolverInterfaceMpsUnitTest(const std::vector< OsiSolverInterface * > &vecEmptySiP,
39 const std::string &mpsDir);
40
41 /** A function that tests the methods in the OsiSolverInterface class.
42 * Some time ago, if this method is compiled with optimization,
43 * the compilation took 10-15 minutes and the machine pages (has 256M core memory!)...
44 */
45 void OsiSolverInterfaceCommonUnitTest(const OsiSolverInterface *emptySi,
46 const std::string &mpsDir,
47 const std::string &netlibDir);
48
49 /** A function that tests the methods in the OsiColCut class. */
50 void OsiColCutUnitTest(const OsiSolverInterface *baseSiP,
51 const std::string &mpsDir);
52
53 /** A function that tests the methods in the OsiRowCut class. */
54 void OsiRowCutUnitTest(const OsiSolverInterface *baseSiP,
55 const std::string &mpsDir);
56
57 /** A function that tests the methods in the OsiRowCutDebugger class. */
58 void OsiRowCutDebuggerUnitTest(const OsiSolverInterface *siP,
59 const std::string &mpsDir);
60
61 /** A function that tests the methods in the OsiCuts class. */
62 void OsiCutsUnitTest();
63
64 /// A namespace so we can define a few `global' variables to use during tests.
65 namespace OsiUnitTest {
66
67 class TestOutcomes;
68
69 /*! \brief Verbosity level of unit tests
70
71 0 (default) for minimal output; larger numbers produce more output
72 */
73 extern unsigned int verbosity;
74
75 /*! \brief Behaviour on failing a test
76
77 - 0 (= default) continue
78 - 1 press any key to continue
79 - 2 stop with abort()
80 */
81 extern unsigned int haltonerror;
82
83 /*! \brief Test outcomes
84
85 A global TestOutcomes object to store test outcomes during the run of the unit test
86 for an OSI.
87 */
88 extern TestOutcomes outcomes;
89
90 /*! \brief Print an error message
91
92 Formatted as "XxxSolverInterface testing issue: message" where Xxx is the string
93 provided as \p solverName.
94
95 Flushes std::cout before printing to std::cerr.
96 */
97 void failureMessage(const std::string &solverName,
98 const std::string &message);
99 /// \overload
100 void failureMessage(const OsiSolverInterface &si,
101 const std::string &message);
102
103 /*! \brief Print an error message, specifying the test name and condition
104
105 Formatted as "XxxSolverInterface testing issue: testname failed: testcond" where
106 Xxx is the OsiStrParam::OsiSolverName parameter of the \p si.
107 Flushes std::cout before printing to std::cerr.
108 */
109 void failureMessage(const std::string &solverName,
110 const std::string &testname, const std::string &testcond);
111
112 /// \overload
113 void failureMessage(const OsiSolverInterface &si,
114 const std::string &testname, const std::string &testcond);
115
116 /*! \brief Print a message.
117
118 Prints the message as given. Flushes std::cout before printing to std::cerr.
119 */
120 void testingMessage(const char *const msg);
121
122 /*! \brief Utility method to check equality
123
124 Tests for equality using CoinRelFltEq with tolerance \p tol. Understands the
125 notion of solver infinity and obtains the value for infinity from the solver
126 interfaces supplied as parameters.
127 */
128 bool equivalentVectors(const OsiSolverInterface *si1,
129 const OsiSolverInterface *si2,
130 double tol, const double *v1, const double *v2, int size);
131
132 /*! \brief Compare two problems for equality
133
134 Compares the problems held in the two solvers: constraint matrix, row and column
135 bounds, column type, and objective. Rows are checked using upper and lower bounds
136 and using sense, bound, and range.
137 */
138 bool compareProblems(OsiSolverInterface *osi1, OsiSolverInterface *osi2);
139
140 /*! \brief Compare a packed vector with an expanded vector
141
142 Checks that all values present in the packed vector are present in the full vector
143 and checks that there are no extra entries in the full vector. Uses CoinRelFltEq
144 with the default tolerance.
145 */
146 bool isEquivalent(const CoinPackedVectorBase &pv, int n, const double *fv);
147
148 /*! \brief Process command line parameters.
149
150 An unrecognised keyword which is not in the \p ignorekeywords map will trigger the
151 help message and a return value of false. For each keyword in \p ignorekeywords, you
152 can specify the number of following parameters that should be ignored.
153
154 This should be replaced with the one of the standard CoinUtils parameter mechanisms.
155 */
156 bool processParameters(int argc, const char **argv,
157 std::map< std::string, std::string > &parms,
158 const std::map< std::string, int > &ignorekeywords = std::map< std::string, int >());
159
160 /// A single test outcome record.
161 class TestOutcome {
162 public:
163 /// Test result
164 typedef enum {
165 NOTE = 0,
166 PASSED = 1,
167 WARNING = 2,
168 ERROR = 3,
169 LAST = 4
170 } SeverityLevel;
171 /// Print strings for SeverityLevel
172 static std::string SeverityLevelName[LAST];
173 /// Name of component under test
174 std::string component;
175 /// Name of test
176 std::string testname;
177 /// Condition being tested
178 std::string testcond;
179 /// Test result
180 SeverityLevel severity;
181 /// Set to true if problem is expected
182 bool expected;
183 /// Name of code file where test executed
184 std::string filename;
185 /// Line number in code file where test executed
186 int linenumber;
187 /// Standard constructor
188 TestOutcome(const std::string &comp, const std::string &tst,
189 const char *cond, SeverityLevel sev,
190 const char *file, int line, bool exp = false)
191 : component(comp)
192 , testname(tst)
193 , testcond(cond)
194 , severity(sev)
195 , expected(exp)
196 , filename(file)
197 , linenumber(line)
198 {
199 }
200 /// Print the test outcome
201 void print() const;
202 };
203
204 /// Utility class to maintain a list of test outcomes.
205 class TestOutcomes : public std::list< TestOutcome > {
206 public:
207 /// Add an outcome to the list
208 void add(std::string comp, std::string tst, const char *cond,
209 TestOutcome::SeverityLevel sev, const char *file, int line,
210 bool exp = false)
211 {
212 push_back(TestOutcome(comp, tst, cond, sev, file, line, exp));
213 }
214
215 /*! \brief Add an outcome to the list
216
217 Get the component name from the solver interface.
218 */
219 void add(const OsiSolverInterface &si, std::string tst, const char *cond,
220 TestOutcome::SeverityLevel sev, const char *file, int line,
221 bool exp = false);
222 /// Print the list of outcomes
223 void print() const;
224 /*! \brief Count total and expected outcomes at given severity level
225
226 Given a severity level, walk the list of outcomes and count the total number
227 of outcomes at this severity level and the number expected.
228 */
229 void getCountBySeverity(TestOutcome::SeverityLevel sev,
230 int &total, int &expected) const;
231 };
232
233 /// Convert parameter to a string (stringification)
234 #define OSIUNITTEST_QUOTEME_(x) #x
235 /// Convert to string with one level of expansion of the parameter
236 #define OSIUNITTEST_QUOTEME(x) OSIUNITTEST_QUOTEME_(x)
237
238 template < typename Component >
239 bool OsiUnitTestAssertSeverityExpected(
240 bool condition, const char *condition_str, const char *filename,
241 int line, const Component &component, const std::string &testname,
242 TestOutcome::SeverityLevel severity, bool expected)
243 {
244 if (condition) {
245 OsiUnitTest::outcomes.add(component, testname, condition_str,
246 OsiUnitTest::TestOutcome::PASSED, filename, line, false);
247 if (OsiUnitTest::verbosity >= 2) {
248 std::ostringstream successmsg;
249 successmsg << __FILE__ << ":" << __LINE__ << ": " << testname
250 << " (condition \'" << condition_str << "\') passed.\n";
251 OsiUnitTest::testingMessage(successmsg.str().c_str());
252 }
253 return true;
254 }
255 OsiUnitTest::outcomes.add(component, testname, condition_str,
256 severity, filename, line, expected);
257 OsiUnitTest::failureMessage(component, testname, condition_str);
258 switch (OsiUnitTest::haltonerror) {
259 case 2: {
260 if (severity >= OsiUnitTest::TestOutcome::ERROR)
261 std::abort();
262 break;
263 }
264 case 1: {
265 std::cout << std::endl
266 << "press any key to continue..." << std::endl;
267 std::getchar();
268 break;
269 }
270 default:;
271 }
272 return false;
273 }
274
275 /// Add a test outcome to the list held in OsiUnitTest::outcomes
276 #define OSIUNITTEST_ADD_OUTCOME(component, testname, testcondition, severity, expected) \
277 OsiUnitTest::outcomes.add(component, testname, testcondition, severity, \
278 __FILE__, __LINE__, expected)
279 /*! \brief Test for a condition and record the result
280
281 Test \p condition and record the result in OsiUnitTest::outcomes.
282 If it succeeds, record the result as OsiUnitTest::TestOutcome::PASSED and print
283 a message for OsiUnitTest::verbosity >= 2.
284 If it fails, record the test as failed with \p severity and \p expected and
285 react as specified by OsiUnitTest::haltonerror.
286
287 \p failurecode is executed when failure is not fatal.
288 */
289 #define OSIUNITTEST_ASSERT_SEVERITY_EXPECTED(condition, failurecode, component, \
290 testname, severity, expected) \
291 { \
292 if (!OsiUnitTestAssertSeverityExpected(condition, #condition, \
293 __FILE__, __LINE__, component, testname, severity, expected)) { \
294 failurecode; \
295 } \
296 }
297
298 /*! \brief Perform a test with severity OsiUnitTest::TestOutcome::ERROR, failure not
299 expected.
300 */
301 #define OSIUNITTEST_ASSERT_ERROR(condition, failurecode, component, testname) \
302 OSIUNITTEST_ASSERT_SEVERITY_EXPECTED(condition, failurecode, component, testname, \
303 OsiUnitTest::TestOutcome::ERROR, false)
304
305 /*! \brief Perform a test with severity OsiUnitTest::TestOutcome::WARNING, failure
306 not expected.
307 */
308 #define OSIUNITTEST_ASSERT_WARNING(condition, failurecode, component, testname) \
309 OSIUNITTEST_ASSERT_SEVERITY_EXPECTED(condition, failurecode, component, testname, \
310 OsiUnitTest::TestOutcome::WARNING, false)
311
312 /*! \brief Perform a test surrounded by a try/catch block
313
314 \p trycode is executed in a try/catch block; if there's no throw the test is deemed
315 to have succeeded and is recorded in OsiUnitTest::outcomes with status
316 OsiUnitTest::TestOutcome::PASSED. If the \p trycode throws a CoinError, the failure
317 is recorded with status \p severity and \p expected and the value of
318 OsiUnitTest::haltonerror is consulted. If the failure is not fatal, \p catchcode is
319 executed. If any other error is thrown, the failure is recorded as for a CoinError
320 and \p catchcode is executed (haltonerror is not consulted).
321 */
322 #define OSIUNITTEST_CATCH_SEVERITY_EXPECTED(trycode, catchcode, component, testname, \
323 severity, expected) \
324 { \
325 try { \
326 trycode; \
327 OSIUNITTEST_ADD_OUTCOME(component, testname, #trycode " did not throw exception", \
328 OsiUnitTest::TestOutcome::PASSED, false); \
329 if (OsiUnitTest::verbosity >= 2) { \
330 std::string successmsg(__FILE__ ":" OSIUNITTEST_QUOTEME(__LINE__) ": "); \
331 successmsg = successmsg + testname; \
332 successmsg = successmsg + " (code \'" #trycode "\') did not throw exception"; \
333 successmsg = successmsg + ".\n"; \
334 OsiUnitTest::testingMessage(successmsg.c_str()); \
335 } \
336 } catch (CoinError & e) { \
337 std::stringstream errmsg; \
338 errmsg << #trycode " threw CoinError: " << e.message(); \
339 if (e.className().length() > 0) \
340 errmsg << " in " << e.className(); \
341 if (e.methodName().length() > 0) \
342 errmsg << " in " << e.methodName(); \
343 if (e.lineNumber() >= 0) \
344 errmsg << " at " << e.fileName() << ":" << e.lineNumber(); \
345 OSIUNITTEST_ADD_OUTCOME(component, testname, errmsg.str().c_str(), \
346 severity, expected); \
347 OsiUnitTest::failureMessage(component, testname, errmsg.str().c_str()); \
348 switch (OsiUnitTest::haltonerror) { \
349 case 2: { \
350 if (severity >= OsiUnitTest::TestOutcome::ERROR) \
351 abort(); \
352 break; \
353 } \
354 case 1: { \
355 std::cout << std::endl \
356 << "press any key to continue..." << std::endl; \
357 getchar(); \
358 break; \
359 } \
360 default:; \
361 } \
362 catchcode; \
363 } catch (...) { \
364 std::string errmsg; \
365 errmsg = #trycode; \
366 errmsg = errmsg + " threw unknown exception"; \
367 OSIUNITTEST_ADD_OUTCOME(component, testname, errmsg.c_str(), severity, false); \
368 OsiUnitTest::failureMessage(component, testname, errmsg.c_str()); \
369 catchcode; \
370 } \
371 }
372
373 /*! \brief Perform a try/catch test with severity OsiUnitTest::TestOutcome::ERROR,
374 failure not expected.
375 */
376 #define OSIUNITTEST_CATCH_ERROR(trycode, catchcode, component, testname) \
377 OSIUNITTEST_CATCH_SEVERITY_EXPECTED(trycode, catchcode, component, testname, OsiUnitTest::TestOutcome::ERROR, false)
378
379 /*! \brief Perform a try/catch test with severity OsiUnitTest::TestOutcome::WARNING,
380 failure not expected.
381 */
382 #define OSIUNITTEST_CATCH_WARNING(trycode, catchcode, component, testname) \
383 OSIUNITTEST_CATCH_SEVERITY_EXPECTED(trycode, catchcode, component, testname, OsiUnitTest::TestOutcome::WARNING, false)
384
385 } // end namespace OsiUnitTest
386
387 #endif /*OSISOLVERINTERFACETEST_HPP_*/
388
389 /* vi: softtabstop=2 shiftwidth=2 expandtab tabstop=2
390 */