Created
January 10, 2014 20:07
-
-
Save mrnugget/8361640 to your computer and use it in GitHub Desktop.
Simple C test macros based on the LCTHW macros by Zed Shaw (thanks!) with added colors and line number support.
This file contains hidden or 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 RED "\x1B[31m" | |
| #define GREEN "\x1B[32m" | |
| #define NORMAL "\x1B[0m" | |
| #define STR(s) #s | |
| #define TESTLINE(line) " ("__FILE__ ":" STR(line) ")" | |
| #define test_assert(A, B) if(!(A)) { return B TESTLINE(__LINE__); }; | |
| #define test_setup() char *test_error_message = NULL; | |
| #define run_test(T) printf(" %s: ", " " #T);\ | |
| test_error_message = T();\ | |
| if (test_error_message) { printf(RED"FAIL: %s\n"NORMAL, test_error_message); return -1; }\ | |
| else { printf(GREEN"OK!\n"NORMAL); } | |
| int my_function() | |
| { | |
| return 1; | |
| } | |
| char *test_my_function() | |
| { | |
| test_assert(my_function(), "my_function() failed"); | |
| return NULL; | |
| } | |
| int main(int argc, char *argv[]) | |
| { | |
| printf("Running tests...\n"); | |
| test_setup(); | |
| run_test(test_my_function); | |
| printf("All tests passed.\n"); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment