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