tests/test_string.c

changeset 777
e5b29e6f0615
child 806
e06249e09f99
equal deleted inserted replaced
776:874cc78cdffd 777:e5b29e6f0615
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2023 Mike Becker, Olaf Wintermann All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "cx/test.h"
30 #include "util_allocator.h"
31
32 #include "cx/string.h"
33
34 #define ASSERT_ZERO_TERMINATED(str) CX_TEST_ASSERTM((str).ptr[(str).length] == '\0', \
35 #str " is not zero terminated")
36
37 CX_TEST(test_string_construct) {
38 cxstring s1 = CX_STR("1234");
39 cxstring s2 = cx_strn("abcd", 2);
40 cxmutstr s3 = cx_mutstr((char *) "1234");
41 cxmutstr s4 = cx_mutstrn((char *) "abcd", 2);
42 CX_TEST_DO {
43 CX_TEST_ASSERT(s1.length == 4);
44 CX_TEST_ASSERT(strncmp(s1.ptr, "1234", 4) == 0);
45 CX_TEST_ASSERT(s2.length == 2);
46 CX_TEST_ASSERT(strncmp(s2.ptr, "ab", 2) == 0);
47 CX_TEST_ASSERT(s3.length == 4);
48 CX_TEST_ASSERT(strncmp(s3.ptr, "1234", 4) == 0);
49 CX_TEST_ASSERT(s4.length == 2);
50 CX_TEST_ASSERT(strncmp(s4.ptr, "ab", 2) == 0);
51 }
52 }
53
54 CX_TEST(test_strfree) {
55 CxTestingAllocator talloc;
56 cx_testing_allocator_init(&talloc);
57 CxAllocator *alloc = &talloc.base;
58 CX_TEST_DO {
59 char *test = cxMalloc(alloc, 16);
60 cxmutstr str = cx_mutstrn(test, 16);
61 CX_TEST_ASSERT(str.ptr == test);
62 CX_TEST_ASSERT(str.length == 16);
63 cx_strfree_a(alloc, &str);
64 CX_TEST_ASSERT(str.ptr == NULL);
65 CX_TEST_ASSERT(str.length == 0);
66 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
67 }
68 cx_testing_allocator_destroy(&talloc);
69 }
70
71 CX_TEST(test_strdup) {
72 cxstring str = CX_STR("test");
73 cxmutstr dup = cx_strdup(str);
74 CX_TEST_DO {
75 CX_TEST_ASSERT(dup.length == str.length);
76 CX_TEST_ASSERT(0 == strcmp(dup.ptr, str.ptr));
77 ASSERT_ZERO_TERMINATED(dup);
78 }
79 cx_strfree(&dup);
80 }
81
82 CX_TEST(test_strdup_shortened) {
83 cxstring str = CX_STR("test");
84 str.length = 2;
85 cxmutstr dup = cx_strdup(str);
86 CX_TEST_DO {
87 CX_TEST_ASSERT(dup.length == str.length);
88 CX_TEST_ASSERT(0 == strcmp(dup.ptr, "te"));
89 ASSERT_ZERO_TERMINATED(dup);
90 }
91 cx_strfree(&dup);
92 }
93
94 CX_TEST(test_strlen) {
95 cxstring s1 = CX_STR("1234");
96 cxstring s2 = CX_STR(".:.:.");
97 cxstring s3 = CX_STR("X");
98 CX_TEST_DO {
99 size_t len0 = cx_strlen(0);
100 size_t len1 = cx_strlen(1, s1);
101 size_t len2 = cx_strlen(2, s1, s2);
102 size_t len3 = cx_strlen(3, s1, s2, s3);
103
104 CX_TEST_ASSERT(len0 == 0);
105 CX_TEST_ASSERT(len1 == 4);
106 CX_TEST_ASSERT(len2 == 9);
107 CX_TEST_ASSERT(len3 == 10);
108 }
109 }
110
111 CX_TEST(test_strsubs) {
112 cxstring str = CX_STR("A test string");
113
114 CX_TEST_DO {
115 cxstring sub = cx_strsubs(str, 0);
116 CX_TEST_ASSERT(0 == cx_strcmp(sub, str));
117
118 sub = cx_strsubs(str, 2);
119 CX_TEST_ASSERT(0 == cx_strcmp(sub, CX_STR("test string")));
120
121 sub = cx_strsubs(str, 7);
122 CX_TEST_ASSERT(0 == cx_strcmp(sub, CX_STR("string")));
123
124 sub = cx_strsubs(str, 15);
125 CX_TEST_ASSERT(0 == cx_strcmp(sub, CX_STR("")));
126
127 sub = cx_strsubsl(str, 2, 4);
128 CX_TEST_ASSERT(0 == cx_strcmp(sub, CX_STR("test")));
129
130 sub = cx_strsubsl(str, 7, 3);
131 CX_TEST_ASSERT(0 == cx_strcmp(sub, CX_STR("str")));
132
133 sub = cx_strsubsl(str, 7, 20);
134 CX_TEST_ASSERT(0 == cx_strcmp(sub, CX_STR("string")));
135
136 // just for coverage, call the _m variant
137 cxmutstr m = cx_strsubs_m(cx_mutstrn(NULL, 0), 0);
138 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(m), CX_STR("")));
139 }
140 }
141
142 CX_TEST(test_strchr) {
143 cxstring str = CX_STR("I will find you - and I will kill you");
144
145 CX_TEST_DO {
146 cxstring notfound = cx_strchr(str, 'x');
147 CX_TEST_ASSERT(notfound.length == 0);
148
149 cxstring result = cx_strchr(str, 'w');
150 CX_TEST_ASSERT(result.length == 35);
151 CX_TEST_ASSERT(0 == strcmp(result.ptr, "will find you - and I will kill you"));
152
153 // just for coverage, call the _m variant
154 cxmutstr m = cx_strchr_m(cx_mutstrn(NULL, 0), 'a');
155 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(m), CX_STR("")));
156 }
157 }
158
159 CX_TEST(test_strrchr) {
160 cxstring str = CX_STR("I will find you - and I will kill you");
161
162 CX_TEST_DO {
163 cxstring notfound = cx_strrchr(str, 'x');
164 CX_TEST_ASSERT(notfound.length == 0);
165
166 cxstring result = cx_strrchr(str, 'w');
167 CX_TEST_ASSERT(result.length == 13);
168 CX_TEST_ASSERT(0 == strcmp(result.ptr, "will kill you"));
169
170 // just for coverage, call the _m variant
171 cxmutstr m = cx_strrchr_m(cx_mutstrn(NULL, 0), 'a');
172 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(m), CX_STR("")));
173 }
174 }
175
176 CX_TEST(test_strstr) {
177 cxstring str = CX_STR("find the match in this string");
178 cxstring longstr = CX_STR(
179 "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl"
180 "mnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx"
181 "yzabcdeababababnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghij"
182 "klmnopqrstuvwxyzaababababababababrstuvwxyzabcdefghijklmnopqrstuv"
183 "abababababababababababababababababababababababababababababababab"
184 "abababababababababababababababababababababababababababababababab"
185 "abababababababababababababababababababababababababababababababab"
186 "abababababababababababababababababababababababababababababababab"
187 "abababababababababababababababababababababababababababababababab"
188 "abababababababababababababababababababababababababababababababab"
189 "wxyz1234567890");
190 cxstring longstrpattern = CX_STR(
191 "abababababababababababababababababababababababababababababababab"
192 "abababababababababababababababababababababababababababababababab"
193 "abababababababababababababababababababababababababababababababab"
194 "abababababababababababababababababababababababababababababababab"
195 "abababababababababababababababababababababababababababababababab"
196 );
197 cxstring longstrresult = CX_STR(
198 "abababababababababababababababababababababababababababababababab"
199 "abababababababababababababababababababababababababababababababab"
200 "abababababababababababababababababababababababababababababababab"
201 "abababababababababababababababababababababababababababababababab"
202 "abababababababababababababababababababababababababababababababab"
203 "abababababababababababababababababababababababababababababababab"
204 "wxyz1234567890"
205 );
206
207 CX_TEST_DO {
208 cxstring notfound = cx_strstr(str, CX_STR("no match"));
209 CX_TEST_ASSERT(notfound.length == 0);
210
211 cxstring result = cx_strstr(str, CX_STR("match"));
212 CX_TEST_ASSERT(result.length == 20);
213 CX_TEST_ASSERT(0 == strcmp(result.ptr, "match in this string"));
214
215 result = cx_strstr(str, CX_STR(""));
216 CX_TEST_ASSERT(result.length == str.length);
217 CX_TEST_ASSERT(0 == strcmp(result.ptr, str.ptr));
218
219 result = cx_strstr(longstr, longstrpattern);
220 CX_TEST_ASSERT(result.length == longstrresult.length);
221 CX_TEST_ASSERT(0 == strcmp(result.ptr, longstrresult.ptr));
222
223 // just for coverage, call the _m variant
224 cxmutstr mstr = cx_strdup(longstr);
225 cxmutstr m = cx_strstr_m(mstr, longstrpattern);
226 CX_TEST_ASSERT(m.length == longstrresult.length);
227 CX_TEST_ASSERT(0 == strcmp(m.ptr, longstrresult.ptr));
228 cx_strfree(&mstr);
229 }
230 }
231
232 CX_TEST(test_strcmp) {
233 cxstring str = CX_STR("compare this");
234 CX_TEST_DO {
235 CX_TEST_ASSERT(0 == cx_strcmp(CX_STR(""), CX_STR("")));
236 CX_TEST_ASSERT(0 < cx_strcmp(str, CX_STR("")));
237 CX_TEST_ASSERT(0 == cx_strcmp(str, CX_STR("compare this")));
238 CX_TEST_ASSERT(0 != cx_strcmp(str, CX_STR("Compare This")));
239 CX_TEST_ASSERT(0 > cx_strcmp(str, CX_STR("compare tool")));
240 CX_TEST_ASSERT(0 < cx_strcmp(str, CX_STR("compare shit")));
241 CX_TEST_ASSERT(0 > cx_strcmp(str, CX_STR("compare this not")));
242 CX_TEST_ASSERT(0 < cx_strcmp(str, CX_STR("compare")));
243
244 cxstring str2 = CX_STR("Compare This");
245 CX_TEST_ASSERT(0 != cx_strcmp_p(&str, &str2));
246 str2 = CX_STR("compare this");
247 CX_TEST_ASSERT(0 == cx_strcmp_p(&str, &str2));
248 }
249 }
250
251 CX_TEST(test_strcasecmp) {
252 cxstring str = CX_STR("compare this");
253 CX_TEST_DO {
254 CX_TEST_ASSERT(0 == cx_strcasecmp(CX_STR(""), CX_STR("")));
255 CX_TEST_ASSERT(0 < cx_strcasecmp(str, CX_STR("")));
256 CX_TEST_ASSERT(0 == cx_strcasecmp(str, CX_STR("compare this")));
257 CX_TEST_ASSERT(0 == cx_strcasecmp(str, CX_STR("Compare This")));
258 CX_TEST_ASSERT(0 > cx_strcasecmp(str, CX_STR("compare tool")));
259 CX_TEST_ASSERT(0 < cx_strcasecmp(str, CX_STR("compare shit")));
260 CX_TEST_ASSERT(0 > cx_strcasecmp(str, CX_STR("compare this not")));
261 CX_TEST_ASSERT(0 < cx_strcasecmp(str, CX_STR("compare")));
262
263 cxstring str2 = CX_STR("Compare This");
264 CX_TEST_ASSERT(0 == cx_strcasecmp_p(&str, &str2));
265 str2 = CX_STR("Compare Tool");
266 CX_TEST_ASSERT(0 > cx_strcasecmp_p(&str, &str2));
267 }
268 }
269
270 CX_TEST(test_strcat) {
271 cxstring s1 = CX_STR("12");
272 cxstring s2 = CX_STR("34");
273 cxstring s3 = CX_STR("56");
274 cxstring sn = {NULL, 0};
275
276 CxTestingAllocator talloc;
277 cx_testing_allocator_init(&talloc);
278 CxAllocator *alloc = &talloc.base;
279
280 CX_TEST_DO {
281 cxmutstr t1 = cx_strcat_a(alloc, 2, s1, s2);
282 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(t1), CX_STR("1234")));
283 ASSERT_ZERO_TERMINATED(t1);
284 cx_strfree_a(alloc, &t1);
285
286 cxmutstr t2 = cx_strcat_a(alloc, 3, s1, s2, s3);
287 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(t2), CX_STR("123456")));
288 ASSERT_ZERO_TERMINATED(t2);
289 cx_strfree_a(alloc, &t2);
290
291 cxmutstr t3 = cx_strcat_a(alloc, 6, s1, sn, s2, sn, s3, sn);
292 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(t3), CX_STR("123456")));
293 ASSERT_ZERO_TERMINATED(t3);
294 cx_strfree_a(alloc, &t3);
295
296 cxmutstr t4 = cx_strcat_a(alloc, 2, sn, sn);
297 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(t4), CX_STR("")));
298 ASSERT_ZERO_TERMINATED(t4);
299 cx_strfree_a(alloc, &t4);
300
301 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
302
303 // use the macro
304 cxmutstr t5 = cx_strcat(3, s3, s1, s2);
305 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(t5), CX_STR("561234")));
306 ASSERT_ZERO_TERMINATED(t5);
307 cx_strfree(&t5);
308
309 // use an initial string
310 cxmutstr t6 = cx_strdup(CX_STR("Hello"));
311 t6 = cx_strcat_m(t6, 2, CX_STR(", "), CX_STR("World!"));
312 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(t6), CX_STR("Hello, World!")));
313 ASSERT_ZERO_TERMINATED(t6);
314 cx_strfree(&t6);
315 }
316 cx_testing_allocator_destroy(&talloc);
317 }
318
319 CX_TEST(test_strsplit) {
320 cxstring test = CX_STR("this,is,a,csv,string");
321 size_t capa = 8;
322 cxstring list[8];
323 size_t n;
324 CX_TEST_DO {
325 // special case: empty string
326 n = cx_strsplit(test, CX_STR(""), capa, list);
327 CX_TEST_ASSERT(n == 1);
328 CX_TEST_ASSERT(0 == cx_strcmp(list[0], test));
329
330 // no delimiter occurrence
331 n = cx_strsplit(test, CX_STR("z"), capa, list);
332 CX_TEST_ASSERT(n == 1);
333 CX_TEST_ASSERT(0 == cx_strcmp(list[0], test));
334
335 // partially matching delimiter
336 n = cx_strsplit(test, CX_STR("is,not"), capa, list);
337 CX_TEST_ASSERT(n == 1);
338 CX_TEST_ASSERT(0 == cx_strcmp(list[0], test));
339
340 // matching single-char delimiter
341 n = cx_strsplit(test, CX_STR(","), capa, list);
342 CX_TEST_ASSERT(n == 5);
343 CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("this")));
344 CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR("is")));
345 CX_TEST_ASSERT(0 == cx_strcmp(list[2], CX_STR("a")));
346 CX_TEST_ASSERT(0 == cx_strcmp(list[3], CX_STR("csv")));
347 CX_TEST_ASSERT(0 == cx_strcmp(list[4], CX_STR("string")));
348
349 // matching multi-char delimiter
350 n = cx_strsplit(test, CX_STR("is"), capa, list);
351 CX_TEST_ASSERT(n == 3);
352 CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("th")));
353 CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR(",")));
354 CX_TEST_ASSERT(0 == cx_strcmp(list[2], CX_STR(",a,csv,string")));
355
356 // bounded list using single-char delimiter
357 n = cx_strsplit(test, CX_STR(","), 3, list);
358 CX_TEST_ASSERT(n == 3);
359 CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("this")));
360 CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR("is")));
361 CX_TEST_ASSERT(0 == cx_strcmp(list[2], CX_STR("a,csv,string")));
362
363 // bounded list using multi-char delimiter
364 n = cx_strsplit(test, CX_STR("is"), 2, list);
365 CX_TEST_ASSERT(n == 2);
366 CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("th")));
367 CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR(",is,a,csv,string")));
368
369 // start with delimiter
370 n = cx_strsplit(test, CX_STR("this"), capa, list);
371 CX_TEST_ASSERT(n == 2);
372 CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("")));
373 CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR(",is,a,csv,string")));
374
375 // end with delimiter
376 n = cx_strsplit(test, CX_STR("string"), capa, list);
377 CX_TEST_ASSERT(n == 2);
378 CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("this,is,a,csv,")));
379 CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR("")));
380
381
382 // end with delimiter exceed bound
383 n = cx_strsplit(CX_STR("a,b,c,"), CX_STR(","), 3, list);
384 CX_TEST_ASSERT(n == 3);
385 CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("a")));
386 CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR("b")));
387 CX_TEST_ASSERT(0 == cx_strcmp(list[2], CX_STR("c,")));
388
389 // exact match
390 n = cx_strsplit(test, CX_STR("this,is,a,csv,string"), capa, list);
391 CX_TEST_ASSERT(n == 2);
392 CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("")));
393 CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR("")));
394
395 // string to be split is only substring
396 n = cx_strsplit(test, CX_STR("this,is,a,csv,string,with,extension"), capa, list);
397 CX_TEST_ASSERT(n == 1);
398 CX_TEST_ASSERT(0 == cx_strcmp(list[0], test));
399
400 // subsequent encounter of delimiter (the string between is empty)
401 n = cx_strsplit(test, CX_STR("is,"), capa, list);
402 CX_TEST_ASSERT(n == 3);
403 CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("th")));
404 CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR("")));
405 CX_TEST_ASSERT(0 == cx_strcmp(list[2], CX_STR("a,csv,string")));
406
407 // call the _m variant just for coverage
408 cxmutstr mtest = cx_strdup(test);
409 cxmutstr mlist[4];
410 n = cx_strsplit_m(mtest, CX_STR("is,"), 4, mlist);
411 CX_TEST_ASSERT(n == 3);
412 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(mlist[0]), CX_STR("th")));
413 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(mlist[1]), CX_STR("")));
414 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(mlist[2]), CX_STR("a,csv,string")));
415 cx_strfree(&mtest);
416 }
417 }
418
419 CX_TEST(test_strsplit_a) {
420 CxTestingAllocator talloc;
421 cx_testing_allocator_init(&talloc);
422 CxAllocator *alloc = &talloc.base;
423
424 cxstring test = CX_STR("this,is,a,csv,string");
425 size_t capa = 8;
426 cxstring *list;
427 size_t n;
428 CX_TEST_DO {
429 // special case: empty string
430 n = cx_strsplit_a(alloc, test, CX_STR(""), capa, &list);
431 CX_TEST_ASSERT(n == 1);
432 CX_TEST_ASSERT(0 == cx_strcmp(list[0], test));
433 cxFree(alloc, list);
434
435 // no delimiter occurrence
436 n = cx_strsplit_a(alloc, test, CX_STR("z"), capa, &list);
437 CX_TEST_ASSERT(n == 1);
438 CX_TEST_ASSERT(0 == cx_strcmp(list[0], test));
439 cxFree(alloc, list);
440
441 // partially matching delimiter
442 n = cx_strsplit_a(alloc, test, CX_STR("is,not"), capa, &list);
443 CX_TEST_ASSERT(n == 1);
444 CX_TEST_ASSERT(0 == cx_strcmp(list[0], test));
445 cxFree(alloc, list);
446
447 // matching single-char delimiter
448 n = cx_strsplit_a(alloc, test, CX_STR(","), capa, &list);
449 CX_TEST_ASSERT(n == 5);
450 CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("this")));
451 CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR("is")));
452 CX_TEST_ASSERT(0 == cx_strcmp(list[2], CX_STR("a")));
453 CX_TEST_ASSERT(0 == cx_strcmp(list[3], CX_STR("csv")));
454 CX_TEST_ASSERT(0 == cx_strcmp(list[4], CX_STR("string")));
455 cxFree(alloc, list);
456
457 // matching multi-char delimiter
458 n = cx_strsplit_a(alloc, test, CX_STR("is"), capa, &list);
459 CX_TEST_ASSERT(n == 3);
460 CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("th")));
461 CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR(",")));
462 CX_TEST_ASSERT(0 == cx_strcmp(list[2], CX_STR(",a,csv,string")));
463 cxFree(alloc, list);
464
465 // bounded list using single-char delimiter
466 n = cx_strsplit_a(alloc, test, CX_STR(","), 3, &list);
467 CX_TEST_ASSERT(n == 3);
468 CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("this")));
469 CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR("is")));
470 CX_TEST_ASSERT(0 == cx_strcmp(list[2], CX_STR("a,csv,string")));
471 cxFree(alloc, list);
472
473 // bounded list using multi-char delimiter
474 n = cx_strsplit_a(alloc, test, CX_STR("is"), 2, &list);
475 CX_TEST_ASSERT(n == 2);
476 CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("th")));
477 CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR(",is,a,csv,string")));
478 cxFree(alloc, list);
479
480 // start with delimiter
481 n = cx_strsplit_a(alloc, test, CX_STR("this"), capa, &list);
482 CX_TEST_ASSERT(n == 2);
483 CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("")));
484 CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR(",is,a,csv,string")));
485 cxFree(alloc, list);
486
487 // end with delimiter
488 n = cx_strsplit_a(alloc, test, CX_STR("string"), capa, &list);
489 CX_TEST_ASSERT(n == 2);
490 CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("this,is,a,csv,")));
491 CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR("")));
492 cxFree(alloc, list);
493
494 // end with delimiter exceed bound
495 n = cx_strsplit_a(alloc, CX_STR("a,b,c,"), CX_STR(","), 3, &list);
496 CX_TEST_ASSERT(n == 3);
497 CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("a")));
498 CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR("b")));
499 CX_TEST_ASSERT(0 == cx_strcmp(list[2], CX_STR("c,")));
500 cxFree(alloc, list);
501
502 // exact match
503 n = cx_strsplit_a(alloc, test, CX_STR("this,is,a,csv,string"), capa, &list);
504 CX_TEST_ASSERT(n == 2);
505 CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("")));
506 CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR("")));
507 cxFree(alloc, list);
508
509 // string to be split is only substring
510 n = cx_strsplit_a(alloc, test, CX_STR("this,is,a,csv,string,with,extension"), capa, &list);
511 CX_TEST_ASSERT(n == 1);
512 CX_TEST_ASSERT(0 == cx_strcmp(list[0], test));
513 cxFree(alloc, list);
514
515 // subsequent encounter of delimiter (the string between is empty)
516 n = cx_strsplit_a(alloc, test, CX_STR("is,"), capa, &list);
517 CX_TEST_ASSERT(n == 3);
518 CX_TEST_ASSERT(0 == cx_strcmp(list[0], CX_STR("th")));
519 CX_TEST_ASSERT(0 == cx_strcmp(list[1], CX_STR("")));
520 CX_TEST_ASSERT(0 == cx_strcmp(list[2], CX_STR("a,csv,string")));
521 cxFree(alloc, list);
522
523 // call the _m variant just for coverage
524 cxmutstr mtest = cx_strdup(test);
525 cxmutstr *mlist;
526 n = cx_strsplit_ma(alloc, mtest, CX_STR("is,"), 4, &mlist);
527 CX_TEST_ASSERT(n == 3);
528 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(mlist[0]), CX_STR("th")));
529 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(mlist[1]), CX_STR("")));
530 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(mlist[2]), CX_STR("a,csv,string")));
531 cxFree(alloc, mlist);
532 cx_strfree(&mtest);
533
534 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
535 }
536 cx_testing_allocator_destroy(&talloc);
537 }
538
539 CX_TEST(test_strtrim) {
540 cxstring t1 = cx_strtrim(CX_STR(" ein test \t "));
541 cxstring t2 = cx_strtrim(CX_STR("abc"));
542 cxstring t3 = cx_strtrim(CX_STR(" 123"));
543 cxstring t4 = cx_strtrim(CX_STR("xyz "));
544 cxstring t5 = cx_strtrim(CX_STR(" "));
545 cxstring empty = cx_strtrim(CX_STR(""));
546
547 CX_TEST_DO {
548 CX_TEST_ASSERT(0 == cx_strcmp(t1, CX_STR("ein test")));
549 CX_TEST_ASSERT(0 == cx_strcmp(t2, CX_STR("abc")));
550 CX_TEST_ASSERT(0 == cx_strcmp(t3, CX_STR("123")));
551 CX_TEST_ASSERT(0 == cx_strcmp(t4, CX_STR("xyz")));
552 CX_TEST_ASSERT(0 == cx_strcmp(t5, CX_STR("")));
553 CX_TEST_ASSERT(0 == cx_strcmp(empty, CX_STR("")));
554
555 // call the _m variant just for coverage
556 cxmutstr m1 = cx_strtrim_m(cx_mutstr((char *) " ein test \t "));
557 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(m1), CX_STR("ein test")));
558 }
559 }
560
561 CX_TEST(test_strprefix) {
562 cxstring str = CX_STR("test my prefix and my suffix");
563 cxstring empty = CX_STR("");
564 CX_TEST_DO {
565 CX_TEST_ASSERT(!cx_strprefix(empty, CX_STR("pref")));
566 CX_TEST_ASSERT(cx_strprefix(str, empty));
567 CX_TEST_ASSERT(cx_strprefix(empty, empty));
568 CX_TEST_ASSERT(cx_strprefix(str, CX_STR("test ")));
569 CX_TEST_ASSERT(!cx_strprefix(str, CX_STR("8-) fsck ")));
570 }
571 }
572
573 CX_TEST(test_strsuffix) {
574 cxstring str = CX_STR("test my prefix and my suffix");
575 cxstring empty = CX_STR("");
576 CX_TEST_DO {
577 CX_TEST_ASSERT(!cx_strsuffix(empty, CX_STR("suf")));
578 CX_TEST_ASSERT(cx_strsuffix(str, empty));
579 CX_TEST_ASSERT(cx_strsuffix(empty, empty));
580 CX_TEST_ASSERT(cx_strsuffix(str, CX_STR("fix")));
581 CX_TEST_ASSERT(!cx_strsuffix(str, CX_STR("fox")));
582 }
583 }
584
585 CX_TEST(test_strcaseprefix) {
586 cxstring str = CX_STR("test my prefix and my suffix");
587 cxstring empty = CX_STR("");
588 CX_TEST_DO {
589 CX_TEST_ASSERT(!cx_strcaseprefix(empty, CX_STR("pREf")));
590 CX_TEST_ASSERT(cx_strcaseprefix(str, empty));
591 CX_TEST_ASSERT(cx_strcaseprefix(empty, empty));
592 CX_TEST_ASSERT(cx_strcaseprefix(str, CX_STR("TEST ")));
593 CX_TEST_ASSERT(!cx_strcaseprefix(str, CX_STR("8-) fsck ")));
594 }
595 }
596
597 CX_TEST(test_strcasesuffix) {
598 cxstring str = CX_STR("test my prefix and my suffix");
599 cxstring empty = CX_STR("");
600 CX_TEST_DO {
601 CX_TEST_ASSERT(!cx_strcasesuffix(empty, CX_STR("sUf")));
602 CX_TEST_ASSERT(cx_strcasesuffix(str, empty));
603 CX_TEST_ASSERT(cx_strcasesuffix(empty, empty));
604 CX_TEST_ASSERT(cx_strcasesuffix(str, CX_STR("FIX")));
605 CX_TEST_ASSERT(!cx_strcasesuffix(str, CX_STR("fox")));
606 }
607 }
608
609 CX_TEST(test_strreplace) {
610 CxTestingAllocator talloc;
611 cx_testing_allocator_init(&talloc);
612 CxAllocator *alloc = &talloc.base;
613
614 cxstring str = CX_STR("test ababab string aba");
615 cxstring longstr = CX_STR(
616 "xyaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaacd");
617 cxstring notrail = CX_STR("test abab");
618 cxstring empty = CX_STR("");
619 cxstring astr = CX_STR("aaaaaaaaaa");
620 cxstring csstr = CX_STR("test AB ab TEST xyz");
621
622 cxmutstr repl = cx_strreplace(str, CX_STR("abab"), CX_STR("muchlonger"));
623 char const *expected = "test muchlongerab string aba";
624
625 cxmutstr repln = cx_strreplacen(str, CX_STR("ab"), CX_STR("c"), 2);
626 char const *expectedn = "test ccab string aba";
627
628 cxmutstr longrepl = cx_strreplace(longstr, CX_STR("a"), CX_STR("z"));
629 char const *longexpect = "xyzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzcd";
630
631 cxmutstr replnotrail = cx_strreplace(notrail, CX_STR("ab"), CX_STR("z"));
632 char const *notrailexpect = "test zz";
633
634 cxmutstr repleq = cx_strreplace(str, str, CX_STR("hello"));
635 char const *eqexpect = "hello";
636
637 cxmutstr replempty1 = cx_strreplace(empty, CX_STR("ab"), CX_STR("c")); // expect: empty
638 cxmutstr replempty2 = cx_strreplace(str, CX_STR("abab"), empty);
639 char const *emptyexpect2 = "test ab string aba";
640
641 cxmutstr replpre = cx_strreplace(str, CX_STR("test "), CX_STR("TEST "));
642 char const *preexpected = "TEST ababab string aba";
643
644 cxmutstr replan1 = cx_strreplacen(astr, CX_STR("a"), CX_STR("x"), 1);
645 char const *an1expected = "xaaaaaaaaa";
646
647 cxmutstr replan4 = cx_strreplacen(astr, CX_STR("a"), CX_STR("x"), 4);
648 char const *an4expected = "xxxxaaaaaa";
649
650 cxmutstr replan9 = cx_strreplacen(astr, CX_STR("a"), CX_STR("x"), 9);
651 char const *an9expected = "xxxxxxxxxa";
652
653 cxmutstr replan10 = cx_strreplacen(astr, CX_STR("a"), CX_STR("x"), 10);
654 char const *an10expected = "xxxxxxxxxx";
655
656 CX_TEST_DO {
657 cxmutstr repl1_a = cx_strreplace_a(alloc, csstr, CX_STR("AB"), CX_STR("*"));
658 char const *expeced1_a = "test * ab TEST xyz";
659
660 cxmutstr repl2_a = cx_strreplace_a(alloc, csstr, CX_STR("test"), CX_STR("TEST"));
661 char const *expected2_a = "TEST AB ab TEST xyz";
662
663 CX_TEST_ASSERT(repl.ptr != str.ptr);
664 ASSERT_ZERO_TERMINATED(repl);
665 CX_TEST_ASSERT(0 == strcmp(repl.ptr, expected));
666 ASSERT_ZERO_TERMINATED(repln);
667 CX_TEST_ASSERT(0 == strcmp(repln.ptr, expectedn));
668 ASSERT_ZERO_TERMINATED(longrepl);
669 CX_TEST_ASSERT(0 == strcmp(longrepl.ptr, longexpect));
670 ASSERT_ZERO_TERMINATED(replnotrail);
671 CX_TEST_ASSERT(0 == strcmp(replnotrail.ptr, notrailexpect));
672 ASSERT_ZERO_TERMINATED(repleq);
673 CX_TEST_ASSERT(0 == strcmp(repleq.ptr, eqexpect));
674 ASSERT_ZERO_TERMINATED(replempty1);
675 CX_TEST_ASSERT(0 == strcmp(replempty1.ptr, ""));
676 ASSERT_ZERO_TERMINATED(replempty2);
677 CX_TEST_ASSERT(0 == strcmp(replempty2.ptr, emptyexpect2));
678 ASSERT_ZERO_TERMINATED(replpre);
679 CX_TEST_ASSERT(0 == strcmp(replpre.ptr, preexpected));
680 ASSERT_ZERO_TERMINATED(replan1);
681 CX_TEST_ASSERT(0 == strcmp(replan1.ptr, an1expected));
682 ASSERT_ZERO_TERMINATED(replan4);
683 CX_TEST_ASSERT(0 == strcmp(replan4.ptr, an4expected));
684 ASSERT_ZERO_TERMINATED(replan9);
685 CX_TEST_ASSERT(0 == strcmp(replan9.ptr, an9expected));
686 ASSERT_ZERO_TERMINATED(replan10);
687 CX_TEST_ASSERT(0 == strcmp(replan10.ptr, an10expected));
688 ASSERT_ZERO_TERMINATED(repl1_a);
689 CX_TEST_ASSERT(0 == strcmp(repl1_a.ptr, expeced1_a));
690 ASSERT_ZERO_TERMINATED(repl2_a);
691 CX_TEST_ASSERT(0 == strcmp(repl2_a.ptr, expected2_a));
692
693 cx_strfree_a(alloc, &repl1_a);
694 cx_strfree_a(alloc, &repl2_a);
695 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
696 }
697
698 cx_strfree(&repl);
699 cx_strfree(&repln);
700 cx_strfree(&longrepl);
701 cx_strfree(&replnotrail);
702 cx_strfree(&repleq);
703 cx_strfree(&replempty1);
704 cx_strfree(&replempty2);
705 cx_strfree(&replpre);
706 cx_strfree(&replan1);
707 cx_strfree(&replan4);
708 cx_strfree(&replan9);
709 cx_strfree(&replan10);
710 cx_testing_allocator_destroy(&talloc);
711 }
712
713 CX_TEST(test_strupper) {
714 cxmutstr str = cx_strdup(CX_STR("thIs 1s @ Te$t"));
715 CX_TEST_DO {
716 cx_strupper(str);
717 CX_TEST_ASSERT(0 == strcmp(str.ptr, "THIS 1S @ TE$T"));
718 }
719 cx_strfree(&str);
720 }
721
722 CX_TEST(test_strlower) {
723 cxmutstr str = cx_strdup(CX_STR("thIs 1s @ Te$t"));
724 CX_TEST_DO {
725 cx_strlower(str);
726 CX_TEST_ASSERT(0 == strcmp(str.ptr, "this 1s @ te$t"));
727 }
728 cx_strfree(&str);
729 }
730
731 CX_TEST(test_strtok) {
732 cxstring str = CX_STR("a,comma,separated,string");
733 cxstring delim = CX_STR(",");
734 CX_TEST_DO {
735 CxStrtokCtx ctx = cx_strtok(str, delim, 3);
736 CX_TEST_ASSERT(ctx.str.ptr == str.ptr);
737 CX_TEST_ASSERT(ctx.str.length == str.length);
738 CX_TEST_ASSERT(ctx.delim.ptr == delim.ptr);
739 CX_TEST_ASSERT(ctx.delim.length == delim.length);
740 CX_TEST_ASSERT(ctx.limit == 3);
741 CX_TEST_ASSERT(ctx.found == 0);
742 CX_TEST_ASSERT(ctx.pos == 0);
743 CX_TEST_ASSERT(ctx.next_pos == 0);
744 CX_TEST_ASSERT(ctx.delim_more == NULL);
745 CX_TEST_ASSERT(ctx.delim_more_count == 0);
746 }
747 }
748
749 CX_TEST(test_strtok_m) {
750 cxmutstr str = cx_strdup(CX_STR("a,comma,separated,string"));
751 cxstring delim = CX_STR(",");
752 CX_TEST_DO {
753 CxStrtokCtx ctx = cx_strtok_m(str, delim, 3);
754 CX_TEST_ASSERT(ctx.str.ptr == str.ptr);
755 CX_TEST_ASSERT(ctx.str.length == str.length);
756 CX_TEST_ASSERT(ctx.delim.ptr == delim.ptr);
757 CX_TEST_ASSERT(ctx.delim.length == delim.length);
758 CX_TEST_ASSERT(ctx.limit == 3);
759 CX_TEST_ASSERT(ctx.found == 0);
760 CX_TEST_ASSERT(ctx.pos == 0);
761 CX_TEST_ASSERT(ctx.next_pos == 0);
762 CX_TEST_ASSERT(ctx.delim_more == NULL);
763 CX_TEST_ASSERT(ctx.delim_more_count == 0);
764 }
765 cx_strfree(&str);
766 }
767
768 CX_TEST(test_strtok_delim) {
769 cxstring str = CX_STR("an,arbitrarily|separated;string");
770 cxstring delim = CX_STR(",");
771 cxstring delim_more[2] = {CX_STR("|"), CX_STR(";")};
772 CX_TEST_DO {
773 CxStrtokCtx ctx = cx_strtok(str, delim, 3);
774 cx_strtok_delim(&ctx, delim_more, 2);
775 CX_TEST_ASSERT(ctx.str.ptr == str.ptr);
776 CX_TEST_ASSERT(ctx.str.length == str.length);
777 CX_TEST_ASSERT(ctx.delim.ptr == delim.ptr);
778 CX_TEST_ASSERT(ctx.delim.length == delim.length);
779 CX_TEST_ASSERT(ctx.limit == 3);
780 CX_TEST_ASSERT(ctx.found == 0);
781 CX_TEST_ASSERT(ctx.pos == 0);
782 CX_TEST_ASSERT(ctx.next_pos == 0);
783 CX_TEST_ASSERT(ctx.delim_more == delim_more);
784 CX_TEST_ASSERT(ctx.delim_more_count == 2);
785 }
786 }
787
788 CX_TEST(test_strtok_next_easy) {
789 cxstring str = CX_STR("a,comma,separated,string");
790 cxstring delim = CX_STR(",");
791 CX_TEST_DO {
792 CxStrtokCtx ctx = cx_strtok(str, delim, 3);
793 bool ret;
794 cxstring tok;
795
796 ret = cx_strtok_next(&ctx, &tok);
797 CX_TEST_ASSERT(ret);
798 CX_TEST_ASSERT(0 == cx_strcmp(tok, CX_STR("a")));
799 CX_TEST_ASSERT(ctx.pos == 0);
800 CX_TEST_ASSERT(ctx.next_pos == 2);
801 CX_TEST_ASSERT(ctx.delim_pos == 1);
802 CX_TEST_ASSERT(ctx.found == 1);
803
804 ret = cx_strtok_next(&ctx, &tok);
805 CX_TEST_ASSERT(ret);
806 CX_TEST_ASSERT(0 == cx_strcmp(tok, CX_STR("comma")));
807 CX_TEST_ASSERT(ctx.pos == 2);
808 CX_TEST_ASSERT(ctx.next_pos == 8);
809 CX_TEST_ASSERT(ctx.delim_pos == 7);
810 CX_TEST_ASSERT(ctx.found == 2);
811
812 ret = cx_strtok_next(&ctx, &tok);
813 CX_TEST_ASSERT(ret);
814 CX_TEST_ASSERT(0 == cx_strcmp(tok, CX_STR("separated")));
815 CX_TEST_ASSERT(ctx.pos == 8);
816 CX_TEST_ASSERT(ctx.next_pos == 18);
817 CX_TEST_ASSERT(ctx.delim_pos == 17);
818 CX_TEST_ASSERT(ctx.found == 3);
819
820 ret = cx_strtok_next(&ctx, &tok);
821 CX_TEST_ASSERT(!ret);
822 CX_TEST_ASSERT(ctx.pos == 8);
823 CX_TEST_ASSERT(ctx.next_pos == 18);
824 CX_TEST_ASSERT(ctx.delim_pos == 17);
825 CX_TEST_ASSERT(ctx.found == 3);
826 }
827 }
828
829 CX_TEST(test_strtok_next_unlimited) {
830 cxstring str = CX_STR("some;-;otherwise;-;separated;-;string;-;");
831 cxstring delim = CX_STR(";-;");
832 CX_TEST_DO {
833 CxStrtokCtx ctx = cx_strtok(str, delim, SIZE_MAX);
834 bool ret;
835 cxstring tok;
836
837 ret = cx_strtok_next(&ctx, &tok);
838 CX_TEST_ASSERT(ret);
839 CX_TEST_ASSERT(0 == cx_strcmp(tok, CX_STR("some")));
840 CX_TEST_ASSERT(ctx.pos == 0);
841 CX_TEST_ASSERT(ctx.next_pos == 7);
842 CX_TEST_ASSERT(ctx.delim_pos == 4);
843 CX_TEST_ASSERT(ctx.found == 1);
844
845 ret = cx_strtok_next(&ctx, &tok);
846 CX_TEST_ASSERT(ret);
847 CX_TEST_ASSERT(0 == cx_strcmp(tok, CX_STR("otherwise")));
848 CX_TEST_ASSERT(ctx.pos == 7);
849 CX_TEST_ASSERT(ctx.next_pos == 19);
850 CX_TEST_ASSERT(ctx.delim_pos == 16);
851 CX_TEST_ASSERT(ctx.found == 2);
852
853 ret = cx_strtok_next(&ctx, &tok);
854 CX_TEST_ASSERT(ret);
855 CX_TEST_ASSERT(0 == cx_strcmp(tok, CX_STR("separated")));
856 CX_TEST_ASSERT(ctx.pos == 19);
857 CX_TEST_ASSERT(ctx.next_pos == 31);
858 CX_TEST_ASSERT(ctx.delim_pos == 28);
859 CX_TEST_ASSERT(ctx.found == 3);
860
861 ret = cx_strtok_next(&ctx, &tok);
862 CX_TEST_ASSERT(ret);
863 CX_TEST_ASSERT(0 == cx_strcmp(tok, CX_STR("string")));
864 CX_TEST_ASSERT(ctx.pos == 31);
865 CX_TEST_ASSERT(ctx.next_pos == 40);
866 CX_TEST_ASSERT(ctx.delim_pos == 37);
867 CX_TEST_ASSERT(ctx.found == 4);
868
869 ret = cx_strtok_next(&ctx, &tok);
870 CX_TEST_ASSERT(ret);
871 CX_TEST_ASSERT(0 == cx_strcmp(tok, CX_STR("")));
872 CX_TEST_ASSERT(ctx.pos == 40);
873 CX_TEST_ASSERT(ctx.next_pos == 40);
874 CX_TEST_ASSERT(ctx.delim_pos == 40);
875 CX_TEST_ASSERT(ctx.found == 5);
876
877 ret = cx_strtok_next(&ctx, &tok);
878 CX_TEST_ASSERT(!ret);
879 CX_TEST_ASSERT(ctx.pos == 40);
880 CX_TEST_ASSERT(ctx.delim_pos == 40);
881 CX_TEST_ASSERT(ctx.found == 5);
882 }
883 }
884
885 CX_TEST(test_strtok_next_advanced) {
886 cxmutstr str = cx_strdup(CX_STR("an,arbitrarily;||separated;string"));
887 cxstring delim = CX_STR(",");
888 cxstring delim_more[2] = {CX_STR("||"), CX_STR(";")};
889 CX_TEST_DO {
890 CxStrtokCtx ctx = cx_strtok_m(str, delim, 10);
891 cx_strtok_delim(&ctx, delim_more, 2);
892 bool ret;
893 cxmutstr tok;
894
895 ret = cx_strtok_next_m(&ctx, &tok);
896 CX_TEST_ASSERT(ret);
897 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(tok), CX_STR("an")));
898 CX_TEST_ASSERT(ctx.pos == 0);
899 CX_TEST_ASSERT(ctx.next_pos == 3);
900 CX_TEST_ASSERT(ctx.delim_pos == 2);
901 CX_TEST_ASSERT(ctx.found == 1);
902 cx_strupper(tok);
903
904 ret = cx_strtok_next_m(&ctx, &tok);
905 CX_TEST_ASSERT(ret);
906 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(tok), CX_STR("arbitrarily")));
907 CX_TEST_ASSERT(ctx.pos == 3);
908 CX_TEST_ASSERT(ctx.next_pos == 15);
909 CX_TEST_ASSERT(ctx.delim_pos == 14);
910 CX_TEST_ASSERT(ctx.found == 2);
911 cx_strupper(tok);
912
913 ret = cx_strtok_next_m(&ctx, &tok);
914 CX_TEST_ASSERT(ret);
915 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(tok), CX_STR("")));
916 CX_TEST_ASSERT(ctx.pos == 15);
917 CX_TEST_ASSERT(ctx.next_pos == 17);
918 CX_TEST_ASSERT(ctx.delim_pos == 15);
919 CX_TEST_ASSERT(ctx.found == 3);
920 cx_strupper(tok);
921
922 ret = cx_strtok_next_m(&ctx, &tok);
923 CX_TEST_ASSERT(ret);
924 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(tok), CX_STR("separated")));
925 CX_TEST_ASSERT(ctx.pos == 17);
926 CX_TEST_ASSERT(ctx.next_pos == 27);
927 CX_TEST_ASSERT(ctx.delim_pos == 26);
928 CX_TEST_ASSERT(ctx.found == 4);
929 cx_strupper(tok);
930
931 ret = cx_strtok_next_m(&ctx, &tok);
932 CX_TEST_ASSERT(ret);
933 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(tok), CX_STR("string")));
934 CX_TEST_ASSERT(ctx.pos == 27);
935 CX_TEST_ASSERT(ctx.next_pos == 33);
936 CX_TEST_ASSERT(ctx.delim_pos == 33);
937 CX_TEST_ASSERT(ctx.found == 5);
938 cx_strupper(tok);
939
940 ret = cx_strtok_next_m(&ctx, &tok);
941 CX_TEST_ASSERT(!ret);
942 CX_TEST_ASSERT(ctx.pos == 27);
943 CX_TEST_ASSERT(ctx.next_pos == 33);
944 CX_TEST_ASSERT(ctx.delim_pos == 33);
945 CX_TEST_ASSERT(ctx.found == 5);
946
947 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(str), CX_STR("AN,ARBITRARILY;||SEPARATED;STRING")));
948 }
949 cx_strfree(&str);
950 }
951
952
953 CxTestSuite *cx_test_suite_string(void) {
954 CxTestSuite *suite = cx_test_suite_new("string");
955
956 cx_test_register(suite, test_string_construct);
957 cx_test_register(suite, test_strfree);
958 cx_test_register(suite, test_strdup);
959 cx_test_register(suite, test_strdup_shortened);
960 cx_test_register(suite, test_strlen);
961 cx_test_register(suite, test_strsubs);
962 cx_test_register(suite, test_strchr);
963 cx_test_register(suite, test_strrchr);
964 cx_test_register(suite, test_strstr);
965 cx_test_register(suite, test_strcmp);
966 cx_test_register(suite, test_strcasecmp);
967 cx_test_register(suite, test_strcat);
968 cx_test_register(suite, test_strsplit);
969 cx_test_register(suite, test_strsplit_a);
970 cx_test_register(suite, test_strtrim);
971 cx_test_register(suite, test_strprefix);
972 cx_test_register(suite, test_strsuffix);
973 cx_test_register(suite, test_strcaseprefix);
974 cx_test_register(suite, test_strcasesuffix);
975 cx_test_register(suite, test_strreplace);
976 cx_test_register(suite, test_strupper);
977 cx_test_register(suite, test_strlower);
978 cx_test_register(suite, test_strtok);
979 cx_test_register(suite, test_strtok_m);
980 cx_test_register(suite, test_strtok_delim);
981 cx_test_register(suite, test_strtok_next_easy);
982 cx_test_register(suite, test_strtok_next_unlimited);
983 cx_test_register(suite, test_strtok_next_advanced);
984
985 return suite;
986 }

mercurial