Skip to content

Instantly share code, notes, and snippets.

@erincatto
Created January 8, 2023 17:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erincatto/b51e64e5cb9dd1a5c9fc70c4005f6e8e to your computer and use it in GitHub Desktop.
Save erincatto/b51e64e5cb9dd1a5c9fc70c4005f6e8e to your computer and use it in GitHub Desktop.
Simple Unit Testing
// 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment