burkey.co /docs/libflint/test
index test
~/docs/libflint/test.md
libflint

Test

Header-only asserts for programs under tests/. Not part of the flint library. Include lftest.h and end main with TEST_REPORT().

Usage#

TEST(name) names the following statement, usually a brace block. name is a C string. A passing block prints nothing. A failing assert inside it prints name and the same file, line, and expression text as an unnamed failure. Asserts outside TEST keep the unnamed line.

#include "lftest.h"

int main(void) {
    int n = 2;

    TEST("double") {
        ASSERT_EQ(n, 2);
    }
    TEST_REPORT();
}

TEST_REPORT() prints passed/total (N/M passed) and returns 0 when every assert passed, or 1 when any failed. Return that value from main.

A failure inside TEST("double") looks like this:

  FAIL double: demo.c:8: n != 2 (1 != 2)

With no name active, the same assert stays in the old form:

  FAIL demo.c:8: n != 2 (1 != 2)

break, return, or goto out of the block skips the step that clears the name, so later asserts in that translation unit still print it.

Counters are static per translation unit. A multi-file binary must call a report from the translation unit that ran the asserts, or the counts stay split.

Macros#

TEST#

#define TEST(name)

Runs the following statement once while name is active. Passing asserts print nothing. A NULL name prints the unnamed failure line.

ASSERT_TRUE#

#define ASSERT_TRUE(expr)

Counts one assert. On failure, prints the file, line, and expression text.

ASSERT_FALSE#

#define ASSERT_FALSE(expr)

Same as ASSERT_TRUE(!(expr)).

ASSERT_EQ#

#define ASSERT_EQ(a, b)

Fails when (a) != (b). Printed values use (long long)(intptr_t), so this is for integers and pointers, not a type-generic printer. On failure each side is evaluated again for that line.

ASSERT_NEQ#

#define ASSERT_NEQ(a, b)

Fails when (a) == (b). Same cast as ASSERT_EQ.

ASSERT_LT#

#define ASSERT_LT(a, b)

Fails when (a) < (b) is false. Same cast as ASSERT_EQ.

ASSERT_STR_EQ#

#define ASSERT_STR_EQ(a, b)

Compares two C strings with strcmp. A NULL argument is not read. A mismatch prints the string values, or NULL.

ASSERT_NULL#

#define ASSERT_NULL(expr)

Same as ASSERT_EQ((void *)(expr), NULL).

ASSERT_NOT_NULL#

#define ASSERT_NOT_NULL(expr)

Same as ASSERT_NEQ((void *)(expr), NULL).

TEST_REPORT#

#define TEST_REPORT()

Prints this translation unit's passed/total and returns 0 or 1.