Fri, 29 Dec 2023 17:27:14 +0100
migrate printf tests - relates to #342
tests/Makefile | file | annotate | diff | comparison | revisions | |
tests/test_printf.c | file | annotate | diff | comparison | revisions | |
tests/test_printf.cpp | file | annotate | diff | comparison | revisions | |
tests/ucxtest.c | file | annotate | diff | comparison | revisions |
--- a/tests/Makefile Fri Dec 29 17:17:43 2023 +0100 +++ b/tests/Makefile Fri Dec 29 17:27:14 2023 +0100 @@ -27,7 +27,8 @@ TEST_DIR=$(build_dir)/tests -SRC = util_allocator.c test_utils.c test_hash_key.c test_string.c ucxtest.c +SRC = util_allocator.c test_utils.c test_hash_key.c test_string.c \ + test_printf.c ucxtest.c OBJ_EXT=.o OBJ=$(SRC:%.c=$(TEST_DIR)/%$(OBJ_EXT)) @@ -59,6 +60,13 @@ @echo "Compiling $<" $(CC) -o $@ $(CFLAGS) -c $< +$(TEST_DIR)/test_printf$(OBJ_EXT): test_printf.c ../src/cx/test.h \ + util_allocator.h ../src/cx/allocator.h ../src/cx/common.h \ + ../src/cx/printf.h ../src/cx/string.h ../src/cx/allocator.h \ + ../src/cx/buffer.h + @echo "Compiling $<" + $(CC) -o $@ $(CFLAGS) -c $< + $(TEST_DIR)/test_string$(OBJ_EXT): test_string.c ../src/cx/test.h \ util_allocator.h ../src/cx/allocator.h ../src/cx/common.h \ ../src/cx/string.h ../src/cx/allocator.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test_printf.c Fri Dec 29 17:27:14 2023 +0100 @@ -0,0 +1,305 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2023 Mike Becker, Olaf Wintermann All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "cx/test.h" +#include "util_allocator.h" + +#include "cx/printf.h" +#include "cx/buffer.h" + +#define ASSERT_ZERO_TERMINATED(str) CX_TEST_ASSERTM((str).ptr[(str).length] == '\0', \ + #str " is not zero terminated") + +static size_t test_printf_write_func( + void const *src, + size_t esize, + size_t ecount, + void *target +) { + memcpy(target, src, esize * ecount); + return esize * ecount; +} + +CX_TEST(test_bprintf) { + CxTestingAllocator talloc; + cx_testing_allocator_init(&talloc); + CxAllocator *alloc = &talloc.base; + CX_TEST_DO { + CxBuffer buf; + cxBufferInit(&buf, NULL, 64, alloc, 0); + size_t r = cx_bprintf(&buf, "This %s aged %u years in a %2XSK.", "Test", 10, 0xca); + CX_TEST_ASSERT(r == 34); + CX_TEST_ASSERT(buf.size == 34); + buf.space[r] = '\0'; + CX_TEST_ASSERT(0 == strcmp(buf.space, "This Test aged 10 years in a CASK.")); + cxBufferDestroy(&buf); + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); + } + cx_testing_allocator_destroy(&talloc); +} + +CX_TEST(test_bprintf_large_string) { + CxTestingAllocator talloc; + cx_testing_allocator_init(&talloc); + CxAllocator *alloc = &talloc.base; + char *aaa = malloc(512); + char *bbb = malloc(512); + char *expected = malloc(1040); + memset(aaa, 'a', 511); + aaa[511] = 0; + memset(bbb, 'b', 511); + bbb[511] = 0; + sprintf(expected, "After %s comes %s.", aaa, bbb); + CX_TEST_DO { + CxBuffer buf; + cxBufferInit(&buf, NULL, 64, alloc, CX_BUFFER_AUTO_EXTEND); + size_t r = cx_bprintf(&buf, "After %s comes %s.", aaa, bbb); + CX_TEST_ASSERT(r == 1036); + CX_TEST_ASSERT(buf.size == 1036); + cxBufferPut(&buf, 0); + CX_TEST_ASSERT(0 == strcmp(expected, buf.space)); + cxBufferDestroy(&buf); + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); + } + free(aaa); + free(bbb); + free(expected); + cx_testing_allocator_destroy(&talloc); +} + +CX_TEST(test_bprintf_nocap) { + CxTestingAllocator talloc; + cx_testing_allocator_init(&talloc); + CxAllocator *alloc = &talloc.base; + char space[20]; + memset(space, 'a', 20); + CX_TEST_DO { + CxBuffer buf; + cxBufferInit(&buf, space, 16, alloc, 0); + size_t r = cx_bprintf(&buf, "Hello %s with more than %d chars.", "string", 16); + CX_TEST_ASSERT(r == 16); + CX_TEST_ASSERT(buf.size == 16); + CX_TEST_ASSERT(0 == memcmp(space, "Hello string witaaaa", 20)); + cxBufferDestroy(&buf); + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); + } + cx_testing_allocator_destroy(&talloc); +} + +CX_TEST(test_fprintf) { + char const *h = "Hello"; + char buf[32]; + size_t r; + CX_TEST_DO { + r = cx_fprintf(buf, test_printf_write_func, "teststring"); + CX_TEST_ASSERT(r == 10); + CX_TEST_ASSERT(0 == memcmp(buf, "teststring", r)); + + r = cx_fprintf(buf, test_printf_write_func, "[%10s]", h); + CX_TEST_ASSERT(r == 12); + CX_TEST_ASSERT(0 == memcmp(buf, "[ Hello]", r)); + + r = cx_fprintf(buf, test_printf_write_func, "[%-10s]", h); + CX_TEST_ASSERT(r == 12); + CX_TEST_ASSERT(0 == memcmp(buf, "[Hello ]", r)); + + r = cx_fprintf(buf, test_printf_write_func, "[%*s]", 10, h); + CX_TEST_ASSERT(r == 12); + CX_TEST_ASSERT(0 == memcmp(buf, "[ Hello]", r)); + + r = cx_fprintf(buf, test_printf_write_func, "[%-10.*s]", 4, h); + CX_TEST_ASSERT(r == 12); + CX_TEST_ASSERT(0 == memcmp(buf, "[Hell ]", r)); + + r = cx_fprintf(buf, test_printf_write_func, "[%-*.*s]", 10, 4, h); + CX_TEST_ASSERT(r == 12); + CX_TEST_ASSERT(0 == memcmp(buf, "[Hell ]", r)); + + r = cx_fprintf(buf, test_printf_write_func, "%c", 'A'); + CX_TEST_ASSERT(r == 1); + CX_TEST_ASSERT(0 == memcmp(buf, "A", r)); + + r = cx_fprintf(buf, test_printf_write_func, "%i %d %.6i %i %.0i %+i %i", 1, 2, 3, 0, 0, 4, -4); + CX_TEST_ASSERT(r == 19); + CX_TEST_ASSERT(0 == memcmp(buf, "1 2 000003 0 +4 -4", r)); + + r = cx_fprintf(buf, test_printf_write_func, "%x %x %X %#x", 5, 10, 10, 6); + CX_TEST_ASSERT(r == 9); + CX_TEST_ASSERT(0 == memcmp(buf, "5 a A 0x6", r)); + + r = cx_fprintf(buf, test_printf_write_func, "%o %#o %#o", 10, 10, 4); + CX_TEST_ASSERT(r == 9); + CX_TEST_ASSERT(0 == memcmp(buf, "12 012 04", r)); + + r = cx_fprintf(buf, test_printf_write_func, "%05.2f %.2f %5.2f", 1.5, 1.5, 1.5); + CX_TEST_ASSERT(r == 16); + CX_TEST_ASSERT(0 == memcmp(buf, "01.50 1.50 1.50", r)); + + r = cx_fprintf(buf, test_printf_write_func, "'%*c'", 5, 'x'); + CX_TEST_ASSERT(r == 7); + CX_TEST_ASSERT(0 == memcmp(buf, "' x'", r)); + + r = cx_fprintf(buf, test_printf_write_func, "'%*c'", -5, 'x'); + CX_TEST_ASSERT(r == 7); + CX_TEST_ASSERT(0 == memcmp(buf, "'x '", r)); + } +} + +CX_TEST(test_asprintf) { + CxTestingAllocator talloc; + cx_testing_allocator_init(&talloc); + CxAllocator *alloc = &talloc.base; + + char const *h = "Hello"; + + int const specimen_count = 13; + cxmutstr r[specimen_count]; + int specimen = 0; + + CX_TEST_DO { + r[specimen] = cx_asprintf_a(alloc, "teststring"); + CX_TEST_ASSERT(r[specimen].length == 10); + ASSERT_ZERO_TERMINATED(r[specimen]); + CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "teststring")); + specimen++; + + r[specimen] = cx_asprintf_a(alloc, "[%10s]", h); + CX_TEST_ASSERT(r[specimen].length == 12); + ASSERT_ZERO_TERMINATED(r[specimen]); + CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "[ Hello]")); + specimen++; + + r[specimen] = cx_asprintf_a(alloc, "[%-10s]", h); + CX_TEST_ASSERT(r[specimen].length == 12); + ASSERT_ZERO_TERMINATED(r[specimen]); + CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "[Hello ]")); + specimen++; + + r[specimen] = cx_asprintf_a(alloc, "[%*s]", 10, h); + CX_TEST_ASSERT(r[specimen].length == 12); + ASSERT_ZERO_TERMINATED(r[specimen]); + CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "[ Hello]")); + specimen++; + + r[specimen] = cx_asprintf_a(alloc, "[%-10.*s]", 4, h); + CX_TEST_ASSERT(r[specimen].length == 12); + ASSERT_ZERO_TERMINATED(r[specimen]); + CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "[Hell ]")); + specimen++; + + r[specimen] = cx_asprintf_a(alloc, "[%-*.*s]", 10, 4, h); + CX_TEST_ASSERT(r[specimen].length == 12); + ASSERT_ZERO_TERMINATED(r[specimen]); + CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "[Hell ]")); + specimen++; + + r[specimen] = cx_asprintf_a(alloc, "%c", 'A'); + CX_TEST_ASSERT(r[specimen].length == 1); + ASSERT_ZERO_TERMINATED(r[specimen]); + CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "A")); + specimen++; + + r[specimen] = cx_asprintf_a(alloc, "%i %d %.6i %i %.0i %+i %i", 1, 2, 3, 0, 0, 4, -4); + CX_TEST_ASSERT(r[specimen].length == 19); + ASSERT_ZERO_TERMINATED(r[specimen]); + CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "1 2 000003 0 +4 -4")); + specimen++; + + r[specimen] = cx_asprintf_a(alloc, "%x %x %X %#x", 5, 10, 10, 6); + CX_TEST_ASSERT(r[specimen].length == 9); + ASSERT_ZERO_TERMINATED(r[specimen]); + CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "5 a A 0x6")); + specimen++; + + r[specimen] = cx_asprintf_a(alloc, "%o %#o %#o", 10, 10, 4); + CX_TEST_ASSERT(r[specimen].length == 9); + ASSERT_ZERO_TERMINATED(r[specimen]); + CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "12 012 04")); + specimen++; + + r[specimen] = cx_asprintf_a(alloc, "%05.2f %.2f %5.2f", 1.5, 1.5, 1.5); + CX_TEST_ASSERT(r[specimen].length == 16); + ASSERT_ZERO_TERMINATED(r[specimen]); + CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "01.50 1.50 1.50")); + specimen++; + + r[specimen] = cx_asprintf_a(alloc, "'%*c'", 5, 'x'); + CX_TEST_ASSERT(r[specimen].length == 7); + ASSERT_ZERO_TERMINATED(r[specimen]); + CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "' x'")); + specimen++; + + r[specimen] = cx_asprintf_a(alloc, "'%*c'", -5, 'x'); + CX_TEST_ASSERT(r[specimen].length == 7); + ASSERT_ZERO_TERMINATED(r[specimen]); + CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "'x '")); + specimen++; + + CX_TEST_ASSERT(specimen == specimen_count); // self-test + + for (int i = 0; i < specimen_count; i++) { + cx_strfree_a(alloc, &r[i]); + } + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); + } + cx_testing_allocator_destroy(&talloc); +} + +CX_TEST(test_asprintf_large_string) { + char *aaa = malloc(512); + char *bbb = malloc(512); + char *expected = malloc(1040); + memset(aaa, 'a', 511); + aaa[511] = 0; + memset(bbb, 'b', 511); + bbb[511] = 0; + sprintf(expected, "After %s comes %s.", aaa, bbb); + CX_TEST_DO { + cxmutstr r = cx_asprintf("After %s comes %s.", aaa, bbb); + CX_TEST_ASSERT(r.length == 1036); + ASSERT_ZERO_TERMINATED(r); + CX_TEST_ASSERT(0 == strcmp(r.ptr, expected)); + cx_strfree(&r); + } + free(aaa); + free(bbb); + free(expected); +} + +CxTestSuite *cx_test_suite_printf(void) { + CxTestSuite *suite = cx_test_suite_new("printf"); + + cx_test_register(suite, test_bprintf); + cx_test_register(suite, test_bprintf_large_string); + cx_test_register(suite, test_bprintf_nocap); + cx_test_register(suite, test_fprintf); + cx_test_register(suite, test_asprintf); + cx_test_register(suite, test_asprintf_large_string); + + return suite; +}
--- a/tests/test_printf.cpp Fri Dec 29 17:17:43 2023 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,248 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include "cx/printf.h" -#include "cx/buffer.h" - -#include <gtest/gtest.h> -#include "util_allocator.h" - -class PrintfFixture : public ::testing::Test { -protected: - std::string buf; - CxTestingAllocator alloc; - - void TearDown() override { - buf.clear(); - ASSERT_TRUE(alloc.verify()); - } - - static size_t write_func( - void const *src, - size_t esize, - size_t ecount, - void *target - ) { - auto str = reinterpret_cast<char const *>(src); - auto buf = reinterpret_cast<std::string *>(target); - EXPECT_EQ(esize, 1); - EXPECT_EQ(strlen(str), ecount); - *buf = str; - return ecount; - } -}; - - -TEST_F(PrintfFixture, BPrintf) { - CxBuffer buf; - cxBufferInit(&buf, nullptr, 64, &alloc, 0); - - auto r = cx_bprintf(&buf, "This %s aged %u years in a %2XSK.", "Test", 10, 0xca); - EXPECT_EQ(r, 34); - EXPECT_EQ(buf.size, 34); - buf.space[r] = '\0'; - EXPECT_STREQ(buf.space, "This Test aged 10 years in a CASK."); - - cxBufferDestroy(&buf); -} - -TEST_F(PrintfFixture, FPrintf) { - auto h = "Hello"; - size_t r; - - r = cx_fprintf(&buf, PrintfFixture::write_func, "teststring"); - EXPECT_EQ(r, 10); - EXPECT_EQ(buf, "teststring"); - - r = cx_fprintf(&buf, PrintfFixture::write_func, "[%10s]", h); - EXPECT_EQ(r, 12); - EXPECT_EQ(buf, "[ Hello]"); - - r = cx_fprintf(&buf, PrintfFixture::write_func, "[%-10s]", h); - EXPECT_EQ(r, 12); - EXPECT_EQ(buf, "[Hello ]"); - - r = cx_fprintf(&buf, PrintfFixture::write_func, "[%*s]", 10, h); - EXPECT_EQ(r, 12); - EXPECT_EQ(buf, "[ Hello]"); - - r = cx_fprintf(&buf, PrintfFixture::write_func, "[%-10.*s]", 4, h); - EXPECT_EQ(r, 12); - EXPECT_EQ(buf, "[Hell ]"); - - r = cx_fprintf(&buf, PrintfFixture::write_func, "[%-*.*s]", 10, 4, h); - EXPECT_EQ(r, 12); - EXPECT_EQ(buf, "[Hell ]"); - - r = cx_fprintf(&buf, PrintfFixture::write_func, "%c", 'A'); - EXPECT_EQ(r, 1); - EXPECT_EQ(buf, "A"); - - r = cx_fprintf(&buf, PrintfFixture::write_func, "%i %d %.6i %i %.0i %+i %i", 1, 2, 3, 0, 0, 4, -4); - EXPECT_EQ(r, 19); - EXPECT_EQ(buf, "1 2 000003 0 +4 -4"); - - r = cx_fprintf(&buf, PrintfFixture::write_func, "%x %x %X %#x", 5, 10, 10, 6); - EXPECT_EQ(r, 9); - EXPECT_EQ(buf, "5 a A 0x6"); - - r = cx_fprintf(&buf, PrintfFixture::write_func, "%o %#o %#o", 10, 10, 4); - EXPECT_EQ(r, 9); - EXPECT_EQ(buf, "12 012 04"); - - r = cx_fprintf(&buf, PrintfFixture::write_func, "%05.2f %.2f %5.2f", 1.5, 1.5, 1.5); - EXPECT_EQ(r, 16); - EXPECT_EQ(buf, "01.50 1.50 1.50"); - - r = cx_fprintf(&buf, PrintfFixture::write_func, "'%*c'", 5, 'x'); - EXPECT_EQ(r, 7); - EXPECT_EQ(buf, "' x'"); - - r = cx_fprintf(&buf, PrintfFixture::write_func, "'%*c'", -5, 'x'); - EXPECT_EQ(r, 7); - EXPECT_EQ(buf, "'x '"); -} - -TEST_F(PrintfFixture, BPrintfLargeString) { - CxBuffer buf; - cxBufferInit(&buf, nullptr, 64, &alloc, CX_BUFFER_AUTO_EXTEND); - - auto aaa = std::string(512, 'a'); - auto bbb = std::string(512, 'b'); - - auto r = cx_bprintf(&buf, "After %s comes %s.", aaa.data(), bbb.data()); - EXPECT_EQ(r, 1038); - EXPECT_EQ(buf.size, 1038); - cxBufferPut(&buf, 0); - EXPECT_EQ(buf.space, std::string("After ") + aaa + " comes " + bbb + "."); - - cxBufferDestroy(&buf); -} - -TEST_F(PrintfFixture, BPrintfNoCap) { - CxBuffer buf; - char space[20]; - memset(space, 'a', 20); - cxBufferInit(&buf, space, 16, &alloc, 0); - - auto r = cx_bprintf(&buf, "Hello %s with more than %d chars.", "string", 16); - EXPECT_EQ(r, 16); - EXPECT_EQ(buf.size, 16); - EXPECT_EQ(0, memcmp(space, "Hello string witaaaa", 20)); - - cxBufferDestroy(&buf); -} - -TEST_F(PrintfFixture, SPrintf) { - auto h = "Hello"; - - std::vector<char *> fl; - cxmutstr r; - - r = cx_asprintf_a(&alloc, "teststring"); - EXPECT_EQ(r.length, 10); - EXPECT_STREQ(r.ptr, "teststring"); - fl.push_back(r.ptr); - - r = cx_asprintf_a(&alloc, "[%10s]", h); - EXPECT_EQ(r.length, 12); - EXPECT_STREQ(r.ptr, "[ Hello]"); - fl.push_back(r.ptr); - - r = cx_asprintf_a(&alloc, "[%-10s]", h); - EXPECT_EQ(r.length, 12); - EXPECT_STREQ(r.ptr, "[Hello ]"); - fl.push_back(r.ptr); - - r = cx_asprintf_a(&alloc, "[%*s]", 10, h); - EXPECT_EQ(r.length, 12); - EXPECT_STREQ(r.ptr, "[ Hello]"); - fl.push_back(r.ptr); - - r = cx_asprintf_a(&alloc, "[%-10.*s]", 4, h); - EXPECT_EQ(r.length, 12); - EXPECT_STREQ(r.ptr, "[Hell ]"); - fl.push_back(r.ptr); - - r = cx_asprintf_a(&alloc, "[%-*.*s]", 10, 4, h); - EXPECT_EQ(r.length, 12); - EXPECT_STREQ(r.ptr, "[Hell ]"); - fl.push_back(r.ptr); - - r = cx_asprintf_a(&alloc, "%c", 'A'); - EXPECT_EQ(r.length, 1); - EXPECT_STREQ(r.ptr, "A"); - fl.push_back(r.ptr); - - r = cx_asprintf_a(&alloc, "%i %d %.6i %i %.0i %+i %i", 1, 2, 3, 0, 0, 4, -4); - EXPECT_EQ(r.length, 19); - EXPECT_STREQ(r.ptr, "1 2 000003 0 +4 -4"); - fl.push_back(r.ptr); - - r = cx_asprintf_a(&alloc, "%x %x %X %#x", 5, 10, 10, 6); - EXPECT_EQ(r.length, 9); - EXPECT_STREQ(r.ptr, "5 a A 0x6"); - fl.push_back(r.ptr); - - r = cx_asprintf_a(&alloc, "%o %#o %#o", 10, 10, 4); - EXPECT_EQ(r.length, 9); - EXPECT_STREQ(r.ptr, "12 012 04"); - fl.push_back(r.ptr); - - r = cx_asprintf_a(&alloc, "%05.2f %.2f %5.2f", 1.5, 1.5, 1.5); - EXPECT_EQ(r.length, 16); - EXPECT_STREQ(r.ptr, "01.50 1.50 1.50"); - fl.push_back(r.ptr); - - r = cx_asprintf_a(&alloc, "'%*c'", 5, 'x'); - EXPECT_EQ(r.length, 7); - EXPECT_STREQ(r.ptr, "' x'"); - fl.push_back(r.ptr); - - r = cx_asprintf_a(&alloc, "'%*c'", -5, 'x'); - EXPECT_EQ(r.length, 7); - EXPECT_STREQ(r.ptr, "'x '"); - fl.push_back(r.ptr); - - for (auto c: fl) { - auto s = cx_mutstrn(c, 0); - cx_strfree_a(&alloc, &s); - } -} - -TEST_F(PrintfFixture, SPrintfLargeString) { - auto aaa = std::string(512, 'a'); - auto bbb = std::string(512, 'b'); - - auto r = cx_asprintf_a(&alloc, "After %s comes %s.", aaa.data(), bbb.data()); - EXPECT_EQ(r.length, 1038); - EXPECT_EQ(r.ptr, std::string("After ") + aaa + " comes " + bbb + "."); - EXPECT_EQ(r.ptr[1038], '\0'); - - cx_strfree_a(&alloc, &r); -} \ No newline at end of file
--- a/tests/ucxtest.c Fri Dec 29 17:17:43 2023 +0100 +++ b/tests/ucxtest.c Fri Dec 29 17:27:14 2023 +0100 @@ -31,8 +31,9 @@ CxTestSuite *cx_test_suite_testing_allocator(void); CxTestSuite *cx_test_suite_utils(void); CxTestSuite *cx_test_suite_hash_key(void); +CxTestSuite *cx_test_suite_string(void); -CxTestSuite *cx_test_suite_string(void); +CxTestSuite *cx_test_suite_printf(void); #define run_tests(suite) cx_test_run_stdout(suite); success += (suite)->success; failure += (suite)->failure #define execute_test_suites(...) unsigned success = 0, failure = 0; CxTestSuite* test_suites[] = {__VA_ARGS__}; \ @@ -46,7 +47,8 @@ cx_test_suite_testing_allocator(), cx_test_suite_utils(), cx_test_suite_hash_key(), - cx_test_suite_string() + cx_test_suite_string(), + cx_test_suite_printf() ); printf("=== OVERALL RESULT ===\n"); printf(" Total: %u\n Success: %u\n Failure: %u\n",