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(condition)

condition is truthy

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:

  1. operator==(const YourType&, const YourType&)

  2. mu::tiny::String string_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

STRCMP_EQUAL(expected, actual)

strcmp equality for const char*

STRNCMP_EQUAL(expected, actual, length)

strncmp for first length chars

STRCMP_CONTAINS(expected, actual)

checks that actual contains expected as a substring

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

ENUMS_EQUAL_INT(expected, actual)

Compares two enum values as int

ENUMS_EQUAL_TYPE(underlying_type, expected, actual)

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

FAIL_TEST(text)

Unconditional failure with message.

PASS_TEST()

Exits the test immediately without marking it as failed — useful in assertion-faking helpers

SKIP_TEST(text)

Marks the test as skipped and exits the body. The message appears in JUnit XML as <skipped message="..."/>. Tests registered with SKIPPED_TEST are also reported as skipped.

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

CheatSheet.test.cpp

CHECK_EQUAL, CHECK, STRCMP_EQUAL in a minimal test

CircularBuffer.test.cpp

CHECK, CHECK_EQUAL, STRCMP_EQUAL across a variety of test scenarios