Fri, 05 Jan 2024 20:35:13 +0100
update uwproj configure template
universe@780 | 1 | /* |
universe@780 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@780 | 3 | * |
universe@780 | 4 | * Copyright 2023 Mike Becker, Olaf Wintermann All rights reserved. |
universe@780 | 5 | * |
universe@780 | 6 | * Redistribution and use in source and binary forms, with or without |
universe@780 | 7 | * modification, are permitted provided that the following conditions are met: |
universe@780 | 8 | * |
universe@780 | 9 | * 1. Redistributions of source code must retain the above copyright |
universe@780 | 10 | * notice, this list of conditions and the following disclaimer. |
universe@780 | 11 | * |
universe@780 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@780 | 13 | * notice, this list of conditions and the following disclaimer in the |
universe@780 | 14 | * documentation and/or other materials provided with the distribution. |
universe@780 | 15 | * |
universe@780 | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@780 | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@780 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@780 | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@780 | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@780 | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@780 | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@780 | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@780 | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@780 | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@780 | 26 | * POSSIBILITY OF SUCH DAMAGE. |
universe@780 | 27 | */ |
universe@780 | 28 | |
universe@780 | 29 | #include "cx/test.h" |
universe@780 | 30 | #include "util_allocator.h" |
universe@780 | 31 | |
universe@780 | 32 | #include "cx/printf.h" |
universe@780 | 33 | #include "cx/buffer.h" |
universe@780 | 34 | |
universe@780 | 35 | #define ASSERT_ZERO_TERMINATED(str) CX_TEST_ASSERTM((str).ptr[(str).length] == '\0', \ |
universe@780 | 36 | #str " is not zero terminated") |
universe@780 | 37 | |
universe@780 | 38 | static size_t test_printf_write_func( |
universe@780 | 39 | void const *src, |
universe@780 | 40 | size_t esize, |
universe@780 | 41 | size_t ecount, |
universe@780 | 42 | void *target |
universe@780 | 43 | ) { |
universe@780 | 44 | memcpy(target, src, esize * ecount); |
universe@780 | 45 | return esize * ecount; |
universe@780 | 46 | } |
universe@780 | 47 | |
universe@780 | 48 | CX_TEST(test_bprintf) { |
universe@780 | 49 | CxTestingAllocator talloc; |
universe@780 | 50 | cx_testing_allocator_init(&talloc); |
universe@780 | 51 | CxAllocator *alloc = &talloc.base; |
universe@780 | 52 | CX_TEST_DO { |
universe@780 | 53 | CxBuffer buf; |
universe@780 | 54 | cxBufferInit(&buf, NULL, 64, alloc, 0); |
universe@780 | 55 | size_t r = cx_bprintf(&buf, "This %s aged %u years in a %2XSK.", "Test", 10, 0xca); |
universe@780 | 56 | CX_TEST_ASSERT(r == 34); |
universe@780 | 57 | CX_TEST_ASSERT(buf.size == 34); |
universe@780 | 58 | buf.space[r] = '\0'; |
universe@780 | 59 | CX_TEST_ASSERT(0 == strcmp(buf.space, "This Test aged 10 years in a CASK.")); |
universe@780 | 60 | cxBufferDestroy(&buf); |
universe@780 | 61 | CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
universe@780 | 62 | } |
universe@780 | 63 | cx_testing_allocator_destroy(&talloc); |
universe@780 | 64 | } |
universe@780 | 65 | |
universe@780 | 66 | CX_TEST(test_bprintf_large_string) { |
universe@780 | 67 | CxTestingAllocator talloc; |
universe@780 | 68 | cx_testing_allocator_init(&talloc); |
universe@780 | 69 | CxAllocator *alloc = &talloc.base; |
universe@780 | 70 | char *aaa = malloc(512); |
universe@780 | 71 | char *bbb = malloc(512); |
universe@780 | 72 | char *expected = malloc(1040); |
universe@780 | 73 | memset(aaa, 'a', 511); |
universe@780 | 74 | aaa[511] = 0; |
universe@780 | 75 | memset(bbb, 'b', 511); |
universe@780 | 76 | bbb[511] = 0; |
universe@780 | 77 | sprintf(expected, "After %s comes %s.", aaa, bbb); |
universe@780 | 78 | CX_TEST_DO { |
universe@780 | 79 | CxBuffer buf; |
universe@780 | 80 | cxBufferInit(&buf, NULL, 64, alloc, CX_BUFFER_AUTO_EXTEND); |
universe@780 | 81 | size_t r = cx_bprintf(&buf, "After %s comes %s.", aaa, bbb); |
universe@780 | 82 | CX_TEST_ASSERT(r == 1036); |
universe@780 | 83 | CX_TEST_ASSERT(buf.size == 1036); |
universe@780 | 84 | cxBufferPut(&buf, 0); |
universe@780 | 85 | CX_TEST_ASSERT(0 == strcmp(expected, buf.space)); |
universe@780 | 86 | cxBufferDestroy(&buf); |
universe@780 | 87 | CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
universe@780 | 88 | } |
universe@780 | 89 | free(aaa); |
universe@780 | 90 | free(bbb); |
universe@780 | 91 | free(expected); |
universe@780 | 92 | cx_testing_allocator_destroy(&talloc); |
universe@780 | 93 | } |
universe@780 | 94 | |
universe@780 | 95 | CX_TEST(test_bprintf_nocap) { |
universe@780 | 96 | CxTestingAllocator talloc; |
universe@780 | 97 | cx_testing_allocator_init(&talloc); |
universe@780 | 98 | CxAllocator *alloc = &talloc.base; |
universe@780 | 99 | char space[20]; |
universe@780 | 100 | memset(space, 'a', 20); |
universe@780 | 101 | CX_TEST_DO { |
universe@780 | 102 | CxBuffer buf; |
universe@780 | 103 | cxBufferInit(&buf, space, 16, alloc, 0); |
universe@780 | 104 | size_t r = cx_bprintf(&buf, "Hello %s with more than %d chars.", "string", 16); |
universe@780 | 105 | CX_TEST_ASSERT(r == 16); |
universe@780 | 106 | CX_TEST_ASSERT(buf.size == 16); |
universe@780 | 107 | CX_TEST_ASSERT(0 == memcmp(space, "Hello string witaaaa", 20)); |
universe@780 | 108 | cxBufferDestroy(&buf); |
universe@780 | 109 | CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
universe@780 | 110 | } |
universe@780 | 111 | cx_testing_allocator_destroy(&talloc); |
universe@780 | 112 | } |
universe@780 | 113 | |
universe@780 | 114 | CX_TEST(test_fprintf) { |
universe@780 | 115 | char const *h = "Hello"; |
universe@780 | 116 | char buf[32]; |
universe@780 | 117 | size_t r; |
universe@780 | 118 | CX_TEST_DO { |
universe@780 | 119 | r = cx_fprintf(buf, test_printf_write_func, "teststring"); |
universe@780 | 120 | CX_TEST_ASSERT(r == 10); |
universe@780 | 121 | CX_TEST_ASSERT(0 == memcmp(buf, "teststring", r)); |
universe@780 | 122 | |
universe@780 | 123 | r = cx_fprintf(buf, test_printf_write_func, "[%10s]", h); |
universe@780 | 124 | CX_TEST_ASSERT(r == 12); |
universe@780 | 125 | CX_TEST_ASSERT(0 == memcmp(buf, "[ Hello]", r)); |
universe@780 | 126 | |
universe@780 | 127 | r = cx_fprintf(buf, test_printf_write_func, "[%-10s]", h); |
universe@780 | 128 | CX_TEST_ASSERT(r == 12); |
universe@780 | 129 | CX_TEST_ASSERT(0 == memcmp(buf, "[Hello ]", r)); |
universe@780 | 130 | |
universe@780 | 131 | r = cx_fprintf(buf, test_printf_write_func, "[%*s]", 10, h); |
universe@780 | 132 | CX_TEST_ASSERT(r == 12); |
universe@780 | 133 | CX_TEST_ASSERT(0 == memcmp(buf, "[ Hello]", r)); |
universe@780 | 134 | |
universe@780 | 135 | r = cx_fprintf(buf, test_printf_write_func, "[%-10.*s]", 4, h); |
universe@780 | 136 | CX_TEST_ASSERT(r == 12); |
universe@780 | 137 | CX_TEST_ASSERT(0 == memcmp(buf, "[Hell ]", r)); |
universe@780 | 138 | |
universe@780 | 139 | r = cx_fprintf(buf, test_printf_write_func, "[%-*.*s]", 10, 4, h); |
universe@780 | 140 | CX_TEST_ASSERT(r == 12); |
universe@780 | 141 | CX_TEST_ASSERT(0 == memcmp(buf, "[Hell ]", r)); |
universe@780 | 142 | |
universe@780 | 143 | r = cx_fprintf(buf, test_printf_write_func, "%c", 'A'); |
universe@780 | 144 | CX_TEST_ASSERT(r == 1); |
universe@780 | 145 | CX_TEST_ASSERT(0 == memcmp(buf, "A", r)); |
universe@780 | 146 | |
universe@780 | 147 | r = cx_fprintf(buf, test_printf_write_func, "%i %d %.6i %i %.0i %+i %i", 1, 2, 3, 0, 0, 4, -4); |
universe@780 | 148 | CX_TEST_ASSERT(r == 19); |
universe@780 | 149 | CX_TEST_ASSERT(0 == memcmp(buf, "1 2 000003 0 +4 -4", r)); |
universe@780 | 150 | |
universe@780 | 151 | r = cx_fprintf(buf, test_printf_write_func, "%x %x %X %#x", 5, 10, 10, 6); |
universe@780 | 152 | CX_TEST_ASSERT(r == 9); |
universe@780 | 153 | CX_TEST_ASSERT(0 == memcmp(buf, "5 a A 0x6", r)); |
universe@780 | 154 | |
universe@780 | 155 | r = cx_fprintf(buf, test_printf_write_func, "%o %#o %#o", 10, 10, 4); |
universe@780 | 156 | CX_TEST_ASSERT(r == 9); |
universe@780 | 157 | CX_TEST_ASSERT(0 == memcmp(buf, "12 012 04", r)); |
universe@780 | 158 | |
universe@780 | 159 | r = cx_fprintf(buf, test_printf_write_func, "%05.2f %.2f %5.2f", 1.5, 1.5, 1.5); |
universe@780 | 160 | CX_TEST_ASSERT(r == 16); |
universe@780 | 161 | CX_TEST_ASSERT(0 == memcmp(buf, "01.50 1.50 1.50", r)); |
universe@780 | 162 | |
universe@780 | 163 | r = cx_fprintf(buf, test_printf_write_func, "'%*c'", 5, 'x'); |
universe@780 | 164 | CX_TEST_ASSERT(r == 7); |
universe@780 | 165 | CX_TEST_ASSERT(0 == memcmp(buf, "' x'", r)); |
universe@780 | 166 | |
universe@780 | 167 | r = cx_fprintf(buf, test_printf_write_func, "'%*c'", -5, 'x'); |
universe@780 | 168 | CX_TEST_ASSERT(r == 7); |
universe@780 | 169 | CX_TEST_ASSERT(0 == memcmp(buf, "'x '", r)); |
universe@780 | 170 | } |
universe@780 | 171 | } |
universe@780 | 172 | |
universe@780 | 173 | CX_TEST(test_asprintf) { |
universe@780 | 174 | CxTestingAllocator talloc; |
universe@780 | 175 | cx_testing_allocator_init(&talloc); |
universe@780 | 176 | CxAllocator *alloc = &talloc.base; |
universe@780 | 177 | |
universe@780 | 178 | char const *h = "Hello"; |
universe@780 | 179 | |
universe@780 | 180 | int const specimen_count = 13; |
universe@780 | 181 | cxmutstr r[specimen_count]; |
universe@780 | 182 | int specimen = 0; |
universe@780 | 183 | |
universe@780 | 184 | CX_TEST_DO { |
universe@780 | 185 | r[specimen] = cx_asprintf_a(alloc, "teststring"); |
universe@780 | 186 | CX_TEST_ASSERT(r[specimen].length == 10); |
universe@780 | 187 | ASSERT_ZERO_TERMINATED(r[specimen]); |
universe@780 | 188 | CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "teststring")); |
universe@780 | 189 | specimen++; |
universe@780 | 190 | |
universe@780 | 191 | r[specimen] = cx_asprintf_a(alloc, "[%10s]", h); |
universe@780 | 192 | CX_TEST_ASSERT(r[specimen].length == 12); |
universe@780 | 193 | ASSERT_ZERO_TERMINATED(r[specimen]); |
universe@780 | 194 | CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "[ Hello]")); |
universe@780 | 195 | specimen++; |
universe@780 | 196 | |
universe@780 | 197 | r[specimen] = cx_asprintf_a(alloc, "[%-10s]", h); |
universe@780 | 198 | CX_TEST_ASSERT(r[specimen].length == 12); |
universe@780 | 199 | ASSERT_ZERO_TERMINATED(r[specimen]); |
universe@780 | 200 | CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "[Hello ]")); |
universe@780 | 201 | specimen++; |
universe@780 | 202 | |
universe@780 | 203 | r[specimen] = cx_asprintf_a(alloc, "[%*s]", 10, h); |
universe@780 | 204 | CX_TEST_ASSERT(r[specimen].length == 12); |
universe@780 | 205 | ASSERT_ZERO_TERMINATED(r[specimen]); |
universe@780 | 206 | CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "[ Hello]")); |
universe@780 | 207 | specimen++; |
universe@780 | 208 | |
universe@780 | 209 | r[specimen] = cx_asprintf_a(alloc, "[%-10.*s]", 4, h); |
universe@780 | 210 | CX_TEST_ASSERT(r[specimen].length == 12); |
universe@780 | 211 | ASSERT_ZERO_TERMINATED(r[specimen]); |
universe@780 | 212 | CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "[Hell ]")); |
universe@780 | 213 | specimen++; |
universe@780 | 214 | |
universe@780 | 215 | r[specimen] = cx_asprintf_a(alloc, "[%-*.*s]", 10, 4, h); |
universe@780 | 216 | CX_TEST_ASSERT(r[specimen].length == 12); |
universe@780 | 217 | ASSERT_ZERO_TERMINATED(r[specimen]); |
universe@780 | 218 | CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "[Hell ]")); |
universe@780 | 219 | specimen++; |
universe@780 | 220 | |
universe@780 | 221 | r[specimen] = cx_asprintf_a(alloc, "%c", 'A'); |
universe@780 | 222 | CX_TEST_ASSERT(r[specimen].length == 1); |
universe@780 | 223 | ASSERT_ZERO_TERMINATED(r[specimen]); |
universe@780 | 224 | CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "A")); |
universe@780 | 225 | specimen++; |
universe@780 | 226 | |
universe@780 | 227 | r[specimen] = cx_asprintf_a(alloc, "%i %d %.6i %i %.0i %+i %i", 1, 2, 3, 0, 0, 4, -4); |
universe@780 | 228 | CX_TEST_ASSERT(r[specimen].length == 19); |
universe@780 | 229 | ASSERT_ZERO_TERMINATED(r[specimen]); |
universe@780 | 230 | CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "1 2 000003 0 +4 -4")); |
universe@780 | 231 | specimen++; |
universe@780 | 232 | |
universe@780 | 233 | r[specimen] = cx_asprintf_a(alloc, "%x %x %X %#x", 5, 10, 10, 6); |
universe@780 | 234 | CX_TEST_ASSERT(r[specimen].length == 9); |
universe@780 | 235 | ASSERT_ZERO_TERMINATED(r[specimen]); |
universe@780 | 236 | CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "5 a A 0x6")); |
universe@780 | 237 | specimen++; |
universe@780 | 238 | |
universe@780 | 239 | r[specimen] = cx_asprintf_a(alloc, "%o %#o %#o", 10, 10, 4); |
universe@780 | 240 | CX_TEST_ASSERT(r[specimen].length == 9); |
universe@780 | 241 | ASSERT_ZERO_TERMINATED(r[specimen]); |
universe@780 | 242 | CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "12 012 04")); |
universe@780 | 243 | specimen++; |
universe@780 | 244 | |
universe@780 | 245 | r[specimen] = cx_asprintf_a(alloc, "%05.2f %.2f %5.2f", 1.5, 1.5, 1.5); |
universe@780 | 246 | CX_TEST_ASSERT(r[specimen].length == 16); |
universe@780 | 247 | ASSERT_ZERO_TERMINATED(r[specimen]); |
universe@780 | 248 | CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "01.50 1.50 1.50")); |
universe@780 | 249 | specimen++; |
universe@780 | 250 | |
universe@780 | 251 | r[specimen] = cx_asprintf_a(alloc, "'%*c'", 5, 'x'); |
universe@780 | 252 | CX_TEST_ASSERT(r[specimen].length == 7); |
universe@780 | 253 | ASSERT_ZERO_TERMINATED(r[specimen]); |
universe@780 | 254 | CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "' x'")); |
universe@780 | 255 | specimen++; |
universe@780 | 256 | |
universe@780 | 257 | r[specimen] = cx_asprintf_a(alloc, "'%*c'", -5, 'x'); |
universe@780 | 258 | CX_TEST_ASSERT(r[specimen].length == 7); |
universe@780 | 259 | ASSERT_ZERO_TERMINATED(r[specimen]); |
universe@780 | 260 | CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "'x '")); |
universe@780 | 261 | specimen++; |
universe@780 | 262 | |
universe@780 | 263 | CX_TEST_ASSERT(specimen == specimen_count); // self-test |
universe@780 | 264 | |
universe@780 | 265 | for (int i = 0; i < specimen_count; i++) { |
universe@780 | 266 | cx_strfree_a(alloc, &r[i]); |
universe@780 | 267 | } |
universe@780 | 268 | CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
universe@780 | 269 | } |
universe@780 | 270 | cx_testing_allocator_destroy(&talloc); |
universe@780 | 271 | } |
universe@780 | 272 | |
universe@780 | 273 | CX_TEST(test_asprintf_large_string) { |
universe@780 | 274 | char *aaa = malloc(512); |
universe@780 | 275 | char *bbb = malloc(512); |
universe@780 | 276 | char *expected = malloc(1040); |
universe@780 | 277 | memset(aaa, 'a', 511); |
universe@780 | 278 | aaa[511] = 0; |
universe@780 | 279 | memset(bbb, 'b', 511); |
universe@780 | 280 | bbb[511] = 0; |
universe@780 | 281 | sprintf(expected, "After %s comes %s.", aaa, bbb); |
universe@780 | 282 | CX_TEST_DO { |
universe@780 | 283 | cxmutstr r = cx_asprintf("After %s comes %s.", aaa, bbb); |
universe@780 | 284 | CX_TEST_ASSERT(r.length == 1036); |
universe@780 | 285 | ASSERT_ZERO_TERMINATED(r); |
universe@780 | 286 | CX_TEST_ASSERT(0 == strcmp(r.ptr, expected)); |
universe@780 | 287 | cx_strfree(&r); |
universe@780 | 288 | } |
universe@780 | 289 | free(aaa); |
universe@780 | 290 | free(bbb); |
universe@780 | 291 | free(expected); |
universe@780 | 292 | } |
universe@780 | 293 | |
universe@780 | 294 | CxTestSuite *cx_test_suite_printf(void) { |
universe@780 | 295 | CxTestSuite *suite = cx_test_suite_new("printf"); |
universe@780 | 296 | |
universe@780 | 297 | cx_test_register(suite, test_bprintf); |
universe@780 | 298 | cx_test_register(suite, test_bprintf_large_string); |
universe@780 | 299 | cx_test_register(suite, test_bprintf_nocap); |
universe@780 | 300 | cx_test_register(suite, test_fprintf); |
universe@780 | 301 | cx_test_register(suite, test_asprintf); |
universe@780 | 302 | cx_test_register(suite, test_asprintf_large_string); |
universe@780 | 303 | |
universe@780 | 304 | return suite; |
universe@780 | 305 | } |