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 <krzysztof.chruscinski@nordicsemi.no>
This commit is contained in:
Krzysztof Chruściński 2024-10-30 12:13:40 +01:00 committed by Mahesh Mahadevan
parent 9cfd185ab0
commit f82ffc9213
1 changed files with 54 additions and 0 deletions

View File

@ -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;