Salome HOME
refs #1341: debug of automatic tests
[modules/hydro.git] / src / HYDRO_tests / TestLib_Listener.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include <TestLib_Listener.h>
20 #include <cppunit/Test.h>
21 #include <cppunit/TestFailure.h>
22 #include <cppunit/SourceLine.h>
23 #include <iostream>
24 #include <QStringList>
25
26 #ifdef WIN32
27   #include <windows.h>
28 #endif
29
30 #ifdef WIN32
31 /**
32   Convert the given time structure to milliseconds number
33   @param theTime the time structure to convert
34   @return the calculated number of milliseconds
35 */
36 INT64 GetMilliseconds( const FILETIME& theTime )
37 {
38   SYSTEMTIME aTime;
39   FileTimeToSystemTime( &theTime, &aTime );
40   INT64 aRes = 0;
41   aRes += aTime.wHour * ( 3600 * 1000 );
42   aRes += aTime.wMinute * ( 60 * 1000 );
43   aRes += aTime.wSecond * ( 1000 );
44   aRes += aTime.wMilliseconds;
45   return aRes;
46 }
47 #endif
48
49 /**
50   Calculate the complete execution time for the current thread 
51   @return the calculated time as the number of milliseconds
52 */
53 INT64 GetTimeForCurrentThreadInMs()
54 {
55 #ifdef WIN32
56   FILETIME aCreationTime, anExitTime, aKernelTime, aUserTime;
57   GetThreadTimes( GetCurrentThread(), &aCreationTime, &anExitTime, &aKernelTime, &aUserTime );
58   INT64 aKernelMs = GetMilliseconds( aKernelTime );
59   INT64 aUserMs = GetMilliseconds( aUserTime );
60   return aKernelMs + aUserMs;
61 #else
62   struct timespec aTime;
63   clock_gettime( CLOCK_MONOTONIC, &aTime );
64   INT64 aSec = aTime.tv_sec;
65   aSec *= 1000;
66   aSec += aTime.tv_nsec / 1000000;
67   return aSec;
68 #endif
69 }
70
71 /**
72   \brief Constructor
73 */
74 TestLib_Listener::TestLib_Listener()
75 : myStart( 0 ), myComplete( 0 ), myNbTests( 0 ), myNbSuites( 0 )
76 {
77 }
78
79 /**
80   \brief Destructor
81 */
82 TestLib_Listener::~TestLib_Listener()
83 {
84 }
85
86 /**
87   \brief Clear the internal state of the listener
88 */
89 void TestLib_Listener::Clear()
90 {
91   myStart = 0;
92   myComplete = 0;
93 }
94
95 /**
96   Get complete time of all tests execution in milliseconds
97   @return complete time in milliseconds
98 */
99 INT64 TestLib_Listener::GetCompleteTimeInMS() const
100 {
101   return myComplete;
102 }
103
104 /**
105   Handler for test start
106   @param theTest the started tests
107 */
108 void TestLib_Listener::startTest( CppUnit::Test* theTest )
109 {
110   std::string aName;
111   if( theTest )
112     aName = theTest->getName();
113
114   std::cout << aName << "...";
115   myStart = GetTimeForCurrentThreadInMs();
116 }
117
118 /**
119   Handler for test finish
120   @param theTest the finished tests
121 */
122 void TestLib_Listener::endTest( CppUnit::Test* theTest )
123 {
124   INT64 aCurTime = GetTimeForCurrentThreadInMs();
125   INT64 anExecTimeMS = aCurTime - myStart;
126   myComplete += anExecTimeMS;
127   std::cout << " " << anExecTimeMS << " ms" << std::endl;
128
129   myNbTests++;
130 }
131
132 int TestLib_Listener::GetNbTests() const
133 {
134   return myNbTests;
135 }
136
137 int TestLib_Listener::GetNbSuites() const
138 {
139   return myNbSuites;
140 }
141
142 /**
143   Handler for test suite start
144   @param theTest the started test suite
145 */
146 void TestLib_Listener::startSuite( CppUnit::Test* theTest )
147 {
148 }
149
150 /**
151   Handler for test suite finish
152   @param theTest the finished test suite
153 */
154 void TestLib_Listener::endSuite( CppUnit::Test* theTest )
155 {
156   myNbSuites++;
157 }
158
159 /**
160   Handler for test failure
161   @param theFailure the failure information
162 */
163 void TestLib_Listener::addFailure( const CppUnit::TestFailure& theFailure )
164 {
165   std::string failedTest = theFailure.failedTest()->getName();
166   CppUnit::SourceLine failedLocation = theFailure.sourceLine();
167
168   QString aFile = QString::fromStdString( failedLocation.fileName() );
169   aFile.replace( '\\', '/' );
170   QStringList parts = aFile.split( '/' );
171   aFile = parts.last();
172   std::string cFile = aFile.toStdString();
173
174   int aLine = failedLocation.lineNumber();
175
176   static char aBuf[1024];
177   sprintf( aBuf, "Failed %s: %s : %i", failedTest.c_str(), cFile.c_str(), aLine );
178
179   myFailures.push_back( aBuf );
180 }
181
182 void TestLib_Listener::DumpFailures()
183 {
184   int n = (int)myFailures.size();
185   printf( "FAILED TESTS: %i\n", n );
186   for( int i=0; i<n; i++ )
187     printf( "  %s\n", myFailures[i].c_str() );
188 }