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 |
1.1 --- a/tests/Makefile Fri Dec 29 17:17:43 2023 +0100 1.2 +++ b/tests/Makefile Fri Dec 29 17:27:14 2023 +0100 1.3 @@ -27,7 +27,8 @@ 1.4 1.5 TEST_DIR=$(build_dir)/tests 1.6 1.7 -SRC = util_allocator.c test_utils.c test_hash_key.c test_string.c ucxtest.c 1.8 +SRC = util_allocator.c test_utils.c test_hash_key.c test_string.c \ 1.9 + test_printf.c ucxtest.c 1.10 1.11 OBJ_EXT=.o 1.12 OBJ=$(SRC:%.c=$(TEST_DIR)/%$(OBJ_EXT)) 1.13 @@ -59,6 +60,13 @@ 1.14 @echo "Compiling $<" 1.15 $(CC) -o $@ $(CFLAGS) -c $< 1.16 1.17 +$(TEST_DIR)/test_printf$(OBJ_EXT): test_printf.c ../src/cx/test.h \ 1.18 + util_allocator.h ../src/cx/allocator.h ../src/cx/common.h \ 1.19 + ../src/cx/printf.h ../src/cx/string.h ../src/cx/allocator.h \ 1.20 + ../src/cx/buffer.h 1.21 + @echo "Compiling $<" 1.22 + $(CC) -o $@ $(CFLAGS) -c $< 1.23 + 1.24 $(TEST_DIR)/test_string$(OBJ_EXT): test_string.c ../src/cx/test.h \ 1.25 util_allocator.h ../src/cx/allocator.h ../src/cx/common.h \ 1.26 ../src/cx/string.h ../src/cx/allocator.h
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/tests/test_printf.c Fri Dec 29 17:27:14 2023 +0100 2.3 @@ -0,0 +1,305 @@ 2.4 +/* 2.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 2.6 + * 2.7 + * Copyright 2023 Mike Becker, Olaf Wintermann All rights reserved. 2.8 + * 2.9 + * Redistribution and use in source and binary forms, with or without 2.10 + * modification, are permitted provided that the following conditions are met: 2.11 + * 2.12 + * 1. Redistributions of source code must retain the above copyright 2.13 + * notice, this list of conditions and the following disclaimer. 2.14 + * 2.15 + * 2. Redistributions in binary form must reproduce the above copyright 2.16 + * notice, this list of conditions and the following disclaimer in the 2.17 + * documentation and/or other materials provided with the distribution. 2.18 + * 2.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 2.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 2.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 2.29 + * POSSIBILITY OF SUCH DAMAGE. 2.30 + */ 2.31 + 2.32 +#include "cx/test.h" 2.33 +#include "util_allocator.h" 2.34 + 2.35 +#include "cx/printf.h" 2.36 +#include "cx/buffer.h" 2.37 + 2.38 +#define ASSERT_ZERO_TERMINATED(str) CX_TEST_ASSERTM((str).ptr[(str).length] == '\0', \ 2.39 + #str " is not zero terminated") 2.40 + 2.41 +static size_t test_printf_write_func( 2.42 + void const *src, 2.43 + size_t esize, 2.44 + size_t ecount, 2.45 + void *target 2.46 +) { 2.47 + memcpy(target, src, esize * ecount); 2.48 + return esize * ecount; 2.49 +} 2.50 + 2.51 +CX_TEST(test_bprintf) { 2.52 + CxTestingAllocator talloc; 2.53 + cx_testing_allocator_init(&talloc); 2.54 + CxAllocator *alloc = &talloc.base; 2.55 + CX_TEST_DO { 2.56 + CxBuffer buf; 2.57 + cxBufferInit(&buf, NULL, 64, alloc, 0); 2.58 + size_t r = cx_bprintf(&buf, "This %s aged %u years in a %2XSK.", "Test", 10, 0xca); 2.59 + CX_TEST_ASSERT(r == 34); 2.60 + CX_TEST_ASSERT(buf.size == 34); 2.61 + buf.space[r] = '\0'; 2.62 + CX_TEST_ASSERT(0 == strcmp(buf.space, "This Test aged 10 years in a CASK.")); 2.63 + cxBufferDestroy(&buf); 2.64 + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 2.65 + } 2.66 + cx_testing_allocator_destroy(&talloc); 2.67 +} 2.68 + 2.69 +CX_TEST(test_bprintf_large_string) { 2.70 + CxTestingAllocator talloc; 2.71 + cx_testing_allocator_init(&talloc); 2.72 + CxAllocator *alloc = &talloc.base; 2.73 + char *aaa = malloc(512); 2.74 + char *bbb = malloc(512); 2.75 + char *expected = malloc(1040); 2.76 + memset(aaa, 'a', 511); 2.77 + aaa[511] = 0; 2.78 + memset(bbb, 'b', 511); 2.79 + bbb[511] = 0; 2.80 + sprintf(expected, "After %s comes %s.", aaa, bbb); 2.81 + CX_TEST_DO { 2.82 + CxBuffer buf; 2.83 + cxBufferInit(&buf, NULL, 64, alloc, CX_BUFFER_AUTO_EXTEND); 2.84 + size_t r = cx_bprintf(&buf, "After %s comes %s.", aaa, bbb); 2.85 + CX_TEST_ASSERT(r == 1036); 2.86 + CX_TEST_ASSERT(buf.size == 1036); 2.87 + cxBufferPut(&buf, 0); 2.88 + CX_TEST_ASSERT(0 == strcmp(expected, buf.space)); 2.89 + cxBufferDestroy(&buf); 2.90 + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 2.91 + } 2.92 + free(aaa); 2.93 + free(bbb); 2.94 + free(expected); 2.95 + cx_testing_allocator_destroy(&talloc); 2.96 +} 2.97 + 2.98 +CX_TEST(test_bprintf_nocap) { 2.99 + CxTestingAllocator talloc; 2.100 + cx_testing_allocator_init(&talloc); 2.101 + CxAllocator *alloc = &talloc.base; 2.102 + char space[20]; 2.103 + memset(space, 'a', 20); 2.104 + CX_TEST_DO { 2.105 + CxBuffer buf; 2.106 + cxBufferInit(&buf, space, 16, alloc, 0); 2.107 + size_t r = cx_bprintf(&buf, "Hello %s with more than %d chars.", "string", 16); 2.108 + CX_TEST_ASSERT(r == 16); 2.109 + CX_TEST_ASSERT(buf.size == 16); 2.110 + CX_TEST_ASSERT(0 == memcmp(space, "Hello string witaaaa", 20)); 2.111 + cxBufferDestroy(&buf); 2.112 + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 2.113 + } 2.114 + cx_testing_allocator_destroy(&talloc); 2.115 +} 2.116 + 2.117 +CX_TEST(test_fprintf) { 2.118 + char const *h = "Hello"; 2.119 + char buf[32]; 2.120 + size_t r; 2.121 + CX_TEST_DO { 2.122 + r = cx_fprintf(buf, test_printf_write_func, "teststring"); 2.123 + CX_TEST_ASSERT(r == 10); 2.124 + CX_TEST_ASSERT(0 == memcmp(buf, "teststring", r)); 2.125 + 2.126 + r = cx_fprintf(buf, test_printf_write_func, "[%10s]", h); 2.127 + CX_TEST_ASSERT(r == 12); 2.128 + CX_TEST_ASSERT(0 == memcmp(buf, "[ Hello]", r)); 2.129 + 2.130 + r = cx_fprintf(buf, test_printf_write_func, "[%-10s]", h); 2.131 + CX_TEST_ASSERT(r == 12); 2.132 + CX_TEST_ASSERT(0 == memcmp(buf, "[Hello ]", r)); 2.133 + 2.134 + r = cx_fprintf(buf, test_printf_write_func, "[%*s]", 10, h); 2.135 + CX_TEST_ASSERT(r == 12); 2.136 + CX_TEST_ASSERT(0 == memcmp(buf, "[ Hello]", r)); 2.137 + 2.138 + r = cx_fprintf(buf, test_printf_write_func, "[%-10.*s]", 4, h); 2.139 + CX_TEST_ASSERT(r == 12); 2.140 + CX_TEST_ASSERT(0 == memcmp(buf, "[Hell ]", r)); 2.141 + 2.142 + r = cx_fprintf(buf, test_printf_write_func, "[%-*.*s]", 10, 4, h); 2.143 + CX_TEST_ASSERT(r == 12); 2.144 + CX_TEST_ASSERT(0 == memcmp(buf, "[Hell ]", r)); 2.145 + 2.146 + r = cx_fprintf(buf, test_printf_write_func, "%c", 'A'); 2.147 + CX_TEST_ASSERT(r == 1); 2.148 + CX_TEST_ASSERT(0 == memcmp(buf, "A", r)); 2.149 + 2.150 + r = cx_fprintf(buf, test_printf_write_func, "%i %d %.6i %i %.0i %+i %i", 1, 2, 3, 0, 0, 4, -4); 2.151 + CX_TEST_ASSERT(r == 19); 2.152 + CX_TEST_ASSERT(0 == memcmp(buf, "1 2 000003 0 +4 -4", r)); 2.153 + 2.154 + r = cx_fprintf(buf, test_printf_write_func, "%x %x %X %#x", 5, 10, 10, 6); 2.155 + CX_TEST_ASSERT(r == 9); 2.156 + CX_TEST_ASSERT(0 == memcmp(buf, "5 a A 0x6", r)); 2.157 + 2.158 + r = cx_fprintf(buf, test_printf_write_func, "%o %#o %#o", 10, 10, 4); 2.159 + CX_TEST_ASSERT(r == 9); 2.160 + CX_TEST_ASSERT(0 == memcmp(buf, "12 012 04", r)); 2.161 + 2.162 + r = cx_fprintf(buf, test_printf_write_func, "%05.2f %.2f %5.2f", 1.5, 1.5, 1.5); 2.163 + CX_TEST_ASSERT(r == 16); 2.164 + CX_TEST_ASSERT(0 == memcmp(buf, "01.50 1.50 1.50", r)); 2.165 + 2.166 + r = cx_fprintf(buf, test_printf_write_func, "'%*c'", 5, 'x'); 2.167 + CX_TEST_ASSERT(r == 7); 2.168 + CX_TEST_ASSERT(0 == memcmp(buf, "' x'", r)); 2.169 + 2.170 + r = cx_fprintf(buf, test_printf_write_func, "'%*c'", -5, 'x'); 2.171 + CX_TEST_ASSERT(r == 7); 2.172 + CX_TEST_ASSERT(0 == memcmp(buf, "'x '", r)); 2.173 + } 2.174 +} 2.175 + 2.176 +CX_TEST(test_asprintf) { 2.177 + CxTestingAllocator talloc; 2.178 + cx_testing_allocator_init(&talloc); 2.179 + CxAllocator *alloc = &talloc.base; 2.180 + 2.181 + char const *h = "Hello"; 2.182 + 2.183 + int const specimen_count = 13; 2.184 + cxmutstr r[specimen_count]; 2.185 + int specimen = 0; 2.186 + 2.187 + CX_TEST_DO { 2.188 + r[specimen] = cx_asprintf_a(alloc, "teststring"); 2.189 + CX_TEST_ASSERT(r[specimen].length == 10); 2.190 + ASSERT_ZERO_TERMINATED(r[specimen]); 2.191 + CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "teststring")); 2.192 + specimen++; 2.193 + 2.194 + r[specimen] = cx_asprintf_a(alloc, "[%10s]", h); 2.195 + CX_TEST_ASSERT(r[specimen].length == 12); 2.196 + ASSERT_ZERO_TERMINATED(r[specimen]); 2.197 + CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "[ Hello]")); 2.198 + specimen++; 2.199 + 2.200 + r[specimen] = cx_asprintf_a(alloc, "[%-10s]", h); 2.201 + CX_TEST_ASSERT(r[specimen].length == 12); 2.202 + ASSERT_ZERO_TERMINATED(r[specimen]); 2.203 + CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "[Hello ]")); 2.204 + specimen++; 2.205 + 2.206 + r[specimen] = cx_asprintf_a(alloc, "[%*s]", 10, h); 2.207 + CX_TEST_ASSERT(r[specimen].length == 12); 2.208 + ASSERT_ZERO_TERMINATED(r[specimen]); 2.209 + CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "[ Hello]")); 2.210 + specimen++; 2.211 + 2.212 + r[specimen] = cx_asprintf_a(alloc, "[%-10.*s]", 4, h); 2.213 + CX_TEST_ASSERT(r[specimen].length == 12); 2.214 + ASSERT_ZERO_TERMINATED(r[specimen]); 2.215 + CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "[Hell ]")); 2.216 + specimen++; 2.217 + 2.218 + r[specimen] = cx_asprintf_a(alloc, "[%-*.*s]", 10, 4, h); 2.219 + CX_TEST_ASSERT(r[specimen].length == 12); 2.220 + ASSERT_ZERO_TERMINATED(r[specimen]); 2.221 + CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "[Hell ]")); 2.222 + specimen++; 2.223 + 2.224 + r[specimen] = cx_asprintf_a(alloc, "%c", 'A'); 2.225 + CX_TEST_ASSERT(r[specimen].length == 1); 2.226 + ASSERT_ZERO_TERMINATED(r[specimen]); 2.227 + CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "A")); 2.228 + specimen++; 2.229 + 2.230 + r[specimen] = cx_asprintf_a(alloc, "%i %d %.6i %i %.0i %+i %i", 1, 2, 3, 0, 0, 4, -4); 2.231 + CX_TEST_ASSERT(r[specimen].length == 19); 2.232 + ASSERT_ZERO_TERMINATED(r[specimen]); 2.233 + CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "1 2 000003 0 +4 -4")); 2.234 + specimen++; 2.235 + 2.236 + r[specimen] = cx_asprintf_a(alloc, "%x %x %X %#x", 5, 10, 10, 6); 2.237 + CX_TEST_ASSERT(r[specimen].length == 9); 2.238 + ASSERT_ZERO_TERMINATED(r[specimen]); 2.239 + CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "5 a A 0x6")); 2.240 + specimen++; 2.241 + 2.242 + r[specimen] = cx_asprintf_a(alloc, "%o %#o %#o", 10, 10, 4); 2.243 + CX_TEST_ASSERT(r[specimen].length == 9); 2.244 + ASSERT_ZERO_TERMINATED(r[specimen]); 2.245 + CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "12 012 04")); 2.246 + specimen++; 2.247 + 2.248 + r[specimen] = cx_asprintf_a(alloc, "%05.2f %.2f %5.2f", 1.5, 1.5, 1.5); 2.249 + CX_TEST_ASSERT(r[specimen].length == 16); 2.250 + ASSERT_ZERO_TERMINATED(r[specimen]); 2.251 + CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "01.50 1.50 1.50")); 2.252 + specimen++; 2.253 + 2.254 + r[specimen] = cx_asprintf_a(alloc, "'%*c'", 5, 'x'); 2.255 + CX_TEST_ASSERT(r[specimen].length == 7); 2.256 + ASSERT_ZERO_TERMINATED(r[specimen]); 2.257 + CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "' x'")); 2.258 + specimen++; 2.259 + 2.260 + r[specimen] = cx_asprintf_a(alloc, "'%*c'", -5, 'x'); 2.261 + CX_TEST_ASSERT(r[specimen].length == 7); 2.262 + ASSERT_ZERO_TERMINATED(r[specimen]); 2.263 + CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "'x '")); 2.264 + specimen++; 2.265 + 2.266 + CX_TEST_ASSERT(specimen == specimen_count); // self-test 2.267 + 2.268 + for (int i = 0; i < specimen_count; i++) { 2.269 + cx_strfree_a(alloc, &r[i]); 2.270 + } 2.271 + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 2.272 + } 2.273 + cx_testing_allocator_destroy(&talloc); 2.274 +} 2.275 + 2.276 +CX_TEST(test_asprintf_large_string) { 2.277 + char *aaa = malloc(512); 2.278 + char *bbb = malloc(512); 2.279 + char *expected = malloc(1040); 2.280 + memset(aaa, 'a', 511); 2.281 + aaa[511] = 0; 2.282 + memset(bbb, 'b', 511); 2.283 + bbb[511] = 0; 2.284 + sprintf(expected, "After %s comes %s.", aaa, bbb); 2.285 + CX_TEST_DO { 2.286 + cxmutstr r = cx_asprintf("After %s comes %s.", aaa, bbb); 2.287 + CX_TEST_ASSERT(r.length == 1036); 2.288 + ASSERT_ZERO_TERMINATED(r); 2.289 + CX_TEST_ASSERT(0 == strcmp(r.ptr, expected)); 2.290 + cx_strfree(&r); 2.291 + } 2.292 + free(aaa); 2.293 + free(bbb); 2.294 + free(expected); 2.295 +} 2.296 + 2.297 +CxTestSuite *cx_test_suite_printf(void) { 2.298 + CxTestSuite *suite = cx_test_suite_new("printf"); 2.299 + 2.300 + cx_test_register(suite, test_bprintf); 2.301 + cx_test_register(suite, test_bprintf_large_string); 2.302 + cx_test_register(suite, test_bprintf_nocap); 2.303 + cx_test_register(suite, test_fprintf); 2.304 + cx_test_register(suite, test_asprintf); 2.305 + cx_test_register(suite, test_asprintf_large_string); 2.306 + 2.307 + return suite; 2.308 +}
3.1 --- a/tests/test_printf.cpp Fri Dec 29 17:17:43 2023 +0100 3.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 3.3 @@ -1,248 +0,0 @@ 3.4 -/* 3.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3.6 - * 3.7 - * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. 3.8 - * 3.9 - * Redistribution and use in source and binary forms, with or without 3.10 - * modification, are permitted provided that the following conditions are met: 3.11 - * 3.12 - * 1. Redistributions of source code must retain the above copyright 3.13 - * notice, this list of conditions and the following disclaimer. 3.14 - * 3.15 - * 2. Redistributions in binary form must reproduce the above copyright 3.16 - * notice, this list of conditions and the following disclaimer in the 3.17 - * documentation and/or other materials provided with the distribution. 3.18 - * 3.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 3.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 3.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 3.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 3.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 3.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 3.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 3.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 3.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 3.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 3.29 - * POSSIBILITY OF SUCH DAMAGE. 3.30 - */ 3.31 - 3.32 -#include "cx/printf.h" 3.33 -#include "cx/buffer.h" 3.34 - 3.35 -#include <gtest/gtest.h> 3.36 -#include "util_allocator.h" 3.37 - 3.38 -class PrintfFixture : public ::testing::Test { 3.39 -protected: 3.40 - std::string buf; 3.41 - CxTestingAllocator alloc; 3.42 - 3.43 - void TearDown() override { 3.44 - buf.clear(); 3.45 - ASSERT_TRUE(alloc.verify()); 3.46 - } 3.47 - 3.48 - static size_t write_func( 3.49 - void const *src, 3.50 - size_t esize, 3.51 - size_t ecount, 3.52 - void *target 3.53 - ) { 3.54 - auto str = reinterpret_cast<char const *>(src); 3.55 - auto buf = reinterpret_cast<std::string *>(target); 3.56 - EXPECT_EQ(esize, 1); 3.57 - EXPECT_EQ(strlen(str), ecount); 3.58 - *buf = str; 3.59 - return ecount; 3.60 - } 3.61 -}; 3.62 - 3.63 - 3.64 -TEST_F(PrintfFixture, BPrintf) { 3.65 - CxBuffer buf; 3.66 - cxBufferInit(&buf, nullptr, 64, &alloc, 0); 3.67 - 3.68 - auto r = cx_bprintf(&buf, "This %s aged %u years in a %2XSK.", "Test", 10, 0xca); 3.69 - EXPECT_EQ(r, 34); 3.70 - EXPECT_EQ(buf.size, 34); 3.71 - buf.space[r] = '\0'; 3.72 - EXPECT_STREQ(buf.space, "This Test aged 10 years in a CASK."); 3.73 - 3.74 - cxBufferDestroy(&buf); 3.75 -} 3.76 - 3.77 -TEST_F(PrintfFixture, FPrintf) { 3.78 - auto h = "Hello"; 3.79 - size_t r; 3.80 - 3.81 - r = cx_fprintf(&buf, PrintfFixture::write_func, "teststring"); 3.82 - EXPECT_EQ(r, 10); 3.83 - EXPECT_EQ(buf, "teststring"); 3.84 - 3.85 - r = cx_fprintf(&buf, PrintfFixture::write_func, "[%10s]", h); 3.86 - EXPECT_EQ(r, 12); 3.87 - EXPECT_EQ(buf, "[ Hello]"); 3.88 - 3.89 - r = cx_fprintf(&buf, PrintfFixture::write_func, "[%-10s]", h); 3.90 - EXPECT_EQ(r, 12); 3.91 - EXPECT_EQ(buf, "[Hello ]"); 3.92 - 3.93 - r = cx_fprintf(&buf, PrintfFixture::write_func, "[%*s]", 10, h); 3.94 - EXPECT_EQ(r, 12); 3.95 - EXPECT_EQ(buf, "[ Hello]"); 3.96 - 3.97 - r = cx_fprintf(&buf, PrintfFixture::write_func, "[%-10.*s]", 4, h); 3.98 - EXPECT_EQ(r, 12); 3.99 - EXPECT_EQ(buf, "[Hell ]"); 3.100 - 3.101 - r = cx_fprintf(&buf, PrintfFixture::write_func, "[%-*.*s]", 10, 4, h); 3.102 - EXPECT_EQ(r, 12); 3.103 - EXPECT_EQ(buf, "[Hell ]"); 3.104 - 3.105 - r = cx_fprintf(&buf, PrintfFixture::write_func, "%c", 'A'); 3.106 - EXPECT_EQ(r, 1); 3.107 - EXPECT_EQ(buf, "A"); 3.108 - 3.109 - r = cx_fprintf(&buf, PrintfFixture::write_func, "%i %d %.6i %i %.0i %+i %i", 1, 2, 3, 0, 0, 4, -4); 3.110 - EXPECT_EQ(r, 19); 3.111 - EXPECT_EQ(buf, "1 2 000003 0 +4 -4"); 3.112 - 3.113 - r = cx_fprintf(&buf, PrintfFixture::write_func, "%x %x %X %#x", 5, 10, 10, 6); 3.114 - EXPECT_EQ(r, 9); 3.115 - EXPECT_EQ(buf, "5 a A 0x6"); 3.116 - 3.117 - r = cx_fprintf(&buf, PrintfFixture::write_func, "%o %#o %#o", 10, 10, 4); 3.118 - EXPECT_EQ(r, 9); 3.119 - EXPECT_EQ(buf, "12 012 04"); 3.120 - 3.121 - r = cx_fprintf(&buf, PrintfFixture::write_func, "%05.2f %.2f %5.2f", 1.5, 1.5, 1.5); 3.122 - EXPECT_EQ(r, 16); 3.123 - EXPECT_EQ(buf, "01.50 1.50 1.50"); 3.124 - 3.125 - r = cx_fprintf(&buf, PrintfFixture::write_func, "'%*c'", 5, 'x'); 3.126 - EXPECT_EQ(r, 7); 3.127 - EXPECT_EQ(buf, "' x'"); 3.128 - 3.129 - r = cx_fprintf(&buf, PrintfFixture::write_func, "'%*c'", -5, 'x'); 3.130 - EXPECT_EQ(r, 7); 3.131 - EXPECT_EQ(buf, "'x '"); 3.132 -} 3.133 - 3.134 -TEST_F(PrintfFixture, BPrintfLargeString) { 3.135 - CxBuffer buf; 3.136 - cxBufferInit(&buf, nullptr, 64, &alloc, CX_BUFFER_AUTO_EXTEND); 3.137 - 3.138 - auto aaa = std::string(512, 'a'); 3.139 - auto bbb = std::string(512, 'b'); 3.140 - 3.141 - auto r = cx_bprintf(&buf, "After %s comes %s.", aaa.data(), bbb.data()); 3.142 - EXPECT_EQ(r, 1038); 3.143 - EXPECT_EQ(buf.size, 1038); 3.144 - cxBufferPut(&buf, 0); 3.145 - EXPECT_EQ(buf.space, std::string("After ") + aaa + " comes " + bbb + "."); 3.146 - 3.147 - cxBufferDestroy(&buf); 3.148 -} 3.149 - 3.150 -TEST_F(PrintfFixture, BPrintfNoCap) { 3.151 - CxBuffer buf; 3.152 - char space[20]; 3.153 - memset(space, 'a', 20); 3.154 - cxBufferInit(&buf, space, 16, &alloc, 0); 3.155 - 3.156 - auto r = cx_bprintf(&buf, "Hello %s with more than %d chars.", "string", 16); 3.157 - EXPECT_EQ(r, 16); 3.158 - EXPECT_EQ(buf.size, 16); 3.159 - EXPECT_EQ(0, memcmp(space, "Hello string witaaaa", 20)); 3.160 - 3.161 - cxBufferDestroy(&buf); 3.162 -} 3.163 - 3.164 -TEST_F(PrintfFixture, SPrintf) { 3.165 - auto h = "Hello"; 3.166 - 3.167 - std::vector<char *> fl; 3.168 - cxmutstr r; 3.169 - 3.170 - r = cx_asprintf_a(&alloc, "teststring"); 3.171 - EXPECT_EQ(r.length, 10); 3.172 - EXPECT_STREQ(r.ptr, "teststring"); 3.173 - fl.push_back(r.ptr); 3.174 - 3.175 - r = cx_asprintf_a(&alloc, "[%10s]", h); 3.176 - EXPECT_EQ(r.length, 12); 3.177 - EXPECT_STREQ(r.ptr, "[ Hello]"); 3.178 - fl.push_back(r.ptr); 3.179 - 3.180 - r = cx_asprintf_a(&alloc, "[%-10s]", h); 3.181 - EXPECT_EQ(r.length, 12); 3.182 - EXPECT_STREQ(r.ptr, "[Hello ]"); 3.183 - fl.push_back(r.ptr); 3.184 - 3.185 - r = cx_asprintf_a(&alloc, "[%*s]", 10, h); 3.186 - EXPECT_EQ(r.length, 12); 3.187 - EXPECT_STREQ(r.ptr, "[ Hello]"); 3.188 - fl.push_back(r.ptr); 3.189 - 3.190 - r = cx_asprintf_a(&alloc, "[%-10.*s]", 4, h); 3.191 - EXPECT_EQ(r.length, 12); 3.192 - EXPECT_STREQ(r.ptr, "[Hell ]"); 3.193 - fl.push_back(r.ptr); 3.194 - 3.195 - r = cx_asprintf_a(&alloc, "[%-*.*s]", 10, 4, h); 3.196 - EXPECT_EQ(r.length, 12); 3.197 - EXPECT_STREQ(r.ptr, "[Hell ]"); 3.198 - fl.push_back(r.ptr); 3.199 - 3.200 - r = cx_asprintf_a(&alloc, "%c", 'A'); 3.201 - EXPECT_EQ(r.length, 1); 3.202 - EXPECT_STREQ(r.ptr, "A"); 3.203 - fl.push_back(r.ptr); 3.204 - 3.205 - r = cx_asprintf_a(&alloc, "%i %d %.6i %i %.0i %+i %i", 1, 2, 3, 0, 0, 4, -4); 3.206 - EXPECT_EQ(r.length, 19); 3.207 - EXPECT_STREQ(r.ptr, "1 2 000003 0 +4 -4"); 3.208 - fl.push_back(r.ptr); 3.209 - 3.210 - r = cx_asprintf_a(&alloc, "%x %x %X %#x", 5, 10, 10, 6); 3.211 - EXPECT_EQ(r.length, 9); 3.212 - EXPECT_STREQ(r.ptr, "5 a A 0x6"); 3.213 - fl.push_back(r.ptr); 3.214 - 3.215 - r = cx_asprintf_a(&alloc, "%o %#o %#o", 10, 10, 4); 3.216 - EXPECT_EQ(r.length, 9); 3.217 - EXPECT_STREQ(r.ptr, "12 012 04"); 3.218 - fl.push_back(r.ptr); 3.219 - 3.220 - r = cx_asprintf_a(&alloc, "%05.2f %.2f %5.2f", 1.5, 1.5, 1.5); 3.221 - EXPECT_EQ(r.length, 16); 3.222 - EXPECT_STREQ(r.ptr, "01.50 1.50 1.50"); 3.223 - fl.push_back(r.ptr); 3.224 - 3.225 - r = cx_asprintf_a(&alloc, "'%*c'", 5, 'x'); 3.226 - EXPECT_EQ(r.length, 7); 3.227 - EXPECT_STREQ(r.ptr, "' x'"); 3.228 - fl.push_back(r.ptr); 3.229 - 3.230 - r = cx_asprintf_a(&alloc, "'%*c'", -5, 'x'); 3.231 - EXPECT_EQ(r.length, 7); 3.232 - EXPECT_STREQ(r.ptr, "'x '"); 3.233 - fl.push_back(r.ptr); 3.234 - 3.235 - for (auto c: fl) { 3.236 - auto s = cx_mutstrn(c, 0); 3.237 - cx_strfree_a(&alloc, &s); 3.238 - } 3.239 -} 3.240 - 3.241 -TEST_F(PrintfFixture, SPrintfLargeString) { 3.242 - auto aaa = std::string(512, 'a'); 3.243 - auto bbb = std::string(512, 'b'); 3.244 - 3.245 - auto r = cx_asprintf_a(&alloc, "After %s comes %s.", aaa.data(), bbb.data()); 3.246 - EXPECT_EQ(r.length, 1038); 3.247 - EXPECT_EQ(r.ptr, std::string("After ") + aaa + " comes " + bbb + "."); 3.248 - EXPECT_EQ(r.ptr[1038], '\0'); 3.249 - 3.250 - cx_strfree_a(&alloc, &r); 3.251 -} 3.252 \ No newline at end of file
4.1 --- a/tests/ucxtest.c Fri Dec 29 17:17:43 2023 +0100 4.2 +++ b/tests/ucxtest.c Fri Dec 29 17:27:14 2023 +0100 4.3 @@ -31,8 +31,9 @@ 4.4 CxTestSuite *cx_test_suite_testing_allocator(void); 4.5 CxTestSuite *cx_test_suite_utils(void); 4.6 CxTestSuite *cx_test_suite_hash_key(void); 4.7 +CxTestSuite *cx_test_suite_string(void); 4.8 4.9 -CxTestSuite *cx_test_suite_string(void); 4.10 +CxTestSuite *cx_test_suite_printf(void); 4.11 4.12 #define run_tests(suite) cx_test_run_stdout(suite); success += (suite)->success; failure += (suite)->failure 4.13 #define execute_test_suites(...) unsigned success = 0, failure = 0; CxTestSuite* test_suites[] = {__VA_ARGS__}; \ 4.14 @@ -46,7 +47,8 @@ 4.15 cx_test_suite_testing_allocator(), 4.16 cx_test_suite_utils(), 4.17 cx_test_suite_hash_key(), 4.18 - cx_test_suite_string() 4.19 + cx_test_suite_string(), 4.20 + cx_test_suite_printf() 4.21 ); 4.22 printf("=== OVERALL RESULT ===\n"); 4.23 printf(" Total: %u\n Success: %u\n Failure: %u\n",