]> git.ozlabs.org Git - ccan/blob - ccan/list/test/run-CCAN_LIST_DEBUG.c
base64: fix for unsigned chars (e.g. ARM).
[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         (void)stream;
21         va_start(ap, format);
22         ret = vsprintf(printf_buffer, format, ap);
23         va_end(ap);
24         return ret;
25 }
26
27 #define CCAN_LIST_DEBUG 1
28 #include <ccan/list/list.h>
29 #include <ccan/tap/tap.h>
30 #include <ccan/list/list.c>
31
32 int main(void)
33 {
34         struct list_head list;
35         struct list_node n1;
36         char expect[100];
37
38         plan_tests(2);
39         /* Empty list. */
40         list.n.next = &list.n;
41         list.n.prev = &list.n;
42         ok1(list_check(&list, NULL) == &list);
43
44         /* Bad back ptr */
45         list.n.prev = &n1;
46
47         /* Aborting version. */
48         sprintf(expect, "run-CCAN_LIST_DEBUG.c:51: prev corrupt in node %p (0) of %p\n",
49                 &list, &list);
50         if (setjmp(aborted) == 0) {
51                 assert(list_empty(&list));
52                 fail("list_empty on empty with bad back ptr didn't fail!");
53         } else {
54                 /* __FILE__ might give full path. */
55                 int prep = strlen(printf_buffer) - strlen(expect);
56                 ok1(prep >= 0 && strcmp(printf_buffer + prep, expect) == 0);
57         }
58
59         return exit_status();
60 }