universe@780: /* universe@780: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@780: * universe@780: * Copyright 2023 Mike Becker, Olaf Wintermann All rights reserved. universe@780: * universe@780: * Redistribution and use in source and binary forms, with or without universe@780: * modification, are permitted provided that the following conditions are met: universe@780: * universe@780: * 1. Redistributions of source code must retain the above copyright universe@780: * notice, this list of conditions and the following disclaimer. universe@780: * universe@780: * 2. Redistributions in binary form must reproduce the above copyright universe@780: * notice, this list of conditions and the following disclaimer in the universe@780: * documentation and/or other materials provided with the distribution. universe@780: * universe@780: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@780: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@780: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@780: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@780: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@780: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@780: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@780: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@780: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@780: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@780: * POSSIBILITY OF SUCH DAMAGE. universe@780: */ universe@780: universe@780: #include "cx/test.h" universe@780: #include "util_allocator.h" universe@780: universe@780: #include "cx/printf.h" universe@780: #include "cx/buffer.h" universe@780: universe@780: #define ASSERT_ZERO_TERMINATED(str) CX_TEST_ASSERTM((str).ptr[(str).length] == '\0', \ universe@780: #str " is not zero terminated") universe@780: universe@780: static size_t test_printf_write_func( universe@780: void const *src, universe@780: size_t esize, universe@780: size_t ecount, universe@780: void *target universe@780: ) { universe@780: memcpy(target, src, esize * ecount); universe@780: return esize * ecount; universe@780: } universe@780: universe@780: CX_TEST(test_bprintf) { universe@780: CxTestingAllocator talloc; universe@780: cx_testing_allocator_init(&talloc); universe@780: CxAllocator *alloc = &talloc.base; universe@780: CX_TEST_DO { universe@780: CxBuffer buf; universe@780: cxBufferInit(&buf, NULL, 64, alloc, 0); universe@780: size_t r = cx_bprintf(&buf, "This %s aged %u years in a %2XSK.", "Test", 10, 0xca); universe@780: CX_TEST_ASSERT(r == 34); universe@780: CX_TEST_ASSERT(buf.size == 34); universe@780: buf.space[r] = '\0'; universe@780: CX_TEST_ASSERT(0 == strcmp(buf.space, "This Test aged 10 years in a CASK.")); universe@780: cxBufferDestroy(&buf); universe@780: CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); universe@780: } universe@780: cx_testing_allocator_destroy(&talloc); universe@780: } universe@780: universe@780: CX_TEST(test_bprintf_large_string) { universe@805: unsigned len = cx_printf_sbo_size; universe@780: CxTestingAllocator talloc; universe@780: cx_testing_allocator_init(&talloc); universe@780: CxAllocator *alloc = &talloc.base; universe@805: char *aaa = malloc(len); universe@805: char *bbb = malloc(len); universe@805: char *expected = malloc(2*len+16); universe@805: memset(aaa, 'a', len-1); universe@805: aaa[len-1] = 0; universe@805: memset(bbb, 'b', len-1); universe@805: bbb[len-1] = 0; universe@780: sprintf(expected, "After %s comes %s.", aaa, bbb); universe@780: CX_TEST_DO { universe@780: CxBuffer buf; universe@780: cxBufferInit(&buf, NULL, 64, alloc, CX_BUFFER_AUTO_EXTEND); universe@780: size_t r = cx_bprintf(&buf, "After %s comes %s.", aaa, bbb); universe@805: size_t er = 2*len-2+14; universe@805: CX_TEST_ASSERT(r == er); universe@805: CX_TEST_ASSERT(buf.size == er); universe@780: cxBufferPut(&buf, 0); universe@780: CX_TEST_ASSERT(0 == strcmp(expected, buf.space)); universe@780: cxBufferDestroy(&buf); universe@780: CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); universe@780: } universe@780: free(aaa); universe@780: free(bbb); universe@780: free(expected); universe@780: cx_testing_allocator_destroy(&talloc); universe@780: } universe@780: universe@780: CX_TEST(test_bprintf_nocap) { universe@780: CxTestingAllocator talloc; universe@780: cx_testing_allocator_init(&talloc); universe@780: CxAllocator *alloc = &talloc.base; universe@780: char space[20]; universe@780: memset(space, 'a', 20); universe@780: CX_TEST_DO { universe@780: CxBuffer buf; universe@780: cxBufferInit(&buf, space, 16, alloc, 0); universe@780: size_t r = cx_bprintf(&buf, "Hello %s with more than %d chars.", "string", 16); universe@780: CX_TEST_ASSERT(r == 16); universe@780: CX_TEST_ASSERT(buf.size == 16); universe@780: CX_TEST_ASSERT(0 == memcmp(space, "Hello string witaaaa", 20)); universe@780: cxBufferDestroy(&buf); universe@780: CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); universe@780: } universe@780: cx_testing_allocator_destroy(&talloc); universe@780: } universe@780: universe@780: CX_TEST(test_fprintf) { universe@780: char const *h = "Hello"; universe@780: char buf[32]; universe@780: size_t r; universe@780: CX_TEST_DO { universe@780: r = cx_fprintf(buf, test_printf_write_func, "teststring"); universe@780: CX_TEST_ASSERT(r == 10); universe@780: CX_TEST_ASSERT(0 == memcmp(buf, "teststring", r)); universe@780: universe@780: r = cx_fprintf(buf, test_printf_write_func, "[%10s]", h); universe@780: CX_TEST_ASSERT(r == 12); universe@780: CX_TEST_ASSERT(0 == memcmp(buf, "[ Hello]", r)); universe@780: universe@780: r = cx_fprintf(buf, test_printf_write_func, "[%-10s]", h); universe@780: CX_TEST_ASSERT(r == 12); universe@780: CX_TEST_ASSERT(0 == memcmp(buf, "[Hello ]", r)); universe@780: universe@780: r = cx_fprintf(buf, test_printf_write_func, "[%*s]", 10, h); universe@780: CX_TEST_ASSERT(r == 12); universe@780: CX_TEST_ASSERT(0 == memcmp(buf, "[ Hello]", r)); universe@780: universe@780: r = cx_fprintf(buf, test_printf_write_func, "[%-10.*s]", 4, h); universe@780: CX_TEST_ASSERT(r == 12); universe@780: CX_TEST_ASSERT(0 == memcmp(buf, "[Hell ]", r)); universe@780: universe@780: r = cx_fprintf(buf, test_printf_write_func, "[%-*.*s]", 10, 4, h); universe@780: CX_TEST_ASSERT(r == 12); universe@780: CX_TEST_ASSERT(0 == memcmp(buf, "[Hell ]", r)); universe@780: universe@780: r = cx_fprintf(buf, test_printf_write_func, "%c", 'A'); universe@780: CX_TEST_ASSERT(r == 1); universe@780: CX_TEST_ASSERT(0 == memcmp(buf, "A", r)); universe@780: universe@780: r = cx_fprintf(buf, test_printf_write_func, "%i %d %.6i %i %.0i %+i %i", 1, 2, 3, 0, 0, 4, -4); universe@780: CX_TEST_ASSERT(r == 19); universe@780: CX_TEST_ASSERT(0 == memcmp(buf, "1 2 000003 0 +4 -4", r)); universe@780: universe@780: r = cx_fprintf(buf, test_printf_write_func, "%x %x %X %#x", 5, 10, 10, 6); universe@780: CX_TEST_ASSERT(r == 9); universe@780: CX_TEST_ASSERT(0 == memcmp(buf, "5 a A 0x6", r)); universe@780: universe@780: r = cx_fprintf(buf, test_printf_write_func, "%o %#o %#o", 10, 10, 4); universe@780: CX_TEST_ASSERT(r == 9); universe@780: CX_TEST_ASSERT(0 == memcmp(buf, "12 012 04", r)); universe@780: universe@780: r = cx_fprintf(buf, test_printf_write_func, "%05.2f %.2f %5.2f", 1.5, 1.5, 1.5); universe@780: CX_TEST_ASSERT(r == 16); universe@780: CX_TEST_ASSERT(0 == memcmp(buf, "01.50 1.50 1.50", r)); universe@780: universe@780: r = cx_fprintf(buf, test_printf_write_func, "'%*c'", 5, 'x'); universe@780: CX_TEST_ASSERT(r == 7); universe@780: CX_TEST_ASSERT(0 == memcmp(buf, "' x'", r)); universe@780: universe@780: r = cx_fprintf(buf, test_printf_write_func, "'%*c'", -5, 'x'); universe@780: CX_TEST_ASSERT(r == 7); universe@780: CX_TEST_ASSERT(0 == memcmp(buf, "'x '", r)); universe@780: } universe@780: } universe@780: universe@780: CX_TEST(test_asprintf) { universe@780: CxTestingAllocator talloc; universe@780: cx_testing_allocator_init(&talloc); universe@780: CxAllocator *alloc = &talloc.base; universe@780: universe@780: char const *h = "Hello"; universe@780: universe@780: int const specimen_count = 13; universe@780: cxmutstr r[specimen_count]; universe@780: int specimen = 0; universe@780: universe@780: CX_TEST_DO { universe@780: r[specimen] = cx_asprintf_a(alloc, "teststring"); universe@780: CX_TEST_ASSERT(r[specimen].length == 10); universe@780: ASSERT_ZERO_TERMINATED(r[specimen]); universe@780: CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "teststring")); universe@780: specimen++; universe@780: universe@780: r[specimen] = cx_asprintf_a(alloc, "[%10s]", h); universe@780: CX_TEST_ASSERT(r[specimen].length == 12); universe@780: ASSERT_ZERO_TERMINATED(r[specimen]); universe@780: CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "[ Hello]")); universe@780: specimen++; universe@780: universe@780: r[specimen] = cx_asprintf_a(alloc, "[%-10s]", h); universe@780: CX_TEST_ASSERT(r[specimen].length == 12); universe@780: ASSERT_ZERO_TERMINATED(r[specimen]); universe@780: CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "[Hello ]")); universe@780: specimen++; universe@780: universe@780: r[specimen] = cx_asprintf_a(alloc, "[%*s]", 10, h); universe@780: CX_TEST_ASSERT(r[specimen].length == 12); universe@780: ASSERT_ZERO_TERMINATED(r[specimen]); universe@780: CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "[ Hello]")); universe@780: specimen++; universe@780: universe@780: r[specimen] = cx_asprintf_a(alloc, "[%-10.*s]", 4, h); universe@780: CX_TEST_ASSERT(r[specimen].length == 12); universe@780: ASSERT_ZERO_TERMINATED(r[specimen]); universe@780: CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "[Hell ]")); universe@780: specimen++; universe@780: universe@780: r[specimen] = cx_asprintf_a(alloc, "[%-*.*s]", 10, 4, h); universe@780: CX_TEST_ASSERT(r[specimen].length == 12); universe@780: ASSERT_ZERO_TERMINATED(r[specimen]); universe@780: CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "[Hell ]")); universe@780: specimen++; universe@780: universe@780: r[specimen] = cx_asprintf_a(alloc, "%c", 'A'); universe@780: CX_TEST_ASSERT(r[specimen].length == 1); universe@780: ASSERT_ZERO_TERMINATED(r[specimen]); universe@780: CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "A")); universe@780: specimen++; universe@780: universe@780: r[specimen] = cx_asprintf_a(alloc, "%i %d %.6i %i %.0i %+i %i", 1, 2, 3, 0, 0, 4, -4); universe@780: CX_TEST_ASSERT(r[specimen].length == 19); universe@780: ASSERT_ZERO_TERMINATED(r[specimen]); universe@780: CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "1 2 000003 0 +4 -4")); universe@780: specimen++; universe@780: universe@780: r[specimen] = cx_asprintf_a(alloc, "%x %x %X %#x", 5, 10, 10, 6); universe@780: CX_TEST_ASSERT(r[specimen].length == 9); universe@780: ASSERT_ZERO_TERMINATED(r[specimen]); universe@780: CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "5 a A 0x6")); universe@780: specimen++; universe@780: universe@780: r[specimen] = cx_asprintf_a(alloc, "%o %#o %#o", 10, 10, 4); universe@780: CX_TEST_ASSERT(r[specimen].length == 9); universe@780: ASSERT_ZERO_TERMINATED(r[specimen]); universe@780: CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "12 012 04")); universe@780: specimen++; universe@780: universe@780: r[specimen] = cx_asprintf_a(alloc, "%05.2f %.2f %5.2f", 1.5, 1.5, 1.5); universe@780: CX_TEST_ASSERT(r[specimen].length == 16); universe@780: ASSERT_ZERO_TERMINATED(r[specimen]); universe@780: CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "01.50 1.50 1.50")); universe@780: specimen++; universe@780: universe@780: r[specimen] = cx_asprintf_a(alloc, "'%*c'", 5, 'x'); universe@780: CX_TEST_ASSERT(r[specimen].length == 7); universe@780: ASSERT_ZERO_TERMINATED(r[specimen]); universe@780: CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "' x'")); universe@780: specimen++; universe@780: universe@780: r[specimen] = cx_asprintf_a(alloc, "'%*c'", -5, 'x'); universe@780: CX_TEST_ASSERT(r[specimen].length == 7); universe@780: ASSERT_ZERO_TERMINATED(r[specimen]); universe@780: CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "'x '")); universe@780: specimen++; universe@780: universe@780: CX_TEST_ASSERT(specimen == specimen_count); // self-test universe@780: universe@780: for (int i = 0; i < specimen_count; i++) { universe@780: cx_strfree_a(alloc, &r[i]); universe@780: } universe@780: CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); universe@780: } universe@780: cx_testing_allocator_destroy(&talloc); universe@780: } universe@780: universe@780: CX_TEST(test_asprintf_large_string) { universe@805: unsigned len = cx_printf_sbo_size; universe@805: char *aaa = malloc(len); universe@805: char *bbb = malloc(len); universe@805: char *expected = malloc(2*len+16); universe@805: memset(aaa, 'a', len-1); universe@805: aaa[len-1] = 0; universe@805: memset(bbb, 'b', len-1); universe@805: bbb[len-1] = 0; universe@780: sprintf(expected, "After %s comes %s.", aaa, bbb); universe@780: CX_TEST_DO { universe@780: cxmutstr r = cx_asprintf("After %s comes %s.", aaa, bbb); universe@805: CX_TEST_ASSERT(r.length == 2*len-2+14); universe@780: ASSERT_ZERO_TERMINATED(r); universe@780: CX_TEST_ASSERT(0 == strcmp(r.ptr, expected)); universe@780: cx_strfree(&r); universe@780: } universe@780: free(aaa); universe@780: free(bbb); universe@780: free(expected); universe@780: } universe@780: universe@780: CxTestSuite *cx_test_suite_printf(void) { universe@780: CxTestSuite *suite = cx_test_suite_new("printf"); universe@780: universe@780: cx_test_register(suite, test_bprintf); universe@780: cx_test_register(suite, test_bprintf_large_string); universe@780: cx_test_register(suite, test_bprintf_nocap); universe@780: cx_test_register(suite, test_fprintf); universe@780: cx_test_register(suite, test_asprintf); universe@780: cx_test_register(suite, test_asprintf_large_string); universe@780: universe@780: return suite; universe@780: }