47 #include "Teuchos_Assert.hpp" 48 #include "Teuchos_VerboseObject.hpp" 50 #include "Teuchos_Assert.hpp" 52 #include "Teuchos_StandardCatchMacros.hpp" 61 std::string groupName;
67 const std::string groupName_in,
68 const std::string testName_in
70 : unitTest(unitTest_in), groupName(groupName_in), testName(testName_in),
71 insertionIndex(insersionIndexCounter_++)
80 static int insersionIndexCounter_;
84 int UnitTestData::insersionIndexCounter_ = 0;
87 bool operator<(
const UnitTestData &a,
const UnitTestData &b)
89 if (a.groupName < b.groupName) {
92 else if (a.groupName > b.groupName) {
95 return a.insertionIndex < b.insertionIndex;
100 std::string getUnitTestName(
const std::string groupName,
101 const std::string testName)
103 std::ostringstream oss;
104 oss << groupName<<
"_"<<testName<<
"_UnitTest";
109 enum EShowTestDetails {
110 SHOW_TEST_DETAILS_ALL,
111 SHOW_TEST_DETAILS_TEST_NAMES,
112 SHOW_TEST_DETAILS_FINAL_RESULTS
116 bool strMatch(
const std::string &fullMatchStr,
const std::string &str )
119 const std::string::size_type npos = std::string::npos;
121 const int strLen = str.length();
122 const int fullMatchStrLen = fullMatchStr.length();
124 if (fullMatchStrLen == 0) {
128 const bool beginGlob = fullMatchStr[0] ==
'*';
129 const bool endGlob = fullMatchStr[fullMatchStrLen-1] ==
'*';
131 const int matchStrLen =
132 fullMatchStrLen + (beginGlob ? -1 : 0) + (endGlob ? -1 : 0);
134 if (matchStrLen == 0) {
138 if (matchStrLen > strLen) {
142 if (beginGlob && endGlob) {
143 return str.find(fullMatchStr.substr(1, matchStrLen)) != npos;
147 return fullMatchStr.substr(0, matchStrLen) == str.substr(0, matchStrLen);
151 return fullMatchStr.substr(1, matchStrLen) ==
152 str.substr(strLen-matchStrLen, matchStrLen);
155 return fullMatchStr == str;
171 class UnitTestRepository::InstanceData {
176 unitTests_t unitTests;
177 CommandLineProcessor clp;
178 EShowTestDetails showTestDetails;
179 bool globallyReduceUnitTestResult;
180 bool showSrcLocation;
181 bool showFailSrcLocation;
183 std::string groupName;
184 std::string testName;
185 std::string notUnitTestName;
190 showTestDetails(SHOW_TEST_DETAILS_TEST_NAMES),
191 globallyReduceUnitTestResult(false),
192 showSrcLocation(false),
193 showFailSrcLocation(true),
206 return getData().clp;
211 const bool globallyReduceUnitTestResult)
213 getData().globallyReduceUnitTestResult = globallyReduceUnitTestResult;
219 return getData().globallyReduceUnitTestResult;
228 using std::setprecision;
230 Time overallTimer(
"overallTimer",
true);
233 const int timerPrec = 3;
235 out <<
"\n***\n*** Unit test suite ...\n***\n\n";
237 InstanceData &data = getData();
239 const bool showAll = data.showTestDetails == SHOW_TEST_DETAILS_ALL;
240 const bool showTestNames = data.showTestDetails == SHOW_TEST_DETAILS_TEST_NAMES || showAll;
247 int numTestsFailed = 0;
253 out <<
"\nSorting tests by group name then by the order they were added ...";
255 std::sort( data.unitTests.begin(), data.unitTests.end() );
257 out <<
" (time = "<<setprecision(timerPrec)<<timer.
totalElapsedTime()<<
")\n";
259 out <<
"\nRunning unit tests ...\n\n";
260 unitTests_t::iterator iter = data.unitTests.begin();
261 for ( ; iter != data.unitTests.end(); ++iter, ++testCounter ) {
263 const UnitTestData &utd = (*iter);
265 const std::string unitTestName = getUnitTestName(utd.groupName, utd.testName);
269 strMatch(data.groupName, utd.groupName)
271 strMatch(data.testName, utd.testName)
275 data.notUnitTestName.length() == 0
277 !strMatch(data.notUnitTestName, unitTestName)
284 std::ostringstream testHeaderOSS;
285 testHeaderOSS <<testCounter<<
". "<<unitTestName<<
" ... ";
286 const std::string testHeader = testHeaderOSS.str();
292 out <<testHeader<<std::flush;
300 localOut = rcpFromRef(out);
303 oss =
rcp(
new std::ostringstream);
304 localOut = fancyOStream(rcp_implicit_cast<std::ostream>(oss));
312 const bool result = runUnitTestImpl(*utd.unitTest, *localOut);
320 out <<testHeader<<
"\n"<<std::flush;
330 <<
" "<<unitTestName<<
"\n" 331 <<
"Location: "<<utd.unitTest->unitTestFile()<<
":" 332 <<utd.unitTest->unitTestFileLineNumber()<<
"\n";
348 if (showAll && data.showSrcLocation)
350 <<
"Location: "<<utd.unitTest->unitTestFile()<<
":" 351 <<utd.unitTest->unitTestFileLineNumber()<<
"\n";
359 out <<
"[Not Run]\n";
374 if (failedTests.
size()) {
375 out <<
"\nThe following tests FAILED:\n";
376 for (Teuchos_Ordinal i = 0; i < failedTests.
size(); ++i)
377 out <<
" " << failedTests[i] <<
"\n";
381 out <<
"\nTotal Time: " << setprecision(timerPrec)
385 <<
"\nSummary: total = " << testCounter
386 <<
", run = " << numTestsRun;
390 <<
", passed = " << (numTestsRun-numTestsFailed)
391 <<
", failed = " << numTestsFailed <<
"\n";
396 <<
", failed = ???\n";
410 setUpCLP(outArg(clp));
412 clp.
parse(argc,argv);
414 *out <<
"\nEnd Result: TEST FAILED" << std::endl;
418 const bool success = runUnitTests(*out);
421 *out <<
"\nEnd Result: TEST PASSED" << std::endl;
423 *out <<
"\nEnd Result: TEST FAILED" << std::endl;
427 return (success ? 0 : 1);
433 const std::string groupName,
const std::string testName_in )
435 InstanceData &data = getData();
436 std::string testName = testName_in;
437 data.unitTests.push_back(UnitTestData(unitTest, groupName, testName));
443 return (getData().showTestDetails == SHOW_TEST_DETAILS_ALL);
450 UnitTestRepository::UnitTestRepository()
457 clp->addOutputSetupOptions(
true);
459 const int numShowTestDetails = 3;
460 const EShowTestDetails showTestDetailsValues[numShowTestDetails] =
461 { SHOW_TEST_DETAILS_ALL,
462 SHOW_TEST_DETAILS_TEST_NAMES,
463 SHOW_TEST_DETAILS_FINAL_RESULTS
465 const char* showTestDetailsNames[numShowTestDetails] =
471 "show-test-details", &getData().showTestDetails,
472 numShowTestDetails, showTestDetailsValues, showTestDetailsNames,
473 "Level of detail to show in the tests" 476 "details", &getData().showTestDetails,
477 numShowTestDetails, showTestDetailsValues, showTestDetailsNames,
478 "Short for --show-test-details" 482 "show-src-location",
"no-show-src-location", &getData().showSrcLocation,
483 "If true, then the location of the unit test source code is shown." 484 " Only meaningfull if --show-test-details=ALL." 488 "show-fail-src-location",
"no-show-fail-src-location", &getData().showFailSrcLocation,
489 "If true, then the location of every failed unit test check is printed." 493 "globally-reduce-test-result",
"no-globally-reduce-test-result",
494 &getData().globallyReduceUnitTestResult,
495 "If true, individual unit test pass/fail is globally reduced across MPI processes." 499 "group-name", &getData().groupName,
500 "If specified, selects only tests that match the group name glob." );
502 "group", &getData().groupName,
503 "Short for --group-name." );
506 "test-name", &getData().testName,
507 "If specified, selects only tests that match the test name glob." );
509 "test", &getData().testName,
510 "Short for --test-name." );
513 "not-unit-test", &getData().notUnitTestName,
514 "If specified, full unit tests with glob matches will *not* be run." );
517 "no-op",
"do-op", &getData().noOp,
518 "If --no-op, then only the names of the tests that would be run are run." 524 UnitTestRepository::InstanceData& UnitTestRepository::getData()
526 static UnitTestRepository::InstanceData data;
531 bool UnitTestRepository::runUnitTestImpl(
const UnitTestBase &unitTest,
535 if (getData().globallyReduceUnitTestResult) {
537 if (globalSum == 0) {
546 out <<
"NOTE: Global reduction shows failures on other processes!\n" 547 <<
"(rerun with --output-to-root-rank-only=-1 to see output\n" 548 <<
"from other processes to see what process failed!)\n";
558 for (
int proc_k = 0; proc_k < numProcs; ++proc_k ) {
559 if (passFailFlags[proc_k] != 0) {
566 if (procsThatFailed.
size() == numProcs) {
567 out <<
"NOTE: Unit test failed on all processes!\n";
572 out <<
"NOTE: Unit test failed on processes = " << procsThatFailed <<
"\n" 573 <<
"(rerun with --output-to-root-rank-only=<procID> to see output\n" 574 <<
"from individual processes where the unit test is failing!)\n";
bool is_null(const boost::shared_ptr< T > &p)
Returns true if p.get()==NULL.
static bool verboseUnitTests()
Returns if unit tests are verbose or not.
static int getNProc()
Returns the number of processors relative to MPI_COMM_WORLD
static CommandLineProcessor & getCLP()
Return the CLP to add options to.
Basic wall-clock timer class.
Tabbing class for helping to create formated, indented output for a basic_FancyOStream object...
static void addUnitTest(UnitTestBase *unitTest, const std::string groupName, const std::string testName)
Add an unit test (called indirectly through macros.
void showTestFailureLocation(bool)
Set if TEUCHOS_PASS_FAIL(...) should print test failure location.
static int runUnitTestsFromMain(int argc, char *argv[])
Run the unit tests from main() passing in (argc, argv).
void start(bool reset=false)
Start the timer, if the timer is enabled (see disable()).
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
double stop()
Stop the timer, if the timer is enabled (see disable()).
std::ostream subclass that performs the magic of indenting data sent to an std::ostream object among ...
Ptr< T > ptr() const
Get a safer wrapper raw C++ pointer to the underlying object.
static RCP< FancyOStream > getDefaultOStream()
Get the default output stream object.
EParseCommandLineReturn parse(int argc, char *argv[], std::ostream *errout=&std::cerr) const
Parse a command line.
static void setGloballyReduceTestResult(const bool globallyReduceUnitTestResult)
Set if the unit tests should reduce pass/fail across processes.
bool runUnitTest(FancyOStream &out) const
static int sum(int localVal)
Sum a set of integers across processes.
EParseCommandLineReturn
Return value for CommandLineProcessor::parse(). Note: These enums are all given non-negative values s...
Templated array class derived from the STL std::vector.
Utilities to make writing tests easier.
static bool runUnitTests(FancyOStream &out)
Run the registered unit tests.
void push_back(const value_type &x)
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos, as well as a number of utility routines.
Basic command line parser for input from (argc,argv[])
static bool getGloballyReduceTestResult()
Get if the unit tests should reduce across processes or not.
double totalElapsedTime(bool readCurrentTime=false) const
The total time in seconds accumulated by this timer.
static void allGather(int localVal, const ArrayView< int > &allVals)
Global all-to-all of a set of integers across processes.
void printFinalTimerSummary(const Ptr< std::ostream > &out=null)
Call to print timers so that they don't get printed in the destructor.
#define TEUCHOS_STANDARD_CATCH_STATEMENTS(VERBOSE, ERR_STREAM, SUCCESS_FLAG)
Simple macro that catches and reports standard exceptions and other exceptions.
#define TEUCHOS_ASSERT(assertion_test)
This macro is throws when an assert fails.
#define TEUCHOS_ASSERT_EQUALITY(val1, val2)
This macro is checks that to numbers are equal and if not then throws an exception with a good error ...
Class that helps parse command line input arguments from (argc,argv[]) and set options.
Simple wrapper class for raw pointers to single objects where no persisting relationship exists...