Created
April 26, 2024 19:11
-
-
Save pankkor/acdf424724620d29b56555ff1f2e63a6 to your computer and use it in GitHub Desktop.
All you ever need test utils (https://godbolt.org/z/WEoavaqGc)
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
#include <stdio.h> | |
#define STR1(s) # s | |
#define STR(s) STR1(s) | |
#define TEST_EXPECT(condition) test_expect(!!(condition), __FILE__ ":" STR(__LINE__) ": (" STR(condition) ") expected to be true\n") | |
static inline void test_expect(int condition, const char *msg) { | |
if (!condition) { | |
fputs(msg, stderr); | |
// __debugbreak and __builtin_debugtrap() would allow SIGTRAPping into a debugger | |
// __builtin_trap generates SIGILL and code after it will be optmized away. | |
#if defined(_MSC_VER) | |
__debugbreak(); | |
#elif defined(__clang__) | |
__builtin_debugtrap(); | |
#else | |
__builtin_trap(); // gcc doesn't have __builtin_debugtrap equivalent | |
#endif | |
} | |
} | |
int main(void) { | |
int one = 1; | |
fputs("--- First TEST_EXPECT succeds\n", stderr); | |
TEST_EXPECT(one == 1); | |
fputs("--- Second TEST_EXPECT breaks into debugger or crashes\n", stderr); | |
TEST_EXPECT(one == 2); | |
fputs("--- This line won't be printed!\n", stderr); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment