]> git.ozlabs.org Git - ccan/blob - ccan/xstring/test/run.c
Add xstring module - bounded string builder with three valued comparator
[ccan] / ccan / xstring / test / run.c
1 #include <ccan/xstring/xstring.h>
2 /* Include the C files directly. */
3 #include <ccan/xstring/xstring.c>
4 #include <ccan/tap/tap.h>
5
6 int main(void)
7 {
8         plan_tests(70);
9
10         { //12
11                 char buf[4] = { 'a', 'a', 'a', 'a' };
12                 xstring a;
13
14                 ok1(xstrInit(&a, buf, sizeof(buf), 0) == 0);
15                 ok1(a.str == buf && a.len == 0 && a.cap == sizeof(buf) && a.truncated == 0 && a.str[0] == '\0');
16
17                 buf[0] = 'a';
18                 ok1(xstrInit(&a, buf, sizeof(buf), 1) == -1);
19                 ok1(a.str == buf && a.len == 3 && a.cap == sizeof(buf) && a.truncated == -1 && strcmp(a.str, "aaa") == 0);
20
21                 ok1(xstrInit(&a, buf, sizeof(buf), 1) == 0);
22                 ok1(a.str == buf && a.len == 3 && a.cap == sizeof(buf) && a.truncated == 0 && strcmp(a.str, "aaa") == 0);
23
24                 xstrClear(&a);
25                 ok1(xstrAddChar(&a, '1') == 0 && a.truncated == 0 && strcmp(a.str, "1") == 0);
26                 ok1(xstrAddChar(&a, '2') == 0 && a.truncated == 0 && strcmp(a.str, "12") == 0);
27                 ok1(xstrAddChar(&a, '3') == 0 && a.truncated == 0 && strcmp(a.str, "123") == 0);
28                 ok1(xstrAddChar(&a, '\0') == 0 && a.truncated == 0 && strcmp(a.str, "123") == 0);
29                 ok1(xstrAddChar(&a, '4') == -1 && a.truncated == -1 && strcmp(a.str, "123") == 0);
30                 ok1(xstrAddChar(&a, '5') == -1 && a.truncated == -1 && strcmp(a.str, "123") == 0);
31         }
32
33         { //21
34                 char buf[10];
35                 xstring _x, *x = &_x;
36
37                 ok1(xstrInit(x, buf, sizeof(buf), 0) == 0);
38                 ok1(x->str == buf && x->len == 0 && x->cap == sizeof(buf) && x->truncated == 0 && *x->str == '\0');
39
40                 ok1(xstrAdd(x, "") == 0 && x->len == 0 && x->truncated == 0 && *x->str == '\0');
41                 ok1(xstrAdd(x, NULL) == 0 && x->len == 0 && x->truncated == 0 && *x->str == '\0');
42
43                 ok1(xstrAdd(x, "foo") == 0 && x->len == 3 && x->truncated == 0 && strcmp(x->str, "foo") == 0);
44                 ok1(xstrAdd(x, "bar") == 0 && x->len == 6 && x->truncated == 0 && strcmp(x->str, "foobar") == 0);
45                 ok1(xstrAdd(x, "baz") == 0 && x->len == 9 && x->truncated == 0 && strcmp(x->str, "foobarbaz") == 0);
46                 ok1(xstrAdd(x, "oof") == -1 && x->len == 9 && x->truncated == -1 && strcmp(x->str, "foobarbaz") == 0);
47                 ok1(xstrAdd(x, "rab") == -1 && x->len == 9 && x->truncated == -1 && strcmp(x->str, "foobarbaz") == 0);
48
49                 xstrClear(x);
50                 ok1(x->str == buf && x->len == 0 && x->cap == sizeof(buf) && x->truncated == 0 && *x->str == '\0');
51                 ok1(xstrAdd(x, "foobarbazoof") == -1 && x->len == 9 && x->truncated == -1 && strcmp(x->str, "foobarbaz") == 0);
52
53                 xstrClear(x);
54                 ok1(xstrAdd(x, "foo") == 0 && x->len == 3 && x->truncated == 0 && strcmp(x->str, "foo") == 0);
55                 ok1(xstrAddT(x, "foobarbazoof") == -1 && x->len == 3 && x->truncated == -1 && strcmp(x->str, "foo") == 0);
56                 xstrClear(x);
57                 ok1(xstrAddT(&_x, "foobarbazoof") == -1 && x->len == 0 && x->truncated == -1 && *x->str == '\0');
58
59                 xstrClear(x);
60                 ok1(xstrCat(x, "foo", "bar", "baz", NULL) == 0 && x->len == 9 && x->truncated == 0 && strcmp(x->str, "foobarbaz") == 0);
61                 xstrClear(x);
62                 ok1(xstrCat(x, "foo", "bar", "baz", "oof", NULL) == -1 && x->len == 9 && x->truncated == -1 && strcmp(x->str, "foobarbaz") == 0);
63                 xstrClear(x);
64                 ok1(xstrCatT(x, "foo", "bar", "baz", "oof", NULL) == -1 && x->len == 0 && x->truncated == -1 && *x->str == '\0');
65                 xstrClear(x);
66                 ok1(xstrCatT(&_x, "foo", "bar", "baz", "oof", NULL) == -1 && x->len == 0 && x->truncated == -1 && *x->str == '\0');
67
68                 xstrClear(x);
69                 ok1(xstrJoin(x, ",", "fo", "ba", "ba", NULL) == 0 && x->len == 8 && x->truncated == 0 && strcmp(x->str, "fo,ba,ba") == 0);
70                 xstrClear(x);
71                 ok1(xstrJoin(x, ",", "foobarbaz", "oof", NULL) == -1 && x->len == 9 && x->truncated == -1 && strcmp(x->str, "foobarbaz") == 0);
72                 xstrClear(x);
73                 ok1(xstrJoin(x, ",", "foobarba", "oof", NULL) == -1 && x->len == 9 && x->truncated == -1 && strcmp(x->str, "foobarba,") == 0);
74         }
75
76         { //9
77                 char buf[10];
78                 xstring _x, *x = &_x;
79
80                 ok1(xstrInit(x, buf, sizeof(buf), 0) == 0);
81                 ok1(xstrAddSub(x, NULL, 0) == 0 && x->len == 0 && x->truncated == 0 && *x->str == '\0');
82                 ok1(xstrAddSub(x, "foo", 0) == 0 && x->len == 0 && x->truncated == 0 && *x->str == '\0');
83                 ok1(xstrAddSub(x, "foobar", 3) == 0 && x->len == 3 && x->truncated == 0 && strcmp(x->str, "foo") == 0);
84                 ok1(xstrAddSub(x, "foobarbaz", 7) == -1 && x->len == 3 && x->truncated == -1 && strcmp(x->str, "foo") == 0);
85
86                 xstrClear(x);
87                 ok1(xstrAddSubs(x, "aa", 1, "bbb", 2, NULL) == 0 && x->len == 3 && x->truncated == 0 && strcmp(x->str, "abb") == 0);
88                 ok1(xstrAddSubs(x, "cccccccc", 7, NULL) == -1 && x->len == 3 && x->truncated == -1 && strcmp(x->str, "abb") == 0);
89                 xstrClear(x);
90                 ok1(xstrAddSubsT(x, "aa", 1, "bbb", 2, NULL) == 0 && x->len == 3 && x->truncated == 0 && strcmp(x->str, "abb") == 0);
91                 ok1(xstrAddSubsT(x, "cccccccc", 7, NULL) == -1 && x->len == 3 && x->truncated == -1 && strcmp(x->str, "abb") == 0);
92         }
93
94         { //28
95                 char a[10], b[10];
96                 xstring _x, *x = &_x;
97                 xstring _y, *y = &_y;
98
99                 xstrInit(x, a, sizeof(a), 0);
100                 xstrInit(y, b, sizeof(b), 0);
101                 xstrAdd(x, "foobarbaz");
102                 xstrAdd(y, "foobarbaz");
103                 ok1(xstrEq3(x, y) == 1);
104                 ok1(xstrEq(x, y) == 1);
105                 ok1(xstrContains3(x, y, 1) == 1);
106                 ok1(xstrContains(x, y, 1) == 1);
107                 xstrAddChar(x, 'x');
108                 ok1(xstrEq3(x, y) == -1);
109                 ok1(xstrEq(x, y) == 0);
110                 ok1(xstrContains3(x, y, 1) == 1);
111                 ok1(xstrContains(x, y, 1) == 1);
112                 xstrClear(x);
113                 xstrAdd(x, "foobarbaz");
114                 xstrAddChar(y, 'x');
115                 ok1(xstrEq3(x, y) == -1);
116                 ok1(xstrEq(x, y) == 0);
117                 ok1(xstrContains3(x, y, 1) == -1);
118                 ok1(xstrContains(x, y, 1) == 0);
119                 xstrClear(y);
120                 xstrAdd(y, "Foobarbaz");
121                 ok1(xstrEq3(x, y) == 0);
122                 ok1(xstrEq(x, y) == 0);
123
124                 xstrClear(x);
125                 xstrClear(y);
126                 xstrAdd(x, "foo");
127                 xstrAdd(y, "foobarbaz");
128                 ok1(xstrContains3(x, y, 1) == 0);
129                 ok1(xstrContains(x, y, 1) == 0);
130                 ok1(xstrContains3(y, x, 1) == 1);
131                 ok1(xstrContains(y, x, 1) == 1);
132                 ok1(xstrContains3(y, x, 0) == 1);
133                 ok1(xstrContains(y, x, 0) == 1);
134                 ok1(xstrContains3(y, x, -1) == 0);
135                 ok1(xstrContains(y, x, -1) == 0);
136                 xstrClear(x);
137                 xstrAdd(x, "baz");
138                 ok1(xstrContains3(y, x, -1) == 1);
139                 ok1(xstrContains(y, x, -1) == 1);
140                 ok1(xstrContains3(y, x, 0) == 1);
141                 ok1(xstrContains(y, x, 0) == 1);
142                 ok1(xstrContains3(y, x, 1) == 0);
143                 ok1(xstrContains(y, x, 1) == 0);
144         }
145
146         return exit_status();
147 }