]> git.ozlabs.org Git - ccan/blob - ccan/asprintf/test/run.c
tdb2: rename internal hashfn and logfn to hash_fn and log_fn.
[ccan] / ccan / asprintf / test / run.c
1 #include <ccan/asprintf/asprintf.h>
2 /* Include the C files directly. */
3
4 /* Override vasprintf for testing. */
5 #if HAVE_ASPRINTF
6 #define vasprintf my_vasprintf
7 static int my_vasprintf(char **strp, const char *fmt, va_list ap);
8 #else
9 #include <stdio.h>
10 #include <stdarg.h>
11 #define vsnprintf my_vsnprintf
12 static int my_vsnprintf(char *str, size_t size, const char *format, va_list ap);
13 #endif
14
15 #include <ccan/asprintf/asprintf.c>
16 #include <ccan/tap/tap.h>
17 #include <string.h>
18 #include <stdlib.h>
19 #include <stdbool.h>
20
21 static bool fail;
22
23 #if HAVE_ASPRINTF
24 #undef vasprintf
25 static int my_vasprintf(char **strp, const char *fmt, va_list ap)
26 {
27         if (fail) {
28                 /* Set strp to crap. */
29                 *strp = (char *)(long)1;
30                 return -1;
31         }
32         return vasprintf(strp, fmt, ap);
33 }
34 #else
35 #undef vsnprintf
36 static int my_vsnprintf(char *str, size_t size, const char *format, va_list ap)
37 {
38         if (fail) {
39                 return -1;
40         }
41         return vsnprintf(str, size, format, ap);
42 }
43 #endif
44
45 int main(void)
46 {
47         char *p, nul = '\0';
48         int ret;
49
50         /* This is how many tests you plan to run */
51         plan_tests(8);
52
53         fail = false;
54         p = afmt("Test %u%cafter-nul", 1, nul);
55         ok1(p);
56         ok1(strlen(p) == strlen("Test 1"));
57         ok1(memcmp(p, "Test 1\0after-nul\0", 17) == 0);
58         free(p);
59
60         ret = asprintf(&p, "Test %u%cafter-nul", 1, nul);
61         ok1(ret == 16);
62         ok1(p);
63         ok1(strlen(p) == strlen("Test 1"));
64         ok1(memcmp(p, "Test 1\0after-nul\0", 17) == 0);
65         free(p);
66
67         fail = true;
68         p = afmt("Test %u%cafter-nul", 1, nul);
69         ok1(p == NULL);
70
71         /* This exits depending on whether all tests passed */
72         return exit_status();
73 }