A minimal C unit testing framework (only 3 LOC)
jera.com
jera.com
#include <stdio.h>
#define test(expr) do { \
if (!(expr)) printf("FAILED: %s\n", #expr); \
else printf(" ok: %s\n", #expr); \
} while (0)
int main(void) {
int a = 1;
test(a == 1);
test(a == 0);
return 0;
}
I don't actually use the direct printf() approach, but this shows the idea. Is there a reason more people don't 'stringify' macro arguments? I'm pretty tied to GCC, and am not always aware of what works on other compilers.puts("FAILED: " #expr )
(Or you could keep the printf, as it looks like GCC optimizes it to puts when just used to print a string, without variable interpolation)
For those of you working in embedded environments where even that is too heavy: cross-compile or write that stupid macro yourself (it's 3 lines for pity's sake!).
#define mu_run_test(test) do { char message = test(); tests_run++; if (message) return message; } while (0)
Isn't one line but
#define mu_run_test(test) do {
char *message = test();
tests_run++;
if (message)
return message;
} while (0You downvote me now, but you'll be upvoting me when I make infinite amounts of money on my 2 LOC implementations of.. everything :-D