Skip to content

Commit 95f1432

Browse files
Andrew Boieandrewboie
authored andcommitted
sys_mem_pool: add test case
This is loosely based on the existing mheap_api_concept test case. Signed-off-by: Andrew Boie <[email protected]>
1 parent aa6de29 commit 95f1432

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
2+
project(NONE)
3+
4+
FILE(GLOB app_sources src/*.c)
5+
target_sources(app PRIVATE ${app_sources})
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CONFIG_ZTEST=y
2+
CONFIG_TEST_USERSPACE=y
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright (c) 2018 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <ztest.h>
8+
#include <misc/mempool.h>
9+
10+
11+
#define BLK_SIZE_MIN 256
12+
#define BLK_SIZE_MAX 1024
13+
#define BLK_NUM_MAX 8
14+
#define TOTAL_POOL_SIZE (BLK_SIZE_MAX * BLK_NUM_MAX)
15+
#define TOTAL_MIN_BLKS (TOTAL_POOL_SIZE / BLK_SIZE_MIN)
16+
17+
#define DESC_SIZE sizeof(struct sys_mem_pool_block)
18+
19+
#define BLK_SIZE_EXCLUDE_DESC (BLK_SIZE_MIN - 16)
20+
#define BLK_ALIGN BLK_SIZE_MIN
21+
22+
23+
K_MUTEX_DEFINE(pool_mutex);
24+
SYS_MEM_POOL_DEFINE(pool, &pool_mutex, BLK_SIZE_MIN, BLK_SIZE_MAX,
25+
BLK_NUM_MAX, BLK_ALIGN, .data);
26+
27+
void test_sys_mem_pool_alloc_free(void)
28+
{
29+
void *block[BLK_NUM_MAX], *block_fail;
30+
31+
for (int i = 0; i < BLK_NUM_MAX; i++) {
32+
/* Allocate the largest possible blocks in the pool, using up
33+
* the entire thing.
34+
*/
35+
block[i] = sys_mem_pool_alloc(&pool, BLK_SIZE_MAX - DESC_SIZE);
36+
/** TESTPOINT: Address of the allocated memory if successful;*/
37+
zassert_not_null(block[i], NULL);
38+
}
39+
40+
block_fail = sys_mem_pool_alloc(&pool, BLK_SIZE_MIN);
41+
/** TESTPOINT: Return NULL if fail.*/
42+
zassert_is_null(block_fail, NULL);
43+
44+
for (int i = 0; i < BLK_NUM_MAX; i++) {
45+
/**
46+
* TESTPOINT: This routine provides traditional free()
47+
* semantics. The memory being returned must have been allocated
48+
* from the heap memory pool.
49+
*/
50+
sys_mem_pool_free(block[i]);
51+
}
52+
/** TESTPOINT: If ptr is NULL, no operation is performed.*/
53+
sys_mem_pool_free(NULL);
54+
}
55+
56+
void test_sys_mem_pool_alloc_align4(void)
57+
{
58+
void *block[BLK_NUM_MAX];
59+
60+
/**
61+
* TESTPOINT: The address of the allocated chunk is guaranteed to be
62+
* aligned on a multiple of 4 bytes.
63+
*/
64+
for (int i = 0; i < BLK_NUM_MAX; i++) {
65+
block[i] = sys_mem_pool_alloc(&pool, i);
66+
zassert_not_null(block[i], NULL);
67+
zassert_false((int)block[i] % 4, NULL);
68+
}
69+
70+
/* test case tear down*/
71+
for (int i = 0; i < BLK_NUM_MAX; i++) {
72+
sys_mem_pool_free(block[i]);
73+
}
74+
}
75+
76+
void test_sys_mem_pool_min_block_size(void)
77+
{
78+
void *block[TOTAL_MIN_BLKS], *block_fail;
79+
80+
/**
81+
* TESTPOINT: The heap memory pool also defines a minimum block
82+
* size of 64 bytes.
83+
* Test steps:
84+
* initial memory heap status (F for free, U for used):
85+
* 64F, 64F, 64F, 64F
86+
* 1. request 4 blocks: each 0-byte plus 16-byte block desc,
87+
* indeed 64-byte allocated
88+
* 2. verify no more free blocks, any further allocation failed
89+
*/
90+
for (int i = 0; i < TOTAL_MIN_BLKS; i++) {
91+
block[i] = sys_mem_pool_alloc(&pool, 0);
92+
zassert_not_null(block[i], NULL);
93+
}
94+
/* verify no more free blocks available*/
95+
block_fail = sys_mem_pool_alloc(&pool, BLK_SIZE_MIN);
96+
zassert_is_null(block_fail, NULL);
97+
98+
/* test case tear down*/
99+
for (int i = 0; i < BLK_NUM_MAX; i++) {
100+
sys_mem_pool_free(block[i]);
101+
}
102+
}
103+
104+
/*test case main entry*/
105+
void test_main(void)
106+
{
107+
k_thread_access_grant(k_current_get(), &pool_mutex, NULL);
108+
sys_mem_pool_init(&pool);
109+
110+
ztest_test_suite(test_sys_mem_pool_api,
111+
ztest_user_unit_test(test_sys_mem_pool_alloc_free),
112+
ztest_user_unit_test(test_sys_mem_pool_alloc_align4),
113+
ztest_user_unit_test(test_sys_mem_pool_min_block_size)
114+
);
115+
ztest_run_test_suite(test_sys_mem_pool_api);
116+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
tests:
2+
kernel.sys_mem_pool:
3+
min_ram: 32
4+
tags: kernel userspace mem_pool

0 commit comments

Comments
 (0)