-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtesthelper.h
91 lines (80 loc) · 1.48 KB
/
testhelper.h
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
#include <sstream>
#include <vector>
using namespace std;
ostringstream cerrmsgs;
struct TestCase
{
bool success;
string testName;
ostringstream cmsg;
TestCase(string&& testName)
: success(true), testName(testName)
{
}
~TestCase()
{
}
template <typename T1, typename T2>
void assertEqual(const T1& expected, const T2& actual)
{
if (expected != actual)
{
cmsg << "assertEqual fail: expected " << expected << " but " << actual << endl;
success = false;
}
}
void assert(bool v, string&& msg)
{
if (!v)
{
success = false;
cmsg << msg << endl;
}
}
bool Test()
{
DoTest();
if (success)
cout << '.';
else
{
cout << 'F';
cerrmsgs << "FAILED TEST " << testName << endl;
cerrmsgs << cmsg.str();
cerrmsgs << endl;
}
return success;
}
private:
virtual void DoTest() = 0;
};
vector<TestCase*> testCases;
void run()
{
int total = 0;
int success = 0;
for(auto it = testCases.begin(); it != testCases.end(); ++it)
{
total ++;
if ((*it)->Test())
success++;
}
cout << endl;
if (!cerrmsgs.str().empty())
cout << endl << cerrmsgs.str() << endl;
cout << success << '/' << total << " tests passed." << endl;
}
#define DISABLED_TEST(x) \
struct S__##x : public TestCase \
{ \
S__##x() : TestCase(#x){} \
void DoTest(); \
} inst_S__##x; \
void S__##x::DoTest()
#define TEST(x) \
struct S__##x : public TestCase \
{ \
S__##x() : TestCase(#x){testCases.push_back(this);} \
void DoTest();\
} inst_S__##x; \
void S__##x::DoTest()