forked from kth-competitive-programming/kactl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnitTest.h
95 lines (71 loc) · 1.43 KB
/
UnitTest.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
92
93
94
95
#pragma once
#include "global.h"
#include "UnitTestManager.h"
#include <sstream>
class UnitTestFailed
{
};
class UnitTest
{
public:
UnitTest(const string& testName):
m_name(testName)
{
}
virtual ~UnitTest()
{
}
public:
virtual void run(int subcase) = 0;
virtual int getCount() const
{
return 1;
}
protected:
template<class T>
void check(const T& have, const T& want, const string& message = "")
{
if(have == want)
return;
UnitTestManager* unitTestManager = UnitTestManager::getInstance();
unitTestManager->reportCheckFailure(convertToString(have), convertToString(want), message);
throw UnitTestFailed();
}
void fail(const string& message)
{
UnitTestManager* unitTestManager = UnitTestManager::getInstance();
unitTestManager->reportFailure(message);
throw UnitTestFailed();
}
private:
template<class T>
string convertToString(const T& data)
{
ostringstream oss;
oss.precision(20);
oss << data;
return oss.str();
}
template<class T>
string convertToString(const vector<T>& data)
{
ostringstream oss;
oss << "{ ";
for(auto &it: data)
oss << convertToString(it) << " ";
oss << "}";
return oss.str();
}
public:
string getName()
{
return m_name;
}
protected:
string m_name;
};
#define KACTL_AUTOREGISTER_TEST(x) UnitTestWrapper* g__KACTL__temp__##x = new UnitTestWrapper(new x())
#include "UnitTestWrapper.h"
#ifndef KACTL_UNITTEST_BATCH
# include "UnitTestManager.cpp"
#endif