forked from burner/sweet.hpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unit.hpp
498 lines (427 loc) · 16.3 KB
/
unit.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/* unit.hpp - a small but elegant unit test framework for C++ based on QUnit
and Testdog
Author: Robert "burner" Schadek rburners@gmail.com License: LGPL 3 or higher
Example:
#include <fstream>
#include "unit.hpp"
UNITTEST(fancyname) {
// std::ofstream unittestlog("testlog.tst"); optional
// this->setOutputStream(&unittestlog); optional
AS_T(true);
AS_F(4!=4);
AS_EQ(42, 42);
AS_NEQ(42, 43);
if(AS_T(true)) {
// do a test that is only possible if the previous test passed
}
// unittests with sections are executed |sections|+1 times
// every run a new section will be executed
SECTION("someSection") {
std::cout<<"\tInside section 1"<<std::endl;
}
SECTION("someOtherSection") {
std::cout<<"\tInside section 2"<<std::endl;
}
}
UNITTEST(foo, 66) {
AS_T(4==4);
}
UNITTEST(foon, 66666, "-O3") {
AS_T(4==4);
}
int main() {
if(!sweet::Unit::runTests(
"TheOptionalNameOfTheFileWithTheBenchmarkResults")) {
return 1;
}
}
*/
#pragma once
#include <sstream>
#include <string>
#include <string.h>
#include <vector>
#include <iostream>
#include <fstream>
#include <functional>
#include <chrono>
#include <cmath>
#include <type_traits>
#include <limits>
#include <iomanip>
#include <random>
// RANDOMIZED_TEST macros
#define VA_NARGS_IMPL(_1,_2,_3,_4,_5,_6,_7,_8,N,...) N
#define VA_NARGS(...) VA_NARGS_IMPL(__VA_ARGS__, 8, 7, 6, 5, 4, 3, 2, 1)
#define SET_STATEGENERATE(sweetGen, count, ...) \
[&](auto __funcToCall, size_t numRuns) { \
auto& __sweetGenerator = sweetGen; \
DEC ## count (__VA_ARGS__) \
for(size_t cnt = 0; cnt < numRuns; ++cnt) { \
__funcToCall(REF ## count (__VA_ARGS__)); \
} \
}
#define SET_STATEP(sweetGen, count, ...) \
SET_STATEGENERATE(sweetGen, count, __VA_ARGS__)
#define RANDOMIZED_TEST(sweetGen, ...) \
SET_STATEP(sweetGen, VA_NARGS(__VA_ARGS__), __VA_ARGS__)
/* args */
#define DEC1(a) auto __sweet_value_A = a;
#define DEC2(a,b) DEC1(a) auto __sweet_value_B = b;
#define DEC3(a,b,c) DEC2(a, b) auto __sweet_value_C = c;
#define DEC4(a,b,c,d) DEC3(a,b,c) auto __sweet_value_D = d;
#define DEC5(a,b,c,d,e) DEC4(a,b,c,d) auto __sweet_value_E = e;
#define DEC6(a,b,c,d,e,f) DEC5(a,b,c,d,e) auto __sweet_value_F = f;
#define DEC7(a,b,c,d,e,f,g) DEC6(a,b,c,d,e,f) auto __sweet_value_G = g;
#define DEC8(a,b,c,d,e,f,g,h) DEC7(a,b,c,d,e,f,g) auto __sweet_value_H = h;
#define REF1(a) __sweet_value_A(__sweetGenerator)
#define REF2(a,b) REF1(a), __sweet_value_B(__sweetGenerator)
#define REF3(a,b,c) REF2(a,b), __sweet_value_C(__sweetGenerator)
#define REF4(a,b,c,d) REF3(a,b,c), __sweet_value_D(__sweetGenerator)
#define REF5(a,b,c,d,e) REF4(a,b,c,d), __sweet_value_E(__sweetGenerator)
#define REF6(a,b,c,d,e,f) REF5(a,b,c,d,e), __sweet_value_F(__sweetGenerator)
#define REF7(a,b,c,d,e,f,g) REF6(a,b,c,d,e,f), __sweet_value_G(__sweetGenerator)
#define REF8(a,b,c,d,e,f,g,h) REF7(a,b,c,d,e,f,g), __sweet_value_H(__sweetGenerator)
// RANDOMIZED_TEST macros END
#define PP_HAS_ARGS_IMPL2(_1, _2, _3, N, ...) N
#define PP_HAS_ARGS_SOURCE() MULTI, MULTI, ONE, ERROR
#define PP_HAS_ARGS_IMPL(...) PP_HAS_ARGS_IMPL2(__VA_ARGS__)
#define PP_HAS_ARGS(...) PP_HAS_ARGS_IMPL(__VA_ARGS__, \
PP_HAS_ARGS_SOURCE())
#define __UTEST_NOTEST_ONE(test_name) \
class test_name##_test_class : public sweet::Unit::Unittest { \
virtual void nevercall(const size_t, size_t&); \
public: \
test_name##_test_class() : sweet::Unit::Unittest(#test_name,__FILE__,__LINE__) {} \
} static test_name##_test_class_impl; \
void test_name##_test_class::nevercall(const size_t __cnt, size_t& __localCnt)
#define __UTEST_NOTEST_MULTI(test_name,...) \
class test_name##_test_class : public sweet::Unit::Unittest { \
virtual void nevercall(const size_t __cnt, size_t& __localCnt); \
public: \
test_name##_test_class() : sweet::Unit::Unittest(#test_name,__FILE__,__LINE__,\
__VA_ARGS__) {} \
} static test_name##_test_class_impl; \
void test_name##_test_class::nevercall(const size_t __cnt, size_t& __localCnt)
#define __UTEST_NOTEST_DISAMBIGUATE2(has_args, ...) __UTEST_NOTEST_ \
## has_args (__VA_ARGS__)
#define __UTEST_NOTEST_DISAMBIGUATE(has_args, ...) \
__UTEST_NOTEST_DISAMBIGUATE2(has_args, __VA_ARGS__)
#define __UTEST_NOTEST(...) __UTEST_NOTEST_DISAMBIGUATE(PP_HAS_ARGS(\
__VA_ARGS__), __VA_ARGS__)
#define __UTEST_ONE(test_name) \
class test_name##_test_class : public sweet::Unit::Unittest { \
virtual void run_impl(const size_t __cnt __attribute__((unused)), \
size_t& __localCnt __attribute__((unused))); \
public: \
test_name##_test_class() : sweet::Unit::Unittest(#test_name,__FILE__,__LINE__) {} \
} static test_name##_test_class_impl; \
void test_name##_test_class::run_impl(const size_t __cnt __attribute__((unused)), \
size_t& __localCnt __attribute__((unused)))
#define __UTEST_MULTI(test_name,...) \
class test_name##_test_class : public sweet::Unit::Unittest { \
virtual void run_impl(const size_t __cnt __attribute__((unused)), \
size_t& __localCnt __attribute__((unused))); \
public: \
test_name##_test_class() : \
sweet::Unit::Unittest(#test_name,__FILE__,__LINE__, __VA_ARGS__) { \
} \
} static test_name##_test_class_impl; \
void test_name##_test_class::run_impl(const size_t __cnt __attribute__((unused)), \
size_t& __localCnt __attribute__((unused)))
// Section
#define SECTION(secNameUnit) ++__localCnt; this->sectionName = secNameUnit; \
if(__localCnt == this->sectionsCnt)
// Section End
#define __UTEST_DISAMBIGUATE2(has_args, ...) __UTEST_ ## has_args (__VA_ARGS__)
#define __UTEST_DISAMBIGUATE(has_args, ...) __UTEST_DISAMBIGUATE2(has_args, \
__VA_ARGS__)
#define __UTEST(...) __UTEST_DISAMBIGUATE(PP_HAS_ARGS(__VA_ARGS__), __VA_ARGS__)
#ifdef SWEET_NO_UNITTEST
#define UNITTEST(...) __UTEST_NOTEST(__VA_ARGS__)
#else
#define UNITTEST(...) __UTEST(__VA_ARGS__)
#endif
#define AS_EQ(e1,e2) UNIT_COMPARE(true,true,e1,e2,"",[](){})
#define AS_NEQ(e1,e2) UNIT_COMPARE(true,false,e1,e2,"",[](){})
#define AS_T(e) UNIT_COMPARE(false,true,e,true,"",[](){})
#define AS_F(e) UNIT_COMPARE(false,true,e,false,"",[](){})
#define AS_EQ_MSG(e1,e2,msg) UNIT_COMPARE(true,true,e1,e2,msg,[](){})
#define AS_NEQ_MSG(e1,e2,msg) UNIT_COMPARE(true,false,e1,e2,msg,[](){})
#define AS_T_MSG(e,msg) UNIT_COMPARE(false,true,e,true,msg,[](){})
#define AS_F_MSG(e,msg) UNIT_COMPARE(false,true,e,false,msg,[](){})
#define AS_EQ_C(e1,e2,c) UNIT_COMPARE(true,true,e1,e2,"",c)
#define AS_NEQ_C(e1,e2,c) UNIT_COMPARE(true,false,e1,e2,"",c)
#define AS_T_C(e,c) UNIT_COMPARE(false,true,e,true,"",c)
#define AS_F_C(e,c) UNIT_COMPARE(false,true,e,false,"",c)
#define ASSERT_EQ(e1,e2) UNIT_COMPARED(true,true,e1,e2,"",[](){})
#define ASSERT_NEQ(e1,e2) UNIT_COMPARED(true,false,e1,e2,"",[](){})
#define ASSERT_T(e) UNIT_COMPARED(false,true,e,true,"",[](){})
#define ASSERT_F(e) UNIT_COMPARED(false,true,e,false,"",[](){})
#define ASSERT_EQ_C(e1,e2,e) UNIT_COMPARED(true,true,e1,e2,"",e)
#define ASSERT_NEQ_C(e1,e2,e) UNIT_COMPARED(true,false,e1,e2,"",e)
#define ASSERT_T_C(e,ec) UNIT_COMPARED(false,true,e,true,"",ec)
#define ASSERT_F_C(e,ec) UNIT_COMPARED(false,true,e,false,"",ec)
#define ASSERT_EQ_MSG(e1,e2,msg) UNIT_COMPARED(true,true,e1,e2,msg,[](){})
#define ASSERT_NEQ_MSG(e1,e2,msg) UNIT_COMPARED(true,false,e1,e2,msg,[](){})
#define ASSERT_T_MSG(e,msg) UNIT_COMPARED(false,true,e,true,msg,[](){})
#define ASSERT_F_MSG(e,msg) UNIT_COMPARED(false,true,e,false,msg,[](){})
//#define IF_BREAK(e) if(!e) return;
#define IF_BREAK(e) if(!e) throw sweet::Unit::UnitEx();
#define ENF_T(e) UNIT_COMPARED_DIE(false,true,e,true,"",[](){},false)
#define ENF_F(e) UNIT_COMPARED_DIE(false,false,e,false,"",[](){},false)
#define ENF_EQ(e,e2) UNIT_COMPARED_DIE(true,true,e,e2,"",[](){},false)
#define ENF_NEQ(e,e2) UNIT_COMPARED_DIE(true,false,e,e2,"",[](){},false)
#define ENF_T_MSG(e,msg) UNIT_COMPARED_DIE(false,true,e,true,msg,[](){},false)
#define ENF_F_MSG(e,msg) UNIT_COMPARED_DIE(false,false,e,false,msg,[](){},false)
#define ENF_EQ_MSG(e,e2,msg) UNIT_COMPARED_DIE(true,true,e,e2,msg,[](){},false)
#define ENF_NEQ_MSG(e,e2,msg) UNIT_COMPARED_DIE(true,false,e,e2,msg,[](){},false)
#define ENF_T_C(e,c) UNIT_COMPARED_DIE(false,true,e,true,"",c,false)
#define ENF_F_C(e,c) UNIT_COMPARED_DIE(false,false,e,false,"",c,false)
#define ENF_EQ_C(e,e2,c) UNIT_COMPARED_DIE(true,true,e,e2,"",c,false)
#define ENF_NEQ_C(e,e2,c) UNIT_COMPARED_DIE(true,false,e,e2,"",c,false)
#define ENFR_T(e) if(!UNIT_COMPARED_DIE(false,true,e,true,"",[](){},false)) return false;
#define ENFR_F(e) if(!UNIT_COMPARED_DIE(false,false,e,false,"",[](){},false)) return false;
#define ENFR_EQ(e,e2) if(!UNIT_COMPARED_DIE(true,true,e,e2,"",[](){},false)) return false;
#define ENFR_NEQ(e,e2) if(!UNIT_COMPARED_DIE(true,false,e,e2,"",[](){},false)) return false;
#define ENFR_T_MSG(e,msg) if(!UNIT_COMPARED_DIE(false,true,e,true,msg,[](){},false)) return false;
#define ENFR_F_MSG(e,msg) if(!UNIT_COMPARED_DIE(false,false,e,false,msg,[](){},false)) return false;
#define ENFR_EQ_MSG(e,e2,msg) if(!UNIT_COMPARED_DIE(true,true,e,e2,msg,[](){},false)) return false;
#define ENFR_NEQ_MSG(e,e2,msg) if(!UNIT_COMPARED_DIE(true,false,e,e2,msg,[](){},false)) return false;
#define ENFR_T_C(e,c) if(!UNIT_COMPARED_DIE(false,true,e,true,"",c,false)) return false;
#define ENFR_F_C(e,c) if(!UNIT_COMPARED_DIE(false,false,e,false,"",c,false)) return false;
#define ENFR_EQ_C(e,e2,c) if(!UNIT_COMPARED_DIE(true,true,e,e2,"",c,false)) return false;
#define ENFR_NEQ_C(e,e2,c) if(!UNIT_COMPARED_DIE(true,false,e,e2,"",c,false)) return false;
#define UNIT_COMPARE(compare,result,e1,e2,msg,c); this->increNumAsserts(); \
IF_BREAK (sweet::Unit::Unittest::evaluate(compare,result, e1, e2, #e1, #e2,\
__FILE__, __LINE__ ,msg,c))
#define UNIT_COMPARED(compare,result,e1,e2,msg,c) \
sweet::Unit::Unittest::evaluates(compare, result, e1, e2, #e1, #e2,__FILE__, __LINE__,\
&std::cout, "", true, msg, c)
#define UNIT_COMPARED_DIE(compare,result,e1,e2,msg,c,die) \
sweet::Unit::Unittest::evaluates(compare, result, e1, e2, #e1, #e2,__FILE__, __LINE__,\
&std::cout, "", die, msg, c)
namespace sweet {
namespace Unit {
using namespace std;
static inline string sname(const string& str) {
size_t idx(str.rfind('/'));
if(idx != string::npos) {
string ret(str);
ret.erase(0, idx+1);
return ret;
} else
return string(str);
}
class UnitEx : public std::exception {
};
template<typename T, class Enable = void>
struct Gen;
template<typename T>
struct Gen<T, typename std::enable_if<std::is_integral<T>::value>::type> {
T l, h;
std::uniform_int_distribution<T> distribution;
Gen(T ln = std::numeric_limits<T>::min(),
T hn = std::numeric_limits<T>::max()) : l(ln), h(hn), distribution(ln,hn)
{}
template<typename G>
T operator()(G& gen) {
return this->distribution(gen);
}
};
template<typename T>
struct Gen<T, typename std::enable_if<std::is_floating_point<T>::value >::type> {
T l, h;
std::uniform_real_distribution<T> distribution;
Gen(T ln = std::numeric_limits<T>::min(),
T hn = std::numeric_limits<T>::max()) : l(ln), h(hn), distribution(ln,hn)
{}
template<typename G>
T operator()(G& gen) {
return this->distribution(gen);
}
};
template<bool>
struct comp_sel {
template<typename T, typename S>
static bool comp(const T t, const S s) {
return t == s;
}
};
template<>
struct comp_sel<true> {
template<typename T, typename S>
static bool comp(const T t, const S s) {
if(std::numeric_limits<T>::infinity() == t &&
std::numeric_limits<S>::infinity() == s) {
return true;
}
return fabs(t - s) <= 0.000001;
}
};
class Unittest;
inline vector<Unittest*>& getTests() {
static vector<Unittest*> tests;
return tests;
}
inline unsigned& getNumOfAsserts() {
static unsigned numAsserts;
return numAsserts;
}
class Unittest {
public:
inline Unittest(const char* name, const char* f, int l, int count = 1,
const char* more = "") : file(f),
line(l), name_(name), info(more), numRounds(count),
errors_(0), out_(&cerr)
{
getTests().push_back(this);
}
virtual inline ~Unittest() {}
inline void increNumAsserts() {
getNumOfAsserts()++;
}
#ifdef SWEET_NO_ASSERTS
template<typename E1, typename E2, typename C, typename S>
static bool evaluates(bool , bool , const E1& ,
const E2& , const char* , const char* ,
const char* , int , ostream* , const char* ,
bool , S, C ) {
return true;
}
#else
template<typename E1, typename E2, typename C, typename S>
static bool evaluates(bool compare, bool result, const E1& e1,
const E2& e2, const char* str1, const char* str2,
const char* evalfile, int evalline, ostream* out, const char* name,
bool die, S msg, C c)
{
if(result ?
(comp_sel<is_floating_point<E1>::value>::comp(e1, e2)) :
(!comp_sel<is_floating_point<E1>::value>::comp(e1, e2)))
{
return true;
}
//if(name != "") {
if(strlen(name) > 0) {
*out<<sname(evalfile)<< ":"<<evalline<<" in "<<"Unittest("<<name<<
") Assert Failed: ";
} else {
*out<<sname(evalfile)<< ":"<<evalline<<" Assert Failed: ";
}
stringstream s2;
s2<<std::fixed<<std::setprecision(9)<<boolalpha<<(e2);
if(compare) {
stringstream s1;
s1<<std::fixed<<std::setprecision(9)<<boolalpha<<(e1);
const string cmp(result ? "==" : "!=");
*out<<"compare {"<<str1<<"} "<< cmp<<" {"<<str2<<"} "
<<"got {\""<<s1.str()<<"\"} "<<cmp<<" {\""<<s2.str()
<< "\"}";
} else {
*out<<"evalute {"<<str1<<"} == "<<s2.str();
}
*out<<' '<<msg<<endl;
c();
if(die) {
exit(1);
}
return false;
}
#endif
template<typename E1, typename E2, typename C, typename S>
bool evaluate(bool compare, bool result, const E1& e1, const E2& e2,
const char* str1, const char* str2, const char* evalfile, int evalline,
S msg, C c)
{
bool rlst = Unittest::evaluates<E1,E2,C>(compare, result, e1, e2,
str1, str2, evalfile, evalline, out_, name_, false, msg, c);
if(!rlst) {
++errors_;
}
return rlst;
}
#ifdef SWEET_NO_UNITTEST
virtual inline void run_impl(const size_t __attribute__((unused)),size_t& __attribute__((unused))) {}
virtual void nevercall(const size_t __attribute__((unused)),size_t& __attribute__((unused))) = 0;
#else
virtual void run_impl(const size_t __attribute__((unused)),size_t& __attribute__((unused))) = 0;
#endif
inline bool run() {
size_t localCnt = 0;
this->sectionsCnt = 0;
run_impl(0, localCnt);
if(errors_) {
return errors_;
}
const size_t numSections = localCnt+1;
for(size_t i = 1; i < numSections; ++i) {
localCnt = 0;
this->sectionsCnt = i;
run_impl(i, localCnt);
if(errors_) {
return errors_;
}
}
return errors_;
}
inline void setOutputStream(ostream* o) {
out_ = o;
}
const char* file;
int line;
const char* name_;
const char* info;
int numRounds;
size_t sectionsCnt;
std::string sectionName;
private:
int errors_;
ostream* out_;
};
inline unsigned runTests(std::string benmarkrslt =
"UnittestBenchmarkResult.ben")
{
char timeStr[100];
std::time_t now_time = std::time(NULL);
std::strftime(timeStr, 100, "%Y:%m:%d-%H:%M:%S",
std::localtime(&now_time));
unsigned rs = 0;
for(vector<Unittest*>::iterator it = getTests().begin(); it !=
getTests().end(); ++it) {
std::chrono::time_point<std::chrono::system_clock> strt(
std::chrono::system_clock::now());
try {
for(int i = 0; i < (*it)->numRounds; ++i) {
bool tmp = (*it)->run();
rs += tmp;
}
} catch(UnitEx&) {
++rs;
} catch(std::exception& e) {
std::cerr<<sname((*it)->file)<<":"<<(*it)->line<<" Unittest"<<
(*it)->name_<<" has thrown an "<< "uncaught exception "<<
" with message "<<e.what()<<std::endl;
++rs;
} catch(...) {
std::cerr<<sname((*it)->file)<<":"<<(*it)->line<<" Unittest"<<
(*it)->name_<<" has thrown an "<< "uncaught exception "
<<std::endl;
++rs;
}
std::chrono::time_point<std::chrono::system_clock> stp(
std::chrono::system_clock::now());
if((*it)->numRounds > 1) {
std::ofstream o(benmarkrslt, std::ios::app);
o<<(*it)->name_<<':'<<(*it)->line<<':'<<sname((*it)->file)<<' '<<
timeStr<<' '<<(strcmp((*it)->info, "") ? "" :
std::string((*it)->info) + " ")<<
std::chrono::duration_cast<std::chrono::milliseconds>(
stp-strt
).count()<<std::endl;
}
}
return rs;
}
}
}