Skip to content

Commit f82ffc9

Browse files
nordic-krchmmahadevan108
authored andcommitted
tests: lib: cbprintf_package: Extend test coverage
Add test for cbprintf_package_convert function which checks if it correctly handles array that holds string lengths. When convert function is used twice, at first to calculate size of the output package and then to actually convert the package, array of string lengths can be used to optimize operation by not calculating string lengths twice. However, array may not be able to hold all string lengths that are needed for that package. In that case, string lengths that did not fit into the array will be calculated twice, in both conversions. Signed-off-by: Krzysztof Chruściński <[email protected]>
1 parent 9cfd185 commit f82ffc9

File tree

1 file changed

+54
-0
lines changed
  • tests/lib/cbprintf_package/src

1 file changed

+54
-0
lines changed

tests/lib/cbprintf_package/src/main.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,60 @@ ZTEST(cbprintf_package, test_cbprintf_package_convert)
893893

894894
}
895895

896+
/* Test uses package convert with initial size calculation. Array provided to hold
897+
* argument string lengths is shorter than number of string arguments.
898+
*/
899+
ZTEST(cbprintf_package, test_cbprintf_package_convert_strl)
900+
{
901+
int slen, clen;
902+
char test_str[] = "test %s %d %s %s";
903+
char test_str1[] = "test str1";
904+
char test_str2[] = "test str 2";
905+
char test_str3[] = "test str 3";
906+
/* Store indexes of rw strings. */
907+
uint32_t flags = CBPRINTF_PACKAGE_ADD_RW_STR_POS;
908+
struct test_cbprintf_covert_ctx ctx;
909+
uint16_t strl[2];
910+
911+
#define TEST_FMT test_str, test_str1, 100, test_str2, test_str3
912+
char exp_str[256];
913+
914+
snprintfcb(exp_str, sizeof(exp_str), TEST_FMT);
915+
916+
slen = cbprintf_package(NULL, 0, flags, TEST_FMT);
917+
zassert_true(slen > 0);
918+
919+
uint8_t __aligned(CBPRINTF_PACKAGE_ALIGNMENT) spackage[slen];
920+
921+
memset(&ctx, 0, sizeof(ctx));
922+
memset(spackage, 0, slen);
923+
924+
slen = cbprintf_package(spackage, slen, flags, TEST_FMT);
925+
zassert_true(slen > 0);
926+
927+
uint32_t copy_flags = CBPRINTF_PACKAGE_CONVERT_RW_STR |
928+
CBPRINTF_PACKAGE_CONVERT_KEEP_RO_STR;
929+
930+
clen = cbprintf_package_convert(spackage, slen, NULL, 0, copy_flags,
931+
strl, ARRAY_SIZE(strl));
932+
zassert_true(clen > 0);
933+
/* Two locations were provided to store string lengths. 3rd string length
934+
* will need to be calculated in both conversions.
935+
*/
936+
zassert_equal(strl[0], strlen(test_str1) + 1);
937+
zassert_equal(strl[1], strlen(test_str2) + 1);
938+
939+
clen = cbprintf_package_convert(spackage, slen, convert_cb, &ctx, copy_flags,
940+
strl, ARRAY_SIZE(strl));
941+
zassert_true(clen > 0);
942+
zassert_true(ctx.null);
943+
zassert_equal((int)ctx.offset, clen);
944+
945+
check_package(ctx.buf, ctx.offset, exp_str);
946+
#undef TEST_FMT
947+
948+
}
949+
896950
ZTEST(cbprintf_package, test_cbprintf_package_convert_static)
897951
{
898952
int slen, clen, olen;

0 commit comments

Comments
 (0)