Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Kitware
GitHub Repository: Kitware/CMake
Path: blob/master/Help/guide/tutorial/Step10/SimpleTest/SimpleTest.h
5022 views
1
#pragma once
2
3
#include <cstdio>
4
#include <map>
5
#include <string_view>
6
7
namespace SimpleTest {
8
9
using TestFunc = void (*)();
10
11
using Registry = std::map<std::string_view, TestFunc, std::less<>>;
12
inline Registry g_registry;
13
14
inline Registry& registry()
15
{
16
return g_registry;
17
}
18
19
struct failure
20
{
21
char const* file;
22
int line;
23
char const* expr;
24
};
25
26
struct Registrar
27
{
28
template <std::size_t N>
29
Registrar(char const (&name)[N], TestFunc f)
30
{
31
auto [it, inserted] =
32
registry().emplace(std::string_view{ name, N ? (N - 1) : 0 }, f);
33
if (!inserted) {
34
std::printf("[ WARN ] duplicate test name: %.*s\n",
35
int(it->first.size()), it->first.data());
36
}
37
}
38
};
39
40
inline Registry const& all()
41
{
42
return registry();
43
}
44
inline TestFunc find(std::string_view name)
45
{
46
auto it = registry().find(name);
47
return it == registry().end() ? nullptr : it->second;
48
}
49
50
}
51
52
#define SIMPLETEST_CONCAT_(a, b) a##b
53
#define SIMPLETEST_CONCAT(a, b) SIMPLETEST_CONCAT_(a, b)
54
55
#define TEST(name_literal) \
56
static void SIMPLETEST_CONCAT(_simpletest_fn_, __LINE__)(); \
57
static ::SimpleTest::Registrar SIMPLETEST_CONCAT(_simpletest_reg_, \
58
__LINE__)( \
59
name_literal, &SIMPLETEST_CONCAT(_simpletest_fn_, __LINE__)); \
60
static void SIMPLETEST_CONCAT(_simpletest_fn_, __LINE__)()
61
62
// Minimal assertion
63
#define REQUIRE(expr) \
64
do { \
65
if (!(expr)) \
66
throw ::SimpleTest::failure{ __FILE__, __LINE__, #expr }; \
67
} while (0)
68
69
int main(int argc, char** argv)
70
{
71
using namespace ::SimpleTest;
72
73
std::string_view arg1 =
74
(argc >= 2) ? std::string_view{ argv[1] } : std::string_view{};
75
76
if (arg1 == "--list") {
77
bool first = true;
78
for (auto const& [name, _] : registry()) {
79
if (!first)
80
std::printf(",");
81
std::printf("%.*s", int(name.size()), name.data());
82
first = false;
83
}
84
std::printf("\n");
85
return 0;
86
}
87
88
if (arg1 == "--test") {
89
if (argc < 3) {
90
std::printf("usage: %s [--list] [--test <name>]\n", argv[0]);
91
return 2;
92
}
93
94
#ifdef SIMPLETEST_CONFIG
95
std::printf("SimpleTest built with config: %s\n", SIMPLETEST_CONFIG);
96
#endif
97
98
std::string_view name{ argv[2] };
99
auto it = registry().find(name);
100
if (it == registry().end()) {
101
std::printf("[ NOTFOUND ] %s\n", argv[2]);
102
return 2;
103
}
104
105
int failed = 0;
106
std::printf("[ RUN ] %.*s\n", int(it->first.size()),
107
it->first.data());
108
try {
109
it->second();
110
std::printf("[ OK] %.*s\n", int(it->first.size()),
111
it->first.data());
112
} catch (failure const& f) {
113
std::printf("[ FAILED ] %.*s at %s:%d : %s\n", int(it->first.size()),
114
it->first.data(), f.file, f.line, f.expr);
115
failed = 1;
116
} catch (...) {
117
std::printf("[ FAILED ] %.*s : unknown exception\n",
118
int(it->first.size()), it->first.data());
119
failed = 1;
120
}
121
return failed;
122
}
123
124
if (argc > 1) {
125
std::printf("usage: %s [--list] [--test <name>]\n", argv[0]);
126
return 2;
127
}
128
129
#ifdef SIMPLETEST_CONFIG
130
std::printf("SimpleTest built with config: %s\n", SIMPLETEST_CONFIG);
131
#endif
132
133
// Default: run all tests.
134
int failed = 0;
135
for (auto const& [name, func] : all()) {
136
std::printf("[ RUN ] %.*s\n", int(name.size()), name.data());
137
try {
138
func();
139
std::printf("[ OK ] %.*s\n", int(name.size()), name.data());
140
} catch (failure const& f) {
141
std::printf("[ FAILED ] %.*s at %s:%d : %s\n", int(name.size()),
142
name.data(), f.file, f.line, f.expr);
143
failed = 1;
144
} catch (...) {
145
std::printf("[ FAILED ] %.*s : unknown exception\n", int(name.size()),
146
name.data());
147
failed = 1;
148
}
149
}
150
return failed;
151
}
152
153