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.
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
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
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 )
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;
116132static const char sfx_str64 [] = "e7e6e5e4e3e2e1e0" ;
117133static 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+
10671146static 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 );
0 commit comments