UnitTests: lib mem_blocks: block_alloc_free_contiguous/free

Unit tests for mem_blocks library block_alloc_free_contiguous/free

Signed-off-by: Marcin Szkudlinski <marcin.szkudlinski@intel.com>
This commit is contained in:
Marcin Szkudlinski 2022-03-15 10:57:25 +01:00 committed by Anas Nashif
commit 7147f1a704

View file

@ -388,6 +388,188 @@ static void test_mem_block_get(void)
#endif #endif
} }
static void test_mem_block_alloc_free_continuous(void)
{
int i, ret, val;
void *block;
#ifdef CONFIG_SYS_MEM_BLOCKS_LISTENER
listener_idx = 0;
heap_listener_register(&mem_block_01_alloc);
heap_listener_register(&mem_block_01_free);
#endif
/* allocate all available blocks */
ret = sys_mem_blocks_alloc_contiguous(&mem_block_01, NUM_BLOCKS, &block);
zassert_equal(ret, 0,
"sys_mem_blocks_alloc_contiguous failed (%d)", ret);
/* all blocks should be taken */
for (i = 0; i < NUM_BLOCKS; i++) {
ret = sys_bitarray_test_bit(mem_block_01.bitmap,
i, &val);
zassert_equal(val, 1,
"sys_mem_blocks_alloc_contiguous failed, bit %i should be set", i);
break;
}
/* free first 3 memory blocks, use a pointer provided by previous case */
ret = sys_mem_blocks_free_contiguous(&mem_block_01, block, 3);
zassert_equal(ret, 0,
"sys_mem_blocks_free_contiguous failed (%d)", ret);
/* all blocks extept 0,1,2 should be taken */
for (i = 0; i < NUM_BLOCKS; i++) {
ret = sys_bitarray_test_bit(mem_block_01.bitmap,
i, &val);
switch (i) {
case 0:
case 1:
case 2:
zassert_equal(val, 0,
"sys_mem_blocks_alloc_contiguous failed, bit %i should be cleared", i);
break;
default:
zassert_equal(val, 1,
"sys_mem_blocks_alloc_contiguous failed, bit %i should be set", i);
break;
}
}
/* free a memory block, starting from 4, size 4 */
ret = sys_mem_blocks_free_contiguous(&mem_block_01, mem_block_01.buffer+BLK_SZ*4, 4);
zassert_equal(ret, 0,
"sys_mem_blocks_free_contiguous failed (%d)", ret);
/* all blocks extept 0,1,2,4,5,6,7 should be taken */
for (i = 0; i < NUM_BLOCKS; i++) {
ret = sys_bitarray_test_bit(mem_block_01.bitmap,
i, &val);
switch (i) {
case 0:
case 1:
case 2:
case 4:
case 5:
case 6:
case 7:
zassert_equal(val, 0,
"sys_mem_blocks_alloc_contiguous failed, bit %i should be cleared", i);
break;
default:
zassert_equal(val, 1,
"sys_mem_blocks_alloc_contiguous failed, bit %i should be set", i);
break;
}
}
/* at this point, regardless of the memory size, there are 2 free continuous blocks
* sizes: 3 and 4 slots.
* try to allocate 5 blocks, should fail
*/
ret = sys_mem_blocks_alloc_contiguous(&mem_block_01, 5, &block);
zassert_equal(ret, -ENOMEM,
"sys_mem_blocks_free_contiguous failed (%d)", ret);
/* allocate 3 blocks */
ret = sys_mem_blocks_alloc_contiguous(&mem_block_01, 3, &block);
zassert_equal(ret, 0,
"sys_mem_blocks_free_contiguous failed (%d)", ret);
/* all blocks extept 4,5,6,7 should be taken */
for (i = 0; i < NUM_BLOCKS; i++) {
ret = sys_bitarray_test_bit(mem_block_01.bitmap,
i, &val);
switch (i) {
case 4:
case 5:
case 6:
case 7:
zassert_equal(val, 0,
"sys_mem_blocks_alloc_contiguous failed, bit %i should be cleared", i);
break;
default:
zassert_equal(val, 1,
"sys_mem_blocks_alloc_contiguous failed, bit %i should be set", i);
break;
}
}
/* allocate 4 blocks */
ret = sys_mem_blocks_alloc_contiguous(&mem_block_01, 4, &block);
zassert_equal(ret, 0,
"sys_mem_blocks_free_contiguous failed (%d)", ret);
/* all blocks should be taken */
for (i = 0; i < NUM_BLOCKS; i++) {
ret = sys_bitarray_test_bit(mem_block_01.bitmap,
i, &val);
zassert_equal(val, 1,
"sys_mem_blocks_alloc_contiguous failed, bit %i should be set", i);
}
/* cleanup - free all blocks */
ret = sys_mem_blocks_free_contiguous(&mem_block_01, mem_block_01.buffer, NUM_BLOCKS);
zassert_equal(ret, 0,
"sys_mem_blocks_alloc_contiguous failed (%d)", ret);
/* all blocks should be cleared */
for (i = 0; i < NUM_BLOCKS; i++) {
ret = sys_bitarray_test_bit(mem_block_01.bitmap,
i, &val);
zassert_equal(val, 0,
"sys_mem_blocks_alloc_contiguous failed, bit %i should be cleared", i);
}
#ifdef CONFIG_SYS_MEM_BLOCKS_LISTENER
heap_listener_unregister(&mem_block_01_alloc);
heap_listener_unregister(&mem_block_01_free);
/* verify alloc/free log */
zassert_equal(listener_mem[0], mem_block_01.buffer,
"sys_mem_blocks_alloc_contiguous failed, %p != %p",
listener_mem[0], mem_block_01.buffer);
zassert_equal(listener_size[0], BLK_SZ*NUM_BLOCKS,
"sys_mem_blocks_alloc_contiguous failed, %u != %u",
listener_size[0], BLK_SZ*NUM_BLOCKS);
zassert_equal(listener_mem[1], mem_block_01.buffer,
"sys_mem_blocks_alloc_contiguous failed, %p != %p",
listener_mem[1], mem_block_01.buffer);
zassert_equal(listener_size[1], BLK_SZ*3,
"sys_mem_blocks_alloc_contiguous failed, %u != %u",
listener_size[1], BLK_SZ*3);
zassert_equal(listener_mem[2], mem_block_01.buffer+BLK_SZ*4,
"sys_mem_blocks_alloc_contiguous failed, %p != %p",
listener_mem[2], mem_block_01.buffer+BLK_SZ*4);
zassert_equal(listener_size[2], BLK_SZ*4,
"sys_mem_blocks_alloc_contiguous failed, %u != %u",
listener_size[2], BLK_SZ*4);
zassert_equal(listener_mem[3], mem_block_01.buffer,
"sys_mem_blocks_alloc_contiguous failed, %p != %p",
listener_mem[3], mem_block_01.buffer);
zassert_equal(listener_size[3], BLK_SZ*3,
"sys_mem_blocks_alloc_contiguous failed, %u != %u",
listener_size[3], BLK_SZ*3);
zassert_equal(listener_mem[4], mem_block_01.buffer+BLK_SZ*4,
"sys_mem_blocks_alloc_contiguous failed, %p != %p",
listener_mem[4], mem_block_01.buffer+BLK_SZ*4);
zassert_equal(listener_size[4], BLK_SZ*4,
"sys_mem_blocks_alloc_contiguous failed, %u != %u",
listener_size[4], BLK_SZ*4);
zassert_equal(listener_mem[5], mem_block_01.buffer,
"sys_mem_blocks_alloc_contiguous failed, %p != %p",
listener_mem[5], mem_block_01.buffer);
zassert_equal(listener_size[5], BLK_SZ*NUM_BLOCKS,
"sys_mem_blocks_alloc_contiguous failed, %u != %u",
listener_size[5], BLK_SZ*NUM_BLOCKS);
#endif
}
static void test_multi_mem_block_alloc_free(void) static void test_multi_mem_block_alloc_free(void)
{ {
int ret; int ret;
@ -559,7 +741,8 @@ void test_main(void)
ztest_unit_test(test_multi_mem_block_alloc_free), ztest_unit_test(test_multi_mem_block_alloc_free),
ztest_unit_test(test_mem_block_invalid_params), ztest_unit_test(test_mem_block_invalid_params),
ztest_unit_test(test_multi_mem_block_invalid_params), ztest_unit_test(test_multi_mem_block_invalid_params),
ztest_unit_test(test_mem_block_get) ztest_unit_test(test_mem_block_get),
ztest_unit_test(test_mem_block_alloc_free_continuous)
); );
ztest_run_test_suite(lib_mem_block_test); ztest_run_test_suite(lib_mem_block_test);