Skip to content

Instantly share code, notes, and snippets.

@mattiasgustavsson
Last active July 15, 2025 08:34
Show Gist options
  • Save mattiasgustavsson/f0c720e7bffd1d49a38f252820a10261 to your computer and use it in GitHub Desktop.
Save mattiasgustavsson/f0c720e7bffd1d49a38f252820a10261 to your computer and use it in GitHub Desktop.
Very basic "test framework", meant for when I don't want to use my full `testfw.h` lib (usage syntax is the same for both)
char const* g_testfw_desc = NULL; int g_testfw_line = 0, g_testfw_current_test_failed = 0, g_testfw_tests_count = 0, g_testfw_asserts_count = 0, g_testfw_tests_failed = 0, g_testfw_asserts_failed = 0;
#define TESTFW_INIT() g_testfw_desc = NULL; g_testfw_line = 0; g_testfw_current_test_failed = 0; g_testfw_tests_count = 0; g_testfw_asserts_count = 0; g_testfw_tests_failed = 0; g_testfw_asserts_failed = 0;
#define TESTFW_SUMMARY() \
( ( g_testfw_tests_failed == 0 ) ? printf( "\n===============================================================================\nAll tests passed (%d assertions in %d test cases)\n", g_testfw_asserts_count, g_testfw_tests_count ) : \
( printf( "\n===============================================================================\ntest cases: %4d | %4d passed | %4d failed\n", g_testfw_tests_count, g_testfw_tests_count - g_testfw_tests_failed, g_testfw_tests_failed ),\
printf( "assertions: %4d | %4d passed | %4d failed\n", g_testfw_asserts_count, g_testfw_asserts_count - g_testfw_asserts_failed, g_testfw_asserts_failed ) ), g_testfw_tests_failed != 0 )
#define TESTFW_TEST_BEGIN( desc ) { ++g_testfw_tests_count; g_testfw_desc = (desc); g_testfw_line = __LINE__; g_testfw_current_test_failed = 0;
#define TESTFW_TEST_END() if( g_testfw_current_test_failed ) { ++g_testfw_tests_failed; } } g_testfw_desc = NULL; g_testfw_line = 0;
#define TESTFW_EXPECTED( expression ) \
++g_testfw_asserts_count; if( !(expression) ) { ++g_testfw_asserts_failed; g_testfw_current_test_failed = 1; if( g_testfw_desc ) { printf( "\n-------------------------------------------------------------------------------\n" ); \
printf( "%s\n", g_testfw_desc ); printf( "-------------------------------------------------------------------------------\n" ); printf( "%s(%d): %s\n", __FILE__, g_testfw_line, __func__ ); \
printf( "...............................................................................\n" ); g_testfw_desc = NULL; } printf( "\n%s(%d): FAILED:\n", __FILE__, __LINE__ ); printf( "\n TESTFW_EXPECTED( %s )\n", #expression ); }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment