Assertion Macros¶
Include “mu/tiny/test.hpp” (C++) or “mu/tiny/test.h” (C) for assertions.
A failing assertion immediately exits the current test body. Each macro increments an internal check counter on success, which appears in the summary.
Boolean¶
Macro |
Passes when |
|---|---|
|
CHECK(ptr != nullptr);
CHECK(list.empty());
CHECK(!error_occurred);
Equality¶
CHECK_EQUAL(expected, actual)¶
CHECK_EQUAL uses operator== and implicit arithmetic promotion to compare
expected and actual. Works with any type that supports
operator==. On failure, calls string_from() to format the
message.
CHECK_EQUAL(42, compute_answer());
CHECK_EQUAL(Status::OK, get_status());
To make CHECK_EQUAL work with your own types, provide:
operator==(const YourType&, const YourType&)mu::tiny::Stringstring_from(const YourType&)— used in the failure message
namespace mu { namespace tiny {
String string_from(const Colour& c) { return c.name(); }
} }
CHECK_APPROX(expected, actual, threshold)¶
CHECK_APPROX checks that expected and actual differ by at
most threshold. All three operands must share the same numeric type
(floating-point or integral).
CHECK_APPROX(3.14, compute_pi(), 0.001);
CHECK_APPROX(1.0f, compute_float(), 0.01f);
CHECK_APPROX(1000, compute_int(), 10);
Relational¶
CHECK_COMPARE(first, relop, second)¶
CHECK_COMPARE checks that first relop second is true. relop is a
literal operator like <, >=, !=.
CHECK_COMPARE(result, >=, 0);
CHECK_COMPARE(count, !=, 0);
String¶
Macro |
Description |
|---|---|
|
|
|
|
checks that |
STRCMP_EQUAL("hello", get_greeting());
STRCMP_CONTAINS("error", log_output());
Memory¶
MEMCMP_EQUAL(expected, actual, size)¶
MEMCMP_EQUAL compares size bytes starting at expected and
actual using memcmp. On failure, a hex dump is printed.
Enum¶
Macro |
Description |
|---|---|
Compares two enum values as |
|
Compares with the specified underlying type |
ENUMS_EQUAL_INT(State::IDLE, get_state());
ENUMS_EQUAL_TYPE(uint8_t, Color::RED, get_color());
Failure and Exit¶
Macro |
Description |
|---|---|
Unconditional failure with message. |
|
Exits the test immediately without marking it as failed — useful in assertion-faking helpers |
|
Marks the test as skipped and exits the body. The message appears in
JUnit XML as |
if (unexpected_condition)
FAIL_TEST("Should not reach this branch");
Exception¶
CHECK_THROWS(ExceptionType, expression)¶
CHECK_THROWS is only available when exceptions are enabled
(MUTINY_HAVE_EXCEPTIONS). Fails if expression does not throw
ExceptionType.
CHECK_THROWS(std::invalid_argument, parse_int("abc"));
_TEXT Variants¶
Every assertion macro has a _TEXT variant that appends a custom
message to the failure output:
CHECK_EQUAL_TEXT(expected, actual, "iteration " + std::to_string(i));
STRCMP_EQUAL_TEXT("ok", result, "after calling init()");
Examples¶
File |
Demonstrates |
|---|---|
|
|
|