From 143d2633d7150a9311a7c8f27708eea8b9939909 Mon Sep 17 00:00:00 2001 From: Guennadi Liakhovetski Date: Wed, 4 Feb 2026 16:41:01 +0100 Subject: [PATCH] objpool: add an iterator test Add an objpool test for its iterator function. Signed-off-by: Guennadi Liakhovetski --- test/ztest/unit/objpool/test_objpool_ztest.c | 40 ++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/test/ztest/unit/objpool/test_objpool_ztest.c b/test/ztest/unit/objpool/test_objpool_ztest.c index dc5fc458f9bc..596fb3accd70 100644 --- a/test/ztest/unit/objpool/test_objpool_ztest.c +++ b/test/ztest/unit/objpool/test_objpool_ztest.c @@ -75,4 +75,44 @@ ZTEST(objpool_suite, test_objpool) zassert_ok(objpool_free(&head, blocks[k]), "free failed"); } +struct test_objpool_data { + char cnt; + uint8_t reserved[DATA_SIZE - sizeof(char)]; +} __packed; + +static unsigned int test_objpool_check; + +static bool test_objpool_cb(void *data, void *arg) +{ + struct test_objpool_data *odata = data; + + zassert_equal(test_objpool_check++, odata->cnt, "Counter mismatch"); + zassert_equal((unsigned int)arg, 2, "Wrong argument"); + + return odata->cnt == (unsigned int)arg; +} + +ZTEST(objpool_suite, test_objpool_iterate) +{ + struct objpool_head head = {.list = LIST_INIT(head.list)}; + unsigned int i; + + for (i = 0; i < 4; i++) { + struct test_objpool_data *odata = objpool_alloc(&head, sizeof(*odata), 0); + + zassert_not_null(odata, "allocation failed loop %u", i); + + odata->cnt = i; + } + + int ret = objpool_iterate(&head, test_objpool_cb, (void *)2); + + zassert_equal(test_objpool_check, 3); + + zassert_ok(ret); + + /* Reset for a possible rerun */ + test_objpool_check = 0; +} + ZTEST_SUITE(objpool_suite, NULL, NULL, NULL, NULL, NULL);