Skip to content

Commit 09634c9

Browse files
nordic-krchkartben
authored andcommitted
tests: kernel: common: Extend bitarray test
Add test for sys_bitarray_alloc and sys_bitarray_free with 32 bit bitarray which is using an optimized algorithm. Signed-off-by: Krzysztof Chruściński <[email protected]>
1 parent a6c04cf commit 09634c9

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

tests/kernel/common/src/bitarray.c

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,39 @@ ZTEST(bitarray, test_bitarray_set_clear)
282282
"sys_bitarray_test_and_clear_bit() erroneously changed bitarray");
283283
}
284284

285+
static void test_alloc_free_32(uint32_t mask, uint32_t mask_after, size_t num_bits,
286+
size_t exp_offset, int exp_ret)
287+
{
288+
int ret;
289+
size_t offset;
290+
291+
SYS_BITARRAY_DEFINE(ba_32, 32);
292+
293+
ba_32.bundles[0] = mask;
294+
295+
ret = sys_bitarray_alloc(&ba_32, num_bits, &offset);
296+
zassert_equal(ret, exp_ret, "sys_bitarray_alloc() failed: %d", ret);
297+
if (exp_ret < 0) {
298+
return;
299+
}
300+
zassert_equal(offset, exp_offset,
301+
"sys_bitarray_alloc() offset expected %d, got %d", exp_offset, offset);
302+
zassert_equal(ba_32.bundles[0], mask_after, "sys_bitarray_alloc() failed bits comparison");
303+
304+
ret = sys_bitarray_free(&ba_32, num_bits, offset);
305+
zassert_equal(ret, 0, "sys_bitarray_free() failed: %d", ret);
306+
zassert_equal(ba_32.bundles[0], mask, "sys_bitarray_alloc() failed bits comparison");
307+
}
308+
309+
static void alloc_and_free_32_predefined(void)
310+
{
311+
printk("Testing bit array alloc and free with predefined patterns using 32 bit array\n");
312+
313+
test_alloc_free_32(0x0F0F070F, 0x0F0FFF0F, 5, 11, 0);
314+
test_alloc_free_32(0x33333333, 0xF3333333, 3, 0, -ENOSPC);
315+
test_alloc_free_32(0x33333333, 0xF3333333, 2, 30, 0);
316+
}
317+
285318
void alloc_and_free_predefined(void)
286319
{
287320
int ret;
@@ -291,7 +324,7 @@ void alloc_and_free_predefined(void)
291324

292325
SYS_BITARRAY_DEFINE(ba_128, 128);
293326

294-
printk("Testing bit array alloc and free with predefined patterns\n");
327+
printk("Testing bit array alloc and free with predefined patterns using 128 bit array\n");
295328

296329
/* Pre-populate the bits */
297330
ba_128.bundles[0] = 0x0F0F070F;
@@ -514,6 +547,7 @@ ZTEST(bitarray, test_bitarray_alloc_free)
514547
}
515548

516549
alloc_and_free_predefined();
550+
alloc_and_free_32_predefined();
517551

518552
i = 1;
519553
while (i < 65) {

0 commit comments

Comments
 (0)