1 #include <ccan/xstring/xstring.h>
2 /* Include the C files directly. */
3 #include <ccan/xstring/xstring.c>
4 #include <ccan/tap/tap.h>
11 char buf[4] = { 'a', 'a', 'a', 'a' };
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');
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);
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);
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);
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');
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');
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);
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);
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);
57 ok1(xstrAddT(&_x, "foobarbazoof") == -1 && x->len == 0 && x->truncated == -1 && *x->str == '\0');
60 ok1(xstrCat(x, "foo", "bar", "baz", NULL) == 0 && x->len == 9 && x->truncated == 0 && strcmp(x->str, "foobarbaz") == 0);
62 ok1(xstrCat(x, "foo", "bar", "baz", "oof", NULL) == -1 && x->len == 9 && x->truncated == -1 && strcmp(x->str, "foobarbaz") == 0);
64 ok1(xstrCatT(x, "foo", "bar", "baz", "oof", NULL) == -1 && x->len == 0 && x->truncated == -1 && *x->str == '\0');
66 ok1(xstrCatT(&_x, "foo", "bar", "baz", "oof", NULL) == -1 && x->len == 0 && x->truncated == -1 && *x->str == '\0');
69 ok1(xstrJoin(x, ",", "fo", "ba", "ba", NULL) == 0 && x->len == 8 && x->truncated == 0 && strcmp(x->str, "fo,ba,ba") == 0);
71 ok1(xstrJoin(x, ",", "foobarbaz", "oof", NULL) == -1 && x->len == 9 && x->truncated == -1 && strcmp(x->str, "foobarbaz") == 0);
73 ok1(xstrJoin(x, ",", "foobarba", "oof", NULL) == -1 && x->len == 9 && x->truncated == -1 && strcmp(x->str, "foobarba,") == 0);
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);
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);
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);
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);
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);
113 xstrAdd(x, "foobarbaz");
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);
120 xstrAdd(y, "Foobarbaz");
121 ok1(xstrEq3(x, y) == 0);
122 ok1(xstrEq(x, y) == 0);
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);
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);
146 return exit_status();