]> git.ozlabs.org Git - ccan/blob - ccan/list/test/run-CCAN_LIST_DEBUG.c
9ff4107880d0aad772fb68d277800f050fe5f1b3
[ccan] / ccan / list / test / run-CCAN_LIST_DEBUG.c
1 /* Check that CCAN_LIST_DEBUG works */
2 #include <setjmp.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <stdarg.h>
6 #include <string.h>
7 #include <err.h>
8
9 /* We don't actually want it to exit... */
10 static jmp_buf aborted;
11 #define abort() longjmp(aborted, 1)
12
13 #define fprintf my_fprintf
14 static char printf_buffer[1000];
15
16 static int my_fprintf(FILE *stream, const char *format, ...)
17 {
18         va_list ap;
19         int ret;
20         va_start(ap, format);
21         ret = vsprintf(printf_buffer, format, ap);
22         va_end(ap);
23         return ret;
24 }
25
26 #define CCAN_LIST_DEBUG 1
27 #include <ccan/list/list.h>
28 #include <ccan/tap/tap.h>
29 #include <ccan/list/list.c>
30
31 int main(void)
32 {
33         struct list_head list;
34         struct list_node n1;
35         char expect[100];
36
37         plan_tests(2);
38         /* Empty list. */
39         list.n.next = &list.n;
40         list.n.prev = &list.n;
41         ok1(list_check(&list, NULL) == &list);
42
43         /* Bad back ptr */
44         list.n.prev = &n1;
45
46         /* Aborting version. */
47         sprintf(expect, "run-CCAN_LIST_DEBUG.c:50: prev corrupt in node %p (0) of %p\n",
48                 &list, &list);
49         if (setjmp(aborted) == 0) {
50                 assert(list_empty(&list));
51                 fail("list_empty on empty with bad back ptr didn't fail!");
52         } else {
53                 /* __FILE__ might give full path. */
54                 int prep = strlen(printf_buffer) - strlen(expect);
55                 ok1(prep >= 0 && strcmp(printf_buffer + prep, expect) == 0);
56         }
57
58         return exit_status();
59 }