forked from fastbuild/fastbuild
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestGroup.h
More file actions
144 lines (124 loc) · 6.58 KB
/
TestGroup.h
File metadata and controls
144 lines (124 loc) · 6.58 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
// TestGroup.h - interface for a group of related tests
//------------------------------------------------------------------------------
#pragma once
// Includes
//------------------------------------------------------------------------------
#include "TestManager.h"
// Core
#include "Core/Mem/MemTracker.h" // For MEMTRACKER_ENABLED
// TestGroup - Tests derive from this interface
//------------------------------------------------------------------------------
class TestGroup
{
protected:
explicit TestGroup() { m_NextTestGroup = nullptr; }
inline virtual ~TestGroup() = default;
virtual void RunTests() = 0;
virtual const char * GetName() const = 0;
// Run before and after each test
virtual void PreTest() const {}
virtual void PostTest( bool /*passed*/ ) const {}
private:
friend class TestManager;
TestGroup * m_NextTestGroup;
};
// Create a no-return helper to improve static analysis
#if defined( __WINDOWS__ )
__declspec(noreturn) void TestNoReturn();
#define TEST_NO_RETURN TestNoReturn();
#else
#define TEST_NO_RETURN
#endif
// Test Assertions
//------------------------------------------------------------------------------
#define TEST_ASSERT( expression ) \
do { \
PRAGMA_DISABLE_PUSH_MSVC(4127) \
PRAGMA_DISABLE_PUSH_CLANG_WINDOWS( "-Wunreachable-code" ) \
if ( !( expression ) ) \
{ \
if ( TestManager::AssertFailure( #expression, __FILE__, __LINE__ ) ) \
{ \
BREAK_IN_DEBUGGER; \
} \
TEST_NO_RETURN \
} \
} while ( false ) \
PRAGMA_DISABLE_POP_CLANG_WINDOWS \
PRAGMA_DISABLE_POP_MSVC
#define TEST_ASSERTM( expression, ... ) \
do { \
PRAGMA_DISABLE_PUSH_MSVC(4127) \
PRAGMA_DISABLE_PUSH_CLANG_WINDOWS( "-Wunreachable-code" ) \
if ( !( expression ) ) \
{ \
if ( TestManager::AssertFailureM( #expression, __FILE__, __LINE__, __VA_ARGS__ ) ) \
{ \
BREAK_IN_DEBUGGER; \
} \
TEST_NO_RETURN \
} \
} while ( false ) \
PRAGMA_DISABLE_POP_CLANG_WINDOWS \
PRAGMA_DISABLE_POP_MSVC
// Test Declarations
//------------------------------------------------------------------------------
#define DECLARE_TESTS \
virtual void RunTests() override; \
virtual const char * GetName() const override;
#define REGISTER_TESTS_BEGIN( testGroupName ) \
void testGroupName##Register() \
{ \
TestManager::RegisterTestGroup( new testGroupName ); \
} \
const char * testGroupName::GetName() const \
{ \
return #testGroupName; \
} \
void testGroupName::RunTests() \
{ \
TestManager & utm = TestManager::Get(); \
(void)utm;
#define REGISTER_TEST( testFunction ) \
utm.TestBegin( this, #testFunction ); \
testFunction(); \
utm.TestEnd();
#define REGISTER_TESTS_END \
}
#define REGISTER_TESTGROUP( testGroupName ) \
extern void testGroupName##Register(); \
testGroupName##Register();
// Memory snapshots
//------------------------------------------------------------------------------
#if defined( MEMTRACKER_ENABLED )
class TestMemorySnapshot
{
public:
TestMemorySnapshot()
: m_AllocationId( MemTracker::GetCurrentAllocationId() )
, m_ActiveAllocationCount( MemTracker::GetCurrentAllocationCount() )
{}
uint32_t m_AllocationId;
uint32_t m_ActiveAllocationCount;
};
// Take a snapshot of the memory state
#define TEST_MEMORY_SNAPSHOT( snapshot ) \
const TestMemorySnapshot snapshot
// Check for expected or unexpected allocations since a snapshot
#define TEST_EXPECT_ALLOCATION_EVENTS( snapshot, expected ) \
{ \
const uint32_t numAllocs = ( MemTracker::GetCurrentAllocationId() - snapshot.m_AllocationId ); \
TEST_ASSERTM( numAllocs == expected, "%u alloc(s) instead of %u alloc(s)", numAllocs, expected ); \
}
// Test for new active allocations since a snapshot
#define TEST_EXPECT_INCREASED_ACTIVE_ALLOCATIONS( snapshot, expected ) \
{ \
const uint32_t numActiveAllocs = ( MemTracker::GetCurrentAllocationCount() - snapshot.m_ActiveAllocationCount ); \
TEST_ASSERTM( numActiveAllocs == expected, "%u allocs(s) instead of %u alloc(s)", numActiveAllocs, expected ); \
}
#else
#define TEST_MEMORY_SNAPSHOT( snapshot ) (void)0
#define TEST_EXPECT_ALLOCATION_EVENTS( snapshot, expected )
#define TEST_EXPECT_INCREASED_ACTIVE_ALLOCATIONS( snapshot, expected )
#endif
//------------------------------------------------------------------------------