-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUTFramework.h
More file actions
199 lines (156 loc) · 5.98 KB
/
UTFramework.h
File metadata and controls
199 lines (156 loc) · 5.98 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
//-------------------------------------------------------------------------------------
// Alec G Thilenius - Alec.Thilenius@Colorado.EDU
//
// This software is distributed under the MIT License, available at the following URL:
// http://www.opensource.org/licenses/mit-license.php
//-------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------
// V 1.2.2
// IMPORTANT: You MUST specify -pthread at the end of flags in makefile to compile
// the UTFramework on Linux!
// Abstract:
// A simple unit testing framework designed to be bundled with homework assignments.
// The framework itself (UTFramework.h and UTFramework.cpp) should not be edited by
// students.
// Supported:
// - Linux
// - WIN32 VC++ ( Reduced functionality )
// - Infinite loop detection and recovery
// - std::exception catching
// - unknown exception catching
// - Segmentation Fault catching
// - Note SegFault catching via POSIX handler. Must be a POSIX compatible system.
// Reduced functionality
// - Infinite loop detection is disabled in WINDOWS
// - SegFault handling may not work as intended in VS2012
//-------------------------------------------------------------------------------------
#ifndef UTFRAMEWORK_H
#define UTFRAMEWORK_H
#include <cstring>
#include <string>
#include <sstream>
#include <signal.h>
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32)
// Windows
//#include <WinBase.h>
#include <Windows.h>
#include <iostream>
#else
// Linux
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <iostream>
#include <sys/time.h>
#include <sys/select.h>
#endif
#define RUN_TIMEOUE 5000
using namespace std;
namespace Thilenius
{
enum ConsoleColor { White, Red, Yellow, Green, Blue };
enum RunningMode { Normal, RetroMode };
//===========================================================================================
//========================== Testing Framework Class Definition ========================
//===========================================================================================
class UTFramework
{
public:
~UTFramework(void);
static void StartSuite ( std::string suiteName );
static void EndSuite ( );
static bool StartTest ( std::string testName );
static void EndTest ( );
static void IsTrue ( std::string name, bool result, std::string message );
static void Fail ( std::string name, std::string message );
static void SegFaultRecovery ( );
static void FaultException ( const std::exception& exeption );
static void UnknownExHandler ( );
static void TimeoutFault ( );
static void SetMode( RunningMode mode );
static bool IsTesting ( );
static void SetColor ( ConsoleColor color );
private:
UTFramework(void);
UTFramework(UTFramework const&);
void operator=(UTFramework const&);
static void Print ( std::string message, int indentLevel, ConsoleColor color );
static void PrintLine ( std::string message, int indentLevel, ConsoleColor color );
static void PrintIndent ( int indentLevel );
private:
static std::string m_currentSuite;
static std::string m_currentTest;
static bool m_testFault;
static bool m_suiteFault;
static int m_maxDepth;
static int m_currentDepth;
static RunningMode m_mode;
};
//===========================================================================================
//=============================== Free Function Definitions ==============================
//===========================================================================================
extern void(*TestingFunction)( );
extern bool isThreadFinished;
void PosixSegFault(int signum);
unsigned int get_ticks( );
void* PosixThreadFork ( void* );
void ForkThread ( );
//===========================================================================================
//=============================== Macro Debug Directives ==============================
//===========================================================================================
#define DEBUG_INFO(m) \
UTFramework::SetColor ( Green ); \
std::cout << "Info [ " << __FUNCTION__ << " | " << __LINE__ << " ] " << m << std::endl; \
UTFramework::SetColor ( White ); \
#define DEBUG_ERROR(m) \
UTFramework::SetColor ( Red ); \
std::cout << "ERROR [ " << __FUNCTION__ << " | " << __LINE__ << " ] " << m << std::endl; \
UTFramework::SetColor ( White ); \
#define DEBUG_WARNING(m) \
UTFramework::SetColor ( Yellow ); \
std::cout << "WARNING [ " << __FUNCTION__ << " | " << __LINE__ << " ] " << m << std::endl; \
UTFramework::SetColor ( White ); \
//===========================================================================================
//=============================== Unit Testing Macros ==============================
//===========================================================================================
// See UTFramework.cpp for a full expansion.
#define SUITE_BEGIN(SuiteName) \
void(*Thilenius::TestingFunction)( );\
void UTRunAll () \
{ \
Thilenius::UTFramework::StartSuite( SuiteName ); \
if ( false ){ } \
#define SUITE_END \
else \
{ \
Thilenius::UTFramework::EndSuite(); \
} \
} \
#define TEST_BEGIN(TestName) \
else if ( Thilenius::UTFramework::StartTest ( TestName ) ) \
{ \
try \
{ \
#define TEST_END \
} \
catch (const std::exception& ex) { Thilenius::UTFramework::FaultException(ex); } \
catch (...) { Thilenius::UTFramework::UnknownExHandler(); } \
Thilenius::UTFramework::EndTest(); \
Thilenius::TestingFunction(); \
} \
#define UTFrameworkInit \
if (argc == 2 && strcmp(argv[1], "--retrograde") == 0) \
UTFramework::SetMode(RetroMode); \
TestingFunction = &UTRunAll; \
ForkThread( ); \
inline void IsTrue ( std::string testName, bool result, std::string message )
{
Thilenius::UTFramework::IsTrue(testName, result, message);
}
inline void Error ( std::string testName, std::string message )
{
Thilenius::UTFramework::Fail(testName, message);
}
}
#endif