Skip to content

Commit 0d46d34

Browse files
Nicolas Pitrecarlescufi
authored andcommitted
lib: cbprintf: add unit tests for deferred formatting
Tests to exercise the new `cbprintf_package()`, `cbvprintf_package()` and `cbpprintf()`. [ Heavily based on a prior proposal from Peter Bigot. ] Signed-off-by: Nicolas Pitre <[email protected]> Signed-off-by: Peter Bigot <[email protected]>
1 parent 14e5e98 commit 0d46d34

File tree

2 files changed

+124
-4
lines changed

2 files changed

+124
-4
lines changed

tests/unit/cbprintf/main.c

Lines changed: 94 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include <sys/cbprintf.h>
1818
#include <sys/util.h>
1919

20+
#define CBPRINTF_VIA_UNIT_TEST
21+
2022
/* Unit testing doesn't use Kconfig, so if we're not building from
2123
* twister force selection of all features. If we are use flags to
2224
* determine which features are desired. Yes, this is a mess.
@@ -26,6 +28,7 @@
2628
* This should be used with all options enabled.
2729
*/
2830
#define USE_LIBC 0
31+
#define USE_PACKAGED 0
2932
#define CONFIG_CBPRINTF_COMPLETE 1
3033
#define CONFIG_CBPRINTF_FULL_INTEGRAL 1
3134
#define CONFIG_CBPRINTF_FP_SUPPORT 1
@@ -85,6 +88,9 @@
8588
#if (VIA_TWISTER & 0x100) != 0
8689
#define CONFIG_CBPRINTF_LIBC_SUBSTS 1
8790
#endif
91+
#if (VIA_TWISTER & 0x200) != 0
92+
#define USE_PACKAGED 1
93+
#endif
8894

8995
#endif /* VIA_TWISTER */
9096

@@ -97,6 +103,12 @@
97103
#define ENABLED_USE_LIBC false
98104
#endif
99105

106+
#if USE_PACKAGED
107+
#define ENABLED_USE_PACKAGED true
108+
#else
109+
#define ENABLED_USE_PACKAGED false
110+
#endif
111+
100112
#include "../../../lib/os/cbprintf.c"
101113

102114
#if defined(CONFIG_CBPRINTF_COMPLETE)
@@ -105,6 +117,10 @@
105117
#include "../../../lib/os/cbprintf_nano.c"
106118
#endif
107119

120+
#if USE_PACKAGED
121+
#include "../../../lib/os/cbprintf_packaged.c"
122+
#endif
123+
108124
/* We can't determine at build-time whether int is 64-bit, so assume
109125
* it is. If not the values are truncated at build time, and the str
110126
* pointers will be updated during test initialization.
@@ -116,6 +132,11 @@ static const unsigned int sfx_val = (unsigned int)0xe7e6e5e4e3e2e1e0;
116132
static const char sfx_str64[] = "e7e6e5e4e3e2e1e0";
117133
static const char *sfx_str = sfx_str64;
118134

135+
/* Buffer adequate to hold packaged state for all tested
136+
* configurations.
137+
*/
138+
static uint8_t __aligned(__alignof__(long double)) packaged[256];
139+
119140
#define WRAP_FMT(_fmt) "%x" _fmt "%x"
120141
#define PASS_ARG(...) pfx_val, __VA_ARGS__, sfx_val
121142

@@ -173,8 +194,14 @@ static int prf(const char *format, ...)
173194
#if USE_LIBC
174195
rv = vsnprintf(buf, sizeof(buf), format, ap);
175196
#else
176-
reset_out();
197+
#if USE_PACKAGED
198+
rv = cbvprintf_package(packaged, sizeof(packaged), format, ap);
199+
if (rv >= 0) {
200+
rv = cbpprintf(out, NULL, packaged);
201+
}
202+
#else
177203
rv = cbvprintf(out, NULL, format, ap);
204+
#endif
178205
if (bp == (buf + ARRAY_SIZE(buf))) {
179206
--bp;
180207
}
@@ -190,7 +217,14 @@ static int rawprf(const char *format, ...)
190217
int rv;
191218

192219
va_start(ap, format);
220+
#if USE_PACKAGED
221+
rv = cbvprintf_package(packaged, sizeof(packaged), format, ap);
222+
if (rv >= 0) {
223+
rv = cbpprintf(out, NULL, packaged);
224+
}
225+
#else
193226
rv = cbvprintf(out, NULL, format, ap);
227+
#endif
194228
va_end(ap);
195229

196230
if (IS_ENABLED(CONFIG_CBPRINTF_NANO)
@@ -1064,6 +1098,51 @@ static void test_libc_substs(void)
10641098
}
10651099
}
10661100

