forked from dalathegreat/Battery-Emulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
microtest.h
209 lines (170 loc) · 7.21 KB
/
microtest.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
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
//
// microtest.h
//
// URL: https://github.com/torpedro/microtest.h
// Author: Pedro Flemming (http://torpedro.com/)
// License: MIT License (https://github.com/torpedro/microtest.h/blob/master/LICENSE)
// Copyright (c) 2017 Pedro Flemming
//
// This is a small header-only C++ unit testing framework.
// It allows to define small unit tests with set of assertions available.
//
#ifndef __MICROTEST_H__
#define __MICROTEST_H__
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
////////////////
// Assertions //
////////////////
#define ASSERT(cond) ASSERT_TRUE(cond);
#define ASSERT_TRUE(cond) \
if (!(cond)) \
throw mt::AssertFailedException(#cond, __FILE__, __LINE__);
#define ASSERT_FALSE(cond) \
if (cond) \
throw mt::AssertFailedException(#cond, __FILE__, __LINE__);
#define ASSERT_NULL(value) ASSERT_TRUE(value == NULL);
#define ASSERT_NOTNULL(value) ASSERT_TRUE(value != NULL);
#define ASSERT_STREQ(a, b) \
if (std::string(a).compare(std::string(b)) != 0) { \
printf("%s{ info} %s", mt::yellow(), mt::def()); \
std::cout << "Actual values: " << a << " != " << b << std::endl; \
throw mt::AssertFailedException(#a " == " #b, __FILE__, __LINE__); \
}
#define ASSERT_STRNEQ(a, b) \
if (std::string(a).compare(std::string(b)) != = 0) { \
printf("%s{ info} %s", mt::yellow(), mt::def()); \
std::cout << "Actual values: " << a << " == " << b << std::endl; \
throw mt::AssertFailedException(#a " != " #b, __FILE__, __LINE__); \
}
#define ASSERT_EQ(a, b) \
if (a != b) { \
printf("%s{ info} %s", mt::yellow(), mt::def()); \
std::cout << "Actual values: " << a << " != " << b << std::endl; \
} \
ASSERT(a == b);
#define ASSERT_NEQ(a, b) \
if (a == b) { \
printf("%s{ info} %s", mt::yellow(), mt::def()); \
std::cout << "Actual values: " << a << " == " << b << std::endl; \
} \
ASSERT(a != b);
////////////////
// Unit Tests //
////////////////
#define TEST(name) \
void name(); \
namespace { \
bool __##name = mt::TestsManager::AddTest(name, #name); \
} \
void name()
///////////////
// Framework //
///////////////
namespace mt {
inline const char* red() {
return "\033[1;31m";
}
inline const char* green() {
return "\033[0;32m";
}
inline const char* yellow() {
return "\033[0;33m";
}
inline const char* def() {
return "\033[0m";
}
inline void printRunning(const char* message, FILE* file = stdout) {
fprintf(file, "%s{ running}%s %s\n", green(), def(), message);
}
inline void printOk(const char* message, FILE* file = stdout) {
fprintf(file, "%s{ ok}%s %s\n", green(), def(), message);
}
inline void printFailed(const char* message, FILE* file = stdout) {
fprintf(file, "%s{ failed} %s%s\n", red(), message, def());
}
// Exception that is thrown when an assertion fails.
class AssertFailedException : public std::exception {
public:
AssertFailedException(std::string description, std::string filepath, int line)
: std::exception(), description_(description), filepath_(filepath), line_(line){};
virtual const char* what() const throw() { return description_.c_str(); }
inline const char* getFilepath() { return filepath_.c_str(); }
inline int getLine() { return line_; }
protected:
std::string description_;
std::string filepath_;
int line_;
};
class TestsManager {
// Note: static initialization fiasco
// http://www.parashift.com/c++-faq-lite/static-init-order.html
// http://www.parashift.com/c++-faq-lite/static-init-order-on-first-use.html
public:
struct Test {
const char* name;
void (*fn)(void);
};
static std::vector<Test>& tests() {
static std::vector<Test> tests_;
return tests_;
}
// Adds a new test to the current set of tests.
// Returns false if a test with the same name already exists.
inline static bool AddTest(void (*fn)(void), const char* name) {
tests().push_back({name, fn});
return true;
}
// Run all tests that are registered.
// Returns the number of tests that failed.
inline static size_t RunAllTests(FILE* file = stdout) {
size_t num_failed = 0;
for (const Test& test : tests()) {
// Run the test.
// If an AsserFailedException is thrown, the test has failed.
try {
printRunning(test.name, file);
(*test.fn)();
printOk(test.name, file);
} catch (AssertFailedException& e) {
printFailed(test.name, file);
fprintf(file, " %sAssertion failed: %s%s\n", red(), e.what(), def());
fprintf(file, " %s%s:%d%s\n", red(), e.getFilepath(), e.getLine(), def());
++num_failed;
}
}
int return_code = (num_failed > 0) ? 1 : 0;
return return_code;
}
};
// Class that will capture the arguments passed to the program.
class Runtime {
public:
static const std::vector<std::string>& args(int argc = -1, char** argv = NULL) {
static std::vector<std::string> args_;
if (argc >= 0) {
for (int i = 0; i < argc; ++i) {
args_.push_back(argv[i]);
}
}
return args_;
}
};
} // namespace mt
#define TEST_MAIN() \
int main(int argc, char* argv[]) { \
mt::Runtime::args(argc, argv); \
\
size_t num_failed = mt::TestsManager::RunAllTests(stdout); \
if (num_failed == 0) { \
fprintf(stdout, "%s{ summary} All tests succeeded!%s\n", mt::green(), mt::def()); \
return 0; \
} else { \
double percentage = 100.0 * num_failed / mt::TestsManager::tests().size(); \
fprintf(stderr, "%s{ summary} %zu tests failed (%.2f%%)%s\n", mt::red(), num_failed, percentage, mt::def()); \
return -1; \
} \
}
#endif // __MICROTEST_H__