-
Notifications
You must be signed in to change notification settings - Fork 0
/
btl.h
227 lines (184 loc) · 8.65 KB
/
btl.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/**
* Copyright (c) 2022 W. Akira Mizutani
*
* 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.
*/
/*
For more information on how to use this library, please visit: https://github.com/aki-cat/btl
*/
#ifndef BELTINOS_TEST_LIBRARY_H_
#define BELTINOS_TEST_LIBRARY_H_
#include <cfloat>
#include <cmath>
#include <cstdint>
#include <functional>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
namespace std {
// Some std::to_string() extensions
const string& to_string(const string& s) {
return s;
}
string to_string(const bool& value) {
return value ? "true" : "false";
}
// I recommend implementing std::to_string for your types.
} // namespace std
namespace btl {
// NUMBER CONSTANTS
// Espilons are different (and more lenient) than the default ones because for most mathematical
// uses, float is naturally innaccurate, and there's nothing we can do about it.
// If you don't like that, feel free to use different epsilon values by altering these.
#define BTL_FLOAT_EPSILON 1e-5f;
#define BTL_DOUBLE_EPSILON 1e-7;
// OUTPUT COLOR CODES
#define BTL_LOCATION_TEXT_COLOUR "\033[94;1m"
#define BTL_FAILURE_TEXT_COLOUR "\033[91m"
#define BTL_NORMAL_TEXT_COLOUR "\033[0m"
#define BTL_NORMAL_TEXT_COLOUR_BOLD "\033[0;1m"
#define BTL_SUCCESS_TEXT_COLOUR "\033[92;1m"
#define BTL_CLASS_TEXT_COLOUR "\033[95;1m"
// UTILITY
#define BTL_SYMBOL_STRING(SYMBOL) #SYMBOL
#define BTL_GET_SYMBOL_NAME(SYMBOL) BTL_SYMBOL_STRING(SYMBOL)
#define BTL_GET_LOCATION_MESSAGE \
BTL_LOCATION_TEXT_COLOUR __FILE__ "(" BTL_GET_SYMBOL_NAME(__LINE__) ")" \
":" BTL_NORMAL_TEXT_COLOUR
#define BTL_ASSERTION_FAIL_MESSAGE \
"\n\t" BTL_GET_LOCATION_MESSAGE BTL_FAILURE_TEXT_COLOUR \
" Assertion failed! " \
"❌ " BTL_NORMAL_TEXT_COLOUR
#define BTL_PRINT_TEST_SUCCESS() \
std::cout << BTL_SUCCESS_TEXT_COLOUR "OK!" BTL_NORMAL_TEXT_COLOUR << std::endl
#define BTL_PRINT_TEST_FAILURE(ERROR_MSG) \
std::cerr << BTL_ASSERTION_FAIL_MESSAGE << ERROR_MSG << std::endl << std::endl
#define BTL_PRINT_TEST_DESCRIPTION() \
std::cout << "\t" << BTL_CLASS_TEXT_COLOUR << btl::TestRunner<class_t>::CLASS_NAME \
<< BTL_NORMAL_TEXT_COLOUR << "::" << CURRENT_TEST << " "
#define BTL_ASSERT(EXPRESSION, ERROR_MSG) \
BTL_PRINT_TEST_DESCRIPTION(); \
if (!(EXPRESSION)) { \
btl::_BTL_ERROR_COUNT++; \
BTL_PRINT_TEST_FAILURE(ERROR_MSG); \
} else { \
BTL_PRINT_TEST_SUCCESS(); \
}
// END OF INTERNAL UTILITY MACROS
// This is public, but try to use btl::has_errors() instead.
static size_t _BTL_ERROR_COUNT = 0;
static bool has_errors() {
return _BTL_ERROR_COUNT > 0;
}
template <typename T1, typename T2>
bool are_equal(const T1& a, const T2& b) {
return a == b;
}
template <>
bool are_equal(const float& a, const float& b) {
return std::fabs(a - b) <= BTL_FLOAT_EPSILON;
}
template <>
bool are_equal(const double& a, const double& b) {
return std::fabs(a - b) <= BTL_DOUBLE_EPSILON;
}
template <typename T>
class TestRunner {
public:
TestRunner();
static const char* CLASS_NAME;
static void run() {
const TestRunner<T> runner = TestRunner<T>();
for (const auto& test : runner._tests) {
test();
}
std::cout << std::endl;
}
private:
using class_t = T;
std::vector<std::function<void(void)>> _tests;
};
#define ASSERT_ARE_EQUAL(VALUE, EXPECTED) \
{ \
std::stringstream stream; \
stream << std::to_string(EXPECTED) << " expected; got " << std::to_string(VALUE); \
BTL_ASSERT(btl::are_equal(VALUE, EXPECTED), stream.str()); \
}
#define ASSERT_ARE_SAME(VALUE, EXPECTED) \
{ \
void* value_ptr = reinterpret_cast<void*>(&VALUE); \
void* expected_ptr = reinterpret_cast<void*>(&EXPECTED); \
std::stringstream stream; \
stream << expected_ptr << " expected; got " << value_ptr; \
BTL_ASSERT(btl::are_equal(VALUE, EXPECTED), stream.str()); \
}
#define ASSERT_IS_TRUE(VALUE) ASSERT_ARE_EQUAL((VALUE), true);
#define ASSERT_IS_FALSE(VALUE) ASSERT_ARE_EQUAL((VALUE), false);
#define ASSERT_ARRAYS_ARE_EQUAL(VALUE, EXPECTED, RANGE_START, RANGE_LENGTH) \
{ \
BTL_PRINT_TEST_DESCRIPTION(); \
std::stringstream stream{}; \
stream << std::endl; \
bool success = true; \
for (size_t idx = RANGE_START; idx < RANGE_LENGTH; idx++) { \
if (!(btl::are_equal(VALUE[idx], EXPECTED[idx]))) { \
success = false; \
stream << "\t\t* " << std::to_string(EXPECTED[idx]) << " expected at index #" \
<< idx << "; got " << std::to_string(VALUE[idx]) << std::endl; \
} \
} \
if (success) { \
BTL_PRINT_TEST_SUCCESS(); \
} else { \
_BTL_ERROR_COUNT++; \
BTL_PRINT_TEST_FAILURE(stream.str()); \
}; \
}
#define DESCRIBE_CLASS(CLASS_TYPE) \
template <> \
const char* btl::TestRunner<CLASS_TYPE>::CLASS_NAME = #CLASS_TYPE; \
template <> \
btl::TestRunner<CLASS_TYPE>::TestRunner() : _tests()
#define DESCRIBE_TEST(METHOD, SITUATION, EXPECTATION) \
_tests.push_back([]() {}); \
_tests[_tests.size() - 1] = [CURRENT_TEST = BTL_NORMAL_TEXT_COLOUR_BOLD #METHOD \
BTL_NORMAL_TEXT_COLOUR " should " #EXPECTATION \
" when " #SITUATION]()
// Undefine what can be undefined
#undef BTL_FLOAT_EPSILON
#undef BTL_DOUBLE_EPSILON
/*
These are for internal use only, but cannot be undef:
BTL_LOCATION_TEXT_COLOUR
BTL_FAILURE_TEXT_COLOUR
BTL_NORMAL_TEXT_COLOUR
BTL_NORMAL_TEXT_COLOUR_BOLD
BTL_SUCCESS_TEXT_COLOUR
BTL_CLASS_TEXT_COLOUR
BTL_SYMBOL_STRING
BTL_GET_SYMBOL_NAME
BTL_GET_LOCATION_MESSAGE
BTL_ASSERTION_FAIL_MESSAGE
BTL_PRINT_TEST_SUCCESS
BTL_PRINT_TEST_FAILURE
BTL_PRINT_TEST_DESCRIPTION
BTL_ASSERT
*/
} // namespace btl
#endif