Created
February 15, 2015 02:11
-
-
Save mulatinho/658f49b2f7ef698250b5 to your computer and use it in GitHub Desktop.
simple example of using assert for tdd in c
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> | |
#include <assert.h> | |
#define DEBUG(res) printf("\n%s in '%s' on function %s() line %d, tests_run: %d, tests_ok: %d, tests_fail: %d\n", \ | |
res ? "success" : "fail", __FILE__, __func__, __LINE__, tests_run, tests_ok, (tests_run-tests_ok)) | |
#define _assert(test) do { if (!(test)) { DEBUG(test); tests_run++; return 1; } \ | |
DEBUG(test); tests_run++; tests_ok++; } while(0) | |
int tests_ok = 0; | |
int tests_run = 0; | |
struct tmp { | |
int x; | |
}; | |
struct tmp lp; | |
struct tmp *letsgo(struct tmp *lu) | |
{ | |
return lu; | |
} | |
static int letsgo_test(void) | |
{ | |
struct tmp *la = NULL; | |
_assert(letsgo(la) != NULL); | |
return 0; | |
} | |
int power(int x) { | |
_assert(x != 3); | |
return x * x; | |
} | |
int all_tests() { | |
int res = 0, x = 1; | |
letsgo_test(); | |
for (; x < 6; x++) { | |
_assert(power(x) != 25); | |
} | |
return 0; | |
} | |
int main(int argc, char **argv) { | |
int result = all_tests(); | |
if (result == 0) | |
printf("Result: PASSED\n"); | |
else | |
printf("Result: FAIL\n"); | |
printf("Tests run: %d, Tests OK: %d, Tests FAIL: %d\n", | |
tests_run, tests_ok, (tests_run-tests_ok)); | |
return result != 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment