Osi trunk
OsiUnitTests.hpp
Go to the documentation of this file.
00001 // Copyright (C) 2010
00002 // All Rights Reserved.
00003 // This code is licensed under the terms of the Eclipse Public License (EPL).
00004 
00005 #ifndef OSISOLVERINTERFACETEST_HPP_
00006 #define OSISOLVERINTERFACETEST_HPP_
00007 
00008 #include <string>
00009 #include <vector>
00010 #include <list>
00011 
00012 class OsiSolverInterface;
00013 class CoinPackedVectorBase;
00014 
00028 int OsiSolverInterfaceMpsUnitTest
00029   (const std::vector<OsiSolverInterface*> & vecEmptySiP,
00030    const std::string& mpsDir);
00031 
00032 int OsiSolverInterfaceCommonUnitTest
00033   (const OsiSolverInterface* emptySi,
00034    const std::string& mpsDir,
00035    const std::string& netlibDir);
00036 
00037 namespace OsiUnitTest {
00038 
00039 class TestOutcomes;
00040 
00043 extern unsigned int verbosity;
00044 
00047 extern TestOutcomes outcomes;
00048 
00049 void failureMessage(const std::string &solverName,
00050                     const std::string &message) ;
00051 void failureMessage(const OsiSolverInterface &si,
00052                     const std::string &message) ;
00053 void failureMessage(const std::string &solverName,
00054                     const std::string &testname, const std::string &testcond) ;
00055 void failureMessage(const OsiSolverInterface &si,
00056                     const std::string &testname, const std::string &testcond) ;
00057 void testingMessage(const char *const msg) ;
00058 
00059 bool equivalentVectors(const OsiSolverInterface * si1,
00060                        const OsiSolverInterface * si2,
00061                        double tol,
00062                        const double * v1,
00063                        const double * v2,
00064                        int size) ;
00065 
00066 bool compareProblems(OsiSolverInterface *osi1, OsiSolverInterface *osi2) ;
00067 
00068 bool isEquivalent(const CoinPackedVectorBase &pv, int n, const double *fv) ;
00069 
00070 class TestOutcome {
00071 public:
00072         typedef enum {
00073                 NOTE     = 0,
00074                 PASSED   = 1,
00075                 WARNING  = 2,
00076                 ERROR    = 3,
00077                 LAST     = 4
00078         } SeverityLevel;
00079 
00080         static std::string SeverityLevelName[LAST];
00081 
00082         std::string     component;
00083         std::string     testname;
00084         std::string     testcond;
00085         SeverityLevel   severity;
00086         bool            expected;
00087 
00088         std::string     filename;
00089         int             linenumber;
00090 
00091         TestOutcome(const std::string& comp, const std::string& tst, const char* cond, SeverityLevel sev, const char* file, int line, bool exp = false)
00092         : component(comp), testname(tst), testcond(cond), severity(sev), expected(exp), filename(file), linenumber(line)
00093         { }
00094 
00095         void print() const;
00096 };
00097 
00098 class TestOutcomes : public std::list<TestOutcome> {
00099 public:
00100         void add(std::string comp, std::string tst, const char* cond, TestOutcome::SeverityLevel sev, const char* file, int line, bool exp = false)
00101         {
00102                 push_back(TestOutcome(comp, tst, cond, sev, file, line, exp));
00103         }
00104 
00105         void add(const OsiSolverInterface& si, std::string tst, const char* cond, TestOutcome::SeverityLevel sev, const char* file, int line, bool exp = false);
00106 
00107         void print() const;
00108 
00112         void getCountBySeverity(TestOutcome::SeverityLevel sev, int& total, int& expected) const;
00113 };
00114 
00115 #define OSIUNITTEST_QUOTEME_(x) #x
00116 #define OSIUNITTEST_QUOTEME(x) OSIUNITTEST_QUOTEME_(x)
00117 
00118 #define OSIUNITTEST_ADD_OUTCOME(component, testname, testcondition, severity, expected) \
00119         OsiUnitTest::outcomes.add(component, testname, testcondition, severity, __FILE__, __LINE__, expected)
00120 
00121 #define OSIUNITTEST_ASSERT_SEVERITY_EXPECTED(condition, failurecode, component, testname, severity, expected) \
00122         { \
00123                 if( condition ) { \
00124                         OSIUNITTEST_ADD_OUTCOME(component, testname, #condition, OsiUnitTest::TestOutcome::PASSED, false); \
00125                   if (OsiUnitTest::verbosity >= 2) { \
00126                         std::string successmsg(__FILE__ ":" OSIUNITTEST_QUOTEME(__LINE__) ": "); \
00127                           successmsg = successmsg + testname; \
00128                           successmsg = successmsg + " (condition \'" #condition "\') passed.\n"; \
00129                                 OsiUnitTest::testingMessage(successmsg.c_str()); \
00130                   } \
00131                 } else { \
00132                         OSIUNITTEST_ADD_OUTCOME(component, testname, #condition, severity, expected); \
00133                         OsiUnitTest::failureMessage(component, testname, #condition); \
00134                         failurecode; \
00135     } \
00136         }
00137 
00138 #define OSIUNITTEST_ASSERT_ERROR(condition, failurecode, component, testname) \
00139         OSIUNITTEST_ASSERT_SEVERITY_EXPECTED(condition, failurecode, component, testname, OsiUnitTest::TestOutcome::ERROR, false)
00140 
00141 #define OSIUNITTEST_ASSERT_WARNING(condition, failurecode, component, testname) \
00142         OSIUNITTEST_ASSERT_SEVERITY_EXPECTED(condition, failurecode, component, testname, OsiUnitTest::TestOutcome::WARNING, false)
00143 
00144 #define OSIUNITTEST_CATCH_SEVERITY_EXPECTED(trycode, catchcode, component, testname, severity, expected) \
00145         {\
00146                 try {\
00147                         trycode; \
00148                   OSIUNITTEST_ADD_OUTCOME(component, testname, #trycode " did not threw exception", OsiUnitTest::TestOutcome::PASSED, false); \
00149                   if (OsiUnitTest::verbosity >= 2) { \
00150                         std::string successmsg( __FILE__ ":" OSIUNITTEST_QUOTEME(__LINE__) ": "); \
00151                           successmsg = successmsg + testname; \
00152                           successmsg = successmsg + " (code \'" #trycode "\') did not throw exception.\n"; \
00153                                 OsiUnitTest::testingMessage(successmsg.c_str()); \
00154                   } \
00155                 } catch (CoinError& e) { \
00156                         std::string errmsg; \
00157                         errmsg = #trycode; \
00158                         errmsg = errmsg + " threw CoinError: " + e.message(); \
00159                         OSIUNITTEST_ADD_OUTCOME(component, testname, errmsg.c_str(), severity, expected); \
00160                         OsiUnitTest::failureMessage(component, testname, errmsg.c_str()); \
00161                         catchcode; \
00162                 } catch (...) { \
00163                   std::string errmsg; \
00164                   errmsg = #trycode; \
00165                   errmsg = errmsg + " threw unknown exception"; \
00166                   OSIUNITTEST_ADD_OUTCOME(component, testname, errmsg.c_str(), severity, false); \
00167                   OsiUnitTest::failureMessage(component, testname, errmsg.c_str()); \
00168                   catchcode; \
00169                 } \
00170         }
00171 
00172 #define OSIUNITTEST_CATCH_ERROR(trycode, catchcode, component, testname) \
00173         OSIUNITTEST_CATCH_SEVERITY_EXPECTED(trycode, catchcode, component, testname, OsiUnitTest::TestOutcome::ERROR, false)
00174 
00175 #define OSIUNITTEST_CATCH_WARNING(trycode, catchcode, component, testname) \
00176         OSIUNITTEST_CATCH_SEVERITY_EXPECTED(trycode, catchcode, component, testname, OsiUnitTest::TestOutcome::WARNING, false)
00177 
00178 } // end namespace OsiUnitTest
00179 
00180 #endif /*OSISOLVERINTERFACETEST_HPP_*/
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines