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