X-Git-Url: http://git.ozlabs.org/?a=blobdiff_plain;ds=inline;f=ccan%2Flist%2Ftest%2Frun-CCAN_LIST_DEBUG.c;fp=ccan%2Flist%2Ftest%2Frun-CCAN_LIST_DEBUG.c;h=9fcb1d7456a699720f6e9d141039a411aa092a19;hb=ecf907f7e6b41ba69a10b20b2cb5743ed9cdf919;hp=0000000000000000000000000000000000000000;hpb=b989e06c093fb7a2befae277f684fa75b64b9ef5;p=ccan diff --git a/ccan/list/test/run-CCAN_LIST_DEBUG.c b/ccan/list/test/run-CCAN_LIST_DEBUG.c new file mode 100644 index 00000000..9fcb1d74 --- /dev/null +++ b/ccan/list/test/run-CCAN_LIST_DEBUG.c @@ -0,0 +1,59 @@ +/* Check that CCAN_LIST_DEBUG works */ +#include +#include +#include +#include +#include +#include + +/* We don't actually want it to exit... */ +static jmp_buf aborted; +#define abort() longjmp(aborted, 1) + +#define fprintf my_fprintf +static char printf_buffer[1000]; + +static int my_fprintf(FILE *stream, const char *format, ...) +{ + va_list ap; + int ret; + va_start(ap, format); + ret = vsprintf(printf_buffer, format, ap); + va_end(ap); + return ret; +} + +#define CCAN_LIST_DEBUG 1 +#include +#include +#include + +int main(int argc, char *argv[]) +{ + struct list_head list; + struct list_node n1; + char expect[100]; + + plan_tests(2); + /* Empty list. */ + list.n.next = &list.n; + list.n.prev = &list.n; + ok1(list_check(&list, NULL) == &list); + + /* Bad back ptr */ + list.n.prev = &n1; + + /* Aborting version. */ + sprintf(expect, "run-CCAN_LIST_DEBUG.c:50: prev corrupt in node %p (0) of %p\n", + &list, &list); + if (setjmp(aborted) == 0) { + assert(list_empty(&list)); + fail("list_empty on empty with bad back ptr didn't fail!"); + } else { + /* __FILE__ might give full path. */ + int prep = strlen(printf_buffer) - strlen(expect); + ok1(prep >= 0 && strcmp(printf_buffer + prep, expect) == 0); + } + + return exit_status(); +}