| 1 | /* |
| 2 | Copyright (C) 2004 by Intra2net AG |
| 3 | |
| 4 | The software in this package is distributed under the GNU General |
| 5 | Public License version 2 (with a special exception described below). |
| 6 | |
| 7 | A copy of GNU General Public License (GPL) is included in this distribution, |
| 8 | in the file COPYING.GPL. |
| 9 | |
| 10 | As a special exception, if other files instantiate templates or use macros |
| 11 | or inline functions from this file, or you compile this file and link it |
| 12 | with other works to produce a work based on this file, this file |
| 13 | does not by itself cause the resulting work to be covered |
| 14 | by the GNU General Public License. |
| 15 | |
| 16 | However the source code for this file must still be made available |
| 17 | in accordance with section (3) of the GNU General Public License. |
| 18 | |
| 19 | This exception does not invalidate any other reasons why a work based |
| 20 | on this file might be covered by the GNU General Public License. |
| 21 | */ |
| 22 | #include <iostream> |
| 23 | #include <iomanip> |
| 24 | #include <string> |
| 25 | |
| 26 | #include <time.h> |
| 27 | #include <sys/timeb.h> |
| 28 | |
| 29 | #include <cppunit/extensions/TestFactoryRegistry.h> |
| 30 | #include <cppunit/ui/text/TestRunner.h> |
| 31 | #include <cppunit/TestListener.h> |
| 32 | #include <cppunit/TestFailure.h> |
| 33 | #include <cppunit/TestResult.h> |
| 34 | #include <cppunit/CompilerOutputter.h> |
| 35 | |
| 36 | class VerboseTimingListener : public CppUnit::TestListener |
| 37 | { |
| 38 | private: |
| 39 | double start_time; |
| 40 | std::string resultstr; |
| 41 | |
| 42 | double get_time(void) |
| 43 | { |
| 44 | struct timeb tb; |
| 45 | ftime(&tb); |
| 46 | return tb.time+(static_cast<float>(tb.millitm)/1000); |
| 47 | } |
| 48 | |
| 49 | public: |
| 50 | |
| 51 | void startTest( CppUnit::Test *test ) |
| 52 | { |
| 53 | resultstr="OK"; |
| 54 | std::cout << test->getName() << ": "; |
| 55 | start_time=get_time(); |
| 56 | } |
| 57 | |
| 58 | void endTest( CppUnit::Test *test ) |
| 59 | { |
| 60 | double timediff=get_time()-start_time; |
| 61 | |
| 62 | // fix clock unpreciseness for small timespans |
| 63 | if (timediff < 0) timediff=0; |
| 64 | |
| 65 | std::cout << resultstr << " (" |
| 66 | << std::fixed << std::setprecision(3) |
| 67 | << timediff << " sec)" << std::endl; |
| 68 | } |
| 69 | |
| 70 | void addFailure(const CppUnit::TestFailure &failure) |
| 71 | { |
| 72 | if(failure.isError()) |
| 73 | resultstr="ERROR"; |
| 74 | else |
| 75 | resultstr="FAIL"; |
| 76 | } |
| 77 | }; |
| 78 | |
| 79 | int main(int argc, char **argv) |
| 80 | { |
| 81 | CppUnit::TextTestRunner runner; |
| 82 | CppUnit::TestFactoryRegistry ®istry = CppUnit::TestFactoryRegistry::getRegistry(); |
| 83 | |
| 84 | // set output format that KDevelop can catch errors |
| 85 | CppUnit::CompilerOutputter *op=CppUnit::CompilerOutputter::defaultOutputter(&runner.result(),std::cout); |
| 86 | op->setLocationFormat("%p:%l: error: "); |
| 87 | runner.setOutputter(op); |
| 88 | |
| 89 | // show every test with timing |
| 90 | VerboseTimingListener listener; |
| 91 | runner.eventManager().addListener(&listener); |
| 92 | |
| 93 | runner.addTest(registry.makeTest()); |
| 94 | |
| 95 | // run all tests in registry (not using the default progress listener) |
| 96 | bool wasSucessful = runner.run("",false,true,false); |
| 97 | |
| 98 | return (wasSucessful ? 0 : 1); |
| 99 | } |