Created
February 18, 2023 16:38
-
-
Save cbrunnkvist/f2fd7317eff9d7ab70054c8788356039 to your computer and use it in GitHub Desktop.
TAP (Test Anything Protocol) 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
/* from https://www.lemoda.net/c/simple-tap-test/ */ | |
static int tap_count; | |
static int tap_todo; | |
static int tap_fail; | |
#define ENDLINE { \ | |
if (tap_todo) { \ | |
printf (" # TODO\n"); \ | |
} \ | |
else { \ | |
printf ("\n"); \ | |
} \ | |
} | |
#define TAP_TEST(x) { \ | |
tap_count++; \ | |
if (! (x)) { \ | |
if (! tap_todo) { \ | |
tap_fail++; \ | |
} \ | |
printf ("not "); \ | |
} \ | |
printf ("ok %d - %s", tap_count, #x); \ | |
ENDLINE; \ | |
} | |
#define TAP_TEST_EQUAL(x,y) { \ | |
int xval = (int) x; \ | |
int yval = (int) y; \ | |
tap_count++; \ | |
if (! (xval == yval)) { \ | |
if (! tap_todo) { \ | |
tap_fail++; \ | |
} \ | |
printf ("not "); \ | |
} \ | |
printf ("ok %d - %s (%d) == %s (%d)", tap_count, \ | |
#x, xval, #y, yval); \ | |
ENDLINE; \ | |
} | |
#define TAP_TEST_MSG(x,msg,args...) { \ | |
tap_count++; \ | |
if (! (x)) { \ | |
if (! tap_todo) { \ | |
tap_fail++; \ | |
} \ | |
printf ("not "); \ | |
} \ | |
printf ("ok %d - ", tap_count); \ | |
printf (msg, ## args); \ | |
ENDLINE; \ | |
} | |
#define TODO tap_todo = 1 | |
#define END_TODO tap_todo = 0 | |
#define TAP_PLAN { \ | |
printf ("1..%d\n", tap_count); \ | |
} |
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
NAME | |
c-tap-test | |
SYNOPSIS | |
#include <stdio.h> | |
#include "c-tap-test.h" | |
int main () | |
{ | |
int a = 1, b = 1, c = 2; | |
TAP_TEST (a==b); | |
TAP_TEST_EQUAL (a, b); | |
TAP_TEST_MSG (a==b, "a %d is the same as b %d", a, b); | |
TODO; | |
TAP_TEST(a==c); | |
END_TODO; | |
TAP_PLAN; | |
return tap_fail; | |
} | |
MACROS | |
TAP_TEST | |
TAP_TEST(condition) | |
If the condition is true, pass a test. If the condition is false, fail a test. | |
TAP_TEST_MSG | |
TAP_TEST_MSG(condition, format, ...) | |
If the condition is true, pass a test. If the condition is false, fail a test. In either case, print a message | |
using "format". | |
TAP_TEST_EQUAL | |
TAP_TEST_EQUAL(x, y) | |
If x and y are equal, considered as integers, pass a test. If x and y are not equal, considered as integers, fail | |
a test. | |
TODO/END_TODO | |
Start and stop the TAP todo test format. | |
TAP_PLAN | |
Print the plan for the TAP test, the numbers indicating the tests taken. | |
GLOBAL VARIABLES | |
This header uses static global variables as follows. | |
tap_count | |
The total number of tests seen so far, as printed by TAP_PLAN. | |
tap_todo | |
True or false depending on whether in a to-do section. Toggled by TODO and END_TODO. | |
tap_fail | |
The number of failed tests, not including to-do tests. |
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
/* from https://www.lemoda.net/c/simple-tap-test/ */ | |
#include <stdio.h> | |
#include "c-tap-test.h" | |
int main () | |
{ | |
int x; | |
TAP_TEST (1 == 1); | |
TAP_TEST_MSG (1 + 1 == 2, "one plus one equals two"); | |
x = 4; | |
TAP_TEST_MSG (x + 1 == 5, "%d plus one equals five", x); | |
TODO; | |
TAP_TEST (1 == 0); | |
END_TODO; | |
TAP_PLAN; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment