libhatchet
Loading...
Searching...
No Matches
hxtest.hpp File Reference

Go to the source code of this file.

Classes

class  testing::Test

Namespaces

namespace  testing

Macros

#define TEST(suite_name_, case_name_)
#define TEST_F(suite_fixture_, case_name_)
#define RUN_ALL_TESTS(...)
#define SUCCEED()
#define FAIL()
#define ADD_FAILURE()
#define ADD_FAILURE_AT(file_, line_)
#define EXPECT_TRUE(x_)
#define EXPECT_FALSE(x_)
#define EXPECT_NEAR(expected_, actual_, absolute_range_)
#define EXPECT_LT(a_, b_)
#define EXPECT_GT(a_, b_)
#define EXPECT_LE(a_, b_)
#define EXPECT_GE(a_, b_)
#define EXPECT_EQ(a_, b_)
#define EXPECT_NE(a_, b_)
#define EXPECT_FLOAT_EQ(a_, b_)
#define EXPECT_DOUBLE_EQ(a_, b_)
#define EXPECT_STREQ(a_, b_)
#define EXPECT_STRNE(a_, b_)
#define ASSERT_TRUE(x_)
#define ASSERT_FALSE(x_)
#define ASSERT_NEAR(expected_, actual_, absolute_error_)
#define ASSERT_LT(a_, b_)
#define ASSERT_GT(a_, b_)
#define ASSERT_LE(a_, b_)
#define ASSERT_GE(a_, b_)
#define ASSERT_EQ(a_, b_)
#define ASSERT_NE(a_, b_)
#define ASSERT_FLOAT_EQ(a_, b_)
#define ASSERT_DOUBLE_EQ(a_, b_)
#define ASSERT_STREQ(a_, b_)
#define ASSERT_STRNE(a_, b_)

Detailed Description

Google Test-compatible framework for writing unit tests.

It does not spam your system memory allocator with string operations right after an assert fails. Actually, it never allocates. To disable this header and switch to testing with <gtest/gtest.h> directly, use -DHX_USE_GOOGLE_TEST=1. Only core features are provided. This framework uses only operator< and operator== in its assertions. Compatibility with Google Test may require additional relational operators.

  • TEST(suite, name) - Defines a test case without a fixture.
  • TEST_F(fixture, name) - Defines a test case using a fixture class.
  • Use ASSERT_* for fatal assertions and EXPECT_* for non-fatal.
  • See RUN_ALL_TESTS in test/hxtest_main.cpp for example.
  • Simple Test Case (no fixture):
    TEST(Math, Addition) {
    int a = 2, b = 3;
    EXPECT_EQ(a + b, 5);
    EXPECT_TRUE(a < b + 2);
    EXPECT_NEAR(3.14, 3.141, 0.01);
    }
    #define EXPECT_TRUE(x_)
    void EXPECT_TRUE(bool) - Requires that the condition is true.
    Definition hxtest.hpp:180
    #define SUCCEED()
    void SUCCEED(void) - Marks the current test as successful without any checks.
    Definition hxtest.hpp:168
    #define EXPECT_NEAR(expected_, actual_, absolute_range_)
    void EXPECT_NEAR(T expected, T actual, T absolute_range) - Requires that two values are within a give...
    Definition hxtest.hpp:188
    #define EXPECT_EQ(a_, b_)
    void EXPECT_EQ(T a, T b) - Requires a == b.
    Definition hxtest.hpp:203
    #define TEST(suite_name_, case_name_)
    TEST(suite_name, case_name) - Google Test reimplementation.
    Definition hxtest.hpp:132
  • Fixture-Based Test Case (using TEST_F):
    class MyFixture : public testing::Test {
    public:
    void SetUp() override { value = 42; }
    void TearDown() override { EXPECT_EQ(value, 100); }
    void set_value(int x) { value = x; }
    int value;
    };
    TEST_F(MyFixture, ValueIsSet) {
    EXPECT_EQ(value, 42);
    set_value(100);
    EXPECT_NE(value, 42);
    }
    Test - Base class for tests required by Google Test's TEST_F.
    Definition hxtest.hpp:98
    virtual void TearDown(void)
    User override for tests using TEST_F.
    Definition hxtest.hpp:104
    virtual void SetUp(void)
    User override for tests using TEST_F.
    Definition hxtest.hpp:101
    #define EXPECT_NE(a_, b_)
    void EXPECT_NE(T a, T b) - Requires a != b using !(a == b).
    Definition hxtest.hpp:205
    #define TEST_F(suite_fixture_, case_name_)
    TEST_F(suite_name, case_name) - Google Test reimplementation for fixture-based tests.
    Definition hxtest.hpp:149
Macro Assertion
SUCCEED(void) Marks the current test as successful without any checks.
FAIL(void) WARNING. Calls return. Marks the current test as failed.
ADD_FAILURE(void) Adds a non-fatal failure at the current location.
ADD_FAILURE_AT(const char* file, int line) Adds a non-fatal failure at the specified location.
EXPECT_TRUE(bool x) Requires that the condition is true.
EXPECT_FALSE(bool x) Requires that the condition is false.
EXPECT_EQ(T a, T b) Requires a == b.
EXPECT_NE(T a, T b) Requires a != b using !(a == b).
EXPECT_LT(T a, T b) Requires a < b.
EXPECT_GT(T a, T b) Requires a > b using b < a.
EXPECT_LE(T a, T b) Requires ab using !(b < a).
EXPECT_GE(T a, T b) Requires ab using !(a < b).
EXPECT_NEAR(T expected, T actual, T absolute_range) Requires that two values are within a given range.
EXPECT_FLOAT_EQ(float a, float b) Checks floats for equality within a scaled tolerance.
EXPECT_DOUBLE_EQ(double a, double b) Checks doubles for equality within a scaled tolerance.
EXPECT_STREQ(const char* a, const char* b) Requires that two C strings are equal.
EXPECT_STRNE(const char* a, const char* b) Requires that two C strings differ.
ASSERT_TRUE(bool x) Requires that the condition is true.
ASSERT_FALSE(bool x) Requires that the condition is false.
ASSERT_EQ(T a, T b) Requires a == b.
ASSERT_NE(T a, T b) Requires a != b using !(a == b).
ASSERT_LT(T a, T b) Requires a < b.
ASSERT_GT(T a, T b) Requires a > b using b < a.
ASSERT_LE(T a, T b) Requires ab using !(b < a).
ASSERT_GE(T a, T b) Requires ab using !(a < b).
ASSERT_NEAR(T expected, T actual, T absolute_error) Requires that two values are within a given range.
ASSERT_FLOAT_EQ(float a, float b) Checks floats for equality within a scaled tolerance.
ASSERT_DOUBLE_EQ(double a, double b) Checks doubles for equality within a scaled tolerance.
ASSERT_STREQ(const char* a, const char* b) Requires that two C strings are equal.
ASSERT_STRNE(const char* a, const char* b) Requires that two C strings differ.

See: https://google.github.io/googletest/reference/assertions.html

Macro Definition Documentation

◆ ADD_FAILURE

#define ADD_FAILURE ( )
Value:
HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_(false, __FILE__, __LINE__, "ADD_FAILURE()", false)

void ADD_FAILURE(void) - Adds a non-fatal failure at the current location.

◆ ADD_FAILURE_AT

#define ADD_FAILURE_AT ( file_,
line_ )
Value:
HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_(false, (file_), static_cast<int>(line_), "ADD_FAILURE_AT()", false)

void ADD_FAILURE_AT(const char*, int) - Adds a non-fatal failure at the specified file and line.

◆ ASSERT_DOUBLE_EQ

#define ASSERT_DOUBLE_EQ ( a_,
b_ )
Value:
HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_(HX_NS_PREFIX_ hxdetail_::hxtest_double_eq_((a_), (b_)), __FILE__, __LINE__, #a_ " ~= " #b_, true)

void ASSERT_DOUBLE_EQ(double a, double b) - Requires doubles for equality within a scaled tolerance.

◆ ASSERT_EQ

#define ASSERT_EQ ( a_,
b_ )
Value:
HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_((a_) == (b_), __FILE__, __LINE__, #a_ " == " #b_, true)

void ASSERT_EQ(T a, T b) - Requires a == b.

◆ ASSERT_FALSE

#define ASSERT_FALSE ( x_)
Value:
HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_(!(x_), __FILE__, __LINE__, "!" #x_, true)

void ASSERT_FALSE(bool) - Requires that the condition is false.

◆ ASSERT_FLOAT_EQ

#define ASSERT_FLOAT_EQ ( a_,
b_ )
Value:
HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_(HX_NS_PREFIX_ hxdetail_::hxtest_float_eq_((a_), (b_)), __FILE__, __LINE__, #a_ " ~= " #b_, true)

void ASSERT_FLOAT_EQ(float a, float b) - Requires floats for equality within a scaled tolerance.

◆ ASSERT_GE

#define ASSERT_GE ( a_,
b_ )
Value:
HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_(!((a_) < (b_)), __FILE__, __LINE__, #a_ " >= " #b_, true)

void ASSERT_GE(T a, T b) - Requires ab using !(a < b).

◆ ASSERT_GT

#define ASSERT_GT ( a_,
b_ )
Value:
HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_((b_) < (a_), __FILE__, __LINE__, #a_ " > " #b_, true)

void ASSERT_GT(T a, T b) - Requires a > b using b < a.

◆ ASSERT_LE

#define ASSERT_LE ( a_,
b_ )
Value:
HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_(!((b_) < (a_)), __FILE__, __LINE__, #a_ " <= " #b_, true)

void ASSERT_LE(T a, T b) - Requires ab using !(b < a).

◆ ASSERT_LT

#define ASSERT_LT ( a_,
b_ )
Value:
HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_((a_) < (b_), __FILE__, __LINE__, #a_ " < " #b_, true)

void ASSERT_LT(T a, T b) - Requires a < b.

◆ ASSERT_NE

#define ASSERT_NE ( a_,
b_ )
Value:
HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_(!((a_) == (b_)), __FILE__, __LINE__, #a_ " != " #b_, true)

void ASSERT_NE(T a, T b) - Requires a != b using !(a == b).

◆ ASSERT_NEAR

#define ASSERT_NEAR ( expected_,
actual_,
absolute_error_ )
Value:
do { \
const auto hxexp_ = (expected_); const auto hxact_ = (actual_); \
HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_( \
((hxexp_ < hxact_) ? (hxact_ - hxexp_) : (hxexp_ - hxact_)) <= (absolute_error_), \
__FILE__, __LINE__, "abs(" #expected_ " - " #actual_ ") <= " #absolute_error_, true); \
} while(0)

void ASSERT_NEAR(T expected, T actual, T absolute_error) - Requires that two values are within a given range.

  • expected : Reference value to compare against.
  • actual : Value being tested.
  • absolute_error : Maximum permitted absolute difference.

◆ ASSERT_STREQ

#define ASSERT_STREQ ( a_,
b_ )
Value:
HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_( \
HX_NS_PREFIX_ hxdetail_::hxtest_str_eq_((a_), (b_)), __FILE__, __LINE__, #a_ " == " #b_, true)

void ASSERT_STREQ(const char* a, const char* b) - Requires that two C strings are equal.

◆ ASSERT_STRNE

#define ASSERT_STRNE ( a_,
b_ )
Value:
HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_( \
HX_NS_PREFIX_ hxdetail_::hxtest_str_ne_((a_), (b_)), __FILE__, __LINE__, #a_ " != " #b_, true)

void ASSERT_STRNE(const char* a, const char* b) - Requires that two C strings differ.

◆ ASSERT_TRUE

#define ASSERT_TRUE ( x_)
Value:
HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_((x_), __FILE__, __LINE__, #x_, true)

void ASSERT_TRUE(bool) - Requires that the condition is true.

◆ EXPECT_DOUBLE_EQ

#define EXPECT_DOUBLE_EQ ( a_,
b_ )
Value:
HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_(HX_NS_PREFIX_ hxdetail_::hxtest_double_eq_((a_), (b_)), __FILE__, __LINE__, #a_ " ~= " #b_, false)

void EXPECT_DOUBLE_EQ(double a, double b) - Requires doubles for equality within a scaled tolerance.

◆ EXPECT_EQ

#define EXPECT_EQ ( a_,
b_ )
Value:
HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_((a_) == (b_), __FILE__, __LINE__, #a_ " == " #b_, false)

void EXPECT_EQ(T a, T b) - Requires a == b.

◆ EXPECT_FALSE

#define EXPECT_FALSE ( x_)
Value:
HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_(!(x_), __FILE__, __LINE__, "!" #x_, false)

void EXPECT_FALSE(bool) - Requires that the condition is false.

◆ EXPECT_FLOAT_EQ

#define EXPECT_FLOAT_EQ ( a_,
b_ )
Value:
HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_(HX_NS_PREFIX_ hxdetail_::hxtest_float_eq_((a_), (b_)), __FILE__, __LINE__, #a_ " ~= " #b_, false)

void EXPECT_FLOAT_EQ(float a, float b) - Requires floats for equality within a scaled tolerance.

◆ EXPECT_GE

#define EXPECT_GE ( a_,
b_ )
Value:
HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_(!((a_) < (b_)), __FILE__, __LINE__, #a_ " >= " #b_, false)

void EXPECT_GE(T a, T b) - Requires ab using !(a < b).

◆ EXPECT_GT

#define EXPECT_GT ( a_,
b_ )
Value:
HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_((b_) < (a_), __FILE__, __LINE__, #a_ " > " #b_, false)

void EXPECT_GT(T a, T b) - Requires a > b using b < a.

◆ EXPECT_LE

#define EXPECT_LE ( a_,
b_ )
Value:
HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_(!((b_) < (a_)), __FILE__, __LINE__, #a_ " <= " #b_, false)

void EXPECT_LE(T a, T b) - Requires ab using !(b < a).

◆ EXPECT_LT

#define EXPECT_LT ( a_,
b_ )
Value:
HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_((a_) < (b_), __FILE__, __LINE__, #a_ " < " #b_, false)

void EXPECT_LT(T a, T b) - Requires a < b.

◆ EXPECT_NE

#define EXPECT_NE ( a_,
b_ )
Value:
HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_(!((a_) == (b_)), __FILE__, __LINE__, #a_ " != " #b_, false)

void EXPECT_NE(T a, T b) - Requires a != b using !(a == b).

◆ EXPECT_NEAR

#define EXPECT_NEAR ( expected_,
actual_,
absolute_range_ )
Value:
do { \
const auto hxexp_ = (expected_); const auto hxact_ = (actual_); \
HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_( \
((hxexp_ < hxact_) ? (hxact_ - hxexp_) : (hxexp_ - hxact_)) <= (absolute_range_), \
__FILE__, __LINE__, "abs(" #expected_ "-" #actual_ ") <= " #absolute_range_, false); \
} while(0)

void EXPECT_NEAR(T expected, T actual, T absolute_range) - Requires that two values are within a given range.

  • expected : Reference value to compare against.
  • actual : Value being tested.
  • absolute_range : Maximum permitted absolute difference.

◆ EXPECT_STREQ

#define EXPECT_STREQ ( a_,
b_ )
Value:
HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_( \
HX_NS_PREFIX_ hxdetail_::hxtest_str_eq_((a_), (b_)), __FILE__, __LINE__, #a_ " == " #b_, false)

void EXPECT_STREQ(const char* a, const char* b) - Requires that two C strings are equal.

◆ EXPECT_STRNE

#define EXPECT_STRNE ( a_,
b_ )
Value:
HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_( \
HX_NS_PREFIX_ hxdetail_::hxtest_str_ne_((a_), (b_)), __FILE__, __LINE__, #a_ " != " #b_, false)

void EXPECT_STRNE(const char* a, const char* b) - Requires that two C strings differ.

◆ EXPECT_TRUE

#define EXPECT_TRUE ( x_)
Value:
HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_((x_), __FILE__, __LINE__, #x_, false)

void EXPECT_TRUE(bool) - Requires that the condition is true.

◆ FAIL

#define FAIL ( )
Value:
do { HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_(false, __FILE__, __LINE__, "FAIL()", false); return; } while (0)

void FAIL(void) - WARNING.

Calls return. Marks the current test as failed.

◆ RUN_ALL_TESTS

#define RUN_ALL_TESTS ( ...)
Value:
HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().run_all_tests_(__VA_ARGS__)

int RUN_ALL_TESTS(...) - Executes all registered test cases.

  • ... : Optional const char* matching a specific test suite to run. (Non-standard.)

◆ SUCCEED

#define SUCCEED ( )
Value:
HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().condition_check_(true, __FILE__, __LINE__, "SUCCEED()", false)

void SUCCEED(void) - Marks the current test as successful without any checks.

◆ TEST

#define TEST ( suite_name_,
case_name_ )
Value:
class HX_TEST_NAME_(hxtest_, suite_name_, case_name_) : public HX_NS_PREFIX_ hxdetail_::hxtest_case_interface_ { \
public: \
HX_TEST_NAME_(hxtest_, suite_name_, case_name_)(void) { HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().add_test_(this); } \
virtual void run_test_(void) override; \
virtual const char* suite_(void) const override { return #suite_name_; } \
virtual const char* case_(void) const override { return #case_name_; } \
virtual const char* file_(void) const override { return __FILE__; } \
virtual int line_(void) const override { return __LINE__; } \
} static HX_TEST_NAME_(hxs_test_, suite_name_, case_name_); \
void HX_TEST_NAME_(hxtest_, suite_name_, case_name_)::run_test_(void)

TEST(suite_name, case_name) - Google Test reimplementation.

Defines a test case with a suite name and case name.

  • suite_name : A valid C identifier for the test suite.
  • case_name : A valid C identifier for the test case.

◆ TEST_F

#define TEST_F ( suite_fixture_,
case_name_ )
Value:
class HX_TEST_NAME_(hxtest_f_, suite_fixture_, case_name_) : public HX_NS_PREFIX_ hxdetail_::hxtest_case_interface_ { \
public: \
class hxtest_case_subclass_ : public suite_fixture_ { virtual void hxrun_test_f_(void) override; }; \
HX_TEST_NAME_(hxtest_f_, suite_fixture_, case_name_)(void) { HX_NS_PREFIX_ hxdetail_::hxtest_::dispatcher_().add_test_(this); } \
virtual void run_test_(void) override { hxtest_case_subclass_ subclass_; subclass_.hxrun_test_(); } \
virtual const char* suite_(void) const override { return #suite_fixture_; } \
virtual const char* case_(void) const override { return #case_name_; } \
virtual const char* file_(void) const override { return __FILE__; } \
virtual int line_(void) const override { return __LINE__; } \
} static HX_TEST_NAME_(hxs_test_f_, suite_fixture_, case_name_); \
void HX_TEST_NAME_(hxtest_f_, suite_fixture_, case_name_)::hxtest_case_subclass_::hxrun_test_f_(void)

TEST_F(suite_name, case_name) - Google Test reimplementation for fixture-based tests.

Defines a test case where the suite_name is a subclass of testing::Test.

  • suite_fixture : The test suite base class used as a fixture.
  • case_name : A valid C identifier for the test case.