From f82ffc921380689c46185bd63d92e33aaa4c971f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Chru=C5=9Bci=C5=84ski?= Date: Wed, 30 Oct 2024 12:13:40 +0100 Subject: [PATCH] tests: lib: cbprintf_package: Extend test coverage MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- tests/lib/cbprintf_package/src/main.c | 54 +++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/tests/lib/cbprintf_package/src/main.c b/tests/lib/cbprintf_package/src/main.c index adc59cae58e..35deba58013 100644 --- a/tests/lib/cbprintf_package/src/main.c +++ b/tests/lib/cbprintf_package/src/main.c @@ -893,6 +893,60 @@ ZTEST(cbprintf_package, test_cbprintf_package_convert) } +/* Test uses package convert with initial size calculation. Array provided to hold + * argument string lengths is shorter than number of string arguments. + */ +ZTEST(cbprintf_package, test_cbprintf_package_convert_strl) +{ + int slen, clen; + char test_str[] = "test %s %d %s %s"; + char test_str1[] = "test str1"; + char test_str2[] = "test str 2"; + char test_str3[] = "test str 3"; + /* Store indexes of rw strings. */ + uint32_t flags = CBPRINTF_PACKAGE_ADD_RW_STR_POS; + struct test_cbprintf_covert_ctx ctx; + uint16_t strl[2]; + +#define TEST_FMT test_str, test_str1, 100, test_str2, test_str3 + char exp_str[256]; + + snprintfcb(exp_str, sizeof(exp_str), TEST_FMT); + + slen = cbprintf_package(NULL, 0, flags, TEST_FMT); + zassert_true(slen > 0); + + uint8_t __aligned(CBPRINTF_PACKAGE_ALIGNMENT) spackage[slen]; + + memset(&ctx, 0, sizeof(ctx)); + memset(spackage, 0, slen); + + slen = cbprintf_package(spackage, slen, flags, TEST_FMT); + zassert_true(slen > 0); + + uint32_t copy_flags = CBPRINTF_PACKAGE_CONVERT_RW_STR | + CBPRINTF_PACKAGE_CONVERT_KEEP_RO_STR; + + clen = cbprintf_package_convert(spackage, slen, NULL, 0, copy_flags, + strl, ARRAY_SIZE(strl)); + zassert_true(clen > 0); + /* Two locations were provided to store string lengths. 3rd string length + * will need to be calculated in both conversions. + */ + zassert_equal(strl[0], strlen(test_str1) + 1); + zassert_equal(strl[1], strlen(test_str2) + 1); + + clen = cbprintf_package_convert(spackage, slen, convert_cb, &ctx, copy_flags, + strl, ARRAY_SIZE(strl)); + zassert_true(clen > 0); + zassert_true(ctx.null); + zassert_equal((int)ctx.offset, clen); + + check_package(ctx.buf, ctx.offset, exp_str); +#undef TEST_FMT + +} + ZTEST(cbprintf_package, test_cbprintf_package_convert_static) { int slen, clen, olen;