31 #include "dcmtk/config/osconfig.h"
32 #include "dcmtk/ofstd/ofconapp.h"
33 #include "dcmtk/ofstd/ofconsol.h"
34 #include "dcmtk/ofstd/oflist.h"
35 #include "dcmtk/ofstd/ofstream.h"
36 #include "dcmtk/ofstd/ofstring.h"
37 #include "dcmtk/ofstd/oftypes.h"
39 #ifdef OFTEST_OFSTD_ONLY
41 #define OFTEST_LOG_VERBOSE(msg) do { \
43 COUT << msg << OFendl; \
48 #include "dcmtk/oflog/oflog.h"
51 #define OFTEST_LOG_VERBOSE(msg) OFLOG_INFO(testLogger, msg)
110 virtual void run() = 0;
121 oss <<
"FAILED test '" <<
testName_ <<
"' at " << file <<
":" << line
122 <<
": " << message << OFStringStream_ends;
123 OFSTRINGSTREAM_GETOFSTRING(oss, str)
172 unsigned int numFailed = 0;
176 OFTEST_LOG_VERBOSE(
"Running " << tests.
size() <<
" tests" << mod_str <<
":");
178 for (it = tests.
begin(); it != tests.
end(); ++it)
180 OFTEST_LOG_VERBOSE(
" Running test '" << (*it)->getTestName() <<
"'...");
190 for (rit = result.
begin(); rit != result.
end(); ++rit)
193 CERR << *rit << OFendl;
198 COUT <<
"Test results" << mod_str <<
": "
199 << tests.
size() - numFailed <<
" succeeded, "
200 << numFailed <<
" failed." << OFendl;
205 CERR <<
"WARNING: More than 254 tests failed!" << OFendl;
219 int run(
int argc,
char* argv[],
const char* module)
222 OFBool listOnly = OFFalse;
225 #ifdef OFTEST_OFSTD_ONLY
228 rcsid =
"$dcmtk: " +
OFString(module) +
" $";
231 rcsid += OFSTRING_GUARD(module);
242 cmd.addGroup(
"general options:");
245 cmd.addOption(
"--exhaustive",
"-x",
"also run extensive and slow tests");
246 #ifdef OFTEST_OFSTD_ONLY
247 cmd.addOption(
"--verbose",
"-v",
"verbose mode, print processing details");
253 if (app.parseCommandLine(cmd, argc, argv))
258 #ifdef OFTEST_OFSTD_ONLY
259 if (cmd.findOption(
"--verbose")) verbose_ = OFTrue;
266 if (cmd.findOption(
"--exhaustive"))
exhaustive_ = OFTrue;
267 if (cmd.findOption(
"--list")) listOnly = OFTrue;
272 if (testsToRun.
empty())
274 CERR <<
"No tests to run!" << OFendl;
281 COUT <<
"There are " << testsToRun.
size() <<
" tests";
283 COUT <<
" for module '" << module <<
"'";
284 COUT <<
":" << OFendl;
285 for (it = testsToRun.
begin(); it != testsToRun.
end(); ++it)
287 COUT <<
" " << (*it)->getTestName() <<
"\n";
292 return runTests(testsToRun, module);
301 #ifdef OFTEST_OFSTD_ONLY
322 OFBool result = OFTrue;
334 for (
int i = 1; i <= paramCount; i++)
339 OFBool found = OFFalse;
352 CERR <<
"Error: No test matches '" << paramString <<
"'" << OFendl;
362 while (it != tests.
end())
365 it = tests.
erase(it);
383 const char*
string = str.
c_str();
385 for (; *testName !=
'\0' && *
string !=
'\0'; testName++,
string++)
388 if (
string[0] !=
'?' && testName[0] !=
string[0])
394 if (
string[0] ==
'*' &&
string[1] ==
'\0')
398 if (testName[0] ==
'\0' &&
string[0] ==
'\0')
412 #ifdef OFTEST_OFSTD_ONLY
423 #define OFTEST_MAIN(module) \
424 int main(int argc, char* argv[]) \
426 return OFTestManager::instance().run(argc, argv, module); \
430 #define OFTEST_CLASS(testName) \
431 class OFTest ## testName : public OFTestTest \
434 OFTest ## testName(); \
443 #define OFTEST_REGISTER_INT(testName) \
444 OFTest ## testName OFTest ## testName ## instance
449 #define OFTEST_REGISTER(testName) \
450 OFTEST_CLASS(testName); \
451 OFTEST_REGISTER_INT(testName)
459 #define OFTEST(testName) OFTEST_FLAGS(testName, EF_None)
467 #define OFTEST_FLAGS(testName, flags) \
468 OFTEST_CLASS(testName); \
469 OFTest ## testName::OFTest ## testName() \
470 : OFTestTest(#testName, flags) \
472 OFTestManager::instance().addTest(this); \
474 void OFTest ## testName ::run()
485 #define OFCHECK(condition) \
488 OFTestManager::instance().currentTest().recordFailure(__FILE__, __LINE__, #condition); \
496 #define OFCHECK_EQUAL(val1, val2) \
498 if ((val1) != (val2)) { \
499 OFOStringStream oss___; \
500 oss___ << "(" << (val1) << ") should equal (" << (val2) << ")" << OFStringStream_ends; \
501 OFSTRINGSTREAM_GETOFSTRING(oss___, str___) \
502 OFTestManager::instance().currentTest().recordFailure(__FILE__, __LINE__, str___); \
509 #define OFCHECK_FAIL(message) \
511 OFOStringStream oss___; \
512 oss___ << message << OFStringStream_ends; \
513 OFSTRINGSTREAM_GETOFSTRING(oss___, str___) \
514 OFTestManager::instance().currentTest().recordFailure(__FILE__, __LINE__, str___); \
OFString testName_
The unique name of this test.
static void configureFromCommandLine(OFCommandLine &cmd, OFConsoleApplication &app)
handle the command line options used for logging
OFList< OFString > TestResult
This is the type used for test results.
static const int AF_Exclusive
exclusive option that overrides any other option (e.g. "--help")
OFBool buildTestsToRun(OFCommandLine &cmd, OFList< OFTestTest * > &tests) const
Build a list of tests which should be executed from the command line.
OFIterator< T > erase(OFIterator< T > position)
removes the element at the given position from the list.
void recordFailure(const OFString &file, unsigned long int line, const OFString &message)
Add a new failure to the result set.
void setParamColumn(const int column)
sets default width of parameter column
TestResult results_
The test results, empty for success.
OFBool empty() const
returns true if list is empty.
static OFLogger getLogger(const char *name)
create a new Logger object
void clear()
removes all elements from the list.
virtual ~OFTestTest()
Destructor.
int runTests(const OFList< OFTestTest * > &tests, const char *module)
Run a list of test cases.
support class for console applications.
OFIterator< T > end() const
returns an iterator which points to the past-to-end element of the list.
OFList< OFTestTest * > tests_
List of tests. Statically allocated, so don't have to be freed.
size_t size() const
returns number of elements in the list.
virtual void run()=0
Execute this test case.
parameter is optional, more than one value is allowed (# = 0..n), "[option...]"
The test manager singleton manages the list of available test cases and executes them.
OFTestTest(const OFString &testName, int flag)
Contructor.
OFTestTest * curTest_
Currently running test.
OFTestManager()
Private constructor, this is a singleton!
fatal: very severe error events that will presumably lead the application to abort ...
E_Flags
Special flags that a test can have.
int run(int argc, char *argv[], const char *module)
Handle the given arguments and run the requested test case.
OFIterator< T > begin() const
returns an iterator referencing the first element in the list.
int getParamCount() const
gets number of parameters in the parsed command line.
E_ParamValueStatus getParam(const int pos, OFCmdSignedInt &value)
gets value of specified parameter as signed integer.
static void configure(OFLogger::LogLevel level=OFLogger::WARN_LOG_LEVEL)
set up the logging and enable it
OFBool exhaustive_
Should slow tests be run, too?
A single test case which can be run.
void addTest(OFTestTest *test)
Register a test with this test manager.
OFTestManager & operator=(const OFTestManager &obj)
Private undefined assignment operator.
const OFString & getTestName() const
Slow test which should only be run in exhaustive mode.
simple wrapper around the "low-level" Logger object to make it easier to switch to a different system...
a simple string class that implements a subset of std::string.
const int flags_
Flags that this test has.
handles command line arguments.
const char * c_str() const
returns a pointer to the initial element of an array of length size()+1 whose first size() elements e...
static void addOptions(OFCommandLine &cmd)
add the command line options which configureFromCommandLine() checks for
static OFTestManager & instance()
OFBool testMatches(const OFTestTest *test, const OFString &str) const
Test if the test name matches the given name.
const TestResult & runAndReturn()
Execute this test case.
OFTestTest & currentTest()
void push_back(const T &x)
inserts after the last element of the list.