tests/test_string.c

changeset 1667
608cc0b25352
parent 1652
db8299984bfe
child 1668
3ffdfe1776b4
equal deleted inserted replaced
1665:b79405fbf91d 1667:608cc0b25352
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);
435 cxstring s7 = cx_str("de"); 441 cxstring s7 = cx_str("de");
436 cxstring s8 = cx_str("f0"); 442 cxstring s8 = cx_str("f0");
437 cxstring s9 = cx_str("xy"); 443 cxstring s9 = cx_str("xy");
438 444
439 CX_TEST_DO { 445 CX_TEST_DO {
440 cxmutstr r = cx_strcat(9, s1, s2, s3, s4, s5, s6, s7, s8, s9); 446 cxmutstr r = {0};
447 CX_TEST_ASSERT(0 == cx_strcat(&r, 9, s1, s2, s3, s4, s5, s6, s7, s8, s9));
441 CX_TEST_ASSERT(0 == cx_strcmp(r, "123456789abcdef0xy")); 448 CX_TEST_ASSERT(0 == cx_strcmp(r, "123456789abcdef0xy"));
442 ASSERT_ZERO_TERMINATED(r); 449 ASSERT_ZERO_TERMINATED(r);
443 cx_strfree(&r); 450 cx_strfree(&r);
444 } 451 }
445 } 452 }

mercurial