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