OpenBarnyard
 
Loading...
Searching...
No Matches
T2TestingFramework.cpp
Go to the documentation of this file.
1#include "ToshiPCH.h"
3
4//-----------------------------------------------------------------------------
5// Enables memory debugging.
6// Note: Should be the last include!
7//-----------------------------------------------------------------------------
9
11
12// Using native malloc/free instead of TOSHI allocator so that
13// we can be sure nothing goes wrong with the framework.
14
15static void* Malloc( TSIZE a_uiSize )
16{
17 return malloc( a_uiSize );
18}
19
20static void Free( void* a_pMem )
21{
22 free( a_pMem );
23}
24
25template <class T, typename... Args>
26static T* New( Args&&... args )
27{
28 return new ( malloc( sizeof( T ) ) ) T( std::forward( args )... );
29}
30
32
33T2TestingFramework::TestAutoReg::TestAutoReg( const TCHAR* a_pchTestName, const TCHAR* a_pchCategoryName, TestMethod_t a_fnMethod )
34{
36 g_pTestingFramework = New<T2TestingFramework>();
37
38 g_pTestingFramework->RegisterTest(
39 a_pchTestName,
40 g_pTestingFramework->RegisterCategory( a_pchCategoryName ),
41 a_fnMethod
42 );
43}
44
46{
47 Category* pCategory = m_pHeadCategory;
48
49 while ( pCategory )
50 {
51 if ( !strcmp( pCategory->pchName, a_pchName ) )
52 return pCategory;
53
54 pCategory = pCategory->pNext;
55 }
56
57 return TNULL;
58}
59
61{
62 Category* pCategory = FindCategory( a_pchName );
63
64 if ( !pCategory )
65 {
66 pCategory = New<Category>();
67 pCategory->pchName = a_pchName;
68 pCategory->pNext = m_pHeadCategory;
69
70 m_pHeadCategory = pCategory;
71 }
72
73 return pCategory;
74}
75
77{
78 if ( !a_pCategory )
79 return TNULL;
80
81 Test* pTest = New<Test>();
82 pTest->pchName = a_pchTestName;
83 pTest->pCategory = a_pCategory;
84 pTest->fnMethod = a_fnMethod;
85 pTest->pNext = a_pCategory->pHeadTest;
86
87 a_pCategory->pHeadTest = pTest;
88
89 return pTest;
90}
91
94
95void T2TestingFramework::SignalRequirementResult( Test* a_pTest, const TCHAR* a_pchFileName, TINT a_iLineNum, const TCHAR* a_pchStatement, TBOOL a_bFailed )
96{
97 if ( !a_pTest )
98 return;
99
100 Check* pCheck = New<Check>();
101 pCheck->pchFileName = a_pchFileName;
102 pCheck->pchStatement = a_pchStatement;
103 pCheck->iLineNum = a_iLineNum;
104 pCheck->bFailed = a_bFailed;
105 pCheck->pNext = a_pTest->pHeadCheck;
106
107 a_pTest->pHeadCheck = pCheck;
108
109 if ( a_bFailed )
110 {
111 a_pTest->iNumFails += 1;
112
113 if ( g_fnFailCallback )
114 g_fnFailCallback( a_pTest, pCheck );
115 }
116 else if ( g_fnSuccessCallback )
117 {
118 g_fnSuccessCallback( a_pTest, pCheck );
119 }
120}
121
122TINT T2TestingFramework::RunTests( FailCallback_t a_fnFailCallback /* = TNULL*/, SuccessCallback_t a_fnSuccessCallback /* = TNULL */ )
123{
124 TINT iNumFails = 0;
125 Category* pCategory = m_pHeadCategory;
126
127 // Update callbacks
128 g_fnFailCallback = a_fnFailCallback;
129 g_fnSuccessCallback = a_fnSuccessCallback;
130
131 while ( pCategory )
132 {
133 Test* pTest = pCategory->pHeadTest;
134
135 while ( pTest )
136 {
137 if ( !pTest->bWasExecuted )
138 {
139 // Run the actual test
140 m_pCurrentTest = pTest;
141
142 pTest->fnMethod();
143 pTest->bWasExecuted = TTRUE;
144
145 // Count number of fails
146 iNumFails += pTest->iNumFails;
147 pCategory->iNumTotalFails += pTest->iNumFails;
148 }
149
150 m_pCurrentTest = TNULL;
151 pTest = pTest->pNext;
152 }
153
154 pCategory = pCategory->pNext;
155 }
156
157 // Reset callbacks
158 g_fnFailCallback = TNULL;
159 g_fnSuccessCallback = TNULL;
160
161 return iNumFails;
162}
163
#define TOSHI_NAMESPACE_START
Definition Defines.h:47
#define TOSHI_NAMESPACE_END
Definition Defines.h:50
size_t TSIZE
Definition Typedefs.h:9
char TCHAR
Definition Typedefs.h:20
#define TNULL
Definition Typedefs.h:23
int TINT
Definition Typedefs.h:7
#define TTRUE
Definition Typedefs.h:25
bool TBOOL
Definition Typedefs.h:6
T2TestingFramework * g_pTestingFramework
T2TestingFramework::SuccessCallback_t g_fnSuccessCallback
T2TestingFramework * g_pTestingFramework
T2TestingFramework::FailCallback_t g_fnFailCallback
TINT RunTests(FailCallback_t a_fnFailCallback=TNULL, SuccessCallback_t a_fnSuccessCallback=TNULL)
void SignalRequirementResult(Test *a_pTest, const TCHAR *a_pchFileName, TINT a_iLineNum, const TCHAR *a_pchStatement, TBOOL a_bFailed)
void(*)(Test *a_pTest, Check *a_pCheck) SuccessCallback_t
Category * FindCategory(const TCHAR *a_pchName)
Test * RegisterTest(const TCHAR *a_pchTestName, Category *a_pCategory, TestMethod_t a_fnMethod)
void(*)(Test *a_pTest, Check *a_pCheck) FailCallback_t
Category * RegisterCategory(const TCHAR *a_pchName)
TestAutoReg(const TCHAR *a_pchTestName, const TCHAR *a_pchCategoryName, TestMethod_t a_fnMethod)