Skip to content

Commit 6c573ac

Browse files
committed
objpool: add an iterator test
Add an objpool test for its iterator function. Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
1 parent 87ff3ba commit 6c573ac

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

test/ztest/unit/objpool/test_objpool_ztest.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,41 @@ ZTEST(objpool_suite, test_objpool)
7575
zassert_ok(objpool_free(&head, blocks[k]), "free failed");
7676
}
7777

78+
struct test_objpool_data {
79+
char cnt;
80+
uint8_t reserved[DATA_SIZE - sizeof(char)];
81+
} __packed;
82+
83+
static unsigned int test_objpool_check;
84+
85+
static bool test_objpool_cb(void *data, void *arg)
86+
{
87+
struct test_objpool_data *odata = data;
88+
89+
zassert_equal(test_objpool_check++, odata->cnt, "Counter mismatch");
90+
zassert_equal((unsigned int)arg, 2, "Wrong argument");
91+
92+
return odata->cnt == (unsigned int)arg;
93+
}
94+
95+
ZTEST(objpool_suite, test_objpool_iterate)
96+
{
97+
struct objpool_head head = {.list = LIST_INIT(head.list)};
98+
unsigned int i;
99+
100+
for (i = 0; i < 4; i++) {
101+
struct test_objpool_data *odata = objpool_alloc(&head, sizeof(*odata), 0);
102+
103+
zassert_not_null(odata, "allocation failed loop %u", i);
104+
105+
odata->cnt = i;
106+
}
107+
108+
int ret = objpool_iterate(&head, test_objpool_cb, (void *)2);
109+
110+
zassert_equal(test_objpool_check, 3);
111+
112+
zassert_ok(ret);
113+
}
114+
78115
ZTEST_SUITE(objpool_suite, NULL, NULL, NULL, NULL, NULL);

0 commit comments

Comments
 (0)