forked from EbenZhang/kcppunitlite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.h
More file actions
199 lines (162 loc) · 5.36 KB
/
test.h
File metadata and controls
199 lines (162 loc) · 5.36 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
#ifndef TEST_H
#define TEST_H
// Test is a base class for all tests. It provides a command interface for
// running tests (run) as well as a data member for recording the name of
// the test.
//
// Tests are constructed using the TEST macro. TEST creates a subclass of
// Test and static instance of that subclass. If you look at the constructor
// for the Test class, you'll notice that it registers the created object
// with a global TestRegistry. These features combine to make test creation
// particularly easy.
#ifndef __GNUC__
#pragma warning( disable : 4786 )
#endif
#include <vector>
#include <string>
class TestResult;
class Test
{
protected:
virtual ~Test();
public:
Test();
virtual void run (TestResult& result);
virtual void setUp(){}
virtual void tearDown(){}
protected:
virtual void runTest (TestResult& result);
virtual void doTest (TestResult& result) = 0;
virtual const std::string getName() = 0;
bool bIsFailed;
};
class TestCreator
{
protected:
virtual ~TestCreator();
public:
TestCreator (bool bSlow);
public:
virtual void run (TestResult& result);
virtual Test& GetTestInstance() = 0;
};
#define TEST_GROUP_BASE(testGroup, baseclass) \
int externTestGroup##testGroup = 0; \
struct CppUTestGroup##testGroup : public baseclass
#define TEST_BASE(testBaseClass) \
struct testBaseClass : public Test
#define TEST_GROUP(testGroup) \
TEST_GROUP_BASE(testGroup, Test)
#define TEST_GROUP_CTOR(testGroup)\
CppUTestGroup##testGroup()
#define TEST_SETUP() \
virtual void setUp()
#define TEST_TEARDOWN() \
virtual void tearDown()
#define TEST_CASE_IMPL(testGroup,testName)\
class testGroup##_##testName##_Test : public CppUTestGroup##testGroup \
{ \
public: \
testGroup##_##testName##_Test () : CppUTestGroup##testGroup (),name(#testName) {} \
protected:\
void doTest (TestResult& result_); \
const std::string getName(){return name;}\
protected:\
std::string name;\
}; \
#define TEST_CREATOR_IMPL(testGroup, testName,bSlowTest)\
TEST_CASE_IMPL(testGroup,testName)\
class testGroup##_##testName##_TestCreator : public TestCreator\
{\
public:\
testGroup##_##testName##_TestCreator():TestCreator(bSlowTest){}\
virtual Test& GetTestInstance()\
{\
static testGroup##_##testName##_Test s_instance;\
return s_instance;\
}\
}testGroup##_##testName##_TestCreator##Instance;\
void testGroup##_##testName##_Test::doTest (TestResult& result_) \
#define TEST(testGroup, testName) TEST_CREATOR_IMPL(testGroup, testName, false)
#define TEST_SLOW(testGroup, testName) TEST_CREATOR_IMPL(testGroup, testName, true)
class TestCreatorForTestOnlyOneTest : public TestCreator
{
public:
TestCreatorForTestOnlyOneTest();
};
#define TEST_ONLY(testGroup,testName)\
TEST_CASE_IMPL(testGroup,testName)\
class testGroup##_##testName##_TestCreator : public TestCreatorForTestOnlyOneTest\
{\
public:\
testGroup##_##testName##_TestCreator(){}\
virtual Test& GetTestInstance()\
{\
static testGroup##_##testName##_Test s_instance;\
return s_instance;\
}\
}testGroup##_##testName##_TestCreator##Instance;\
void testGroup##_##testName##_Test::doTest (TestResult& result_) \
// Here is a collection of testing macros that can be used in the
// bodies of tests. CHECK tests a boolean expression and records
// a failure if the expression evaluates to false. CHECK_LONGS_EQUAL
// and CHECK_DOUBLES_EQUAL compare longs and doubles respectively.
//
// To make this an industrial strength test harness, you should
// add equals macros for various low level types as you develop them.
// If, for instance, you have a daterange class, the ability to compare
// them directly and print out their values in the test output is
// invaluable.
#define CHECK(condition) \
do{\
if (!(condition)) \
{\
result_.addFailure (Failure (#condition, name, __FILE__, __LINE__));\
bIsFailed = true;\
}\
}while(0)
#define CHECK_DOUBLES_EQUAL(expected,actual)\
do{\
double _expected = (expected);\
double _actual = (actual);\
if (fabs ((expected)-(actual)) > 0.001) {\
char message [80];\
sprintf (message, "expected %lf but was: %lf", (expected), (actual));\
result_.addFailure (Failure (message, name, __FILE__, __LINE__));\
bIsFailed = true;\
}\
}while(0)
#define STRCMP_EQUAL(expected,actual)\
do{\
int nDiff = strcmp((expected),(actual));\
if(nDiff != 0)\
{\
std::string message("expected ");\
message += (expected);\
message += " but was: ";\
message += (actual);\
result_.addFailure (Failure (message, name, __FILE__, __LINE__));\
bIsFailed = true;\
}\
}while(0)
#define CK_EQ(expected,actual) CHECK((expected) == (actual))
#define CK_NOT_EQ(expected,actual) CHECK((expected) != (actual))
#define EXPECT_EQ(expected,actual) CHECK((expected) == (actual))
#define EXPECT_NOT_EQ(expected,actual) CHECK((expected) != (actual))
#define CHECK_DOUBLE_EQ CHECK_DOUBLES_EQUAL
#define CK_STR_EQ STRCMP_EQUAL
#define CK_STR_NOT_EQ(expected,actual)\
do{\
int nDiff = strncmp((expected),(actual),strlen(expected));\
if(nDiff == 0)\
{\
std::string message("expected ");\
message += (expected);\
message += " but was: ";\
message += (actual);\
result_.addFailure (Failure (message, name, __FILE__, __LINE__));\
bIsFailed = true;\
}\
}while(0)
#endif
//END OF FILE