stringbuilder: Functions for joining strings.
[ccan] / ccan / stringbuilder / test / run.c
1 #include <ccan/stringbuilder/stringbuilder.h>
2 #include <ccan/stringbuilder/stringbuilder.c>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <errno.h>
6 /*
7  * This triggers a circular dependency
8  * #include <ccan/str/str.h>
9  *
10  * We only want the following macro:
11  */
12 #define streq(s1,s2) (!strcmp(s1,s2))
13
14 #include <ccan/tap/tap.h>
15
16 int main(int argc, char *argv[])
17 {
18         char string[20];
19         const char* str_array[] = {
20                 "xxx", "yyy"
21         };
22         int res;
23
24         res = stringbuilder(string, sizeof(string), NULL,
25                         "aaa", "bbb");
26         printf("res: %s, string: %s\n",
27                         strerror(res), string);
28         ok1(res == 0);
29         ok1(streq(string, "aaabbb"));
30
31         res = stringbuilder(string, sizeof(string), NULL,
32                         "aaaaa", "bbbbb", "ccccc", "ddddd",
33                         "eeeee", "fffff");
34         printf("res: %s, string: %s\n",
35                         strerror(res), string);
36         ok1(res == EMSGSIZE);
37
38         res = stringbuilder(string, sizeof(string), ", ",
39                         "aaa");
40         printf("res: %s, string: %s\n",
41                         strerror(res), string);
42         ok1(res == 0);
43         ok1(streq(string, "aaa"));
44
45         res = stringbuilder(string, sizeof(string), ", ",
46                         "aaa", "bbb");
47         printf("res: %s, string: %s\n",
48                         strerror(res), string);
49         ok1(res == 0);
50         ok1(streq(string, "aaa, bbb"));
51
52         res = stringbuilder_array(string, sizeof(string), NULL,
53                         sizeof(str_array)/sizeof(str_array[0]), str_array);
54         printf("res: %s, string: %s\n",
55                         strerror(res), string);
56         ok1(res == 0);
57         ok1(streq(string, "xxxyyy"));
58
59         res = stringbuilder_array(string, sizeof(string), ", ",
60                         sizeof(str_array)/sizeof(str_array[0]), str_array);
61         printf("res: %s, string: %s\n",
62                         strerror(res), string);
63         ok1(res == 0);
64         ok1(streq(string, "xxx, yyy"));
65
66         return exit_status();
67 }