-
Notifications
You must be signed in to change notification settings - Fork 0
/
mini_test.h
158 lines (124 loc) · 4.58 KB
/
mini_test.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
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
#ifndef MINI_TEST
#define MINI_TEST
#include <iostream>
#include <sstream>
#include <exception>
namespace mt {
using namespace std;
template<class T>
class ValueExpectationException : public std::exception {
public:
ValueExpectationException() {}
ValueExpectationException(T actual, T expected, string loc) :
_actual(actual), _expected(expected), _location(loc) { }
virtual ~ValueExpectationException() throw() { }
virtual const char* what() const throw() {
ostringstream os;
os << " at " << _location << endl;
os << " expected value: " << _expected << endl;
os << " actual value: " << _actual;
return os.str().c_str();
}
T actual() { return _actual; }
T expected() { return _expected; }
string location() { return _location; }
private:
T _actual;
T _expected;
string _location;
};
template<class T>
void make_error(T lhs, T rhs, string loc) {
throw ValueExpectationException<T>(lhs, rhs, loc);
}
class MissingExceptionExpectationException : public std::exception {
public:
MissingExceptionExpectationException() {}
MissingExceptionExpectationException(string loc) :
_location(loc) { }
virtual ~MissingExceptionExpectationException() throw() { }
virtual const char* what() const throw() {
ostringstream os;
os << " at " << _location << endl;
os << " an exception was expected" << endl;
os << " no exception was thrown";
throw os.str().c_str();
}
string location() { return _location; }
private:
string _location;
};
void make_missing_exception_error(string loc) {
throw MissingExceptionExpectationException(loc);
}
class WrongExceptionExpectationException : public std::exception {
public:
WrongExceptionExpectationException() {}
WrongExceptionExpectationException(string actual, string expected, string loc) :
_actual(actual), _expected(expected), _location(loc) { }
virtual ~WrongExceptionExpectationException() throw() { }
virtual const char* what() const throw() {
ostringstream os;
os << " at " << _location << endl;
os << " an unexpected exception ocurred" << endl;
os << " expected type: " << _expected << endl;
os << " but got: " << _actual;
return os.str().c_str();
}
string actual() { return _actual; }
string expected() { return _expected; }
string location() { return _location; }
private:
string _actual;
string _expected;
string _location;
};
template<typename T>
void make_wrong_type_exception_error(T e, const char* expected, string loc) {
throw WrongExceptionExpectationException(e, expected, loc);
}
string location(const char* file, int line) {
ostringstream os;
os << file << ":" << line;
return os.str().c_str();
}
string bool_to_s(bool b) { return b ? "true" : "false"; }
#define SUPPORT_ASSERT_EQ_ON(T) \
void assert_eq(T lhs, T rhs, string loc) { if (!(lhs == rhs)) { make_error(lhs, rhs, loc); } }\
SUPPORT_ASSERT_EQ_ON(int)
SUPPORT_ASSERT_EQ_ON(double)
SUPPORT_ASSERT_EQ_ON(float)
void assert_eq(bool lhs, bool rhs, string loc) { if (lhs != rhs) { make_error(bool_to_s(lhs), bool_to_s(rhs), loc); } }
void assert_eq(string lhs, string rhs, string loc) { if (lhs.compare(rhs) != 0) { make_error(lhs, rhs, loc); } }
void assert_eq(const char* lhs, const char* rhs, string loc) { assert_eq(string(lhs), string(rhs), loc); }
}
#define RUN_TEST(test) {\
{bool mt_ok = true;\
std::cout << #test << "..." << std::flush;\
try { test(); }\
catch (std::exception& e) { mt_ok = false; std::cout << "failed" << std::endl << e.what(); } \
catch (const char* msg) { mt_ok = false; std::cout << "failed" << std::endl << msg; } \
catch (...) { mt_ok = false; std::cout << "failed"; }\
if (mt_ok) { std::cout << "ok"; }\
std::cout << std::endl << std::flush;\
}\
}
#define ASSERT_EQ(lhs, rhs) { mt::assert_eq((lhs), (rhs), mt::location(__FILE__, __LINE__)); }
#define ASSERT(expr) { mt::assert_eq((expr), true, mt::location(__FILE__, __LINE__)); }
#define ASSERT_RAISE(code) {\
{bool mt_thrown = false;\
try { code; }\
catch (...) { mt_thrown = true; }\
if (!mt_thrown) { mt::make_missing_exception_error(mt::location(__FILE__, __LINE__)); }\
}\
}
#define ASSERT_RAISE_A(e_type, var, code) {\
{bool mt_thrown = false;\
try { code; }\
catch (e_type mt_e) { mt_thrown = true; var = mt_e; }\
catch (const std::exception &e) { mt::make_wrong_type_exception_error(e.what(), #e_type, mt::location(__FILE__, __LINE__)); }\
catch (...) { mt::make_wrong_type_exception_error("<unkown type>", #e_type, mt::location(__FILE__, __LINE__)); }\
if (!mt_thrown) { mt::make_missing_exception_error(mt::location(__FILE__, __LINE__)); }\
}\
}
#endif