From b46d961abf74175f2a003ac4af3a2421ba2d193b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Chru=C5=9Bci=C5=84ski?= Date: Wed, 20 Dec 2023 15:51:37 +0100 Subject: [PATCH] tests: lib: cbprintf_package: Add test case for cbprintf_convert MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add test case for converting statically generated package which contains RO and RW indexes. Converting shall append RW string and discard RO index. Signed-off-by: Krzysztof Chruściński --- tests/lib/cbprintf_package/src/main.c | 42 +++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/tests/lib/cbprintf_package/src/main.c b/tests/lib/cbprintf_package/src/main.c index 3059e68602..7451d8012b 100644 --- a/tests/lib/cbprintf_package/src/main.c +++ b/tests/lib/cbprintf_package/src/main.c @@ -893,6 +893,48 @@ ZTEST(cbprintf_package, test_cbprintf_package_convert) } +ZTEST(cbprintf_package, test_cbprintf_package_convert_static) +{ + int slen, clen, olen; + static const char test_str[] = "test %s"; + char test_str1[] = "test str1"; + /* Store indexes of rw strings. */ + uint32_t flags = CBPRINTF_PACKAGE_ADD_RW_STR_POS | + CBPRINTF_PACKAGE_FIRST_RO_STR_CNT(0) | + CBPRINTF_PACKAGE_ADD_STRING_IDXS; + struct test_cbprintf_covert_ctx ctx; + +#define TEST_FMT test_str, test_str1 + char exp_str[256]; + + snprintfcb(exp_str, sizeof(exp_str), TEST_FMT); + + CBPRINTF_STATIC_PACKAGE(NULL, 0, slen, CBPRINTF_PACKAGE_ALIGNMENT, flags, TEST_FMT); + zassert_true(slen > 0); + + uint8_t __aligned(CBPRINTF_PACKAGE_ALIGNMENT) spackage[slen]; + + memset(&ctx, 0, sizeof(ctx)); + memset(spackage, 0, slen); + + CBPRINTF_STATIC_PACKAGE(spackage, slen, olen, CBPRINTF_PACKAGE_ALIGNMENT, flags, TEST_FMT); + zassert_equal(olen, slen); + + uint32_t copy_flags = CBPRINTF_PACKAGE_CONVERT_RW_STR; + + clen = cbprintf_package_convert(spackage, slen, NULL, 0, copy_flags, NULL, 0); + zassert_true(clen == slen + sizeof(test_str1) + 1/*null*/ - 2 /* arg+ro idx gone*/); + + clen = cbprintf_package_convert(spackage, slen, convert_cb, &ctx, copy_flags, NULL, 0); + 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 + +} + /** * @brief Log information about variable sizes and alignment. *