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.
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.
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
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include <TestLib_Listener.h>
20 #include <cppunit/Test.h>
21 #include <cppunit/TestFailure.h>
22 #include <cppunit/SourceLine.h>
24 #include <QStringList>
32 Convert the given time structure to milliseconds number
33 @param theTime the time structure to convert
34 @return the calculated number of milliseconds
36 INT64 GetMilliseconds( const FILETIME& theTime )
39 FileTimeToSystemTime( &theTime, &aTime );
41 aRes += aTime.wHour * ( 3600 * 1000 );
42 aRes += aTime.wMinute * ( 60 * 1000 );
43 aRes += aTime.wSecond * ( 1000 );
44 aRes += aTime.wMilliseconds;
50 Calculate the complete execution time for the current thread
51 @return the calculated time as the number of milliseconds
53 INT64 GetTimeForCurrentThreadInMs()
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;
62 struct timespec aTime;
63 clock_gettime( CLOCK_MONOTONIC, &aTime );
64 INT64 aSec = aTime.tv_sec;
66 aSec += aTime.tv_nsec / 1000000;
74 TestLib_Listener::TestLib_Listener()
75 : myStart( 0 ), myComplete( 0 ), myNbTests( 0 ), myNbSuites( 0 )
82 TestLib_Listener::~TestLib_Listener()
87 \brief Clear the internal state of the listener
89 void TestLib_Listener::Clear()
96 Get complete time of all tests execution in milliseconds
97 @return complete time in milliseconds
99 INT64 TestLib_Listener::GetCompleteTimeInMS() const
105 Handler for test start
106 @param theTest the started tests
108 void TestLib_Listener::startTest( CppUnit::Test* theTest )
112 aName = theTest->getName();
114 std::cout << aName << "...";
115 myStart = GetTimeForCurrentThreadInMs();
119 Handler for test finish
120 @param theTest the finished tests
122 void TestLib_Listener::endTest( CppUnit::Test* theTest )
124 INT64 aCurTime = GetTimeForCurrentThreadInMs();
125 INT64 anExecTimeMS = aCurTime - myStart;
126 myComplete += anExecTimeMS;
127 std::cout << " " << anExecTimeMS << " ms" << std::endl;
132 int TestLib_Listener::GetNbTests() const
137 int TestLib_Listener::GetNbSuites() const
143 Handler for test suite start
144 @param theTest the started test suite
146 void TestLib_Listener::startSuite( CppUnit::Test* theTest )
151 Handler for test suite finish
152 @param theTest the finished test suite
154 void TestLib_Listener::endSuite( CppUnit::Test* theTest )
160 Handler for test failure
161 @param theFailure the failure information
163 void TestLib_Listener::addFailure( const CppUnit::TestFailure& theFailure )
165 std::string failedTest = theFailure.failedTest()->getName();
166 CppUnit::SourceLine failedLocation = theFailure.sourceLine();
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();
174 int aLine = failedLocation.lineNumber();
176 static char aBuf[1024];
177 sprintf( aBuf, "Failed %s: %s : %i", failedTest.c_str(), cFile.c_str(), aLine );
179 myFailures.push_back( aBuf );
182 void TestLib_Listener::DumpFailures()
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() );