-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtester.hpp
363 lines (295 loc) · 10.4 KB
/
tester.hpp
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/* ************************************************************************ */
/* */
/* Tester library */
/* Copyright (C) 2013 Jiří Fatka <ntsfka@gmail.com> */
/* */
/* The MIT License (MIT) */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be included */
/* in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS */
/* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/* */
/* ************************************************************************ */
#ifndef TESTER_HPP_
#define TESTER_HPP_
/* ************************************************************************ */
/* INCLUDES */
/* ************************************************************************ */
// C++
#include <stdexcept>
#include <vector>
#include <functional>
#if __cplusplus >= 201103L
#include <chrono>
#else
#include <ctime>
#endif
/* ************************************************************************ */
/* MACROS */
/* ************************************************************************ */
/**
* @brief C++11 support.
*/
#if __cplusplus >= 201103L
#define CXX11
#endif
/* ************************************************************************ */
/**
* @brief noexcept alias for older standards.
*/
#ifndef CXX11
#define noexcept throw()
#endif
/* ************************************************************************ */
/**
* @brief Converts argument into a string literal.
*
* @param expr Expression.
*
* @return Expression as a string literal: "expr".
*/
#define STR(expr) # expr
/* ************************************************************************ */
/**
* @brief Evaluate argument and converts it into a string literal.
*
* @param expr Expression.
*
* @return Evaluated expression as a string literal.
*/
#define XSTR(expr) STR(expr)
/* ************************************************************************ */
/**
* @brief Create test function name.
*
* @param name Test name.
*
* @return Name of the test function.
*/
#define TEST_NAME(name) name ## _test
/* ************************************************************************ */
/**
* @brief Create test function declaration.
*
* @param name Test name.
*
* @return Prototype of the test function.
*/
#define TEST(name) void TEST_NAME(name)()
/* ************************************************************************ */
/**
* @brief Create an expression that calls test.
*
* This macro is a shorthand for:
* @code run_test(TEST_NAME(name), name) @endcode
*
* @param name Test name.
*
* @return Test calling expression.
*/
#define TEST_RUN(name) ::tester::run_test(TEST_NAME(name), # name)
/* ************************************************************************ */
/**
* @brief Declare and Run test with given name.
*
* @note This macro using function declaration (prototype) together with
* function calling.
*
* @param name Test name.
*
* @return Test function declaration and it's calling expression.
*/
#define TEST_DECL_RUN(name) TEST(name); TEST_RUN(name)
/* ************************************************************************ */
/**
* @brief Test if given expression is true.
*
* @param expr Tested expression.
*
* @throw assert_error If expression is false. Exception containts information
* about tested expression and it's position.
*/
#define ASSERT(expr) \
::tester::test_assert(expr, "(" # expr ") at line " XSTR(__LINE__))
/* ************************************************************************ */
/**
* @brief Test values equality.
*
* @param lhs Left operand.
* @param rhs Right operand.
*
* @return lhs == rhs assertion.
*/
#define ASSERT_EQ(lhs, rhs) \
ASSERT(lhs == rhs)
/* ************************************************************************ */
/**
* @brief Test values unequality.
*
* @param lhs Left operand.
* @param rhs Right operand.
*
* @return lhs != rhs assertion.
*/
#define ASSERT_NEQ(lhs, rhs) \
ASSERT(lhs != rhs)
/* ************************************************************************ */
namespace tester {
/* ************************************************************************ */
/* TYPES */
/* ************************************************************************ */
/**
* @brief Type that represents a point in time.
*/
#ifdef CXX11
using time_point = std::chrono::high_resolution_clock::time_point;
#else
typedef std::clock_t time_point;
#endif
/* ************************************************************************ */
/**
* @brief Callback test function type.
*/
#ifdef CXX11
using test_func = std::function<void()>;
#else
typedef void (*test_func)();
#endif
/* ************************************************************************ */
/* VARIABLES */
/* ************************************************************************ */
/// Assertion counter.
extern unsigned int assertion_count;
/* ************************************************************************ */
/// Test counter.
extern unsigned int test_count;
/* ************************************************************************ */
/// Error list.
extern std::vector<std::string> errors;
/* ************************************************************************ */
/// Tests start time.
extern time_point start_time;
/* ************************************************************************ */
/// Tests stop time.
extern time_point stop_time;
/* ************************************************************************ */
/* CLASSES */
/* ************************************************************************ */
/**
* @brief Assertion error.
*
* This exception type is used for indication the assertion failed.
*/
class assert_error : public std::runtime_error
{
// Public Ctors
public:
/**
* @brief Creates an assertion error.
*
* @param what Error message.
*/
explicit assert_error(const std::string& what)
: std::runtime_error(what)
{}
/**
* @brief Creates an assertion error.
*
* @param what Error message.
*/
explicit assert_error(const char* what)
: std::runtime_error(what)
{}
};
/* ************************************************************************ */
/* FUNCTIONS */
/* ************************************************************************ */
/**
* @brief Performs a test.
*
* Function calls given test function and prints it's call result onto
* standard output. The output have format where test name is printed and
* after that is printed the test result (OK or FAIL).
*
* Test can be called recursive and function is able to handle that. It's
* a simple way to group tests.
*
* @param test Pointer to test function. It can be lambda under C++11.
* @param name Test name.
*
* @see TEST_RUN
*/
void run_test(test_func test, const std::string& name) noexcept;
/* ************************************************************************ */
/**
* @brief Performs tests.
*
* This function handle the whole testing system and can be used as only one
* function call in the main function. The function calls sequentialy
* following functions:
*
* - start()
* - tests()
* - stop()
* - print_results()
*
* @param test A function that tests all required tests.
*
* @return Tests result. Can be used directly as program exit status.
*/
int run_tests(test_func tests) noexcept;
/* ************************************************************************ */
/**
* @brief Throws an exception when `res` is false.
*
* @param res Evaluation result.
* @param errstr Error string in the thrown exception.
*
* @throw assert_error If `res` is false.
*/
void test_assert(bool res, const std::string& errstr);
/* ************************************************************************ */
/**
* @brief Returns current time.
*
* @return Current time in undefined units.
*/
time_point get_time();
/* ************************************************************************ */
/**
* @brief Starts measuring tests run time.
*
* Resets statistical variables and error list.
*/
void start();
/* ************************************************************************ */
/**
* @brief Stops measuring tests run time.
*/
void stop();
/* ************************************************************************ */
/**
* @brief Prints testing results.
*
* Prints testing statistics onto standard output.
*/
void print_results();
/* ************************************************************************ */
}
/* ************************************************************************ */
#endif // TESTER_HPP_