tests/test_string.c

changeset 1582
32b82c424252
parent 1500
d20037235c9c
equal deleted inserted replaced
1581:814049fb62ee 1582:32b82c424252
37 37
38 #define ASSERT_ZERO_TERMINATED(str) CX_TEST_ASSERTM((str).ptr[(str).length] == '\0', \ 38 #define ASSERT_ZERO_TERMINATED(str) CX_TEST_ASSERTM((str).ptr[(str).length] == '\0', \
39 #str " is not zero terminated") 39 #str " is not zero terminated")
40 40
41 CX_TEST(test_string_construct) { 41 CX_TEST(test_string_construct) {
42 cxstring s1 = CX_STR("1234"); 42 cxstring s1 = cx_str("1234");
43 cxstring s2 = cx_strn("abcd", 2); 43 cxstring s2 = cx_strn("abcd", 2);
44 cxmutstr s3 = cx_mutstr((char *) "1234"); 44 cxmutstr s3 = cx_mutstr((char *) "1234");
45 cxmutstr s4 = cx_mutstrn((char *) "abcd", 2); 45 cxmutstr s4 = cx_mutstrn((char *) "abcd", 2);
46 cxstring snull = cx_str(NULL); 46 cxstring snull = cx_str(NULL);
47 cxmutstr mnull = cx_mutstr(NULL); 47 cxmutstr mnull = cx_mutstr(NULL);
103 } 103 }
104 cx_testing_allocator_destroy(&talloc); 104 cx_testing_allocator_destroy(&talloc);
105 } 105 }
106 106
107 CX_TEST(test_strdup) { 107 CX_TEST(test_strdup) {
108 cxstring str = CX_STR("test"); 108 cxstring str = cx_str("test");
109 cxmutstr dup = cx_strdup(str); 109 cxmutstr dup = cx_strdup(str);
110 CX_TEST_DO { 110 CX_TEST_DO {
111 CX_TEST_ASSERT(dup.length == str.length); 111 CX_TEST_ASSERT(dup.length == str.length);
112 CX_TEST_ASSERT(0 == strcmp(dup.ptr, str.ptr)); 112 CX_TEST_ASSERT(0 == strcmp(dup.ptr, str.ptr));
113 ASSERT_ZERO_TERMINATED(dup); 113 ASSERT_ZERO_TERMINATED(dup);
114 } 114 }
115 cx_strfree(&dup); 115 cx_strfree(&dup);
116 } 116 }
117 117
118 CX_TEST(test_strdup_shortened) { 118 CX_TEST(test_strdup_shortened) {
119 cxstring str = CX_STR("test"); 119 cxstring str = cx_str("test");
120 str.length = 2; 120 str.length = 2;
121 cxmutstr dup = cx_strdup(str); 121 cxmutstr dup = cx_strdup(str);
122 CX_TEST_DO { 122 CX_TEST_DO {
123 CX_TEST_ASSERT(dup.length == str.length); 123 CX_TEST_ASSERT(dup.length == str.length);
124 CX_TEST_ASSERT(0 == strcmp(dup.ptr, "te")); 124 CX_TEST_ASSERT(0 == strcmp(dup.ptr, "te"));
129 129
130 CX_TEST(test_strcpy) { 130 CX_TEST(test_strcpy) {
131 CxTestingAllocator talloc; 131 CxTestingAllocator talloc;
132 cx_testing_allocator_init(&talloc); 132 cx_testing_allocator_init(&talloc);
133 const CxAllocator *alloc = &talloc.base; 133 const CxAllocator *alloc = &talloc.base;
134 cxstring str = CX_STR("test string"); 134 cxstring str = cx_str("test string");
135 str.length = 8; // test with a non-zero-terminated source 135 str.length = 8; // test with a non-zero-terminated source
136 cxmutstr dup; 136 cxmutstr dup;
137 CX_TEST_DO { 137 CX_TEST_DO {
138 // copy into a smaller string 138 // copy into a smaller string
139 dup = cx_strdup_a(alloc, CX_STR("hello")); 139 dup = cx_strdup_a(alloc, "hello");
140 CX_TEST_ASSERT(0 == cx_strcpy_a(alloc, &dup, str)); 140 CX_TEST_ASSERT(0 == cx_strcpy_a(alloc, &dup, str));
141 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(dup), CX_STR("test str"))); 141 CX_TEST_ASSERT(0 == cx_strcmp(dup, "test str"));
142 ASSERT_ZERO_TERMINATED(dup); 142 ASSERT_ZERO_TERMINATED(dup);
143 cx_strfree_a(alloc, &dup); 143 cx_strfree_a(alloc, &dup);
144 144
145 // copy into a larger string 145 // copy into a larger string
146 dup = cx_strdup_a(alloc, CX_STR("hello, world!")); 146 dup = cx_strdup_a(alloc, "hello, world!");
147 CX_TEST_ASSERT(0 == cx_strcpy_a(alloc, &dup, str)); 147 CX_TEST_ASSERT(0 == cx_strcpy_a(alloc, &dup, str));
148 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(dup), CX_STR("test str"))); 148 CX_TEST_ASSERT(0 == cx_strcmp(dup, "test str"));
149 ASSERT_ZERO_TERMINATED(dup); 149 ASSERT_ZERO_TERMINATED(dup);
150 cx_strfree_a(alloc, &dup); 150 cx_strfree_a(alloc, &dup);
151 151
152 // copy into an equal-length string 152 // copy into an equal-length string
153 dup = cx_strdup_a(alloc, CX_STR("testing!")); 153 dup = cx_strdup_a(alloc, "testing!");
154 CX_TEST_ASSERT(0 == cx_strcpy_a(alloc, &dup, str)); 154 CX_TEST_ASSERT(0 == cx_strcpy_a(alloc, &dup, str));
155 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(dup), CX_STR("test str"))); 155 CX_TEST_ASSERT(0 == cx_strcmp(dup, "test str"));
156 ASSERT_ZERO_TERMINATED(dup); 156 ASSERT_ZERO_TERMINATED(dup);
157 cx_strfree_a(alloc, &dup); 157 cx_strfree_a(alloc, &dup);
158 158
159 // copy into a NULL-string 159 // copy into a NULL-string
160 dup.ptr = NULL; 160 dup.ptr = NULL;
161 CX_TEST_ASSERT(0 == cx_strcpy_a(alloc, &dup, str)); 161 CX_TEST_ASSERT(0 == cx_strcpy_a(alloc, &dup, str));
162 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(dup), CX_STR("test str"))); 162 CX_TEST_ASSERT(0 == cx_strcmp(dup, "test str"));
163 ASSERT_ZERO_TERMINATED(dup); 163 ASSERT_ZERO_TERMINATED(dup);
164 cx_strfree_a(alloc, &dup); 164 cx_strfree_a(alloc, &dup);
165 } 165 }
166 cx_testing_allocator_destroy(&talloc); 166 cx_testing_allocator_destroy(&talloc);
167 } 167 }
168 168
169 CX_TEST(test_strlen) { 169 CX_TEST(test_strlen) {
170 cxstring s1 = CX_STR("1234"); 170 cxstring s1 = cx_str("1234");
171 cxstring s2 = CX_STR(".:.:."); 171 cxstring s2 = cx_str(".:.:.");
172 cxstring s3 = CX_STR("X"); 172 cxstring s3 = cx_str("X");
173 CX_TEST_DO { 173 CX_TEST_DO {
174 size_t len0 = cx_strlen(0); 174 size_t len0 = cx_strlen(0);
175 size_t len1 = cx_strlen(1, s1); 175 size_t len1 = cx_strlen(1, s1);
176 size_t len2 = cx_strlen(2, s1, s2); 176 size_t len2 = cx_strlen(2, s1, s2);
177 size_t len3 = cx_strlen(3, s1, s2, s3); 177 size_t len3 = cx_strlen(3, s1, s2, s3);
182 CX_TEST_ASSERT(len3 == 10); 182 CX_TEST_ASSERT(len3 == 10);
183 } 183 }
184 } 184 }
185 185
186 CX_TEST(test_strsubs) { 186 CX_TEST(test_strsubs) {
187 cxstring str = CX_STR("A test string"); 187 cxstring str = cx_str("A test string");
188 188
189 CX_TEST_DO { 189 CX_TEST_DO {
190 cxstring sub = cx_strsubs(str, 0); 190 cxstring sub = cx_strsubs(str, 0);
191 CX_TEST_ASSERT(0 == cx_strcmp(sub, str)); 191 CX_TEST_ASSERT(0 == cx_strcmp(sub, str));
192 192
208 sub = cx_strsubsl(str, 7, 20); 208 sub = cx_strsubsl(str, 7, 20);
209 CX_TEST_ASSERT(0 == cx_strcmp(sub, cx_str("string"))); 209 CX_TEST_ASSERT(0 == cx_strcmp(sub, cx_str("string")));
210 210
211 // just for coverage, call the _m variant 211 // just for coverage, call the _m variant
212 cxmutstr m = cx_strsubs_m(cx_mutstrn(NULL, 0), 0); 212 cxmutstr m = cx_strsubs_m(cx_mutstrn(NULL, 0), 0);
213 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(m), cx_str(""))); 213 CX_TEST_ASSERT(0 == cx_strcmp(m, ""));
214 } 214 }
215 } 215 }
216 216
217 CX_TEST(test_strchr) { 217 CX_TEST(test_strchr) {
218 cxstring str = CX_STR("I will find you - and I will kill you"); 218 cxstring str = cx_str("I will find you - and I will kill you");
219 219
220 CX_TEST_DO { 220 CX_TEST_DO {
221 cxstring notfound = cx_strchr(str, 'x'); 221 cxstring notfound = cx_strchr(str, 'x');
222 CX_TEST_ASSERT(notfound.length == 0); 222 CX_TEST_ASSERT(notfound.length == 0);
223 223
225 CX_TEST_ASSERT(result.length == 35); 225 CX_TEST_ASSERT(result.length == 35);
226 CX_TEST_ASSERT(0 == strcmp(result.ptr, "will find you - and I will kill you")); 226 CX_TEST_ASSERT(0 == strcmp(result.ptr, "will find you - and I will kill you"));
227 227
228 // just for coverage, call the _m variant 228 // just for coverage, call the _m variant
229 cxmutstr m = cx_strchr_m(cx_mutstrn(NULL, 0), 'a'); 229 cxmutstr m = cx_strchr_m(cx_mutstrn(NULL, 0), 'a');
230 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(m), cx_str(""))); 230 CX_TEST_ASSERT(0 == cx_strcmp(m, ""));
231 } 231 }
232 } 232 }
233 233
234 CX_TEST(test_strrchr) { 234 CX_TEST(test_strrchr) {
235 cxstring str = CX_STR("X will find you - and I will kill you"); 235 cxstring str = cx_str("X will find you - and I will kill you");
236 236
237 CX_TEST_DO { 237 CX_TEST_DO {
238 cxstring notfound = cx_strrchr(str, 'x'); 238 cxstring notfound = cx_strrchr(str, 'x');
239 CX_TEST_ASSERT(notfound.length == 0); 239 CX_TEST_ASSERT(notfound.length == 0);
240 240
249 result = cx_strrchr(str, 'X'); 249 result = cx_strrchr(str, 'X');
250 CX_TEST_ASSERT(0 == cx_strcmp(result, str)); 250 CX_TEST_ASSERT(0 == cx_strcmp(result, str));
251 251
252 // just for coverage, call the _m variant 252 // just for coverage, call the _m variant
253 cxmutstr m = cx_strrchr_m(cx_mutstrn(NULL, 0), 'a'); 253 cxmutstr m = cx_strrchr_m(cx_mutstrn(NULL, 0), 'a');
254 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(m), cx_str(""))); 254 CX_TEST_ASSERT(0 == cx_strcmp(m, ""));
255 } 255 }
256 } 256 }
257 257
258 CX_TEST(test_strstr) { 258 CX_TEST(test_strstr) {
259 cxstring str = CX_STR("find the match in this string"); 259 cxstring str = cx_str("find the match in this string");
260 260
261 const size_t longstrpatternlen = 64 + cx_strstr_sbo_size; 261 const size_t longstrpatternlen = 64 + cx_strstr_sbo_size;
262 const size_t longstrlen = 320 + longstrpatternlen + 14; 262 const size_t longstrlen = 320 + longstrpatternlen + 14;
263 263
264 // it is more expensive to use calloc here, because we will overwrite 264 // it is more expensive to use calloc here, because we will overwrite
312 free(longstrc); 312 free(longstrc);
313 free(longstrpatternc); 313 free(longstrpatternc);
314 } 314 }
315 315
316 CX_TEST(test_strcmp) { 316 CX_TEST(test_strcmp) {
317 cxstring str = CX_STR("compare this"); 317 cxstring str = cx_str("compare this");
318 CX_TEST_DO { 318 CX_TEST_DO {
319 CX_TEST_ASSERT(0 == cx_strcmp(cx_str(""), cx_str(""))); 319 CX_TEST_ASSERT(0 == cx_strcmp(cx_str(""), cx_str("")));
320 CX_TEST_ASSERT(0 < cx_strcmp(str, cx_str(""))); 320 CX_TEST_ASSERT(0 < cx_strcmp(str, cx_str("")));
321 CX_TEST_ASSERT(0 == cx_strcmp(str, cx_str("compare this"))); 321 CX_TEST_ASSERT(0 == cx_strcmp(str, cx_str("compare this")));
322 CX_TEST_ASSERT(0 != cx_strcmp(str, cx_str("Compare This"))); 322 CX_TEST_ASSERT(0 != cx_strcmp(str, cx_str("Compare This")));
327 CX_TEST_ASSERT(0 > cx_strcmp(str, cx_str("lex"))); 327 CX_TEST_ASSERT(0 > cx_strcmp(str, cx_str("lex")));
328 CX_TEST_ASSERT(0 < cx_strcmp(str, cx_str("another lex test"))); 328 CX_TEST_ASSERT(0 < cx_strcmp(str, cx_str("another lex test")));
329 CX_TEST_ASSERT(0 < cx_strcmp(str, cx_str("Lex"))); 329 CX_TEST_ASSERT(0 < cx_strcmp(str, cx_str("Lex")));
330 CX_TEST_ASSERT(0 < cx_strcmp(str, cx_str("Another lex test"))); 330 CX_TEST_ASSERT(0 < cx_strcmp(str, cx_str("Another lex test")));
331 331
332 cxstring str2 = CX_STR("Compare This"); 332 cxstring str2 = cx_str("Compare This");
333 CX_TEST_ASSERT(0 != cx_strcmp_p(&str, &str2)); 333 CX_TEST_ASSERT(0 != cx_strcmp_p(&str, &str2));
334 str2 = CX_STR("compare this"); 334 str2 = cx_str("compare this");
335 CX_TEST_ASSERT(0 == cx_strcmp_p(&str, &str2)); 335 CX_TEST_ASSERT(0 == cx_strcmp_p(&str, &str2));
336 } 336 }
337 } 337 }
338 338
339 CX_TEST(test_strcasecmp) { 339 CX_TEST(test_strcasecmp) {
340 cxstring str = CX_STR("compare this"); 340 cxstring str = cx_str("compare this");
341 CX_TEST_DO { 341 CX_TEST_DO {
342 CX_TEST_ASSERT(0 == cx_strcasecmp(cx_str(""), cx_str(""))); 342 CX_TEST_ASSERT(0 == cx_strcasecmp(cx_str(""), cx_str("")));
343 CX_TEST_ASSERT(0 < cx_strcasecmp(str, cx_str(""))); 343 CX_TEST_ASSERT(0 < cx_strcasecmp(str, cx_str("")));
344 CX_TEST_ASSERT(0 == cx_strcasecmp(str, cx_str("compare this"))); 344 CX_TEST_ASSERT(0 == cx_strcasecmp(str, cx_str("compare this")));
345 CX_TEST_ASSERT(0 == cx_strcasecmp(str, cx_str("Compare This"))); 345 CX_TEST_ASSERT(0 == cx_strcasecmp(str, cx_str("Compare This")));
350 CX_TEST_ASSERT(0 > cx_strcasecmp(str, cx_str("lex"))); 350 CX_TEST_ASSERT(0 > cx_strcasecmp(str, cx_str("lex")));
351 CX_TEST_ASSERT(0 < cx_strcasecmp(str, cx_str("another lex test"))); 351 CX_TEST_ASSERT(0 < cx_strcasecmp(str, cx_str("another lex test")));
352 CX_TEST_ASSERT(0 > cx_strcasecmp(str, cx_str("Lex"))); 352 CX_TEST_ASSERT(0 > cx_strcasecmp(str, cx_str("Lex")));
353 CX_TEST_ASSERT(0 < cx_strcasecmp(str, cx_str("Another lex test"))); 353 CX_TEST_ASSERT(0 < cx_strcasecmp(str, cx_str("Another lex test")));
354 354
355 cxstring str2 = CX_STR("Compare This"); 355 cxstring str2 = cx_str("Compare This");
356 CX_TEST_ASSERT(0 == cx_strcasecmp_p(&str, &str2)); 356 CX_TEST_ASSERT(0 == cx_strcasecmp_p(&str, &str2));
357 str2 = CX_STR("Compare Tool"); 357 str2 = cx_str("Compare Tool");
358 CX_TEST_ASSERT(0 > cx_strcasecmp_p(&str, &str2)); 358 CX_TEST_ASSERT(0 > cx_strcasecmp_p(&str, &str2));
359 } 359 }
360 } 360 }
361 361
362 CX_TEST(test_strcat) { 362 CX_TEST(test_strcat) {
363 cxstring s1 = CX_STR("12"); 363 cxstring s1 = cx_str("12");
364 cxstring s2 = CX_STR("34"); 364 cxstring s2 = cx_str("34");
365 cxstring s3 = CX_STR("56"); 365 cxstring s3 = cx_str("56");
366 cxstring sn = {NULL, 0}; 366 cxstring sn = {NULL, 0};
367 367
368 CxTestingAllocator talloc; 368 CxTestingAllocator talloc;
369 cx_testing_allocator_init(&talloc); 369 cx_testing_allocator_init(&talloc);
370 CxAllocator *alloc = &talloc.base; 370 CxAllocator *alloc = &talloc.base;
371 371
372 CX_TEST_DO { 372 CX_TEST_DO {
373 cxmutstr t1 = cx_strcat_a(alloc, 2, s1, s2); 373 cxmutstr t1 = cx_strcat_a(alloc, 2, s1, s2);
374 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(t1), cx_str("1234"))); 374 CX_TEST_ASSERT(0 == cx_strcmp(t1, "1234"));
375 ASSERT_ZERO_TERMINATED(t1); 375 ASSERT_ZERO_TERMINATED(t1);
376 cx_strfree_a(alloc, &t1); 376 cx_strfree_a(alloc, &t1);
377 377
378 cxmutstr t2 = cx_strcat_a(alloc, 3, s1, s2, s3); 378 cxmutstr t2 = cx_strcat_a(alloc, 3, s1, s2, s3);
379 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(t2), cx_str("123456"))); 379 CX_TEST_ASSERT(0 == cx_strcmp(t2, "123456"));
380 ASSERT_ZERO_TERMINATED(t2); 380 ASSERT_ZERO_TERMINATED(t2);
381 cx_strfree_a(alloc, &t2); 381 cx_strfree_a(alloc, &t2);
382 382
383 cxmutstr t3 = cx_strcat_a(alloc, 6, s1, sn, s2, sn, s3, sn); 383 cxmutstr t3 = cx_strcat_a(alloc, 6, s1, sn, s2, sn, s3, sn);
384 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(t3), cx_str("123456"))); 384 CX_TEST_ASSERT(0 == cx_strcmp(t3, "123456"));
385 ASSERT_ZERO_TERMINATED(t3); 385 ASSERT_ZERO_TERMINATED(t3);
386 cx_strfree_a(alloc, &t3); 386 cx_strfree_a(alloc, &t3);
387 387
388 cxmutstr t4 = cx_strcat_a(alloc, 2, sn, sn); 388 cxmutstr t4 = cx_strcat_a(alloc, 2, sn, sn);
389 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(t4), cx_str(""))); 389 CX_TEST_ASSERT(0 == cx_strcmp(t4, ""));
390 ASSERT_ZERO_TERMINATED(t4); 390 ASSERT_ZERO_TERMINATED(t4);
391 cx_strfree_a(alloc, &t4); 391 cx_strfree_a(alloc, &t4);
392 392
393 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 393 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
394 394
395 // use the macro 395 // use the macro
396 cxmutstr t5 = cx_strcat(3, s3, s1, s2); 396 cxmutstr t5 = cx_strcat(3, s3, s1, s2);
397 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(t5), cx_str("561234"))); 397 CX_TEST_ASSERT(0 == cx_strcmp(t5, "561234"));
398 ASSERT_ZERO_TERMINATED(t5); 398 ASSERT_ZERO_TERMINATED(t5);
399 cx_strfree(&t5); 399 cx_strfree(&t5);
400 400
401 // use an initial string 401 // use an initial string
402 cxmutstr t6 = cx_strdup(cx_str("Hello")); 402 cxmutstr t6 = cx_strdup(cx_str("Hello"));
403 t6 = cx_strcat_m(t6, 2, cx_str(", "), cx_str("World!")); 403 t6 = cx_strcat_m(t6, 2, cx_str(", "), cx_str("World!"));
404 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(t6), cx_str("Hello, World!"))); 404 CX_TEST_ASSERT(0 == cx_strcmp(t6, "Hello, World!"));
405 ASSERT_ZERO_TERMINATED(t6); 405 ASSERT_ZERO_TERMINATED(t6);
406 cx_strfree(&t6); 406 cx_strfree(&t6);
407 407
408 // test overflow with fake strings 408 // test overflow with fake strings
409 char *fakestr = NULL; 409 char *fakestr = NULL;
418 } 418 }
419 cx_testing_allocator_destroy(&talloc); 419 cx_testing_allocator_destroy(&talloc);
420 } 420 }
421 421
422 CX_TEST(test_strcat_more_than_eight) { 422 CX_TEST(test_strcat_more_than_eight) {
423 cxstring s1 = CX_STR("12"); 423 cxstring s1 = cx_str("12");
424 cxstring s2 = CX_STR("34"); 424 cxstring s2 = cx_str("34");
425 cxstring s3 = CX_STR("56"); 425 cxstring s3 = cx_str("56");
426 cxstring s4 = CX_STR("78"); 426 cxstring s4 = cx_str("78");
427 cxstring s5 = CX_STR("9a"); 427 cxstring s5 = cx_str("9a");
428 cxstring s6 = CX_STR("bc"); 428 cxstring s6 = cx_str("bc");
429 cxstring s7 = CX_STR("de"); 429 cxstring s7 = cx_str("de");
430 cxstring s8 = CX_STR("f0"); 430 cxstring s8 = cx_str("f0");
431 cxstring s9 = CX_STR("xy"); 431 cxstring s9 = cx_str("xy");
432 432
433 CX_TEST_DO { 433 CX_TEST_DO {
434 cxmutstr r = cx_strcat(9, s1, s2, s3, s4, s5, s6, s7, s8, s9); 434 cxmutstr r = cx_strcat(9, s1, s2, s3, s4, s5, s6, s7, s8, s9);
435 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(r), cx_str("123456789abcdef0xy"))); 435 CX_TEST_ASSERT(0 == cx_strcmp(r, "123456789abcdef0xy"));
436 ASSERT_ZERO_TERMINATED(r); 436 ASSERT_ZERO_TERMINATED(r);
437 cx_strfree(&r); 437 cx_strfree(&r);
438 } 438 }
439 } 439 }
440 440
441 CX_TEST(test_strsplit) { 441 CX_TEST(test_strsplit) {
442 cxstring test = CX_STR("this,is,a,csv,string"); 442 cxstring test = cx_str("this,is,a,csv,string");
443 size_t capa = 8; 443 size_t capa = 8;
444 cxstring list[8]; 444 cxstring list[8];
445 size_t n; 445 size_t n;
446 CX_TEST_DO { 446 CX_TEST_DO {
447 // special case: empty string 447 // special case: empty string
529 // call the _m variant just for coverage 529 // call the _m variant just for coverage
530 cxmutstr mtest = cx_strdup(test); 530 cxmutstr mtest = cx_strdup(test);
531 cxmutstr mlist[4]; 531 cxmutstr mlist[4];
532 n = cx_strsplit_m(mtest, cx_str("is,"), 4, mlist); 532 n = cx_strsplit_m(mtest, cx_str("is,"), 4, mlist);
533 CX_TEST_ASSERT(n == 3); 533 CX_TEST_ASSERT(n == 3);
534 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(mlist[0]), cx_str("th"))); 534 CX_TEST_ASSERT(0 == cx_strcmp(mlist[0], "th"));
535 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(mlist[1]), cx_str(""))); 535 CX_TEST_ASSERT(0 == cx_strcmp(mlist[1], ""));
536 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(mlist[2]), cx_str("a,csv,string"))); 536 CX_TEST_ASSERT(0 == cx_strcmp(mlist[2], "a,csv,string"));
537 cx_strfree(&mtest); 537 cx_strfree(&mtest);
538 } 538 }
539 } 539 }
540 540
541 CX_TEST(test_strsplit_a) { 541 CX_TEST(test_strsplit_a) {
542 CxTestingAllocator talloc; 542 CxTestingAllocator talloc;
543 cx_testing_allocator_init(&talloc); 543 cx_testing_allocator_init(&talloc);
544 CxAllocator *alloc = &talloc.base; 544 CxAllocator *alloc = &talloc.base;
545 545
546 cxstring test = CX_STR("this,is,a,csv,string"); 546 cxstring test = cx_str("this,is,a,csv,string");
547 size_t capa = 8; 547 size_t capa = 8;
548 cxstring *list; 548 cxstring *list;
549 size_t n; 549 size_t n;
550 CX_TEST_DO { 550 CX_TEST_DO {
551 // special case: empty string 551 // special case: empty string
645 // call the _m variant just for coverage 645 // call the _m variant just for coverage
646 cxmutstr mtest = cx_strdup(test); 646 cxmutstr mtest = cx_strdup(test);
647 cxmutstr *mlist; 647 cxmutstr *mlist;
648 n = cx_strsplit_ma(alloc, mtest, cx_str("is,"), 4, &mlist); 648 n = cx_strsplit_ma(alloc, mtest, cx_str("is,"), 4, &mlist);
649 CX_TEST_ASSERT(n == 3); 649 CX_TEST_ASSERT(n == 3);
650 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(mlist[0]), cx_str("th"))); 650 CX_TEST_ASSERT(0 == cx_strcmp(mlist[0], "th"));
651 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(mlist[1]), cx_str(""))); 651 CX_TEST_ASSERT(0 == cx_strcmp(mlist[1], ""));
652 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(mlist[2]), cx_str("a,csv,string"))); 652 CX_TEST_ASSERT(0 == cx_strcmp(mlist[2], "a,csv,string"));
653 cxFree(alloc, mlist); 653 cxFree(alloc, mlist);
654 cx_strfree(&mtest); 654 cx_strfree(&mtest);
655 655
656 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 656 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
657 } 657 }
674 CX_TEST_ASSERT(0 == cx_strcmp(t5, cx_str(""))); 674 CX_TEST_ASSERT(0 == cx_strcmp(t5, cx_str("")));
675 CX_TEST_ASSERT(0 == cx_strcmp(empty, cx_str(""))); 675 CX_TEST_ASSERT(0 == cx_strcmp(empty, cx_str("")));
676 676
677 // call the _m variant just for coverage 677 // call the _m variant just for coverage
678 cxmutstr m1 = cx_strtrim_m(cx_mutstr((char *) " ein test \t ")); 678 cxmutstr m1 = cx_strtrim_m(cx_mutstr((char *) " ein test \t "));
679 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(m1), cx_str("ein test"))); 679 CX_TEST_ASSERT(0 == cx_strcmp(m1, "ein test"));
680 } 680 }
681 } 681 }
682 682
683 CX_TEST(test_strprefix) { 683 CX_TEST(test_strprefix) {
684 cxstring str = CX_STR("test my prefix and my suffix"); 684 cxstring str = cx_str("test my prefix and my suffix");
685 cxstring empty = CX_STR(""); 685 cxstring empty = cx_str("");
686 CX_TEST_DO { 686 CX_TEST_DO {
687 CX_TEST_ASSERT(!cx_strprefix(empty, cx_str("pref"))); 687 CX_TEST_ASSERT(!cx_strprefix(empty, cx_str("pref")));
688 CX_TEST_ASSERT(cx_strprefix(str, empty)); 688 CX_TEST_ASSERT(cx_strprefix(str, empty));
689 CX_TEST_ASSERT(cx_strprefix(empty, empty)); 689 CX_TEST_ASSERT(cx_strprefix(empty, empty));
690 CX_TEST_ASSERT(cx_strprefix(str, cx_str("test "))); 690 CX_TEST_ASSERT(cx_strprefix(str, cx_str("test ")));
691 CX_TEST_ASSERT(!cx_strprefix(str, cx_str("8-) fsck "))); 691 CX_TEST_ASSERT(!cx_strprefix(str, cx_str("8-) fsck ")));
692 } 692 }
693 } 693 }
694 694
695 CX_TEST(test_strsuffix) { 695 CX_TEST(test_strsuffix) {
696 cxstring str = CX_STR("test my prefix and my suffix"); 696 cxstring str = cx_str("test my prefix and my suffix");
697 cxstring empty = CX_STR(""); 697 cxstring empty = cx_str("");
698 CX_TEST_DO { 698 CX_TEST_DO {
699 CX_TEST_ASSERT(!cx_strsuffix(empty, cx_str("suf"))); 699 CX_TEST_ASSERT(!cx_strsuffix(empty, cx_str("suf")));
700 CX_TEST_ASSERT(cx_strsuffix(str, empty)); 700 CX_TEST_ASSERT(cx_strsuffix(str, empty));
701 CX_TEST_ASSERT(cx_strsuffix(empty, empty)); 701 CX_TEST_ASSERT(cx_strsuffix(empty, empty));
702 CX_TEST_ASSERT(cx_strsuffix(str, cx_str("fix"))); 702 CX_TEST_ASSERT(cx_strsuffix(str, cx_str("fix")));
703 CX_TEST_ASSERT(!cx_strsuffix(str, cx_str("fox"))); 703 CX_TEST_ASSERT(!cx_strsuffix(str, cx_str("fox")));
704 } 704 }
705 } 705 }
706 706
707 CX_TEST(test_strcaseprefix) { 707 CX_TEST(test_strcaseprefix) {
708 cxstring str = CX_STR("test my prefix and my suffix"); 708 cxstring str = cx_str("test my prefix and my suffix");
709 cxstring empty = CX_STR(""); 709 cxstring empty = cx_str("");
710 CX_TEST_DO { 710 CX_TEST_DO {
711 CX_TEST_ASSERT(!cx_strcaseprefix(empty, cx_str("pREf"))); 711 CX_TEST_ASSERT(!cx_strcaseprefix(empty, cx_str("pREf")));
712 CX_TEST_ASSERT(cx_strcaseprefix(str, empty)); 712 CX_TEST_ASSERT(cx_strcaseprefix(str, empty));
713 CX_TEST_ASSERT(cx_strcaseprefix(empty, empty)); 713 CX_TEST_ASSERT(cx_strcaseprefix(empty, empty));
714 CX_TEST_ASSERT(cx_strcaseprefix(str, cx_str("TEST "))); 714 CX_TEST_ASSERT(cx_strcaseprefix(str, cx_str("TEST ")));
715 CX_TEST_ASSERT(!cx_strcaseprefix(str, cx_str("8-) fsck "))); 715 CX_TEST_ASSERT(!cx_strcaseprefix(str, cx_str("8-) fsck ")));
716 } 716 }
717 } 717 }
718 718
719 CX_TEST(test_strcasesuffix) { 719 CX_TEST(test_strcasesuffix) {
720 cxstring str = CX_STR("test my prefix and my suffix"); 720 cxstring str = cx_str("test my prefix and my suffix");
721 cxstring empty = CX_STR(""); 721 cxstring empty = cx_str("");
722 CX_TEST_DO { 722 CX_TEST_DO {
723 CX_TEST_ASSERT(!cx_strcasesuffix(empty, cx_str("sUf"))); 723 CX_TEST_ASSERT(!cx_strcasesuffix(empty, cx_str("sUf")));
724 CX_TEST_ASSERT(cx_strcasesuffix(str, empty)); 724 CX_TEST_ASSERT(cx_strcasesuffix(str, empty));
725 CX_TEST_ASSERT(cx_strcasesuffix(empty, empty)); 725 CX_TEST_ASSERT(cx_strcasesuffix(empty, empty));
726 CX_TEST_ASSERT(cx_strcasesuffix(str, cx_str("FIX"))); 726 CX_TEST_ASSERT(cx_strcasesuffix(str, cx_str("FIX")));
731 CX_TEST(test_strreplace) { 731 CX_TEST(test_strreplace) {
732 CxTestingAllocator talloc; 732 CxTestingAllocator talloc;
733 cx_testing_allocator_init(&talloc); 733 cx_testing_allocator_init(&talloc);
734 CxAllocator *alloc = &talloc.base; 734 CxAllocator *alloc = &talloc.base;
735 735
736 cxstring str = CX_STR("test ababab string aba"); 736 cxstring str = cx_str("test ababab string aba");
737 cxstring longstr = CX_STR( 737 cxstring longstr = cx_str(
738 "xyaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaacd"); 738 "xyaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaacd");
739 cxstring notrail = CX_STR("test abab"); 739 cxstring notrail = cx_str("test abab");
740 cxstring empty = CX_STR(""); 740 cxstring empty = cx_str("");
741 cxstring astr = CX_STR("aaaaaaaaaa"); 741 cxstring astr = cx_str("aaaaaaaaaa");
742 cxstring csstr = CX_STR("test AB ab TEST xyz"); 742 cxstring csstr = cx_str("test AB ab TEST xyz");
743 743
744 cxmutstr repl = cx_strreplace(str, cx_str("abab"), cx_str("muchlonger")); 744 cxmutstr repl = cx_strreplace(str, cx_str("abab"), cx_str("muchlonger"));
745 const char *expected = "test muchlongerab string aba"; 745 const char *expected = "test muchlongerab string aba";
746 746
747 cxmutstr repln = cx_strreplacen(str, cx_str("ab"), cx_str("c"), 2); 747 cxmutstr repln = cx_strreplacen(str, cx_str("ab"), cx_str("c"), 2);
837 cx_strfree(&norepl); 837 cx_strfree(&norepl);
838 cx_testing_allocator_destroy(&talloc); 838 cx_testing_allocator_destroy(&talloc);
839 } 839 }
840 840
841 CX_TEST(test_strtok) { 841 CX_TEST(test_strtok) {
842 cxstring str = CX_STR("a,comma,separated,string"); 842 cxstring str = cx_str("a,comma,separated,string");
843 cxstring delim = CX_STR(","); 843 cxstring delim = cx_str(",");
844 CX_TEST_DO { 844 CX_TEST_DO {
845 CxStrtokCtx ctx = cx_strtok(str, delim, 3); 845 CxStrtokCtx ctx = cx_strtok(str, delim, 3);
846 CX_TEST_ASSERT(ctx.str.ptr == str.ptr); 846 CX_TEST_ASSERT(ctx.str.ptr == str.ptr);
847 CX_TEST_ASSERT(ctx.str.length == str.length); 847 CX_TEST_ASSERT(ctx.str.length == str.length);
848 CX_TEST_ASSERT(ctx.delim.ptr == delim.ptr); 848 CX_TEST_ASSERT(ctx.delim.ptr == delim.ptr);
855 CX_TEST_ASSERT(ctx.delim_more_count == 0); 855 CX_TEST_ASSERT(ctx.delim_more_count == 0);
856 } 856 }
857 } 857 }
858 858
859 CX_TEST(test_strtok_delim) { 859 CX_TEST(test_strtok_delim) {
860 cxstring str = CX_STR("an,arbitrarily|separated;string"); 860 cxstring str = cx_str("an,arbitrarily|separated;string");
861 cxstring delim = CX_STR(","); 861 cxstring delim = cx_str(",");
862 cxstring delim_more[2] = {CX_STR("|"), CX_STR(";")}; 862 cxstring delim_more[2] = {cx_str("|"), cx_str(";")};
863 CX_TEST_DO { 863 CX_TEST_DO {
864 CxStrtokCtx ctx = cx_strtok(str, delim, 3); 864 CxStrtokCtx ctx = cx_strtok(str, delim, 3);
865 cx_strtok_delim(&ctx, delim_more, 2); 865 cx_strtok_delim(&ctx, delim_more, 2);
866 CX_TEST_ASSERT(ctx.str.ptr == str.ptr); 866 CX_TEST_ASSERT(ctx.str.ptr == str.ptr);
867 CX_TEST_ASSERT(ctx.str.length == str.length); 867 CX_TEST_ASSERT(ctx.str.length == str.length);
875 CX_TEST_ASSERT(ctx.delim_more_count == 2); 875 CX_TEST_ASSERT(ctx.delim_more_count == 2);
876 } 876 }
877 } 877 }
878 878
879 CX_TEST(test_strtok_next_easy) { 879 CX_TEST(test_strtok_next_easy) {
880 cxstring str = CX_STR("a,comma,separated,string"); 880 cxstring str = cx_str("a,comma,separated,string");
881 cxstring delim = CX_STR(","); 881 cxstring delim = cx_str(",");
882 CX_TEST_DO { 882 CX_TEST_DO {
883 CxStrtokCtx ctx = cx_strtok(str, delim, 3); 883 CxStrtokCtx ctx = cx_strtok(str, delim, 3);
884 bool ret; 884 bool ret;
885 cxstring tok; 885 cxstring tok;
886 886
916 CX_TEST_ASSERT(ctx.found == 3); 916 CX_TEST_ASSERT(ctx.found == 3);
917 } 917 }
918 } 918 }
919 919
920 CX_TEST(test_strtok_next_unlimited) { 920 CX_TEST(test_strtok_next_unlimited) {
921 cxstring str = CX_STR("some;-;otherwise;-;separated;-;string;-;"); 921 cxstring str = cx_str("some;-;otherwise;-;separated;-;string;-;");
922 cxstring delim = CX_STR(";-;"); 922 cxstring delim = cx_str(";-;");
923 CX_TEST_DO { 923 CX_TEST_DO {
924 CxStrtokCtx ctx = cx_strtok(str, delim, SIZE_MAX); 924 CxStrtokCtx ctx = cx_strtok(str, delim, SIZE_MAX);
925 bool ret; 925 bool ret;
926 cxstring tok; 926 cxstring tok;
927 927
981 } 981 }
982 } 982 }
983 983
984 CX_TEST(test_strtok_next_advanced) { 984 CX_TEST(test_strtok_next_advanced) {
985 cxmutstr str = cx_strdup(cx_str("an,arbitrarily;||separated;string")); 985 cxmutstr str = cx_strdup(cx_str("an,arbitrarily;||separated;string"));
986 cxstring delim = CX_STR(","); 986 cxstring delim = cx_str(",");
987 cxstring delim_more[2] = {CX_STR("||"), CX_STR(";")}; 987 cxstring delim_more[2] = {cx_str("||"), cx_str(";")};
988 CX_TEST_DO { 988 CX_TEST_DO {
989 CxStrtokCtx ctx = cx_strtok(str, delim, 10); 989 CxStrtokCtx ctx = cx_strtok(str, delim, 10);
990 cx_strtok_delim(&ctx, delim_more, 2); 990 cx_strtok_delim(&ctx, delim_more, 2);
991 bool ret; 991 bool ret;
992 cxmutstr tok; 992 cxmutstr tok;
993 993
994 ret = cx_strtok_next_m(&ctx, &tok); 994 ret = cx_strtok_next_m(&ctx, &tok);
995 CX_TEST_ASSERT(ret); 995 CX_TEST_ASSERT(ret);
996 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(tok), cx_str("an"))); 996 CX_TEST_ASSERT(0 == cx_strcmp(tok, "an"));
997 CX_TEST_ASSERT(ctx.pos == 0); 997 CX_TEST_ASSERT(ctx.pos == 0);
998 CX_TEST_ASSERT(ctx.next_pos == 3); 998 CX_TEST_ASSERT(ctx.next_pos == 3);
999 CX_TEST_ASSERT(ctx.delim_pos == 2); 999 CX_TEST_ASSERT(ctx.delim_pos == 2);
1000 CX_TEST_ASSERT(ctx.found == 1); 1000 CX_TEST_ASSERT(ctx.found == 1);
1001 test_toupper(tok); 1001 test_toupper(tok);
1002 1002
1003 ret = cx_strtok_next_m(&ctx, &tok); 1003 ret = cx_strtok_next_m(&ctx, &tok);
1004 CX_TEST_ASSERT(ret); 1004 CX_TEST_ASSERT(ret);
1005 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(tok), cx_str("arbitrarily"))); 1005 CX_TEST_ASSERT(0 == cx_strcmp(tok, "arbitrarily"));
1006 CX_TEST_ASSERT(ctx.pos == 3); 1006 CX_TEST_ASSERT(ctx.pos == 3);
1007 CX_TEST_ASSERT(ctx.next_pos == 15); 1007 CX_TEST_ASSERT(ctx.next_pos == 15);
1008 CX_TEST_ASSERT(ctx.delim_pos == 14); 1008 CX_TEST_ASSERT(ctx.delim_pos == 14);
1009 CX_TEST_ASSERT(ctx.found == 2); 1009 CX_TEST_ASSERT(ctx.found == 2);
1010 test_toupper(tok); 1010 test_toupper(tok);
1011 1011
1012 ret = cx_strtok_next_m(&ctx, &tok); 1012 ret = cx_strtok_next_m(&ctx, &tok);
1013 CX_TEST_ASSERT(ret); 1013 CX_TEST_ASSERT(ret);
1014 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(tok), cx_str(""))); 1014 CX_TEST_ASSERT(0 == cx_strcmp(tok, ""));
1015 CX_TEST_ASSERT(ctx.pos == 15); 1015 CX_TEST_ASSERT(ctx.pos == 15);
1016 CX_TEST_ASSERT(ctx.next_pos == 17); 1016 CX_TEST_ASSERT(ctx.next_pos == 17);
1017 CX_TEST_ASSERT(ctx.delim_pos == 15); 1017 CX_TEST_ASSERT(ctx.delim_pos == 15);
1018 CX_TEST_ASSERT(ctx.found == 3); 1018 CX_TEST_ASSERT(ctx.found == 3);
1019 test_toupper(tok); 1019 test_toupper(tok);
1020 1020
1021 ret = cx_strtok_next_m(&ctx, &tok); 1021 ret = cx_strtok_next_m(&ctx, &tok);
1022 CX_TEST_ASSERT(ret); 1022 CX_TEST_ASSERT(ret);
1023 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(tok), cx_str("separated"))); 1023 CX_TEST_ASSERT(0 == cx_strcmp(tok, "separated"));
1024 CX_TEST_ASSERT(ctx.pos == 17); 1024 CX_TEST_ASSERT(ctx.pos == 17);
1025 CX_TEST_ASSERT(ctx.next_pos == 27); 1025 CX_TEST_ASSERT(ctx.next_pos == 27);
1026 CX_TEST_ASSERT(ctx.delim_pos == 26); 1026 CX_TEST_ASSERT(ctx.delim_pos == 26);
1027 CX_TEST_ASSERT(ctx.found == 4); 1027 CX_TEST_ASSERT(ctx.found == 4);
1028 test_toupper(tok); 1028 test_toupper(tok);
1029 1029
1030 ret = cx_strtok_next_m(&ctx, &tok); 1030 ret = cx_strtok_next_m(&ctx, &tok);
1031 CX_TEST_ASSERT(ret); 1031 CX_TEST_ASSERT(ret);
1032 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(tok), cx_str("string"))); 1032 CX_TEST_ASSERT(0 == cx_strcmp(tok, "string"));
1033 CX_TEST_ASSERT(ctx.pos == 27); 1033 CX_TEST_ASSERT(ctx.pos == 27);
1034 CX_TEST_ASSERT(ctx.next_pos == 33); 1034 CX_TEST_ASSERT(ctx.next_pos == 33);
1035 CX_TEST_ASSERT(ctx.delim_pos == 33); 1035 CX_TEST_ASSERT(ctx.delim_pos == 33);
1036 CX_TEST_ASSERT(ctx.found == 5); 1036 CX_TEST_ASSERT(ctx.found == 5);
1037 test_toupper(tok); 1037 test_toupper(tok);
1041 CX_TEST_ASSERT(ctx.pos == 27); 1041 CX_TEST_ASSERT(ctx.pos == 27);
1042 CX_TEST_ASSERT(ctx.next_pos == 33); 1042 CX_TEST_ASSERT(ctx.next_pos == 33);
1043 CX_TEST_ASSERT(ctx.delim_pos == 33); 1043 CX_TEST_ASSERT(ctx.delim_pos == 33);
1044 CX_TEST_ASSERT(ctx.found == 5); 1044 CX_TEST_ASSERT(ctx.found == 5);
1045 1045
1046 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(str), cx_str("AN,ARBITRARILY;||SEPARATED;STRING"))); 1046 CX_TEST_ASSERT(0 == cx_strcmp(str, "AN,ARBITRARILY;||SEPARATED;STRING"));
1047 } 1047 }
1048 cx_strfree(&str); 1048 cx_strfree(&str);
1049 } 1049 }
1050 1050
1051 #define test_strtoint_impl(suffix, num, base, var, min, max) \ 1051 #define test_strtoint_impl(suffix, num, base, var, min, max) \
1481 CX_TEST_ASSERT(0 == cx_vcmp_double(d, 13.37)); 1481 CX_TEST_ASSERT(0 == cx_vcmp_double(d, 13.37));
1482 } 1482 }
1483 } 1483 }
1484 1484
1485 CX_TEST(test_strformat) { 1485 CX_TEST(test_strformat) {
1486 cxstring str = CX_STR("Hello, World!"); 1486 cxstring str = cx_str("Hello, World!");
1487 CX_TEST_DO { 1487 CX_TEST_DO {
1488 char actual[64]; 1488 char actual[64];
1489 snprintf(actual, 64, "Test %"CX_PRIstr " Success.", CX_SFMT(str)); 1489 snprintf(actual, 64, "Test %"CX_PRIstr " Success.", CX_SFMT(str));
1490 CX_TEST_ASSERT(0 == strncmp("Test Hello, World! Success.", actual, 64)); 1490 CX_TEST_ASSERT(0 == strncmp("Test Hello, World! Success.", actual, 64));
1491 } 1491 }

mercurial