/
igtlTestConfig.h.in
274 lines (227 loc) · 8.5 KB
/
igtlTestConfig.h.in
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*=========================================================================
Program: OpenIGTLink Library
Language: C++
Copyright (c) Insight Software Consortium. All rights reserved.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
//
// This header file switches the definition of TEST() macro. It includes
// gtest.h if GTest supports the platform. Otherwise, this header file defines
// its own TEST() macro.
//
// This is a temporary workaround for the GTest issues in Windows. For long term,
// we will switch entirely to the GTest.
//
#cmakedefine OpenIGTLink_USE_GTEST
#if defined(OpenIGTLink_USE_GTEST)
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#else
#include <vector>
#include <cmath>
#include <igtlConfigure.h>
#ifdef _WIN32
#include <typeinfo.h>
#endif
#define TEST_FAILURE 1
#define TEST_SUCCESS 0
// void function to skip InitGoogleTest() call
namespace testing {
void InitGoogleTest(int* argc, char **argv) {}
// Implements ElementsAreArray().
template <typename T>
class IGTLCommon_EXPORT ElementsAreArrayMatcher
{
public:
ElementsAreArrayMatcher(::std::vector<T> a);
int Compare(::std::vector<T> b);
int Compare(const T* b);
private:
::std::vector<T> arrayA;
};
template <typename T>
ElementsAreArrayMatcher<T>::ElementsAreArrayMatcher(::std::vector<T> a)
{
this->arrayA = a;
}
template <typename T>
int ElementsAreArrayMatcher<T>::Compare(::std::vector<T> b)
{
typename ::std::vector<T>::iterator iterA = arrayA.begin();
typename ::std::vector<T>::iterator iterB = b.begin();
while( iterA!=arrayA.end() && iterB!=b.end())
{
if ((*iterA-*iterB)>1e-6 || (*iterA-*iterB)<-1e-6)
{
return 0;
}
iterA ++;
iterB ++;
}
return 1;
}
template <typename T>
int ElementsAreArrayMatcher<T>::Compare(const T* b)
{
int N = arrayA;
for (int i = 0; i < N; i ++)
{
if ((arrayA[i] - b[i])>1e-6 || (arrayA[i] - b[i])<-1e-6)
{
return 0;
}
}
return 1;
}
template <typename T>
inline ElementsAreArrayMatcher<T> ElementsAreArray(::std::vector<T> & array)
{
return ElementsAreArrayMatcher<T>(array);
}
template <typename T, size_t N>
inline ElementsAreArrayMatcher<T> ElementsAreArray(const T (&array)[N])
{
::std::vector<T> vec;
for (int i = 0; i < N; i ++) vec.push_back(array[i]);
return ElementsAreArrayMatcher<T>(vec);
}
class IGTLCommon_EXPORT TestBase {
public:
virtual void Run() = 0;
inline ::std::string GetCaseName() { return caseName; }
inline ::std::string GetTestName() { return testName; }
inline int GetResult() { return result; }
protected:
::std::string caseName;
::std::string testName;
int result;
};
class IGTLCommon_EXPORT TestInfo {
public:
int Add( TestBase* );
int Run();
private:
static ::std::vector< TestBase* > TestList;
};
::std::vector< TestBase* > TestInfo::TestList;
TestInfo testInfo;
int TestInfo::Add( TestBase* test )
{
TestInfo::TestList.push_back(test);
return 1;
}
int TestInfo::Run()
{
::std::vector< TestBase* >::iterator iter;
int result = TEST_SUCCESS;
for (iter = TestInfo::TestList.begin(); iter != TestInfo::TestList.end(); iter ++)
{
(*iter)->Run();
if ((*iter)->GetResult() == TEST_FAILURE)
{
std::cerr << "[ERROR] ";
result = TEST_FAILURE;
}
else
{
std::cerr << "[ OK ] ";
}
std::cerr << (*iter)->GetCaseName() << ".";
std::cerr << (*iter)->GetTestName() << std::endl;
}
return result;
}
}
#define EXPECT_EQ(a, b) \
if (a != b) \
{ \
this->result = TEST_FAILURE; \
return; \
}
#define EXPECT_LT(a, b) \
if (a >= b) \
{ \
this->result = TEST_FAILURE; \
return; \
}
#define EXPECT_NEAR(a, b, err) \
if ((a-b) >= err || (a-b) <= -err) \
{ \
this->result = TEST_FAILURE; \
return; \
}
#define EXPECT_FLOAT_EQ(a, b) \
if ((a-b) > 1e-6 || (a-b) < -1e-6) \
{ \
this->result = TEST_FAILURE; \
return; \
}
#define EXPECT_STREQ(a, b) \
if (strcmp(a, b) != 0) \
{ \
this->result = TEST_FAILURE; \
return; \
}
#ifdef _WIN32
#define EXPECT_THAT(a, comp) \
{ \
const int N = sizeof(a)/sizeof(a[0]); \
auto dataType = a[0]; \
testing::ElementsAreArrayMatcher< decltype(dataType) > matcher = comp; \
::std::vector< decltype(dataType) > vecA; \
for (int i = 0; i < N; i ++) vecA.push_back(a[i]); \
if (!matcher.Compare(vecA)) \
{ \
this->result = TEST_FAILURE; \
return; \
} \
}
#else
#define EXPECT_THAT(a, comp) \
{ \
const int N = sizeof(a)/sizeof(a[0]); \
std::string dataType = typeid(a[0]).name(); \
testing::ElementsAreArrayMatcher< typeof(a[0]) > matcher = comp; \
::std::vector< typeof(a[0]) > vecA; \
for (int i = 0; i < N; i ++) vecA.push_back(a[i]); \
if (!matcher.Compare(vecA)) \
{ \
this->result = TEST_FAILURE; \
return; \
} \
}
#endif
#define EXPECT_TRUE(a) \
if (a != true) \
{ \
this->result = TEST_FAILURE; \
return; \
}
#define TEST(test_case_name, test_name) \
class IGTLCommon_EXPORT test_case_name##_##test_name##_Test : ::testing::TestBase { \
public: \
test_case_name##_##test_name##_Test(); \
void Run(); \
}; \
test_case_name##_##test_name##_Test instance_##test_case_name##_##test_name##_Test; \
test_case_name##_##test_name##_Test::test_case_name##_##test_name##_Test() \
{ \
::testing::testInfo.Add(this); \
caseName = #test_case_name; \
testName = #test_name; \
} \
void test_case_name##_##test_name##_Test::Run()
inline int RUN_ALL_TESTS() {
if (::testing::testInfo.Run() == TEST_FAILURE)
{
return 1;
}
else
{
return 0;
}
}
/// TestList.push_back(new test_case_name##_##test_name##_Test) \
#endif