-
Notifications
You must be signed in to change notification settings - Fork 2
/
unit_test_framework.hpp
751 lines (656 loc) · 24.1 KB
/
unit_test_framework.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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
#ifndef UNIT_TEST_FRAMEWORK_HPP
#define UNIT_TEST_FRAMEWORK_HPP
// C++ unit-test framework inspired by xUnit and its derivatives.
//
// Authors:
// James Perretta <[email protected]>
// Amir Kamil <[email protected]>
//
// See https://github.com/eecs280staff/unit_test_framework for
// documentation and updates.
//
// License information:
//
// The MIT License (MIT)
//
// Copyright 2017-2024 James Perretta and Amir Kamil
//
// 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.
#include <map>
#include <utility>
#include <string>
#include <iostream>
#include <sstream>
#include <cmath>
#include <memory>
#include <vector>
#include <typeinfo>
#include <type_traits>
#include <cstdlib>
#include <iterator>
#include <algorithm>
#include <exception>
#include <stdexcept>
#if UNIT_TEST_ENABLE_REGEXP
# include <regex>
#endif
// For compatibility with Visual Studio
#include <iso646.h> // ciso646 removed in C++20
// For demangling type names
#if defined(__clang__) || defined(__GLIBCXX__) || defined(__GLIBCPP__)
# include <cxxabi.h>
# include <cstdlib>
#endif
// Place the following line of code in your test file to generate a
// main() function:
// TEST_MAIN()
#define TEST(name) \
static void name(); \
static unit_test_framework::TestRegisterer \
register_##name((#name), name); \
static void name()
#define TEST_MAIN() \
int main(int argc, char** argv) { \
return \
unit_test_framework::TestSuite::get().run_tests(argc, argv); \
} \
TEST_SUITE_INSTANCE();
#define ASSERT_EQUAL(first, second) \
unit_test_framework::Assertions::assert_equal( \
(first), (second), __LINE__, \
"ASSERT_EQUAL(" #first ", " #second ")" \
);
#define ASSERT_NOT_EQUAL(first, second) \
unit_test_framework::Assertions::assert_not_equal( \
(first), (second), __LINE__, \
"ASSERT_NOT_EQUAL(" #first ", " #second ")" \
);
#define ASSERT_SEQUENCE_EQUAL(first, second) \
unit_test_framework::Assertions::assert_sequence_equal( \
(first), (second), __LINE__, \
"ASSERT_SEQUENCE_EQUAL(" #first ", " #second ")" \
);
#define ASSERT_TRUE(value) \
unit_test_framework::Assertions::assert_true( \
(value), __LINE__, "ASSERT_TRUE(" #value ")" \
);
#define ASSERT_FALSE(value) \
unit_test_framework::Assertions::assert_false( \
(value), __LINE__, "ASSERT_FALSE(" #value ")" \
);
#define ASSERT_ALMOST_EQUAL(first, second, precision) \
unit_test_framework::Assertions::assert_almost_equal( \
(first), (second), (precision), __LINE__, \
"ASSERT_ALMOST_EQUAL(" #first ", " #second ", " \
#precision ")" \
);
// -----------------------------------------------------------------------------
namespace unit_test_framework {
using Test_func_t = void (*)();
class ExitSuite : public std::exception {
public:
ExitSuite(int status_ = 0) : status(status_) {}
int status;
};
class SetComplete {
public:
SetComplete(bool& incomplete_) : incomplete(incomplete_) {
incomplete = true;
}
~SetComplete() {
incomplete = false;
}
private:
bool& incomplete;
};
class TestFailure {
public:
TestFailure(std::string reason, int line_number, const char* assertion_text)
: reason_m(std::move(reason)), line_number_m(line_number),
assertion_text_m(assertion_text) {}
std::ostream& print(std::ostream& os) const {
os << "In " << assertion_text_m << ", line " << line_number_m << ":\n"
<< reason_m << '\n';
return os;
}
std::string to_string() const {
std::ostringstream oss;
print(oss);
return oss.str();
}
friend std::ostream& operator<<(std::ostream& os,
const TestFailure& test_failure) {
return test_failure.print(os);
}
private:
std::string reason_m;
int line_number_m;
const char* assertion_text_m;
};
// ---------------------------------------------------------------------------
class Diagnostic {
public:
// Demangles a string produced by std::type_info::name.
static std::string demangle(const char* typeinfo_name) {
#if defined(__clang__) || defined(__GLIBCXX__) || defined(__GLIBCPP__)
int status = 0;
char* demangled =
abi::__cxa_demangle(typeinfo_name, nullptr, nullptr, &status);
if (status == 0) {
std::string result = demangled;
std::free(demangled);
return result;
}
else {
return typeinfo_name;
}
#else
return typeinfo_name;
#endif // defined(__clang__) || defined(__GLIBCXX__) || defined(__GLIBCPP__)
}
// Attempts to print the given object to the given stream.
// If T has an available stream insertion operator overload, that
// operator is used. Otherwise, a generic representation of the object
// is printed to os.
template <class T>
static std::ostream& print(std::ostream& os, const T& t) {
// The extra parameters are needed so that the first overload of
// print_helper is preferred, followed by the third one.
return print_helper(os, t, 0, 0);
}
private:
// This version of print_helper will be called when T has an available
// stream insertion operator overload.
template <class T>
static auto print_helper(std::ostream& os, const T& t, int, int)
-> decltype(os << t)& {
return os << t;
}
// This version of print_helper will be called when T is a pair.
template <class First, class Second>
static auto print_helper(std::ostream& os,
const std::pair<First, Second>& t, int,
int)
-> decltype(print(os, t.first), print(os, t.second))& {
os << '(';
print(os, t.first);
os << ',';
print(os, t.second);
return os << ')';
}
// Helper function to print a sequence.
template <class Sequence>
static auto print_sequence_helper(std::ostream &os, const Sequence& seq)
-> decltype(print(os, (*std::begin(seq), *std::end(seq))))& {
if (std::begin(seq) == std::end(seq)) {
return os << "{}";
}
auto it = std::begin(seq);
os << "{ ";
print(os, *it);
for (++it; it != std::end(seq); ++it) {
os << ", ";
print(os, *it);
}
return os << " }";
}
// This version of print_helper will be called when T is a sequence.
template <class Sequence>
static auto print_helper(std::ostream& os, const Sequence& seq, int, ...)
-> decltype(print(os, *seq.begin()), print(os, *seq.end()))& {
return print_sequence_helper(os, seq);
}
// This version of print_helper will be called when T is a non-char array.
// This is separate from the sequence overload so that printing an
// array as a sequence is preferred over printing it as a pointer
// (using the first overload).
template <class Elem, std::size_t N>
static std::ostream& print_helper(std::ostream& os, const Elem (&arr)[N],
int, int) {
return print_sequence_helper(os, arr);
}
// This version of print_helper will be called when T is a char array.
// If the array contains a null terminator, it is printed as a string.
// Otherwise, it is printed as a sequence.
template <std::size_t N>
static std::ostream& print_helper(std::ostream& os, const char (&arr)[N],
int, int) {
for (std::size_t i = 0; i < N; ++i) {
if (!arr[i]) {
return os << arr;
}
}
return print_sequence_helper(os, arr);
}
// This version of print_helper will be called when T does not have an
// available stream insertion operator overload.
template <class T>
static std::ostream& print_helper(std::ostream& os, const T&, ...) {
return os << "<" << demangle(typeid(T).name()) << " object>";
}
};
// ---------------------------------------------------------------------------
struct TestCase {
TestCase(const std::string& name_, Test_func_t test_func_)
: name(name_), test_func(test_func_) {}
void run(bool quiet_mode) {
try {
if (not quiet_mode) {
std::cout << "Running test: " << name << std::endl;
}
test_func();
if (not quiet_mode) {
std::cout << "PASS" << std::endl;
}
}
catch (TestFailure& failure) {
failure_msg = failure.to_string();
if (not quiet_mode) {
std::cout << "FAIL" << std::endl;
}
}
catch (std::exception& e) {
std::ostringstream oss;
oss << "Uncaught " << Diagnostic::demangle(typeid(e).name())
<< " in test \"" << name << "\": \n";
oss << e.what() << '\n';
exception_msg = oss.str();
if (not quiet_mode) {
std::cout << "ERROR" << std::endl;
}
}
}
void print(bool quiet_mode) {
if (quiet_mode) {
std::cout << name << ": ";
}
else {
std::cout << "** Test case \"" << name << "\": ";
}
if (not failure_msg.empty()) {
std::cout << "FAIL" << std::endl;
if (not quiet_mode) {
std::cout << failure_msg << std::endl;
}
}
else if (not exception_msg.empty()) {
std::cout << "ERROR" << std::endl;
if (not quiet_mode) {
std::cout << exception_msg << std::endl;
}
}
else {
std::cout << "PASS" << std::endl;
}
}
std::string name;
Test_func_t test_func;
std::string failure_msg{};
std::string exception_msg{};
};
class TestSuite {
public:
static TestSuite& get() {
if (not instance) {
instance = new TestSuite;
}
return *instance;
}
void add_test(const std::string& test_name, Test_func_t test) {
tests_.insert({test_name, TestCase{test_name, test}});
}
int run_tests(int argc, char** argv) {
SetComplete completer(TestSuite::incomplete);
std::vector<std::string> test_names_to_run;
try {
test_names_to_run = get_test_names_to_run(argc, argv);
}
catch (ExitSuite& e) {
return e.status;
}
for (auto test_name : test_names_to_run) {
if (tests_.find(test_name) == end(tests_)) {
throw std::runtime_error("Test " + test_name + " not found");
}
}
for (auto test_name : test_names_to_run) {
tests_.at(test_name).run(quiet_mode);
}
std::cout << "\n*** Results ***" << std::endl;
for (auto test_name : test_names_to_run) {
tests_.at(test_name).print(quiet_mode);
}
auto num_failures =
std::count_if(tests_.begin(), tests_.end(),
[](std::pair<std::string, TestCase> test_pair) {
return not test_pair.second.failure_msg.empty();
});
auto num_errors =
std::count_if(tests_.begin(), tests_.end(),
[](std::pair<std::string, TestCase> test_pair) {
return not test_pair.second.exception_msg.empty();
});
if (not quiet_mode) {
std::cout << "*** Summary ***" << std::endl;
std::cout << "Out of " << test_names_to_run.size()
<< " tests run:" << std::endl;
std::cout << num_failures << " failure(s), " << num_errors
<< " error(s)" << std::endl;
}
if (num_failures == 0 and num_errors == 0) {
return 0;
}
return 1;
}
void print_results();
void enable_quiet_mode() {
quiet_mode = true;
}
std::ostream& print_test_names(std::ostream& os) {
for (const auto& test_pair : tests_) {
os << test_pair.first << '\n';
}
return os;
}
friend class TestSuiteDestroyer;
private:
TestSuite() {
auto func = []() {
if (TestSuite::incomplete) {
std::cout << "ERROR: premature call to exit()" << std::endl;
std::abort();
}
};
std::atexit(func);
#ifdef _GLIBCXX_HAVE_AT_QUICK_EXIT
std::at_quick_exit(func);
#endif
}
TestSuite(const TestSuite&) = delete;
bool operator=(const TestSuite&) = delete;
~TestSuite() {}
std::vector<std::string> get_test_names_to_run(int argc, char** argv) {
std::vector<std::string> test_names_to_run;
#if UNIT_TEST_ENABLE_REGEXP
bool regexp_matching = false;
#endif
for (auto i = 1; i < argc; ++i) {
if (argv[i] == std::string("--show_test_names") or
argv[i] == std::string("-n")) {
TestSuite::get().print_test_names(std::cout);
std::cout << std::flush;
throw ExitSuite();
}
else if (argv[i] == std::string("--quiet") or
argv[i] == std::string("-q")) {
TestSuite::get().enable_quiet_mode();
}
#if UNIT_TEST_ENABLE_REGEXP
else if (argv[i] == std::string("--regexp") or
argv[i] == std::string("-e")) {
regexp_matching = true;
}
#endif
else if (argv[i] == std::string("--help") or
argv[i] == std::string("-h")) {
std::cout << "usage: " << argv[0]
#if UNIT_TEST_ENABLE_REGEXP
<< " [-h] [-e] [-n] [-q] [[TEST_NAME] ...]\n";
#else
<< " [-h] [-n] [-q] [[TEST_NAME] ...]\n";
#endif
std::cout
<< "optional arguments:\n"
<< " -h, --help\t\t show this help message and exit\n"
#if UNIT_TEST_ENABLE_REGEXP
<< " -e, --regexp\t\t treat TEST_NAME as a regular expression\n"
#endif
<< " -n, --show_test_names\t print the names of all "
"discovered test cases and exit\n"
<< " -q, --quiet\t\t print a reduced summary of test results\n"
<< " TEST_NAME ...\t\t run only the test cases whose names "
"are "
"listed here. Note: If no test names are specified, all "
"discovered tests are run by default."
<< std::endl;
throw ExitSuite();
}
else {
test_names_to_run.push_back(argv[i]);
}
}
if (test_names_to_run.empty()) {
std::transform(
std::begin(tests_), std::end(tests_),
std::back_inserter(test_names_to_run),
[](const std::pair<std::string, TestCase>& p) {
return p.first;
});
}
#if UNIT_TEST_ENABLE_REGEXP
else if (regexp_matching) {
std::ostringstream pattern;
for (auto iter = test_names_to_run.begin();
iter != test_names_to_run.end(); ++iter) {
if (iter != test_names_to_run.begin()) {
pattern << "|";
}
pattern << "(" << *iter << ")";
}
std::regex name_regex{pattern.str()};
test_names_to_run.clear();
for (const auto& test_pair : tests_) {
if (std::regex_match(test_pair.first, name_regex)) {
test_names_to_run.push_back(test_pair.first);
}
}
}
#endif
return test_names_to_run;
}
static TestSuite* instance;
std::map<std::string, TestCase> tests_;
bool quiet_mode = false;
static bool incomplete;
};
class TestSuiteDestroyer {
public:
~TestSuiteDestroyer() {
delete TestSuite::instance;
}
};
class TestRegisterer {
public:
TestRegisterer(const std::string& test_name, Test_func_t test) {
TestSuite::get().add_test(test_name, test);
}
};
// ---------------------------------------------------------------------------
class Assertions {
private:
// Template logic to produce a static assertion failure when comparing
// incomparable types.
template <typename First, typename Second, typename = void>
struct is_equality_comparable : std::false_type {};
template <typename First, typename Second>
using enable_if_equality_comparable = typename std::enable_if<
std::is_same<bool, decltype(std::declval<First>() ==
std::declval<Second>())>::value and
std::is_same<bool, decltype(std::declval<First>() !=
std::declval<Second>())>::value and
(!std::is_array<typename std::remove_reference<First>::type>::value or
!std::is_array<typename std::remove_reference<Second>::type>::value),
void>::type;
template <typename First, typename Second>
struct is_equality_comparable<First, Second,
enable_if_equality_comparable<First, Second>>
: std::true_type {};
// Overloads for equality comparisons.
template <typename First, typename Second>
static bool safe_equals_helper(const First& first, const Second& second) {
return first == second;
}
template <typename First, typename Second>
static bool safe_not_equals_helper(const First& first,
const Second& second) {
return first != second;
}
// Allow size_t to correctly be compared to int.
static bool safe_equals_helper(std::size_t first, int second) {
return second >= 0 && static_cast<long long>(first) == second;
}
static bool safe_equals_helper(int first, std::size_t second) {
return first >= 0 && first == static_cast<long long>(second);
}
static bool safe_not_equals_helper(std::size_t first, int second) {
return second < 0 || static_cast<long long>(first) != second;
}
static bool safe_not_equals_helper(int first, std::size_t second) {
return first < 0 || first != static_cast<long long>(second);
}
template <typename First, typename Second, typename = void>
struct safe_equals {
static_assert(is_equality_comparable<First, Second>::value,
"types cannot be compared with == and !=");
static bool equals(const First& first, const Second& second) {
return safe_equals_helper(first, second);
}
static bool not_equals(const First& first, const Second& second) {
return safe_not_equals_helper(first, second);
}
};
public:
template <typename First, typename Second>
static void assert_equal(First&& first, Second&& second,
int line_number,
const char* assertion_text) {
if (safe_equals<First, Second>::equals(first, second)) {
return;
}
std::ostringstream reason;
Diagnostic::print(reason, first);
reason << " != ";
Diagnostic::print(reason, second);
throw TestFailure(reason.str(), line_number, assertion_text);
}
template <typename First, typename Second>
static void assert_not_equal(First&& first, Second&& second,
int line_number,
const char* assertion_text) {
if (safe_equals<First, Second>::not_equals(first, second)) {
return;
}
std::ostringstream reason;
reason << "Values unexpectedly equal: ";
Diagnostic::print(reason, first);
reason << " == ";
Diagnostic::print(reason, second);
throw TestFailure(reason.str(), line_number, assertion_text);
}
template <typename First, typename Second>
static void assert_sequence_equal(First&& first, Second&& second,
int line_number,
const char* assertion_text) {
using std::begin;
using std::end;
auto it1 = begin(first);
auto it2 = begin(second);
auto end1 = end(first);
auto end2 = end(second);
auto len1 = std::distance(it1, end1);
auto len2 = std::distance(it2, end2);
if (len1 != len2) { // different number of elements
std::ostringstream reason;
Diagnostic::print(reason, first);
reason << " != ";
Diagnostic::print(reason, second);
reason << " (sizes differ: " << len1 << " != " << len2 << ")";
throw TestFailure(reason.str(), line_number, assertion_text);
}
bool equal = true;
std::size_t position = 0;
for (; it1 != end1 and it2 != end2; ++it1, ++it2, ++position) {
if (not safe_equals<decltype(*it1),
decltype(*it2)>::equals(*it1, *it2)) {
equal = false;
break;
}
}
if (not equal) {
std::ostringstream reason;
Diagnostic::print(reason, first);
reason << " != ";
Diagnostic::print(reason, second);
reason << " (elements at position " << position << " differ: ";
Diagnostic::print(reason, *it1);
reason << " != ";
Diagnostic::print(reason, *it2);
reason << ")";
throw TestFailure(reason.str(), line_number, assertion_text);
}
}
static void assert_true(bool value, int line_number,
const char* assertion_text) {
if (value) {
return;
}
std::ostringstream reason;
reason << "Expected true, but was false";
throw TestFailure(reason.str(), line_number, assertion_text);
}
static void assert_false(bool value, int line_number,
const char* assertion_text) {
if (not value) {
return;
}
std::ostringstream reason;
reason << "Expected false, but was true";
throw TestFailure(reason.str(), line_number, assertion_text);
}
static void assert_almost_equal(double first, double second,
double precision, int line_number,
const char* assertion_text) {
if (std::abs(first - second) <= precision) {
return;
}
std::ostringstream reason;
// For now, we'll just set the precision arbitrarily high.
// In the future, we may decide to add an option to configure
// the output precision.
reason.precision(20);
reason << "Values too far apart: " << first << " and " << second;
throw TestFailure(reason.str(), line_number, assertion_text);
}
};
} // namespace unit_test_framework
//------------------------------------------------------------------------------
// THIS IS PART OF A WORKAROUND TO DEAL WITH STATIC INITIALIZATION
// SHENANIGANS.
// DO NOT CHANGE THIS UNLESS YOU REEEEALLY KNOW WHAT YOU'RE DOING.
// CONTACT [email protected] or [email protected] IF YOU HAVE QUESTIONS
// ABOUT THIS.
#define TEST_SUITE_INSTANCE() \
static unit_test_framework::TestSuiteDestroyer destroyer; \
bool unit_test_framework::TestSuite::incomplete = false; \
unit_test_framework::TestSuite* \
unit_test_framework::TestSuite::instance = \
&unit_test_framework::TestSuite::get()
#endif // UNIT_TEST_FRAMEWORK_HPP