Created
January 8, 2023 17:40
-
-
Save erincatto/b51e64e5cb9dd1a5c9fc70c4005f6e8e to your computer and use it in GitHub Desktop.
Simple Unit Testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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