Skip to content

Instantly share code, notes, and snippets.

@erincatto
Created January 8, 2023 17:40
Show Gist options
  • Save erincatto/b51e64e5cb9dd1a5c9fc70c4005f6e8e to your computer and use it in GitHub Desktop.
Save erincatto/b51e64e5cb9dd1a5c9fc70c4005f6e8e to your computer and use it in GitHub Desktop.

Revisions

  1. erincatto created this gist Jan 8, 2023.
    54 changes: 54 additions & 0 deletions test_macros.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    // SPDX-FileCopyrightText: 2022 Erin Catto
    // SPDX-License-Identifier: MIT

    #include <stdbool.h>
    #include <stdio.h>
    #include <assert.h>

    #define RUN_TEST(T) \
    do { \
    int result = T(); \
    if (result == 1) \
    { \
    printf("test failed: " #T "\n"); \
    return 1; \
    } \
    else \
    { \
    printf("test passed: " #T "\n"); \
    } \
    } while (false)

    #define RUN_SUBTEST(T) \
    do { \
    int result = T(); \
    if (result == 1) \
    { \
    printf(" subtest failed: " #T "\n"); \
    return 1; \
    } \
    else \
    { \
    printf(" subtest passed: " #T "\n"); \
    } \
    } while (false)

    #define ENSURE(C) \
    do { \
    if ((C) == false) \
    { \
    printf("condition false: " #C "\n"); \
    assert(false); \
    return 1; \
    } \
    } while (false)

    #define ENSURE_SMALL(C, tol) \
    do { \
    if ((C) < -(tol) || (tol) < (C)) \
    { \
    printf("condition false: abs(" #C ") < %g\n", tol); \
    assert(false); \
    return 1; \
    } \
    } while (false)