-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathunitlib.cpp
154 lines (128 loc) · 3.11 KB
/
unitlib.cpp
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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//
//=============================================================================//
#include "unitlib/unitlib.h"
#include "tier0/dbg.h"
#include <string.h>
//-----------------------------------------------------------------------------
//
// Base class for test cases
//
//-----------------------------------------------------------------------------
CTestCase::CTestCase( char const* pName, ITestSuite* pParent )
{
Assert( pName );
m_pName = new char[strlen(pName) + 1];
strcpy( m_pName, pName );
// Only install the test case if it has no parent
if (pParent)
{
pParent->AddTest( this );
}
else
{
UnitTestInstallTestCase( this );
}
}
CTestCase::~CTestCase()
{
if (m_pName)
delete[] m_pName;
}
char const* CTestCase::GetName()
{
return m_pName;
}
//-----------------------------------------------------------------------------
//
// Test suite class
//
//-----------------------------------------------------------------------------
CTestSuite::CTestSuite( char const* pName, ITestSuite* pParent )
{
m_TestCount = 0;
m_ppTestCases = 0;
m_pName = new char[strlen(pName) + 1];
strcpy( m_pName, pName );
// Only install the test case if it has no parent
if (pParent)
{
pParent->AddTest( this );
}
else
{
UnitTestInstallTestCase( this );
}
}
CTestSuite::~CTestSuite()
{
if (m_ppTestCases)
free(m_ppTestCases);
if (m_pName)
delete[] m_pName;
}
char const* CTestSuite::GetName()
{
return m_pName;
}
void CTestSuite::AddTest( ITestCase* pTest )
{
Assert( pTest );
if (!m_ppTestCases)
{
m_ppTestCases = (ITestCase**)malloc( sizeof(ITestCase**) );
}
else
{
m_ppTestCases = (ITestCase**)realloc( m_ppTestCases, (m_TestCount+1) * sizeof(ITestCase**) );
}
m_ppTestCases[m_TestCount++] = pTest;
}
void CTestSuite::RunTest()
{
for ( int i = 0; i < m_TestCount; ++i )
{
m_ppTestCases[i]->RunTest();
}
}
//-----------------------------------------------------------------------------
// This is the main function exported by the unit test library used by
// unit test DLLs to install their test cases into a list to be run
//-----------------------------------------------------------------------------
static int s_TestCount = 0;
static int s_TestAllocated = 0;
static ITestCase** s_ppTestCases = 0;
void UnitTestInstallTestCase( ITestCase* pTest )
{
Assert( pTest );
if (s_TestCount == s_TestAllocated)
{
if (!s_ppTestCases)
{
s_ppTestCases = (ITestCase**)malloc( 16 * sizeof(ITestCase**) );
s_TestAllocated = 16;
}
else
{
s_ppTestCases = (ITestCase**)realloc( s_ppTestCases, s_TestAllocated * 2 * sizeof(ITestCase**) );
s_TestAllocated *= 2;
}
}
s_ppTestCases[s_TestCount++] = pTest;
}
//-----------------------------------------------------------------------------
// These are the methods used by the unit test running program to run all tests
//-----------------------------------------------------------------------------
int UnitTestCount()
{
return s_TestCount;
}
ITestCase* GetUnitTest( int i )
{
Assert( i < s_TestCount );
return s_ppTestCases[i];
}