| 374 CxTestingAllocator talloc; |
374 CxTestingAllocator talloc; |
| 375 cx_testing_allocator_init(&talloc); |
375 cx_testing_allocator_init(&talloc); |
| 376 CxAllocator *alloc = &talloc.base; |
376 CxAllocator *alloc = &talloc.base; |
| 377 |
377 |
| 378 CX_TEST_DO { |
378 CX_TEST_DO { |
| 379 cxmutstr t1 = cx_strcat_a(alloc, 2, s1, s2); |
379 cxmutstr t1 = {0}; |
| |
380 CX_TEST_ASSERT(0 == cx_strcat_a(alloc, &t1, 2, s1, s2)); |
| 380 CX_TEST_ASSERT(0 == cx_strcmp(t1, "1234")); |
381 CX_TEST_ASSERT(0 == cx_strcmp(t1, "1234")); |
| 381 ASSERT_ZERO_TERMINATED(t1); |
382 ASSERT_ZERO_TERMINATED(t1); |
| 382 cx_strfree_a(alloc, &t1); |
383 cx_strfree_a(alloc, &t1); |
| 383 |
384 |
| 384 cxmutstr t2 = cx_strcat_a(alloc, 3, s1, s2, s3); |
385 cxmutstr t2 = {0}; |
| |
386 CX_TEST_ASSERT(0 == cx_strcat_a(alloc, &t2, 3, s1, s2, s3)); |
| 385 CX_TEST_ASSERT(0 == cx_strcmp(t2, "123456")); |
387 CX_TEST_ASSERT(0 == cx_strcmp(t2, "123456")); |
| 386 ASSERT_ZERO_TERMINATED(t2); |
388 ASSERT_ZERO_TERMINATED(t2); |
| 387 cx_strfree_a(alloc, &t2); |
389 cx_strfree_a(alloc, &t2); |
| 388 |
390 |
| 389 cxmutstr t3 = cx_strcat_a(alloc, 6, s1, sn, s2, sn, s3, sn); |
391 cxmutstr t3 = {0}; |
| |
392 CX_TEST_ASSERT(0 == cx_strcat_a(alloc, &t3, 6, s1, sn, s2, sn, s3, sn)); |
| 390 CX_TEST_ASSERT(0 == cx_strcmp(t3, "123456")); |
393 CX_TEST_ASSERT(0 == cx_strcmp(t3, "123456")); |
| 391 ASSERT_ZERO_TERMINATED(t3); |
394 ASSERT_ZERO_TERMINATED(t3); |
| 392 cx_strfree_a(alloc, &t3); |
395 cx_strfree_a(alloc, &t3); |
| 393 |
396 |
| 394 cxmutstr t4 = cx_strcat_a(alloc, 2, sn, sn); |
397 cxmutstr t4 = {0}; |
| |
398 CX_TEST_ASSERT(0 == cx_strcat_a(alloc, &t4, 2, sn, sn)); |
| 395 CX_TEST_ASSERT(0 == cx_strcmp(t4, "")); |
399 CX_TEST_ASSERT(0 == cx_strcmp(t4, "")); |
| 396 ASSERT_ZERO_TERMINATED(t4); |
400 ASSERT_ZERO_TERMINATED(t4); |
| 397 cx_strfree_a(alloc, &t4); |
401 cx_strfree_a(alloc, &t4); |
| 398 |
402 |
| 399 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
403 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
| 400 |
404 |
| 401 // use the macro |
405 // use the macro |
| 402 cxmutstr t5 = cx_strcat(3, s3, s1, s2); |
406 cxmutstr t5 = {0}; |
| |
407 CX_TEST_ASSERT(0 == cx_strcat(&t5, 3, s3, s1, s2)); |
| 403 CX_TEST_ASSERT(0 == cx_strcmp(t5, "561234")); |
408 CX_TEST_ASSERT(0 == cx_strcmp(t5, "561234")); |
| 404 ASSERT_ZERO_TERMINATED(t5); |
409 ASSERT_ZERO_TERMINATED(t5); |
| 405 cx_strfree(&t5); |
410 cx_strfree(&t5); |
| 406 |
411 |
| 407 // use an initial string |
412 // use an initial string |
| 408 cxmutstr t6 = cx_strdup(cx_str("Hello")); |
413 cxmutstr t6 = cx_strdup(cx_str("Hello")); |
| 409 t6 = cx_strcat_m(t6, 2, cx_str(", "), cx_str("World!")); |
414 CX_TEST_ASSERT(0 == cx_strcat(&t6, 2, cx_str(", "), cx_str("World!"))); |
| 410 CX_TEST_ASSERT(0 == cx_strcmp(t6, "Hello, World!")); |
415 CX_TEST_ASSERT(0 == cx_strcmp(t6, "Hello, World!")); |
| 411 ASSERT_ZERO_TERMINATED(t6); |
416 ASSERT_ZERO_TERMINATED(t6); |
| 412 cx_strfree(&t6); |
417 cx_strfree(&t6); |
| 413 |
418 |
| 414 // test overflow with fake strings |
419 // test overflow with fake strings |
| 415 char *fakestr = NULL; |
420 char *fakestr = NULL; |
| 416 cxstring a = cx_strn(fakestr, SIZE_MAX / 3 - 10); |
421 cxstring a = cx_strn(fakestr, SIZE_MAX / 3 - 10); |
| 417 cxstring b = cx_strn(fakestr, SIZE_MAX / 3 - 5); |
422 cxstring b = cx_strn(fakestr, SIZE_MAX / 3 - 5); |
| 418 cxstring c = cx_strn(fakestr, SIZE_MAX / 3 + 20); |
423 cxstring c = cx_strn(fakestr, SIZE_MAX / 3 + 20); |
| 419 errno = 0; |
424 errno = 0; |
| 420 cxmutstr z = cx_strcat(3, a, b, c); |
425 cxmutstr z = {0}; |
| |
426 CX_TEST_ASSERT(0 != cx_strcat(&z, 3, a, b, c)); |
| 421 CX_TEST_ASSERT(errno == EOVERFLOW); |
427 CX_TEST_ASSERT(errno == EOVERFLOW); |
| 422 CX_TEST_ASSERT(z.ptr == NULL); |
428 CX_TEST_ASSERT(z.ptr == NULL); |
| 423 CX_TEST_ASSERT(z.length == 0); |
429 CX_TEST_ASSERT(z.length == 0); |
| 424 } |
430 } |
| 425 cx_testing_allocator_destroy(&talloc); |
431 cx_testing_allocator_destroy(&talloc); |