StormByte 3.0.0
C++26 foundation of the StormByte suite
Loading...
Searching...
No Matches
test_handlers.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2024-2026 David C. Manuelda (StormBytePP)
3 *
4 * This is the base for StormByte Suite libraries.
5 *
6 * StormByte original source is dual-licensed:
7 *
8 * 1. GNU Lesser General Public License v3.0 (or later)
9 * You may redistribute and/or modify this file under the terms of the
10 * GNU Lesser General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option)
12 * any later version.
13 *
14 * 2. Commercial license
15 * Alternatively, this file may be used under the terms of a commercial
16 * license agreement with the copyright holder
17 * (David C. Manuelda <StormByte@gmail.com>).
18 *
19 * Both licenses apply only to original StormByte source in this repository.
20 * They do not cover other StormByte modules or any third-party material
21 * shipped with this repository (including everything under thirdparty/),
22 * which remains under its own license.
23 *
24 * Neither license grants any patent rights. Any patent licenses required
25 * to use this software or third-party components must be obtained separately
26 * from the patent holders.
27 *
28 * StormByte is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU Lesser General Public License for more details.
32 *
33 * You should have received a copy of the GNU Lesser General Public License
34 * version 3 along with StormByte. If not, see
35 * <https://www.gnu.org/licenses/lgpl-3.0.html>.
36 *
37 * SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-StormByte-Commercial
38 */
39
40#pragma once
41
42#include <exception>
43#include <filesystem>
44#include <iostream>
45#include <ostream>
46#include <string_view>
47
49 template<typename T>
50 void PrintValue(std::ostream& stream, const T& value) {
51 if constexpr (requires { stream << value; }) {
52 stream << value;
53 } else {
54 stream << "<unprintable>";
55 }
56 }
57}
58
66#define RETURN_TEST(fn_result) do { \
67 const int stormbyte_test_result = (fn_result); \
68 if (stormbyte_test_result != 0) { \
69 std::cerr << "Test " << __func__ << " FAILED!" << std::endl; \
70 } \
71 return stormbyte_test_result; \
72} while (false)
73
78#define CurrentFileDirectory std::filesystem::path(__FILE__).parent_path()
79
86#define ASSERT_EQUAL(expected, actual) do { \
87 const auto& stormbyte_expected = (expected); \
88 const auto& stormbyte_actual = (actual); \
89 if (!(stormbyte_expected == stormbyte_actual)) { \
90 std::cerr << __func__ << ": Assertion failed at " << __FILE__ << ":" << __LINE__ << ": expected \""; \
91 ::StormByte::Test::Detail::PrintValue(std::cerr, stormbyte_expected); \
92 std::cerr << "\", got \""; \
93 ::StormByte::Test::Detail::PrintValue(std::cerr, stormbyte_actual); \
94 std::cerr << "\"" << std::endl; \
95 return 1; \
96 } \
97} while (false)
98
105#define ASSERT_NOT_EQUAL(expected, actual) do { \
106 const auto& stormbyte_expected = (expected); \
107 const auto& stormbyte_actual = (actual); \
108 if (stormbyte_expected == stormbyte_actual) { \
109 std::cerr << __func__ << ": Assertion failed at " << __FILE__ << ":" << __LINE__ << ": values should differ; both were \""; \
110 ::StormByte::Test::Detail::PrintValue(std::cerr, stormbyte_actual); \
111 std::cerr << "\"" << std::endl; \
112 return 1; \
113 } \
114} while (false)
115
121#define ASSERT_FALSE(condition) do { \
122 if ((condition)) { \
123 std::cerr << __func__ << ": Assertion failed at " << __FILE__ << ":" << __LINE__ << ": condition is true, expected false" << std::endl; \
124 return 1; \
125 } \
126} while (false)
127
133#define ASSERT_TRUE(condition) do { \
134 if (!(condition)) { \
135 std::cerr << __func__ << ": Assertion failed at " << __FILE__ << ":" << __LINE__ << ": condition is false, expected true" << std::endl; \
136 return 1; \
137 } \
138} while (false)
139
146#define ASSERT_THROWS(expression, exception_type) do { \
147 bool stormbyte_threw = false; \
148 try { \
149 (void)(expression); \
150 } catch (const exception_type&) { \
151 stormbyte_threw = true; \
152 } catch (...) { \
153 } \
154 if (!stormbyte_threw) { \
155 std::cerr << __func__ << ": Assertion failed at " << __FILE__ << ":" << __LINE__ << ": expected " << #exception_type << " to be thrown" << std::endl; \
156 return 1; \
157 } \
158} while (false)
159
165#define ASSERT_NO_THROW(expression) do { \
166 try { \
167 (void)(expression); \
168 } catch (const std::exception& stormbyte_exception) { \
169 std::cerr << __func__ << ": Assertion failed at " << __FILE__ << ":" << __LINE__ << ": unexpected exception: " << stormbyte_exception.what() << std::endl; \
170 return 1; \
171 } catch (...) { \
172 std::cerr << __func__ << ": Assertion failed at " << __FILE__ << ":" << __LINE__ << ": unexpected non-standard exception" << std::endl; \
173 return 1; \
174 } \
175} while (false)
176
184#define ASSERT_NEAR(expected, actual, tolerance) do { \
185 const auto stormbyte_expected = (expected); \
186 const auto stormbyte_actual = (actual); \
187 const auto stormbyte_tolerance = (tolerance); \
188 const auto stormbyte_difference = stormbyte_expected > stormbyte_actual ? \
189 stormbyte_expected - stormbyte_actual : stormbyte_actual - stormbyte_expected; \
190 if (stormbyte_difference > stormbyte_tolerance) { \
191 std::cerr << __func__ << ": Assertion failed at " << __FILE__ << ":" << __LINE__ << ": values differ beyond tolerance" << std::endl; \
192 return 1; \
193 } \
194} while (false)
195
202#define ASSERT_CONTAINS(haystack, needle) do { \
203 const auto& stormbyte_haystack = (haystack); \
204 const auto& stormbyte_needle = (needle); \
205 if (stormbyte_haystack.find(stormbyte_needle) == std::string_view::npos) { \
206 std::cerr << __func__ << ": Assertion failed at " << __FILE__ << ":" << __LINE__ << ": expected text to contain \"" << stormbyte_needle << "\"" << std::endl; \
207 return 1; \
208 } \
209} while (false)
210
217#define ASSERT_NOT_CONTAINS(haystack, needle) do { \
218 const auto& stormbyte_haystack = (haystack); \
219 const auto& stormbyte_needle = (needle); \
220 if (stormbyte_haystack.find(stormbyte_needle) != std::string_view::npos) { \
221 std::cerr << __func__ << ": Assertion failed at " << __FILE__ << ":" << __LINE__ << ": expected text not to contain \"" << stormbyte_needle << "\"" << std::endl; \
222 return 1; \
223 } \
224} while (false)
225
231#define ASSERT_NOT_NULL(pointer) do { \
232 if ((pointer) == nullptr) { \
233 std::cerr << __func__ << ": Assertion failed at " << __FILE__ << ":" << __LINE__ << ": pointer is null" << std::endl; \
234 return 1; \
235 } \
236} while (false)
237
243#define ASSERT_NULL(pointer) do { \
244 if ((pointer) != nullptr) { \
245 std::cerr << __func__ << ": Assertion failed at " << __FILE__ << ":" << __LINE__ << ": pointer is not null" << std::endl; \
246 return 1; \
247 } \
248} while (false)
249
255#define ASSERT_EMPTY(container) do { \
256 const auto& stormbyte_container = (container); \
257 if (!stormbyte_container.empty()) { \
258 std::cerr << __func__ << ": Assertion failed at " << __FILE__ << ":" << __LINE__ << ": expected empty, size is "; \
259 ::StormByte::Test::Detail::PrintValue(std::cerr, stormbyte_container.size()); \
260 std::cerr << std::endl; \
261 return 1; \
262 } \
263} while (false)
264
270#define ASSERT_NOT_EMPTY(container) do { \
271 const auto& stormbyte_container = (container); \
272 if (stormbyte_container.empty()) { \
273 std::cerr << __func__ << ": Assertion failed at " << __FILE__ << ":" << __LINE__ << ": expected not empty" << std::endl; \
274 return 1; \
275 } \
276} while (false)
277
284#define ASSERT_SIZE(container, expected) do { \
285 const auto& stormbyte_container = (container); \
286 const auto stormbyte_expected = (expected); \
287 if (!(stormbyte_container.size() == stormbyte_expected)) { \
288 std::cerr << __func__ << ": Assertion failed at " << __FILE__ << ":" << __LINE__ << ": expected size \""; \
289 ::StormByte::Test::Detail::PrintValue(std::cerr, stormbyte_expected); \
290 std::cerr << "\", got \""; \
291 ::StormByte::Test::Detail::PrintValue(std::cerr, stormbyte_container.size()); \
292 std::cerr << "\"" << std::endl; \
293 return 1; \
294 } \
295} while (false)
296
302#define ASSERT_FAIL(reason) do { \
303 std::cerr << __func__ << ": Assertion failed at " << __FILE__ << ":" << __LINE__ << ": " << (reason) << std::endl; \
304 return 1; \
305} while (false)
Definition test_handlers.h:48
void PrintValue(std::ostream &stream, const T &value)
Definition test_handlers.h:50