forked from fastbuild/fastbuild
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestManager.cpp
More file actions
297 lines (258 loc) · 8.96 KB
/
TestManager.cpp
File metadata and controls
297 lines (258 loc) · 8.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// TestManager.cpp
//------------------------------------------------------------------------------
// Includes
//------------------------------------------------------------------------------
#include "TestManager.h"
#include "TestGroup.h"
#include "Core/Env/Assert.h"
#include "Core/Env/Types.h"
#include "Core/Mem/MemTracker.h"
#include "Core/Profile/Profile.h"
#include "Core/Strings/AStackString.h"
#include "Core/Strings/AString.h"
#include "Core/Tracing/Tracing.h"
#include <stdio.h>
#include <string.h>
#if defined( __WINDOWS__ )
#include "Core/Env/WindowsHeader.h"
#endif
// Static Data
//------------------------------------------------------------------------------
/*static*/ uint32_t TestManager::s_NumTests( 0 );
/*static*/ TestManager::TestInfo TestManager::s_TestInfos[ MAX_TESTS ];
/*static*/ TestGroup * TestManager::s_FirstTest = nullptr;
// OnAssert callback
//------------------------------------------------------------------------------
/*static*/
#if defined( __WINDOWS__ )
__declspec(noreturn)
#endif
void OnAssert( const char * /*message*/ )
{
throw "Assert Failed";
}
// CONSTRUCTOR
//------------------------------------------------------------------------------
TestManager::TestManager()
{
// if we're running outside the debugger, we don't want
// failures to pop up a dialog. We want them to throw so
// the test framework can catch the exception
#ifdef ASSERTS_ENABLED
if ( IsDebuggerAttached() == false )
{
AssertHandler::SetAssertCallback( OnAssert );
}
#endif
}
// DESTRUCTOR
//------------------------------------------------------------------------------
TestManager::~TestManager()
{
#ifdef ASSERTS_ENABLED
if ( IsDebuggerAttached() == false )
{
AssertHandler::SetAssertCallback( nullptr );
}
#endif
// free all registered tests
TestGroup * testGroup = s_FirstTest;
while ( testGroup )
{
TestGroup * next = testGroup->m_NextTestGroup;
FDELETE testGroup;
testGroup = next;
}
}
// RegisterTest
//------------------------------------------------------------------------------
/*static*/ void TestManager::RegisterTestGroup( TestGroup * testGroup )
{
// first ever test? place as head of list
if ( s_FirstTest == nullptr )
{
s_FirstTest = testGroup;
return;
}
// link to end of list
TestGroup * thisGroup = s_FirstTest;
for ( ;; )
{
if ( thisGroup->m_NextTestGroup == nullptr )
{
thisGroup->m_NextTestGroup = testGroup;
return;
}
thisGroup = thisGroup->m_NextTestGroup;
}
}
// RunTests
//------------------------------------------------------------------------------
bool TestManager::RunTests( const char * testGroup )
{
// Reset results so RunTests can be called multiple times
s_NumTests = 0;
// check for compile time filter
TestGroup * test = s_FirstTest;
while ( test )
{
if ( testGroup != nullptr )
{
// is this test the one we want?
if ( AString::StrNCmp( test->GetName(), testGroup, strlen( testGroup ) ) != 0 )
{
// no -skip it
test = test->m_NextTestGroup;
continue;
}
}
OUTPUT( "------------------------------\n" );
OUTPUT( "Test Group: %s\n", test->GetName() );
#ifdef PROFILING_ENABLED
ProfileManager::Start( test->GetName() );
#endif
try
{
test->RunTests();
}
catch ( ... )
{
OUTPUT( " - Test '%s' *** FAILED ***\n", s_TestInfos[ s_NumTests - 1 ].m_TestName );
s_TestInfos[ s_NumTests - 1 ].m_TestGroup->PostTest( false );
}
#ifdef PROFILING_ENABLED
ProfileManager::Stop();
ProfileManager::Synchronize();
#endif
test = test->m_NextTestGroup;
}
OUTPUT( "------------------------------------------------------------\n" );
OUTPUT( "Summary For All Tests\n" );
uint32_t numPassed = 0;
float totalTime = 0.0f;
TestGroup * lastGroup = nullptr;
for ( size_t i = 0; i < s_NumTests; ++i )
{
const TestInfo & info = s_TestInfos[ i ];
if ( info.m_TestGroup != lastGroup )
{
OUTPUT( "------------------------------------------------------------\n" );
OUTPUT( " : %s\n", info.m_TestGroup->GetName() );
lastGroup = info.m_TestGroup;
}
const char * status = "OK";
if ( info.m_Passed )
{
++numPassed;
}
else
{
status = ( info.m_MemoryLeaks ) ? "FAIL (LEAKS)" : "FAIL";
}
OUTPUT( "%12s : %5.3fs : %s\n", status, (double)info.m_TimeTaken, info.m_TestName );
totalTime += info.m_TimeTaken;
}
OUTPUT( "------------------------------------------------------------\n" );
OUTPUT( "Passed: %u / %u (%u failures) in %2.3fs\n", numPassed, s_NumTests, ( s_NumTests - numPassed ), (double)totalTime );
OUTPUT( "------------------------------------------------------------\n" );
#ifdef PROFILING_ENABLED
ProfileManager::SynchronizeNoTag();
#endif
return ( s_NumTests == numPassed );
}
// TestBegin
//------------------------------------------------------------------------------
void TestManager::TestBegin( TestGroup * testGroup, const char * testName )
{
// record info for this test
ASSERT( s_NumTests < MAX_TESTS );
TestInfo & info = s_TestInfos[ s_NumTests ];
info.m_TestGroup = testGroup;
info.m_TestName = testName;
++s_NumTests;
OUTPUT( " - Test '%s'\n", testName );
// Flush the output to ensure that name of the test will be logged in case the test will crash the whole binary.
fflush( stdout );
#ifdef MEMTRACKER_ENABLED
MemTracker::Reset();
#endif
m_Timer.Start();
#ifdef PROFILING_ENABLED
ProfileManager::Start( testName );
#endif
testGroup->PreTest();
}
// TestEnd
//------------------------------------------------------------------------------
void TestManager::TestEnd()
{
TestInfo & info = s_TestInfos[ s_NumTests - 1 ];
info.m_TestGroup->PostTest( true );
#ifdef MEMTRACKER_ENABLED
// Get allocation count here (before profiling, which can cause allocations)
const uint32_t posAllocationCount = MemTracker::GetCurrentAllocationCount();
#endif
// Flush profiling info (track time taken as part of test)
#ifdef PROFILING_ENABLED
ProfileManager::Stop();
ProfileManager::Synchronize();
#endif
const float timeTaken = m_Timer.GetElapsed();
info.m_TimeTaken = timeTaken;
#ifdef MEMTRACKER_ENABLED
if ( posAllocationCount != 0 )
{
info.m_MemoryLeaks = true;
OUTPUT( " - Test '%s' in %2.3fs : *** FAILED (Memory Leaks)***\n", info.m_TestName, (double)timeTaken );
MemTracker::DumpAllocations();
if ( IsDebuggerAttached() )
{
TEST_ASSERT( false && "Memory leaks detected" );
}
return;
}
#endif
OUTPUT( " - Test '%s' in %2.3fs : PASSED\n", info.m_TestName, (double)timeTaken );
info.m_Passed = true;
}
// AssertFailure
//------------------------------------------------------------------------------
/*static*/ bool TestManager::AssertFailure( const char * message,
const char * file,
uint32_t line )
{
OUTPUT( "\n-------- TEST ASSERTION FAILED --------\n" );
OUTPUT( "%s(%u): Assert: %s", file, line, message );
OUTPUT( "\n-----^^^ TEST ASSERTION FAILED ^^^-----\n" );
if ( IsDebuggerAttached() )
{
return true; // tell the calling code to break at the failure site
}
// throw will be caught by the unit test framework and noted as a failure
throw "Test Failed";
}
// AssertFailureM
//------------------------------------------------------------------------------
/*static*/ bool TestManager::AssertFailureM( const char * message,
const char * file,
uint32_t line,
MSVC_SAL_PRINTF const char * formatString,
... )
{
AStackString< 4096 > buffer;
va_list args;
va_start( args, formatString );
buffer.VFormat( formatString, args );
va_end( args );
OUTPUT( "\n-------- TEST ASSERTION FAILED --------\n" );
OUTPUT( "%s(%u): Assert: %s", file, line, message );
OUTPUT( "\n%s", buffer.Get() );
OUTPUT( "\n-----^^^ TEST ASSERTION FAILED ^^^-----\n" );
if ( IsDebuggerAttached() )
{
return true; // tell the calling code to break at the failure site
}
// throw will be caught by the unit test framework and noted as a failure
throw "Test Failed";
}
//------------------------------------------------------------------------------