xref: /haiku/src/tools/cppunit/TestListener.cpp (revision 1e60bdeab63fa7a57bc9a55b032052e95a18bd2c)
1 #include <TestListener.h>
2 
3 #include <cppunit/Exception.h>
4 #include <cppunit/Test.h>
5 #include <cppunit/TestFailure.h>
6 #include <iostream>
7 #include <stdio.h>
8 #include <OS.h>
9 
10 using std::cout;
11 using std::endl;
12 
13 _EXPORT
14 void
15 BTestListener::startTest( CppUnit::Test *test ) {
16    	fOkay = true;
17    	cout << test->getName() << endl;
18    	startTime = real_time_clock_usecs();
19 }
20 
21 _EXPORT
22 void
23 BTestListener::addFailure( const CppUnit::TestFailure &failure ) {
24    	fOkay = false;
25    	cout << "  - ";
26    	cout << (failure.isError() ? "ERROR" : "FAILURE");
27    	cout << " -- ";
28    	cout << (failure.thrownException() != NULL
29    	           ? failure.thrownException()->what()
30    	             : "(unknown error)");
31    	cout << endl;
32 }
33 
34 _EXPORT
35 void
36 BTestListener::endTest( CppUnit::Test *test )  {
37 	bigtime_t length = real_time_clock_usecs() - startTime;
38    	if (fOkay)
39    		cout << "  + PASSED" << endl;
40 //   	else
41 //   		cout << "  - FAILED" << endl;
42 	printTime(length);
43    	cout << endl;
44 }
45 
46 _EXPORT
47 void
48 BTestListener::printTime(bigtime_t time) {
49 	// Print out the elapsed time all pretty and stuff:
50 	// time >= 1 minute: HH:MM:SS
51 	// 1 minute > time:  XXX ms
52 	const bigtime_t oneMillisecond = 1000;
53 	const bigtime_t oneSecond = oneMillisecond*1000;
54 	const bigtime_t oneMinute = oneSecond*60;
55 	const bigtime_t oneHour = oneMinute*60;
56 	const bigtime_t oneDay = oneHour*24;
57 	if (time >= oneDay) {
58 		cout << "    Your test ran for longer than an entire day. Honestly," << endl;
59 		cout << "    that's 24 hours. That's a long time. Please write shorter" << endl;
60 		cout << "    tests. Clock time: " << time << " microseconds." << endl;
61 	} else {
62 		cout << "    Clock time: ";
63 		if (time >= oneMinute) {
64 			bool begun = true;
65 			if (begun || time >= oneHour) {
66 				begun = true;
67 				cout.width(2);
68 				cout.fill('0');
69 				cout << time / oneHour << ":";
70 				time %= oneHour;
71 			}
72 			if (begun || time >= oneMinute) {
73 				begun = true;
74 				cout.width(2);
75 				cout.fill('0');
76 				cout << time / oneMinute << ":";
77 				time %= oneMinute;
78 			}
79 			if (begun || time >= oneSecond) {
80 				begun = true;
81 				cout.width(2);
82 				cout.fill('0');
83 				cout << time / oneSecond;
84 				time %= oneSecond;
85 			}
86 		} else {
87 			cout << time / oneMillisecond << " ms";
88 		}
89 		cout << endl;
90 	}
91 }
92