1.1 --- a/tests/test_printf.c Tue Jan 16 23:12:43 2024 +0100 1.2 +++ b/tests/test_printf.c Tue Jan 16 23:13:01 2024 +0100 1.3 @@ -294,6 +294,107 @@ 1.4 free(expected); 1.5 } 1.6 1.7 +CX_TEST(test_sprintf_no_realloc) { 1.8 + char *buf = malloc(16); 1.9 + CxTestingAllocator talloc; 1.10 + cx_testing_allocator_init(&talloc); 1.11 + CxAllocator *alloc = &talloc.base; 1.12 + CX_TEST_DO { 1.13 + char *oldbuf = buf; 1.14 + size_t len = cx_sprintf_a(alloc, &buf, 16, "Test %d %s", 47, "string"); 1.15 + CX_TEST_ASSERT(oldbuf == buf); 1.16 + CX_TEST_ASSERT(len == 14); 1.17 + CX_TEST_ASSERT(0 == memcmp(buf, "Test 47 string", 15)); 1.18 + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 1.19 + } 1.20 + cx_testing_allocator_destroy(&talloc); 1.21 + free(buf); 1.22 +} 1.23 + 1.24 +CX_TEST(test_sprintf_realloc) { 1.25 + CxTestingAllocator talloc; 1.26 + cx_testing_allocator_init(&talloc); 1.27 + CxAllocator *alloc = &talloc.base; 1.28 + char *buf = cxMalloc(alloc, 8); 1.29 + CX_TEST_DO { 1.30 + size_t len = cx_sprintf_a(alloc, &buf, 8, "Test %d %s", 47, "foobar"); 1.31 + CX_TEST_ASSERT(len == 14); 1.32 + CX_TEST_ASSERT(0 == memcmp(buf, "Test 47 foobar", 15)); 1.33 + cxFree(alloc, buf); 1.34 + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 1.35 + } 1.36 + cx_testing_allocator_destroy(&talloc); 1.37 +} 1.38 + 1.39 +CX_TEST(test_sprintf_realloc_to_fit_terminator) { 1.40 + CxTestingAllocator talloc; 1.41 + cx_testing_allocator_init(&talloc); 1.42 + CxAllocator *alloc = &talloc.base; 1.43 + // make it so that only the zero-terminator does not fit 1.44 + char *buf = cxMalloc(alloc, 14); 1.45 + CX_TEST_DO { 1.46 + size_t len = cx_sprintf_a(alloc, &buf, 14, "Test %d %s", 13, "string"); 1.47 + CX_TEST_ASSERT(len == 14); 1.48 + CX_TEST_ASSERT(0 == memcmp(buf, "Test 13 string", 15)); 1.49 + cxFree(alloc, buf); 1.50 + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 1.51 + } 1.52 + cx_testing_allocator_destroy(&talloc); 1.53 +} 1.54 + 1.55 +CX_TEST(test_sprintf_s_no_alloc) { 1.56 + char buf[16]; 1.57 + CxTestingAllocator talloc; 1.58 + cx_testing_allocator_init(&talloc); 1.59 + CxAllocator *alloc = &talloc.base; 1.60 + CX_TEST_DO { 1.61 + char *str; 1.62 + size_t len = cx_sprintf_sa(alloc, buf, 16, &str, "Test %d %s", 47, "string"); 1.63 + CX_TEST_ASSERT(str == buf); 1.64 + CX_TEST_ASSERT(len == 14); 1.65 + CX_TEST_ASSERT(0 == memcmp(buf, "Test 47 string", 15)); 1.66 + CX_TEST_ASSERT(0 == memcmp(str, "Test 47 string", 15)); 1.67 + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 1.68 + } 1.69 + cx_testing_allocator_destroy(&talloc); 1.70 +} 1.71 + 1.72 +CX_TEST(test_sprintf_s_alloc) { 1.73 + char buf[16]; 1.74 + memcpy(buf, "0123456789abcdef", 16); 1.75 + CxTestingAllocator talloc; 1.76 + cx_testing_allocator_init(&talloc); 1.77 + CxAllocator *alloc = &talloc.base; 1.78 + CX_TEST_DO { 1.79 + char *str; 1.80 + size_t len = cx_sprintf_sa(alloc, buf, 16, &str, "Hello %d %s", 4711, "larger string"); 1.81 + CX_TEST_ASSERT(str != buf); 1.82 + CX_TEST_ASSERT(len == 24); 1.83 + CX_TEST_ASSERT(0 == memcmp(str, "Hello 4711 larger string", 25)); 1.84 + cxFree(alloc, str); 1.85 + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 1.86 + } 1.87 + cx_testing_allocator_destroy(&talloc); 1.88 +} 1.89 + 1.90 +CX_TEST(test_sprintf_s_alloc_to_fit_terminator) { 1.91 + char buf[16]; 1.92 + memcpy(buf, "0123456789abcdef", 16); 1.93 + CxTestingAllocator talloc; 1.94 + cx_testing_allocator_init(&talloc); 1.95 + CxAllocator *alloc = &talloc.base; 1.96 + CX_TEST_DO { 1.97 + char *str; 1.98 + size_t len = cx_sprintf_sa(alloc, buf, 16, &str, "Hello %d %s", 112, "string"); 1.99 + CX_TEST_ASSERT(str != buf); 1.100 + CX_TEST_ASSERT(len == 16); 1.101 + CX_TEST_ASSERT(0 == memcmp(str, "Hello 112 string", 17)); // include terminator 1.102 + cxFree(alloc, str); 1.103 + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 1.104 + } 1.105 + cx_testing_allocator_destroy(&talloc); 1.106 +} 1.107 + 1.108 CxTestSuite *cx_test_suite_printf(void) { 1.109 CxTestSuite *suite = cx_test_suite_new("printf"); 1.110 1.111 @@ -303,6 +404,12 @@ 1.112 cx_test_register(suite, test_fprintf); 1.113 cx_test_register(suite, test_asprintf); 1.114 cx_test_register(suite, test_asprintf_large_string); 1.115 + cx_test_register(suite, test_sprintf_no_realloc); 1.116 + cx_test_register(suite, test_sprintf_realloc); 1.117 + cx_test_register(suite, test_sprintf_realloc_to_fit_terminator); 1.118 + cx_test_register(suite, test_sprintf_s_no_alloc); 1.119 + cx_test_register(suite, test_sprintf_s_alloc); 1.120 + cx_test_register(suite, test_sprintf_s_alloc_to_fit_terminator); 1.121 1.122 return suite; 1.123 }