Merge branch 'jk/mark-unused-parameters'
[git/gitster.git] / t / unit-tests / t-strvec.c
blob5c844cf0c9b3fcbf5b66b033e2e6a345aa88216e
1 #include "test-lib.h"
2 #include "strbuf.h"
3 #include "strvec.h"
5 #define check_strvec(vec, ...) \
6 do { \
7 const char *expect[] = { __VA_ARGS__ }; \
8 if (check_uint(ARRAY_SIZE(expect), >, 0) && \
9 check_pointer_eq(expect[ARRAY_SIZE(expect) - 1], NULL) && \
10 check_uint((vec)->nr, ==, ARRAY_SIZE(expect) - 1) && \
11 check_uint((vec)->nr, <=, (vec)->alloc)) { \
12 for (size_t i = 0; i < ARRAY_SIZE(expect); i++) { \
13 if (!check_str((vec)->v[i], expect[i])) { \
14 test_msg(" i: %"PRIuMAX, \
15 (uintmax_t)i); \
16 break; \
17 } \
18 } \
19 } \
20 } while (0)
22 int cmd_main(int argc UNUSED, const char **argv UNUSED)
24 if_test ("static initialization") {
25 struct strvec vec = STRVEC_INIT;
26 check_pointer_eq(vec.v, empty_strvec);
27 check_uint(vec.nr, ==, 0);
28 check_uint(vec.alloc, ==, 0);
31 if_test ("dynamic initialization") {
32 struct strvec vec;
33 strvec_init(&vec);
34 check_pointer_eq(vec.v, empty_strvec);
35 check_uint(vec.nr, ==, 0);
36 check_uint(vec.alloc, ==, 0);
39 if_test ("clear") {
40 struct strvec vec = STRVEC_INIT;
41 strvec_push(&vec, "foo");
42 strvec_clear(&vec);
43 check_pointer_eq(vec.v, empty_strvec);
44 check_uint(vec.nr, ==, 0);
45 check_uint(vec.alloc, ==, 0);
48 if_test ("push") {
49 struct strvec vec = STRVEC_INIT;
51 strvec_push(&vec, "foo");
52 check_strvec(&vec, "foo", NULL);
54 strvec_push(&vec, "bar");
55 check_strvec(&vec, "foo", "bar", NULL);
57 strvec_clear(&vec);
60 if_test ("pushf") {
61 struct strvec vec = STRVEC_INIT;
62 strvec_pushf(&vec, "foo: %d", 1);
63 check_strvec(&vec, "foo: 1", NULL);
64 strvec_clear(&vec);
67 if_test ("pushl") {
68 struct strvec vec = STRVEC_INIT;
69 strvec_pushl(&vec, "foo", "bar", "baz", NULL);
70 check_strvec(&vec, "foo", "bar", "baz", NULL);
71 strvec_clear(&vec);
74 if_test ("pushv") {
75 const char *strings[] = {
76 "foo", "bar", "baz", NULL,
78 struct strvec vec = STRVEC_INIT;
80 strvec_pushv(&vec, strings);
81 check_strvec(&vec, "foo", "bar", "baz", NULL);
83 strvec_clear(&vec);
86 if_test ("replace at head") {
87 struct strvec vec = STRVEC_INIT;
88 strvec_pushl(&vec, "foo", "bar", "baz", NULL);
89 strvec_replace(&vec, 0, "replaced");
90 check_strvec(&vec, "replaced", "bar", "baz", NULL);
91 strvec_clear(&vec);
94 if_test ("replace at tail") {
95 struct strvec vec = STRVEC_INIT;
96 strvec_pushl(&vec, "foo", "bar", "baz", NULL);
97 strvec_replace(&vec, 2, "replaced");
98 check_strvec(&vec, "foo", "bar", "replaced", NULL);
99 strvec_clear(&vec);
102 if_test ("replace in between") {
103 struct strvec vec = STRVEC_INIT;
104 strvec_pushl(&vec, "foo", "bar", "baz", NULL);
105 strvec_replace(&vec, 1, "replaced");
106 check_strvec(&vec, "foo", "replaced", "baz", NULL);
107 strvec_clear(&vec);
110 if_test ("replace with substring") {
111 struct strvec vec = STRVEC_INIT;
112 strvec_pushl(&vec, "foo", NULL);
113 strvec_replace(&vec, 0, vec.v[0] + 1);
114 check_strvec(&vec, "oo", NULL);
115 strvec_clear(&vec);
118 if_test ("remove at head") {
119 struct strvec vec = STRVEC_INIT;
120 strvec_pushl(&vec, "foo", "bar", "baz", NULL);
121 strvec_remove(&vec, 0);
122 check_strvec(&vec, "bar", "baz", NULL);
123 strvec_clear(&vec);
126 if_test ("remove at tail") {
127 struct strvec vec = STRVEC_INIT;
128 strvec_pushl(&vec, "foo", "bar", "baz", NULL);
129 strvec_remove(&vec, 2);
130 check_strvec(&vec, "foo", "bar", NULL);
131 strvec_clear(&vec);
134 if_test ("remove in between") {
135 struct strvec vec = STRVEC_INIT;
136 strvec_pushl(&vec, "foo", "bar", "baz", NULL);
137 strvec_remove(&vec, 1);
138 check_strvec(&vec, "foo", "baz", NULL);
139 strvec_clear(&vec);
142 if_test ("pop with empty array") {
143 struct strvec vec = STRVEC_INIT;
144 strvec_pop(&vec);
145 check_strvec(&vec, NULL);
146 strvec_clear(&vec);
149 if_test ("pop with non-empty array") {
150 struct strvec vec = STRVEC_INIT;
151 strvec_pushl(&vec, "foo", "bar", "baz", NULL);
152 strvec_pop(&vec);
153 check_strvec(&vec, "foo", "bar", NULL);
154 strvec_clear(&vec);
157 if_test ("split empty string") {
158 struct strvec vec = STRVEC_INIT;
159 strvec_split(&vec, "");
160 check_strvec(&vec, NULL);
161 strvec_clear(&vec);
164 if_test ("split single item") {
165 struct strvec vec = STRVEC_INIT;
166 strvec_split(&vec, "foo");
167 check_strvec(&vec, "foo", NULL);
168 strvec_clear(&vec);
171 if_test ("split multiple items") {
172 struct strvec vec = STRVEC_INIT;
173 strvec_split(&vec, "foo bar baz");
174 check_strvec(&vec, "foo", "bar", "baz", NULL);
175 strvec_clear(&vec);
178 if_test ("split whitespace only") {
179 struct strvec vec = STRVEC_INIT;
180 strvec_split(&vec, " \t\n");
181 check_strvec(&vec, NULL);
182 strvec_clear(&vec);
185 if_test ("split multiple consecutive whitespaces") {
186 struct strvec vec = STRVEC_INIT;
187 strvec_split(&vec, "foo\n\t bar");
188 check_strvec(&vec, "foo", "bar", NULL);
189 strvec_clear(&vec);
192 if_test ("detach") {
193 struct strvec vec = STRVEC_INIT;
194 const char **detached;
196 strvec_push(&vec, "foo");
198 detached = strvec_detach(&vec);
199 check_str(detached[0], "foo");
200 check_pointer_eq(detached[1], NULL);
202 check_pointer_eq(vec.v, empty_strvec);
203 check_uint(vec.nr, ==, 0);
204 check_uint(vec.alloc, ==, 0);
206 free((char *) detached[0]);
207 free(detached);
210 return test_done();