libhatchet
Loading...
Searching...
No Matches
hxtest.hpp
Go to the documentation of this file.
1#pragma once
2// SPDX-FileCopyrightText: © 2017-2026 Adrian Johnston.
3// SPDX-License-Identifier: MIT
4// This file is licensed under the MIT license found in the LICENSE.md file.
5
81
82#include "libhatchet.h"
83
84// HX_USE_GOOGLE_TEST - Enable this to use Google Test instead of hxtest.hpp.
85#if HX_USE_GOOGLE_TEST
86#include <gtest/gtest.h>
87#else
88
89#if !(HX_USE_MACROS_WITH_MODULE)
90
91#include "hxvector.hpp"
92#include "detail/hxtest_detail.hpp"
93
96namespace testing {
97
99class Test {
100public:
102 hxinline void SetUp(void) { }
103
105 hxinline void TearDown(void) { }
106};
107
108} // namespace testing
109#endif // !HX_USE_MACROS_WITH_MODULE
110
112// `HX_TEST_NAME_` - Macro for concatenating three arguments into one name.
113#define HX_TEST_NAME_(x_, y_, z_) x_ ## y_ ## __ ## z_ ## _
115
120#define TEST(suite_name_, case_name_) \
121 static void HX_TEST_NAME_(hxtest_, suite_name_, case_name_)(void); \
122 static HX_NS_PREFIX_ hxdetail_::hxtest_case_ HX_TEST_NAME_(hxs_test_, suite_name_, case_name_)( \
123 HX_TEST_NAME_(hxtest_, suite_name_, case_name_), #suite_name_, #case_name_, __FILE__, __LINE__); \
124 static void HX_TEST_NAME_(hxtest_, suite_name_, case_name_)(void)
125
131#define TEST_F(suite_fixture_, case_name_) \
132 class HX_TEST_NAME_(hxtest_f_, suite_fixture_, case_name_) : public suite_fixture_ { \
133 public: \
134 static void hxrun_(void) { \
135 HX_TEST_NAME_(hxtest_f_, suite_fixture_, case_name_) subclass_; \
136 subclass_.SetUp(); \
137 subclass_.hxrun_test_f_(); \
138 subclass_.TearDown(); \
139 } \
140 private: \
141 void hxrun_test_f_(void); \
142 }; \
143 static HX_NS_PREFIX_ hxdetail_::hxtest_case_ HX_TEST_NAME_(hxs_test_f_, suite_fixture_, case_name_)( \
144 HX_TEST_NAME_(hxtest_f_, suite_fixture_, case_name_)::hxrun_, #suite_fixture_, #case_name_, __FILE__, __LINE__); \
145 void HX_TEST_NAME_(hxtest_f_, suite_fixture_, case_name_)::hxrun_test_f_(void)
146
149#define RUN_ALL_TESTS() HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().run_all_tests_()
150
153#define SUCCEED() HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_( \
154 true, __FILE__, __LINE__, "SUCCEED()", false)
155
158#define FAIL() do { HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_( \
159 false, __FILE__, __LINE__, "FAIL()", false); return; } while (0)
160
161#define ADD_FAILURE() HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_( \
162 false, __FILE__, __LINE__, "ADD_FAILURE()", false)
163
165#define ADD_FAILURE_AT(file_, line_) \
166 HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_( \
167 false, (file_), static_cast<int>(line_), "ADD_FAILURE_AT()", false)
168
170#define EXPECT_TRUE(x_) HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_( \
171 (x_), __FILE__, __LINE__, #x_, false)
172
173#define EXPECT_FALSE(x_) HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_( \
174 !(x_), __FILE__, __LINE__, "!" #x_, false)
175
180#define EXPECT_NEAR(expected_, actual_, absolute_range_) do { \
181 const auto hxexp_ = (expected_); const auto hxact_ = (actual_); \
182 HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_( \
183 ((hxexp_ < hxact_) ? (hxact_ - hxexp_) : (hxexp_ - hxact_)) <= (absolute_range_), \
184 __FILE__, __LINE__, "abs(" #expected_ "-" #actual_ ") <= " #absolute_range_, false); \
185 } while(0)
186
187#define EXPECT_LT(a_, b_) HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_( \
188 (a_) < (b_), __FILE__, __LINE__, #a_ " < " #b_, false)
189
190#define EXPECT_GT(a_, b_) HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_( \
191 (b_) < (a_), __FILE__, __LINE__, #a_ " > " #b_, false)
192
193#define EXPECT_LE(a_, b_) HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_( \
194 !((b_) < (a_)), __FILE__, __LINE__, #a_ " <= " #b_, false)
195
196#define EXPECT_GE(a_, b_) HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_( \
197 !((a_) < (b_)), __FILE__, __LINE__, #a_ " >= " #b_, false)
198
199#define EXPECT_EQ(a_, b_) HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_( \
200 (a_) == (b_), __FILE__, __LINE__, #a_ " == " #b_, false)
201
202#define EXPECT_NE(a_, b_) HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_( \
203 !((a_) == (b_)), __FILE__, __LINE__, #a_ " != " #b_, false)
204
206#define EXPECT_FLOAT_EQ(a_, b_) HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_( \
207 HX_NS_PREFIX_ hxdetail_::hxtest_float_eq_((a_), (b_)), __FILE__, __LINE__, #a_ " ~= " #b_, false)
208
210#define EXPECT_DOUBLE_EQ(a_, b_) HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_( \
211 HX_NS_PREFIX_ hxdetail_::hxtest_double_eq_((a_), (b_)), __FILE__, __LINE__, #a_ " ~= " #b_, false)
212
214#define EXPECT_STREQ(a_, b_) HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_( \
215 HX_NS_PREFIX_ hxdetail_::hxtest_str_eq_((a_), (b_)), __FILE__, __LINE__, #a_ " == " #b_, false)
216
218#define EXPECT_STRNE(a_, b_) HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_( \
219 HX_NS_PREFIX_ hxdetail_::hxtest_str_ne_((a_), (b_)), __FILE__, __LINE__, #a_ " != " #b_, false)
220
221#define ASSERT_TRUE(x_) HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_( \
222 (x_), __FILE__, __LINE__, #x_, true)
223
224#define ASSERT_FALSE(x_) HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_( \
225 !(x_), __FILE__, __LINE__, "!" #x_, true)
226
231#define ASSERT_NEAR(expected_, actual_, absolute_error_) do { \
232 const auto hxexp_ = (expected_); const auto hxact_ = (actual_); \
233 HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_( \
234 ((hxexp_ < hxact_) ? (hxact_ - hxexp_) : (hxexp_ - hxact_)) <= (absolute_error_), \
235 __FILE__, __LINE__, "abs(" #expected_ " - " #actual_ ") <= " #absolute_error_, true); \
236 } while(0)
237
238#define ASSERT_LT(a_, b_) HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_( \
239 (a_) < (b_), __FILE__, __LINE__, #a_ " < " #b_, true)
240
241#define ASSERT_GT(a_, b_) HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_( \
242 (b_) < (a_), __FILE__, __LINE__, #a_ " > " #b_, true)
243
244#define ASSERT_LE(a_, b_) HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_( \
245 !((b_) < (a_)), __FILE__, __LINE__, #a_ " <= " #b_, true)
246
247#define ASSERT_GE(a_, b_) HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_( \
248 !((a_) < (b_)), __FILE__, __LINE__, #a_ " >= " #b_, true)
249
250#define ASSERT_EQ(a_, b_) HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_( \
251 (a_) == (b_), __FILE__, __LINE__, #a_ " == " #b_, true)
252
253#define ASSERT_NE(a_, b_) HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_( \
254 !((a_) == (b_)), __FILE__, __LINE__, #a_ " != " #b_, true)
255
257#define ASSERT_FLOAT_EQ(a_, b_) HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_( \
258 HX_NS_PREFIX_ hxdetail_::hxtest_float_eq_((a_), (b_)), __FILE__, __LINE__, #a_ " ~= " #b_, true)
259
261#define ASSERT_DOUBLE_EQ(a_, b_) HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_( \
262 HX_NS_PREFIX_ hxdetail_::hxtest_double_eq_((a_), (b_)), __FILE__, __LINE__, #a_ " ~= " #b_, true)
263
265#define ASSERT_STREQ(a_, b_) HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_( \
266 HX_NS_PREFIX_ hxdetail_::hxtest_str_eq_((a_), (b_)), __FILE__, __LINE__, #a_ " == " #b_, true)
267
269#define ASSERT_STRNE(a_, b_) HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_( \
270 HX_NS_PREFIX_ hxdetail_::hxtest_str_ne_((a_), (b_)), __FILE__, __LINE__, #a_ " != " #b_, true)
271
272#endif // !HX_USE_GOOGLE_TEST
Test - Base class for tests required by Google Test's TEST_F.
Definition hxtest.hpp:99
void TearDown(void)
User overridable for tests using TEST_F.
Definition hxtest.hpp:105
void SetUp(void)
User overridable for tests using TEST_F.
Definition hxtest.hpp:102
#define hxinline
hxinline - Force a function to be inlined into its callers.
Definition hxsettings.h:125
Implements std::vector, std::inplace_vector and std::back_insert_iterator with a few things added and...
Provides core macros, memory management and feature detection.
testing - A partial Google Test reimplementation.
Definition hxtest.hpp:96