1101+
static void test_cbprintf_package(void)
1102+
{
1103+
if (!ENABLED_USE_PACKAGED) {
1104+
TC_PRINT("disabled\n");
1105+
return;
1106+
}
1107+
1108+
int rc;
1109+
char fmt[] = "/%i/"; /* not const */
1110+
1111+
/* Verify we can calculate length without storing */
1112+
rc = cbprintf_package(NULL, 0, fmt, 3);
1113+
zassert_true(rc > sizeof(int), NULL);
1114+
1115+
/* Capture the base package information for future tests. */
1116+
size_t len = rc;
1117+
1118+
/* Verify we get same length when storing */
1119+
rc = cbprintf_package(packaged, sizeof(packaged), fmt, 3);
1120+
zassert_equal(rc, len, NULL);
1121+
1122+
/* Verify we get an error if can't store */
1123+
len -= 1;
1124+
rc = cbprintf_package(packaged, len, fmt, 3);
1125+
zassert_equal(rc, -ENOSPC, NULL);
1126+
}
1127+
1128+
static void test_cbpprintf(void)
1129+
{
1130+
if (!ENABLED_USE_PACKAGED) {
1131+
TC_PRINT("disabled\n");
1132+
return;
1133+
}
1134+
1135+
int rc;
1136+
1137+
/* This only checks error conditions. Formatting is checked
1138+
* by diverting prf() and related helpers to use the packaged
1139+
* version.
1140+
*/
1141+
reset_out();
1142+
rc = cbpprintf(out, NULL, NULL);
1143+
zassert_equal(rc, -EINVAL, NULL);
1144+
}
1145+
10671146
static void test_nop(void)
10681147
{
10691148
}
@@ -1081,7 +1160,12 @@ void test_main(void)
10811160
TC_PRINT(" LIBC");
10821161
}
10831162
if (IS_ENABLED(CONFIG_CBPRINTF_COMPLETE)) {
1084-
TC_PRINT(" COMPLETE\n");
1163+
TC_PRINT(" COMPLETE");
1164+
if (ENABLED_USE_PACKAGED) {
1165+
TC_PRINT(" PACKAGED\n");
1166+
} else {
1167+
TC_PRINT(" VA_LIST\n");
1168+
}
10851169
} else {
10861170
TC_PRINT(" NANO\n");
10871171
}
@@ -1103,8 +1187,12 @@ void test_main(void)
11031187
TC_PRINT(" LIBC_SUBSTS\n");
11041188
}
11051189

1106-
printf("sizeof: int = %zu ; long = %zu ; ptr = %zu\n",
1107-
sizeof(int), sizeof(long), sizeof(void *));
1190+
printf("sizeof: int=%zu long=%zu ptr=%zu long long=%zu double=%zu long double=%zu\n",
1191+
sizeof(int), sizeof(long), sizeof(void *), sizeof(long long),
1192+
sizeof(double), sizeof(long double));
1193+
printf("alignof: int=%zu long=%zu ptr=%zu long long=%zu double=%zu long double=%zu\n",
1194+
__alignof__(int), __alignof__(long), __alignof__(void *),
1195+
__alignof__(long long), __alignof__(double), __alignof__(long double));
11081196
#ifdef CONFIG_CBPRINTF_COMPLETE
11091197
printf("sizeof(conversion) = %zu\n", sizeof(struct conversion));
11101198
#endif
@@ -1127,6 +1215,8 @@ void test_main(void)
11271215
ztest_unit_test(test_n),
11281216
ztest_unit_test(test_p),
11291217
ztest_unit_test(test_libc_substs),
1218+
ztest_unit_test(test_cbprintf_package),
1219+
ztest_unit_test(test_cbpprintf),
11301220
ztest_unit_test(test_nop)
11311221
);
11321222
ztest_run_test_suite(test_prf);

tests/unit/cbprintf/testcase.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,21 @@ tests:
3030
utilities.prf.m32v181: # NANO + FULL + LIBC
3131
extra_args: M64_MODE=0 EXTRA_CPPFLAGS=-DVIA_TWISTER=0x181
3232

33+
utilities.prf.m32v200: # PACKAGED REDUCED
34+
extra_args: M64_MODE=0 EXTRA_CPPFLAGS=-DVIA_TWISTER=0x200
35+
36+
utilities.prf.m32v201: # PACKAGED FULL
37+
extra_args: M64_MODE=0 EXTRA_CPPFLAGS=-DVIA_TWISTER=0x201
38+
39+
utilities.prf.m32v207: # PACKAGED FULL + FP + FP_A
40+
extra_args: M64_MODE=0 EXTRA_CPPFLAGS=-DVIA_TWISTER=0x207
41+
42+
utilities.prf.m32v208: # PACKAGED %n
43+
extra_args: M64_MODE=0 EXTRA_CPPFLAGS=-DVIA_TWISTER=0x208
44+
45+
utilities.prf.m32v281: # PACKAGED NANO + FULL
46+
extra_args: M64_MODE=0 EXTRA_CPPFLAGS=-DVIA_TWISTER=0x281
47+
3348
utilities.prf.m64v00: # m64
3449
extra_args: M64_MODE=1 EXTRA_CPPFLAGS=-DVIA_TWISTER=0x00
3550

@@ -50,3 +65,18 @@ tests:
5065

5166
utilities.prf.m64v181: # NANO + FULL + LIBC
5267
extra_args: M64_MODE=1 EXTRA_CPPFLAGS=-DVIA_TWISTER=0x181
68+
69+
utilities.prf.m64v200: # PACKAGED REDUCED
70+
extra_args: M64_MODE=1 EXTRA_CPPFLAGS=-DVIA_TWISTER=0x200
71+
72+
utilities.prf.m64v201: # PACKAGED FULL
73+
extra_args: M64_MODE=1 EXTRA_CPPFLAGS=-DVIA_TWISTER=0x201
74+
75+
utilities.prf.m64v207: # PACKAGED FULL + FP + FP_A
76+
extra_args: M64_MODE=1 EXTRA_CPPFLAGS=-DVIA_TWISTER=0x207
77+
78+
utilities.prf.m64v208: # PACKAGED %n
79+
extra_args: M64_MODE=1 EXTRA_CPPFLAGS=-DVIA_TWISTER=0x208
80+
81+
utilities.prf.m64v281: # PACKAGED NANO + FULL
82+
extra_args: M64_MODE=1 EXTRA_CPPFLAGS=-DVIA_TWISTER=0x281

0 commit comments

Comments
 (0)