/usr/include/dcmtk/ofstd/oftest.h is in libdcmtk-dev 3.6.1~20150924-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 | /*
*
* Copyright (C) 2011-2012, OFFIS e.V.
* All rights reserved. See COPYRIGHT file for details.
*
* This code is inspired by quicktest.
* Copyright (C) 2005-2008
* Tyler Streeter (http://www.tylerstreeter.net)
* http://quicktest.sourceforge.net
*
* This software and supporting documentation were developed by
*
* OFFIS e.V.
* R&D Division Health
* Escherweg 2
* D-26121 Oldenburg, Germany
*
*
* Module: ofstd
*
* Author: Uli Schlachter
*
* Purpose: Provide a test framework for the toolkit
*
*/
#ifndef OFTEST_H
#define OFTEST_H
#include "dcmtk/config/osconfig.h"
#include "dcmtk/ofstd/ofconapp.h" /* for OFCommandLine */
#include "dcmtk/ofstd/ofconsol.h" /* for CERR */
#include "dcmtk/ofstd/oflist.h" /* for class OFList */
#include "dcmtk/ofstd/ofstream.h" /* for class OFOStringStream */
#include "dcmtk/ofstd/ofstring.h" /* for class OFString */
#include "dcmtk/ofstd/oftypes.h" /* for OFBool */
#ifdef OFTEST_OFSTD_ONLY
#define OFTEST_LOG_VERBOSE(msg) do { \
if (verbose_) \
COUT << msg << OFendl; \
} while (0)
#else
#include "dcmtk/dcmdata/dcuid.h" /* for dcmtk version name */
#include "dcmtk/oflog/oflog.h"
static OFLogger testLogger = OFLog::getLogger("dcmtk.test");
#define OFTEST_LOG_VERBOSE(msg) OFLOG_INFO(testLogger, msg)
#endif
/** @file oftest.h
* A simple framework for writing and running test cases.
*/
/** A single test case which can be run */
class OFTestTest
{
public:
/// This is the type used for test results.
typedef OFList<OFString> TestResult;
/** Special flags that a test can have. The flags for a test are the result
* of a bitwise or of these individual flags.
*/
enum E_Flags {
EF_None = 0x0,
/// Slow test which should only be run in exhaustive mode.
EF_Slow = 0x1
};
/** Contructor
* @param testName the name of this test case
*/
OFTestTest(const OFString& testName, int flag)
: testName_(testName)
, results_()
, flags_(flag)
{
}
/// Destructor
virtual ~OFTestTest()
{
}
/// @return the flags of this test case
int flags() const { return flags_; }
/// @return the name of this test case
const OFString& getTestName() const { return testName_; }
/** Execute this test case.
* @return Reference to list of errors.
*/
const TestResult& runAndReturn()
{
results_.clear();
run();
return results_;
}
/** Execute this test case.
* @param result the list of error messages generated by this test.
* @see #OFCHECK(condition)
*/
virtual void run() = 0;
/** Add a new failure to the result set.
* @param result list of test failures
* @param file filename for this failure
* @param line line number for this failure
* @param message error description.
*/
void recordFailure(const OFString& file, unsigned long int line, const OFString& message)
{
OFOStringStream oss;
oss << "FAILED test '" << testName_ << "' at " << file << ":" << line
<< ": " << message << OFStringStream_ends;
OFSTRINGSTREAM_GETOFSTRING(oss, str)
results_.push_back(str);
}
private:
/// The unique name of this test.
OFString testName_;
/// The test results, empty for success.
TestResult results_;
/// Flags that this test has.
const int flags_;
};
/** The test manager singleton manages the list of available test cases
* and executes them.
*/
class OFTestManager
{
public:
/// @return the only instance of the test manager
static OFTestManager& instance()
{
static OFTestManager manager;
return manager;
}
/// @return the currently running test case
OFTestTest& currentTest()
{
if (!curTest_)
abort();
return *curTest_;
}
/** Register a test with this test manager.
* @param test the test to register
*/
void addTest(OFTestTest* test)
{
tests_.push_back(test);
}
/** Run a list of test cases. The results will be printed on the console.
* @param tests tests to execute
*/
int runTests(const OFList<OFTestTest*>& tests, const char *module)
{
unsigned int numFailed = 0;
OFListConstIterator(OFTestTest*) it;
OFString mod_str = module ? " for module '" + OFString(module) + "'" : "";
OFTEST_LOG_VERBOSE("Running " << tests.size() << " tests" << mod_str << ":");
for (it = tests.begin(); it != tests.end(); ++it)
{
OFTEST_LOG_VERBOSE(" Running test '" << (*it)->getTestName() << "'...");
curTest_ = *it;
const OFTestTest::TestResult& result = (*it)->runAndReturn();
curTest_ = NULL;
if (!result.empty())
{
numFailed++;
OFListConstIterator(OFString) rit;
for (rit = result.begin(); rit != result.end(); ++rit)
{
// recordFailure() already formatted the message
CERR << *rit << OFendl;
}
}
}
COUT << "Test results" << mod_str << ": "
<< tests.size() - numFailed << " succeeded, "
<< numFailed << " failed." << OFendl;
/* Only the lowest 8 bit of the exit code can be used! */
if (numFailed > 254)
{
CERR << "WARNING: More than 254 tests failed!" << OFendl;
return 254;
}
return numFailed;
}
/** Handle the given arguments and run the requested test case. This
* function should be used for implementing main().
* @param argc number of arguments
* @param argv list of arguments
* @param module name of the module that we are testing for
* @return 0 in case of success, else an error value.
*/
int run(int argc, char* argv[], const char* module)
{
OFList<OFTestTest*> testsToRun;
OFBool listOnly = OFFalse;
OFString rcsid;
#ifdef OFTEST_OFSTD_ONLY
// No proper rcsid because the necessary defines are in dcmdata
if (module != NULL)
rcsid = "$dcmtk: " + OFString(module) + " $";
#else
rcsid = "$dcmtk: ";
rcsid += OFSTRING_GUARD(module);
rcsid += " v" OFFIS_DCMTK_VERSION " " OFFIS_DCMTK_RELEASEDATE " $";
#endif
OFConsoleApplication app("tests", "Run the test suite", rcsid.c_str());
OFCommandLine cmd;
cmd.setParamColumn(13);
cmd.addParam("tests-to-run", "names of tests to run (default: all)", OFCmdParam::PM_MultiOptional);
cmd.addGroup("general options:");
cmd.addOption("--help", "-h", "print this help text and exit", OFCommandLine::AF_Exclusive);
cmd.addOption("--list", "-l", "list available tests and exit", OFCommandLine::AF_Exclusive);
cmd.addOption("--exhaustive", "-x", "also run extensive and slow tests");
#ifdef OFTEST_OFSTD_ONLY
cmd.addOption("--verbose", "-v", "verbose mode, print processing details");
#else
OFLog::addOptions(cmd);
#endif
/* evaluate command line */
if (app.parseCommandLine(cmd, argc, argv))
{
/* check exclusive options first */
}
#ifdef OFTEST_OFSTD_ONLY
if (cmd.findOption("--verbose")) verbose_ = OFTrue;
#else
/* We disable warnings and errors by default since some tests cause
* such messages by testing corner cases. */
OFLog::configureFromCommandLine(cmd, app, OFLogger::FATAL_LOG_LEVEL);
#endif
if (cmd.findOption("--exhaustive")) exhaustive_ = OFTrue;
if (cmd.findOption("--list")) listOnly = OFTrue;
if (!buildTestsToRun(cmd, testsToRun))
return -1;
if (testsToRun.empty())
{
CERR << "No tests to run!" << OFendl;
return 0;
}
if (listOnly)
{
OFListIterator(OFTestTest*) it;
COUT << "There are " << testsToRun.size() << " tests";
if (module)
COUT << " for module '" << module << "'";
COUT << ":" << OFendl;
for (it = testsToRun.begin(); it != testsToRun.end(); ++it)
{
COUT << " " << (*it)->getTestName() << "\n";
}
return 0;
}
return runTests(testsToRun, module);
}
private:
/// Private constructor, this is a singleton!
OFTestManager()
: tests_()
, curTest_(NULL)
, exhaustive_(OFFalse)
#ifdef OFTEST_OFSTD_ONLY
, verbose_(OFFalse)
#endif
{
}
/// Private undefined copy constructor
OFTestManager(const OFTestManager& obj);
/// Private undefined assignment operator
OFTestManager& operator=(const OFTestManager& obj);
/** Build a list of tests which should be executed from the command line.
* @param cmd command line arguments which should be parsed
* @param tests will be set to the list of tests to run
* @return OFFalse if the command line could not be handled.
*/
OFBool buildTestsToRun(OFCommandLine& cmd, OFList<OFTestTest*>& tests) const
{
const int paramCount = cmd.getParamCount();
OFString paramString;
OFBool result = OFTrue;
if (paramCount == 0)
{
// If no arguments are given, run all possible tests
tests = tests_;
}
else
{
for (int i = 1; i <= paramCount; i++)
{
cmd.getParam(i, paramString);
// Find all tests matching this argument
OFBool found = OFFalse;
OFListConstIterator(OFTestTest*) it;
for (it = tests_.begin(); it != tests_.end(); ++it)
{
if (testMatches(*it, paramString))
{
tests.push_back(*it);
found = OFTrue;
}
}
if (!found)
{
CERR << "Error: No test matches '" << paramString << "'" << OFendl;
result = OFFalse;
}
}
}
// If we are not in exhaustive mode, remove all slow tests
if (!exhaustive_)
{
OFListIterator(OFTestTest*) it = tests.begin();
while (it != tests.end())
{
if ((*it)->flags() & OFTestTest::EF_Slow)
it = tests.erase(it);
else
++it;
}
}
return result;
}
/** Test if the test name matches the given name. This function supports '?'
* and '*' for wildcards. However, '*' can only be used at the end of string.
* @param test test to check against
* @param str the string describing the tests
* @return OFTrue if we found a match, else OFFalse
*/
OFBool testMatches(const OFTestTest* test, const OFString& str) const
{
const char* testName = test->getTestName().c_str();
const char* string = str.c_str();
for (; *testName != '\0' && *string != '\0'; testName++, string++)
{
// Does the string still match?
if (string[0] != '?' && testName[0] != string[0])
break;
}
// Is this a wildcard?
// So far we only support '*' at the end of the string
if (string[0] == '*' && string[1] == '\0')
return OFTrue;
// If both strings reached their end, we have a match
if (testName[0] == '\0' && string[0] == '\0')
return OFTrue;
return OFFalse;
}
/// List of tests. Statically allocated, so don't have to be freed.
OFList<OFTestTest*> tests_;
/// Currently running test.
OFTestTest* curTest_;
/// Should slow tests be run, too?
OFBool exhaustive_;
#ifdef OFTEST_OFSTD_ONLY
/// Are we running in verbose mode? Only used if oflog is not available.
OFBool verbose_;
#endif
};
/** Implement a main() function for running tests. The main function will return
* the number of failed tests or -1 if an invalid test name was given.
* @param module the name of the module which is under test.
*/
#define OFTEST_MAIN(module) \
int main(int argc, char* argv[]) \
{ \
return OFTestManager::instance().run(argc, argv, module); \
}
/// Internal macro for generating a class definition, don't use yourself!
#define OFTEST_CLASS(testName) \
class OFTest ## testName : public OFTestTest \
{ \
public: \
OFTest ## testName(); \
void run(); \
}
/** Register a test to the test manager. Normally you should use
* OFTEST_REGISTER instead, but that macro doesn't work if OFTEST and
* OFTEST_REGISTER are called in the same source file.
* @param testName name of the test to register
*/
#define OFTEST_REGISTER_INT(testName) \
OFTest ## testName OFTest ## testName ## instance
/** Register a test to the test manager.
* @param testName name of the test to register
*/
#define OFTEST_REGISTER(testName) \
OFTEST_CLASS(testName); \
OFTEST_REGISTER_INT(testName)
/** Macro to define a new test case. Internally this defines a new class
* inheriting from OFTest.
* This is equivalent to OFTEST_FLAGS(testName, EF_None).
* @param testName name describing the test
* @see OFTEST_FLAGS
*/
#define OFTEST(testName) OFTEST_FLAGS(testName, EF_None)
/** Macro to define a new test case. Internally this defines a new class
* inheriting from OFTest.
* @param flags flags that should be set for this test
* @param testName name describing the test
* @see OFTEST
*/
#define OFTEST_FLAGS(testName, flags) \
OFTEST_CLASS(testName); \
OFTest ## testName::OFTest ## testName() \
: OFTestTest(#testName, flags) \
{ \
OFTestManager::instance().addTest(this); \
} \
void OFTest ## testName ::run()
/** @name macros for checking conditions in tests
* These macros can be used for doing various checks in test cases. In case
* their check fails, they emit a descriptive message explaining the problem.
*/
//@{
/** Check if a condition is true. Can only be used inside OFTEST().
* @param condition condition to check
*/
#define OFCHECK(condition) \
do { \
if (!(condition)) \
OFTestManager::instance().currentTest().recordFailure(__FILE__, __LINE__, #condition); \
} while (0)
/** Check if two values are equal. Can only be used inside OFTEST(). Both
* arguments must be compatible with OFOStringStream's operator<<.
* @param val1 first value to compare
* @param val2 second value to compare
*/
#define OFCHECK_EQUAL(val1, val2) \
do { \
if ((val1) != (val2)) { \
OFOStringStream oss___; \
oss___ << "(" << (val1) << ") should equal (" << (val2) << ")" << OFStringStream_ends; \
OFSTRINGSTREAM_GETOFSTRING(oss___, str___) \
OFTestManager::instance().currentTest().recordFailure(__FILE__, __LINE__, str___); \
} \
} while (0)
/** Unconditionally add a failure
* @param message string describing the failure
*/
#define OFCHECK_FAIL(message) \
do { \
OFOStringStream oss___; \
oss___ << message << OFStringStream_ends; \
OFSTRINGSTREAM_GETOFSTRING(oss___, str___) \
OFTestManager::instance().currentTest().recordFailure(__FILE__, __LINE__, str___); \
} while (0)
//@}
#endif
|