| 1 | /*************************************************************************** |
| 2 | * Copyright (C) 2004 by Intra2net AG * |
| 3 | * info@intra2net.com * |
| 4 | * * |
| 5 | ***************************************************************************/ |
| 6 | |
| 7 | #include <iostream> |
| 8 | #include <iomanip> |
| 9 | #include <string> |
| 10 | |
| 11 | #include <time.h> |
| 12 | #include <sys/timeb.h> |
| 13 | |
| 14 | #include <cppunit/extensions/TestFactoryRegistry.h> |
| 15 | #include <cppunit/ui/text/TestRunner.h> |
| 16 | #include <cppunit/TestListener.h> |
| 17 | #include <cppunit/TestFailure.h> |
| 18 | #include <cppunit/TestResult.h> |
| 19 | #include <cppunit/CompilerOutputter.h> |
| 20 | |
| 21 | class VerboseTimingListener : public CppUnit::TestListener |
| 22 | { |
| 23 | private: |
| 24 | double start_time; |
| 25 | std::string resultstr; |
| 26 | |
| 27 | double get_time(void) |
| 28 | { |
| 29 | struct timeb tb; |
| 30 | ftime(&tb); |
| 31 | return tb.time+(static_cast<float>(tb.millitm)/1000); |
| 32 | } |
| 33 | |
| 34 | public: |
| 35 | |
| 36 | void startTest( CppUnit::Test *test ) |
| 37 | { |
| 38 | resultstr="OK"; |
| 39 | std::cout << test->getName() << ": "; |
| 40 | start_time=get_time(); |
| 41 | } |
| 42 | |
| 43 | void endTest( CppUnit::Test *test ) |
| 44 | { |
| 45 | double timediff=get_time()-start_time; |
| 46 | |
| 47 | // fix clock unpreciseness for small timespans |
| 48 | if (timediff < 0) timediff=0; |
| 49 | |
| 50 | std::cout << resultstr << " (" |
| 51 | << std::fixed << std::setprecision(3) |
| 52 | << timediff << " sec)" << std::endl; |
| 53 | } |
| 54 | |
| 55 | void addFailure(const CppUnit::TestFailure &failure) |
| 56 | { |
| 57 | if(failure.isError()) |
| 58 | resultstr="ERROR"; |
| 59 | else |
| 60 | resultstr="FAIL"; |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | int main(int argc, char **argv) |
| 65 | { |
| 66 | CppUnit::TextTestRunner runner; |
| 67 | CppUnit::TestFactoryRegistry ®istry = CppUnit::TestFactoryRegistry::getRegistry(); |
| 68 | |
| 69 | // set output format that KDevelop can catch errors |
| 70 | CppUnit::CompilerOutputter *op=CppUnit::CompilerOutputter::defaultOutputter(&runner.result(),std::cout); |
| 71 | op->setLocationFormat("%p:%l: error: "); |
| 72 | runner.setOutputter(op); |
| 73 | |
| 74 | // show every test with timing |
| 75 | VerboseTimingListener listener; |
| 76 | runner.eventManager().addListener(&listener); |
| 77 | |
| 78 | runner.addTest(registry.makeTest()); |
| 79 | |
| 80 | // run all tests in registry (not using the default progress listener) |
| 81 | bool wasSucessful = runner.run("",false,true,false); |
| 82 | |
| 83 | return (wasSucessful ? 0 : 1); |
| 84 | } |