tests/test_printf.c

Wed, 14 Feb 2024 21:43:32 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 14 Feb 2024 21:43:32 +0100
changeset 823
f4faa7f73cb8
parent 810
85859399a0cc
child 849
edb9f875b7f9
permissions
-rw-r--r--

declare cx_tree_search_func function pointer

780
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 /*
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 *
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 * Copyright 2023 Mike Becker, Olaf Wintermann All rights reserved.
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 *
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 *
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 *
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 *
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 */
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29 #include "cx/test.h"
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30 #include "util_allocator.h"
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32 #include "cx/printf.h"
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
33 #include "cx/buffer.h"
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
34
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
35 #define ASSERT_ZERO_TERMINATED(str) CX_TEST_ASSERTM((str).ptr[(str).length] == '\0', \
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
36 #str " is not zero terminated")
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38 static size_t test_printf_write_func(
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39 void const *src,
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
40 size_t esize,
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41 size_t ecount,
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
42 void *target
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
43 ) {
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
44 memcpy(target, src, esize * ecount);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
45 return esize * ecount;
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
46 }
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
47
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
48 CX_TEST(test_bprintf) {
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
49 CxTestingAllocator talloc;
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
50 cx_testing_allocator_init(&talloc);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
51 CxAllocator *alloc = &talloc.base;
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
52 CX_TEST_DO {
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
53 CxBuffer buf;
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54 cxBufferInit(&buf, NULL, 64, alloc, 0);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55 size_t r = cx_bprintf(&buf, "This %s aged %u years in a %2XSK.", "Test", 10, 0xca);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56 CX_TEST_ASSERT(r == 34);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57 CX_TEST_ASSERT(buf.size == 34);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58 buf.space[r] = '\0';
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59 CX_TEST_ASSERT(0 == strcmp(buf.space, "This Test aged 10 years in a CASK."));
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60 cxBufferDestroy(&buf);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
62 }
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
63 cx_testing_allocator_destroy(&talloc);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64 }
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
66 CX_TEST(test_bprintf_large_string) {
805
26500fc24058 add constant for reading out printf sbo size - relates to #343
Mike Becker <universe@uap-core.de>
parents: 780
diff changeset
67 unsigned len = cx_printf_sbo_size;
780
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
68 CxTestingAllocator talloc;
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
69 cx_testing_allocator_init(&talloc);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70 CxAllocator *alloc = &talloc.base;
805
26500fc24058 add constant for reading out printf sbo size - relates to #343
Mike Becker <universe@uap-core.de>
parents: 780
diff changeset
71 char *aaa = malloc(len);
26500fc24058 add constant for reading out printf sbo size - relates to #343
Mike Becker <universe@uap-core.de>
parents: 780
diff changeset
72 char *bbb = malloc(len);
26500fc24058 add constant for reading out printf sbo size - relates to #343
Mike Becker <universe@uap-core.de>
parents: 780
diff changeset
73 char *expected = malloc(2*len+16);
26500fc24058 add constant for reading out printf sbo size - relates to #343
Mike Becker <universe@uap-core.de>
parents: 780
diff changeset
74 memset(aaa, 'a', len-1);
26500fc24058 add constant for reading out printf sbo size - relates to #343
Mike Becker <universe@uap-core.de>
parents: 780
diff changeset
75 aaa[len-1] = 0;
26500fc24058 add constant for reading out printf sbo size - relates to #343
Mike Becker <universe@uap-core.de>
parents: 780
diff changeset
76 memset(bbb, 'b', len-1);
26500fc24058 add constant for reading out printf sbo size - relates to #343
Mike Becker <universe@uap-core.de>
parents: 780
diff changeset
77 bbb[len-1] = 0;
780
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
78 sprintf(expected, "After %s comes %s.", aaa, bbb);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
79 CX_TEST_DO {
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80 CxBuffer buf;
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81 cxBufferInit(&buf, NULL, 64, alloc, CX_BUFFER_AUTO_EXTEND);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82 size_t r = cx_bprintf(&buf, "After %s comes %s.", aaa, bbb);
805
26500fc24058 add constant for reading out printf sbo size - relates to #343
Mike Becker <universe@uap-core.de>
parents: 780
diff changeset
83 size_t er = 2*len-2+14;
26500fc24058 add constant for reading out printf sbo size - relates to #343
Mike Becker <universe@uap-core.de>
parents: 780
diff changeset
84 CX_TEST_ASSERT(r == er);
26500fc24058 add constant for reading out printf sbo size - relates to #343
Mike Becker <universe@uap-core.de>
parents: 780
diff changeset
85 CX_TEST_ASSERT(buf.size == er);
780
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86 cxBufferPut(&buf, 0);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87 CX_TEST_ASSERT(0 == strcmp(expected, buf.space));
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88 cxBufferDestroy(&buf);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
89 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
90 }
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
91 free(aaa);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
92 free(bbb);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
93 free(expected);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
94 cx_testing_allocator_destroy(&talloc);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
95 }
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
96
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
97 CX_TEST(test_bprintf_nocap) {
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
98 CxTestingAllocator talloc;
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
99 cx_testing_allocator_init(&talloc);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
100 CxAllocator *alloc = &talloc.base;
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
101 char space[20];
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
102 memset(space, 'a', 20);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
103 CX_TEST_DO {
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
104 CxBuffer buf;
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
105 cxBufferInit(&buf, space, 16, alloc, 0);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
106 size_t r = cx_bprintf(&buf, "Hello %s with more than %d chars.", "string", 16);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
107 CX_TEST_ASSERT(r == 16);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
108 CX_TEST_ASSERT(buf.size == 16);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
109 CX_TEST_ASSERT(0 == memcmp(space, "Hello string witaaaa", 20));
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
110 cxBufferDestroy(&buf);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
111 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
112 }
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
113 cx_testing_allocator_destroy(&talloc);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
114 }
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
115
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
116 CX_TEST(test_fprintf) {
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
117 char const *h = "Hello";
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
118 char buf[32];
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
119 size_t r;
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
120 CX_TEST_DO {
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
121 r = cx_fprintf(buf, test_printf_write_func, "teststring");
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
122 CX_TEST_ASSERT(r == 10);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
123 CX_TEST_ASSERT(0 == memcmp(buf, "teststring", r));
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
124
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
125 r = cx_fprintf(buf, test_printf_write_func, "[%10s]", h);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
126 CX_TEST_ASSERT(r == 12);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
127 CX_TEST_ASSERT(0 == memcmp(buf, "[ Hello]", r));
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
128
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
129 r = cx_fprintf(buf, test_printf_write_func, "[%-10s]", h);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
130 CX_TEST_ASSERT(r == 12);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
131 CX_TEST_ASSERT(0 == memcmp(buf, "[Hello ]", r));
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
132
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
133 r = cx_fprintf(buf, test_printf_write_func, "[%*s]", 10, h);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
134 CX_TEST_ASSERT(r == 12);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
135 CX_TEST_ASSERT(0 == memcmp(buf, "[ Hello]", r));
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
136
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
137 r = cx_fprintf(buf, test_printf_write_func, "[%-10.*s]", 4, h);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
138 CX_TEST_ASSERT(r == 12);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
139 CX_TEST_ASSERT(0 == memcmp(buf, "[Hell ]", r));
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
140
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
141 r = cx_fprintf(buf, test_printf_write_func, "[%-*.*s]", 10, 4, h);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
142 CX_TEST_ASSERT(r == 12);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
143 CX_TEST_ASSERT(0 == memcmp(buf, "[Hell ]", r));
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
144
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
145 r = cx_fprintf(buf, test_printf_write_func, "%c", 'A');
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
146 CX_TEST_ASSERT(r == 1);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
147 CX_TEST_ASSERT(0 == memcmp(buf, "A", r));
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
148
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
149 r = cx_fprintf(buf, test_printf_write_func, "%i %d %.6i %i %.0i %+i %i", 1, 2, 3, 0, 0, 4, -4);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
150 CX_TEST_ASSERT(r == 19);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
151 CX_TEST_ASSERT(0 == memcmp(buf, "1 2 000003 0 +4 -4", r));
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
152
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
153 r = cx_fprintf(buf, test_printf_write_func, "%x %x %X %#x", 5, 10, 10, 6);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
154 CX_TEST_ASSERT(r == 9);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
155 CX_TEST_ASSERT(0 == memcmp(buf, "5 a A 0x6", r));
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
156
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
157 r = cx_fprintf(buf, test_printf_write_func, "%o %#o %#o", 10, 10, 4);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
158 CX_TEST_ASSERT(r == 9);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
159 CX_TEST_ASSERT(0 == memcmp(buf, "12 012 04", r));
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
160
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
161 r = cx_fprintf(buf, test_printf_write_func, "%05.2f %.2f %5.2f", 1.5, 1.5, 1.5);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
162 CX_TEST_ASSERT(r == 16);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
163 CX_TEST_ASSERT(0 == memcmp(buf, "01.50 1.50 1.50", r));
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
164
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
165 r = cx_fprintf(buf, test_printf_write_func, "'%*c'", 5, 'x');
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
166 CX_TEST_ASSERT(r == 7);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
167 CX_TEST_ASSERT(0 == memcmp(buf, "' x'", r));
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
168
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
169 r = cx_fprintf(buf, test_printf_write_func, "'%*c'", -5, 'x');
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
170 CX_TEST_ASSERT(r == 7);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
171 CX_TEST_ASSERT(0 == memcmp(buf, "'x '", r));
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
172 }
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
173 }
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
174
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
175 CX_TEST(test_asprintf) {
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
176 CxTestingAllocator talloc;
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
177 cx_testing_allocator_init(&talloc);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
178 CxAllocator *alloc = &talloc.base;
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
179
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
180 char const *h = "Hello";
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
181
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
182 int const specimen_count = 13;
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
183 cxmutstr r[specimen_count];
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
184 int specimen = 0;
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
185
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
186 CX_TEST_DO {
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
187 r[specimen] = cx_asprintf_a(alloc, "teststring");
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
188 CX_TEST_ASSERT(r[specimen].length == 10);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
189 ASSERT_ZERO_TERMINATED(r[specimen]);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
190 CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "teststring"));
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
191 specimen++;
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
192
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
193 r[specimen] = cx_asprintf_a(alloc, "[%10s]", h);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
194 CX_TEST_ASSERT(r[specimen].length == 12);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
195 ASSERT_ZERO_TERMINATED(r[specimen]);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
196 CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "[ Hello]"));
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
197 specimen++;
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
198
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
199 r[specimen] = cx_asprintf_a(alloc, "[%-10s]", h);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
200 CX_TEST_ASSERT(r[specimen].length == 12);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
201 ASSERT_ZERO_TERMINATED(r[specimen]);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
202 CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "[Hello ]"));
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
203 specimen++;
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
204
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
205 r[specimen] = cx_asprintf_a(alloc, "[%*s]", 10, h);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
206 CX_TEST_ASSERT(r[specimen].length == 12);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
207 ASSERT_ZERO_TERMINATED(r[specimen]);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
208 CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "[ Hello]"));
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
209 specimen++;
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
210
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
211 r[specimen] = cx_asprintf_a(alloc, "[%-10.*s]", 4, h);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
212 CX_TEST_ASSERT(r[specimen].length == 12);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
213 ASSERT_ZERO_TERMINATED(r[specimen]);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
214 CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "[Hell ]"));
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
215 specimen++;
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
216
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
217 r[specimen] = cx_asprintf_a(alloc, "[%-*.*s]", 10, 4, h);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
218 CX_TEST_ASSERT(r[specimen].length == 12);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
219 ASSERT_ZERO_TERMINATED(r[specimen]);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
220 CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "[Hell ]"));
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
221 specimen++;
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
222
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
223 r[specimen] = cx_asprintf_a(alloc, "%c", 'A');
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
224 CX_TEST_ASSERT(r[specimen].length == 1);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
225 ASSERT_ZERO_TERMINATED(r[specimen]);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
226 CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "A"));
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
227 specimen++;
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
228
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
229 r[specimen] = cx_asprintf_a(alloc, "%i %d %.6i %i %.0i %+i %i", 1, 2, 3, 0, 0, 4, -4);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
230 CX_TEST_ASSERT(r[specimen].length == 19);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
231 ASSERT_ZERO_TERMINATED(r[specimen]);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
232 CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "1 2 000003 0 +4 -4"));
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
233 specimen++;
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
234
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
235 r[specimen] = cx_asprintf_a(alloc, "%x %x %X %#x", 5, 10, 10, 6);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
236 CX_TEST_ASSERT(r[specimen].length == 9);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
237 ASSERT_ZERO_TERMINATED(r[specimen]);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
238 CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "5 a A 0x6"));
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
239 specimen++;
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
240
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
241 r[specimen] = cx_asprintf_a(alloc, "%o %#o %#o", 10, 10, 4);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
242 CX_TEST_ASSERT(r[specimen].length == 9);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
243 ASSERT_ZERO_TERMINATED(r[specimen]);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
244 CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "12 012 04"));
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
245 specimen++;
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
246
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
247 r[specimen] = cx_asprintf_a(alloc, "%05.2f %.2f %5.2f", 1.5, 1.5, 1.5);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
248 CX_TEST_ASSERT(r[specimen].length == 16);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
249 ASSERT_ZERO_TERMINATED(r[specimen]);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
250 CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "01.50 1.50 1.50"));
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
251 specimen++;
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
252
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
253 r[specimen] = cx_asprintf_a(alloc, "'%*c'", 5, 'x');
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
254 CX_TEST_ASSERT(r[specimen].length == 7);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
255 ASSERT_ZERO_TERMINATED(r[specimen]);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
256 CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "' x'"));
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
257 specimen++;
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
258
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
259 r[specimen] = cx_asprintf_a(alloc, "'%*c'", -5, 'x');
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
260 CX_TEST_ASSERT(r[specimen].length == 7);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
261 ASSERT_ZERO_TERMINATED(r[specimen]);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
262 CX_TEST_ASSERT(0 == strcmp(r[specimen].ptr, "'x '"));
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
263 specimen++;
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
264
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
265 CX_TEST_ASSERT(specimen == specimen_count); // self-test
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
266
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
267 for (int i = 0; i < specimen_count; i++) {
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
268 cx_strfree_a(alloc, &r[i]);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
269 }
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
270 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
271 }
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
272 cx_testing_allocator_destroy(&talloc);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
273 }
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
274
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
275 CX_TEST(test_asprintf_large_string) {
805
26500fc24058 add constant for reading out printf sbo size - relates to #343
Mike Becker <universe@uap-core.de>
parents: 780
diff changeset
276 unsigned len = cx_printf_sbo_size;
26500fc24058 add constant for reading out printf sbo size - relates to #343
Mike Becker <universe@uap-core.de>
parents: 780
diff changeset
277 char *aaa = malloc(len);
26500fc24058 add constant for reading out printf sbo size - relates to #343
Mike Becker <universe@uap-core.de>
parents: 780
diff changeset
278 char *bbb = malloc(len);
26500fc24058 add constant for reading out printf sbo size - relates to #343
Mike Becker <universe@uap-core.de>
parents: 780
diff changeset
279 char *expected = malloc(2*len+16);
26500fc24058 add constant for reading out printf sbo size - relates to #343
Mike Becker <universe@uap-core.de>
parents: 780
diff changeset
280 memset(aaa, 'a', len-1);
26500fc24058 add constant for reading out printf sbo size - relates to #343
Mike Becker <universe@uap-core.de>
parents: 780
diff changeset
281 aaa[len-1] = 0;
26500fc24058 add constant for reading out printf sbo size - relates to #343
Mike Becker <universe@uap-core.de>
parents: 780
diff changeset
282 memset(bbb, 'b', len-1);
26500fc24058 add constant for reading out printf sbo size - relates to #343
Mike Becker <universe@uap-core.de>
parents: 780
diff changeset
283 bbb[len-1] = 0;
780
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
284 sprintf(expected, "After %s comes %s.", aaa, bbb);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
285 CX_TEST_DO {
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
286 cxmutstr r = cx_asprintf("After %s comes %s.", aaa, bbb);
805
26500fc24058 add constant for reading out printf sbo size - relates to #343
Mike Becker <universe@uap-core.de>
parents: 780
diff changeset
287 CX_TEST_ASSERT(r.length == 2*len-2+14);
780
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
288 ASSERT_ZERO_TERMINATED(r);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
289 CX_TEST_ASSERT(0 == strcmp(r.ptr, expected));
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
290 cx_strfree(&r);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
291 }
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
292 free(aaa);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
293 free(bbb);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
294 free(expected);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
295 }
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
296
810
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
297 CX_TEST(test_sprintf_no_realloc) {
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
298 char *buf = malloc(16);
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
299 CxTestingAllocator talloc;
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
300 cx_testing_allocator_init(&talloc);
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
301 CxAllocator *alloc = &talloc.base;
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
302 CX_TEST_DO {
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
303 char *oldbuf = buf;
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
304 size_t len = cx_sprintf_a(alloc, &buf, 16, "Test %d %s", 47, "string");
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
305 CX_TEST_ASSERT(oldbuf == buf);
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
306 CX_TEST_ASSERT(len == 14);
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
307 CX_TEST_ASSERT(0 == memcmp(buf, "Test 47 string", 15));
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
308 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
309 }
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
310 cx_testing_allocator_destroy(&talloc);
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
311 free(buf);
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
312 }
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
313
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
314 CX_TEST(test_sprintf_realloc) {
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
315 CxTestingAllocator talloc;
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
316 cx_testing_allocator_init(&talloc);
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
317 CxAllocator *alloc = &talloc.base;
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
318 char *buf = cxMalloc(alloc, 8);
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
319 CX_TEST_DO {
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
320 size_t len = cx_sprintf_a(alloc, &buf, 8, "Test %d %s", 47, "foobar");
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
321 CX_TEST_ASSERT(len == 14);
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
322 CX_TEST_ASSERT(0 == memcmp(buf, "Test 47 foobar", 15));
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
323 cxFree(alloc, buf);
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
324 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
325 }
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
326 cx_testing_allocator_destroy(&talloc);
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
327 }
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
328
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
329 CX_TEST(test_sprintf_realloc_to_fit_terminator) {
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
330 CxTestingAllocator talloc;
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
331 cx_testing_allocator_init(&talloc);
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
332 CxAllocator *alloc = &talloc.base;
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
333 // make it so that only the zero-terminator does not fit
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
334 char *buf = cxMalloc(alloc, 14);
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
335 CX_TEST_DO {
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
336 size_t len = cx_sprintf_a(alloc, &buf, 14, "Test %d %s", 13, "string");
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
337 CX_TEST_ASSERT(len == 14);
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
338 CX_TEST_ASSERT(0 == memcmp(buf, "Test 13 string", 15));
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
339 cxFree(alloc, buf);
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
340 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
341 }
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
342 cx_testing_allocator_destroy(&talloc);
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
343 }
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
344
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
345 CX_TEST(test_sprintf_s_no_alloc) {
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
346 char buf[16];
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
347 CxTestingAllocator talloc;
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
348 cx_testing_allocator_init(&talloc);
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
349 CxAllocator *alloc = &talloc.base;
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
350 CX_TEST_DO {
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
351 char *str;
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
352 size_t len = cx_sprintf_sa(alloc, buf, 16, &str, "Test %d %s", 47, "string");
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
353 CX_TEST_ASSERT(str == buf);
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
354 CX_TEST_ASSERT(len == 14);
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
355 CX_TEST_ASSERT(0 == memcmp(buf, "Test 47 string", 15));
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
356 CX_TEST_ASSERT(0 == memcmp(str, "Test 47 string", 15));
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
357 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
358 }
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
359 cx_testing_allocator_destroy(&talloc);
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
360 }
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
361
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
362 CX_TEST(test_sprintf_s_alloc) {
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
363 char buf[16];
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
364 memcpy(buf, "0123456789abcdef", 16);
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
365 CxTestingAllocator talloc;
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
366 cx_testing_allocator_init(&talloc);
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
367 CxAllocator *alloc = &talloc.base;
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
368 CX_TEST_DO {
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
369 char *str;
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
370 size_t len = cx_sprintf_sa(alloc, buf, 16, &str, "Hello %d %s", 4711, "larger string");
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
371 CX_TEST_ASSERT(str != buf);
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
372 CX_TEST_ASSERT(len == 24);
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
373 CX_TEST_ASSERT(0 == memcmp(str, "Hello 4711 larger string", 25));
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
374 cxFree(alloc, str);
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
375 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
376 }
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
377 cx_testing_allocator_destroy(&talloc);
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
378 }
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
379
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
380 CX_TEST(test_sprintf_s_alloc_to_fit_terminator) {
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
381 char buf[16];
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
382 memcpy(buf, "0123456789abcdef", 16);
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
383 CxTestingAllocator talloc;
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
384 cx_testing_allocator_init(&talloc);
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
385 CxAllocator *alloc = &talloc.base;
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
386 CX_TEST_DO {
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
387 char *str;
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
388 size_t len = cx_sprintf_sa(alloc, buf, 16, &str, "Hello %d %s", 112, "string");
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
389 CX_TEST_ASSERT(str != buf);
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
390 CX_TEST_ASSERT(len == 16);
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
391 CX_TEST_ASSERT(0 == memcmp(str, "Hello 112 string", 17)); // include terminator
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
392 cxFree(alloc, str);
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
393 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
394 }
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
395 cx_testing_allocator_destroy(&talloc);
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
396 }
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
397
780
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
398 CxTestSuite *cx_test_suite_printf(void) {
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
399 CxTestSuite *suite = cx_test_suite_new("printf");
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
400
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
401 cx_test_register(suite, test_bprintf);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
402 cx_test_register(suite, test_bprintf_large_string);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
403 cx_test_register(suite, test_bprintf_nocap);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
404 cx_test_register(suite, test_fprintf);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
405 cx_test_register(suite, test_asprintf);
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
406 cx_test_register(suite, test_asprintf_large_string);
810
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
407 cx_test_register(suite, test_sprintf_no_realloc);
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
408 cx_test_register(suite, test_sprintf_realloc);
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
409 cx_test_register(suite, test_sprintf_realloc_to_fit_terminator);
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
410 cx_test_register(suite, test_sprintf_s_no_alloc);
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
411 cx_test_register(suite, test_sprintf_s_alloc);
85859399a0cc add cx_sprintf() variants - fixes #353
Mike Becker <universe@uap-core.de>
parents: 805
diff changeset
412 cx_test_register(suite, test_sprintf_s_alloc_to_fit_terminator);
780
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
413
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
414 return suite;
9965df621652 migrate printf tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
415 }

mercurial