From cf19d7ef825e58247147bae693012e2bb795f6da Mon Sep 17 00:00:00 2001 From: Krzysztof Chruscinski Date: Wed, 1 Jun 2022 13:55:01 +0200 Subject: [PATCH] tests: lib: spsc_pbuf: Add test for getting maximum utilization Add test for CONFIG_SPSC_PBUF_UTILIZATION option. Signed-off-by: Krzysztof Chruscinski --- tests/lib/spsc_pbuf/src/main.c | 42 +++++++++++++++++++++++++++++++ tests/lib/spsc_pbuf/testcase.yaml | 8 ++++++ 2 files changed, 50 insertions(+) diff --git a/tests/lib/spsc_pbuf/src/main.c b/tests/lib/spsc_pbuf/src/main.c index 61a79aed0fb..23b842e884f 100644 --- a/tests/lib/spsc_pbuf/src/main.c +++ b/tests/lib/spsc_pbuf/src/main.c @@ -366,6 +366,48 @@ ZTEST(test_spsc_pbuf, test_largest_alloc) PACKET_WRITE(pb, SPSC_PBUF_MAX_LEN - 1, 0, 1, 12); } +ZTEST(test_spsc_pbuf, test_utilization) +{ + static uint8_t buffer[64] __aligned(MAX(CONFIG_SPSC_PBUF_CACHE_LINE, 4)); + struct spsc_pbuf *pb; + uint32_t capacity; + uint16_t len1, len2, len3; + int u; + + pb = spsc_pbuf_init(buffer, sizeof(buffer), 0); + + if (!IS_ENABLED(CONFIG_SPSC_PBUF_UTILIZATION)) { + zassert_equal(spsc_pbuf_get_utilization(pb), -ENOTSUP, NULL); + return; + } + capacity = spsc_pbuf_capacity(pb); + + len1 = 10; + PACKET_WRITE(pb, len1, len1, 0, len1); + u = spsc_pbuf_get_utilization(pb); + zassert_equal(u, 0, NULL); + + PACKET_CONSUME(pb, len1, 0); + u = spsc_pbuf_get_utilization(pb); + zassert_equal(u, ROUND_UP(len1, sizeof(uint32_t)) + sizeof(uint32_t), NULL); + + len2 = 11; + PACKET_WRITE(pb, len2, len2, 1, len2); + PACKET_CONSUME(pb, len2, 1); + u = spsc_pbuf_get_utilization(pb); + zassert_equal(u, ROUND_UP(len2, sizeof(uint32_t)) + sizeof(uint32_t), NULL); + + len3 = capacity - ROUND_UP(len1, sizeof(uint32_t)) - ROUND_UP(len2, sizeof(uint32_t)) + - 3 * sizeof(uint32_t) + sizeof(uint32_t); + PACKET_WRITE(pb, SPSC_PBUF_MAX_LEN, len3, 2, len3); + PACKET_CONSUME(pb, len3, 2); + + u = spsc_pbuf_get_utilization(pb); + int exp_u = ROUND_UP(len3, sizeof(uint32_t)) + sizeof(uint32_t); + + zassert_equal(u, exp_u, NULL); +} + struct stress_data { struct spsc_pbuf *pbuf; uint32_t capacity; diff --git a/tests/lib/spsc_pbuf/testcase.yaml b/tests/lib/spsc_pbuf/testcase.yaml index 43d170f6204..fef6b172da1 100644 --- a/tests/lib/spsc_pbuf/testcase.yaml +++ b/tests/lib/spsc_pbuf/testcase.yaml @@ -21,6 +21,14 @@ tests: extra_configs: - CONFIG_SPSC_PBUF_CACHE_NEVER=y + lib.spsc_pbuf_utilization: + integration_platforms: + - native_posix + # Exclude platform which does not link with cache functions + platform_exclude: ast1030_evb + extra_configs: + - CONFIG_SPSC_PBUF_UTILIZATION=y + lib.spsc_pbuf_stress: platform_allow: qemu_x86 extra_configs: