Last active
March 6, 2024 12:26
-
-
Save sam159/0849461161e86249f849 to your computer and use it in GitHub Desktop.
minunit header for unit testing c code
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
/* | |
* File: minunit.h | |
* Author: Zed. A. Shaw, Sam | |
* | |
* @see http://c.learncodethehardway.org/book/ex30.html | |
* | |
* Created on 27 August 2014, 22:14 | |
*/ | |
#ifndef MINUNIT_H | |
#define MINUNIT_H | |
#ifdef __cplusplus | |
extern "C" { | |
#endif | |
#include <stdio.h> | |
#include <stdlib.h> | |
#define log_err(message) printf("\tError: %s\n", message) | |
#define mu_suite_start() char *message = NULL | |
#define mu_assert(test, message) \ | |
do { \ | |
if (!(test)) { \ | |
log_err(message); \ | |
return message; \ | |
} \ | |
} while(0) | |
#define mu_run_test(test) \ | |
do { \ | |
printf("\n-----%s", " " #test); \ | |
message = test(); \ | |
tests_run++; \ | |
if (message) { return message; } \ | |
} while(0) | |
#define RUN_TESTS(name) \ | |
int main(int argc, char *argv[]) { \ | |
tests_run = 0; \ | |
argc = 1; \ | |
printf("----\nRUNNING: %s\n", argv[0]); \ | |
char *result = name(); \ | |
if (result != 0) { \ | |
printf("FAILED: %s\n", result); \ | |
} \ | |
else { \ | |
printf("ALL TESTS PASSED\n"); \ | |
} \ | |
printf("Tests run: %d\n", tests_run); \ | |
exit(result == 0 ? EXIT_SUCCESS : EXIT_FAILURE);\ | |
} | |
int tests_run; | |
#ifdef __cplusplus | |
} | |
#endif | |
#endif /* MINUNIT_H */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fixed, took me a while :)