Skip to content

Commit 333f248

Browse files
committed
[add][testcase]add mempool tc.
1 parent 893ae7d commit 333f248

File tree

3 files changed

+219
-0
lines changed

3 files changed

+219
-0
lines changed

examples/utest/testcases/kernel/Kconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ config UTEST_SCHEDULER_TC
8383
bool "scheduler test"
8484
default n
8585

86+
config UTEST_MEMPOOL_TC
87+
bool "mempool test"
88+
default n
89+
depends on RT_USING_MEMPOOL
90+
8691
if RT_USING_SMP
8792
rsource "smp/Kconfig"
8893
endif

examples/utest/testcases/kernel/SConscript

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ if GetDepend(['UTEST_HOOKLIST_TC']):
5757
if GetDepend(['UTEST_MTSAFE_KPRINT_TC']):
5858
src += ['mtsafe_kprint_tc.c']
5959

60+
if GetDepend(['UTEST_MEMPOOL_TC']):
61+
src += ['mempool_tc.c']
62+
6063
# Stressful testcase for scheduler (MP/UP)
6164
if GetDepend(['UTEST_SCHEDULER_TC']):
6265
src += ['sched_timed_sem_tc.c']
@@ -72,3 +75,4 @@ for item in list:
7275
group = group + SConscript(os.path.join(item, 'SConscript'))
7376

7477
Return('group')
78+
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
/*
2+
* Copyright (c) 2006-2025, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2025-09-03 Rbb666 the first version for mempool utest
9+
*/
10+
#include <rtthread.h>
11+
#include <stdlib.h>
12+
#include "utest.h"
13+
14+
#define MEMPOOL_BLOCK_SIZE 80
15+
#define MEMPOOL_BLOCK_COUNT 32
16+
#define MEMPOOL_SIZE (MEMPOOL_BLOCK_SIZE + sizeof(rt_uint8_t *)) * MEMPOOL_BLOCK_COUNT
17+
18+
static rt_uint8_t mempool_static[MEMPOOL_SIZE];
19+
static struct rt_mempool mp_static;
20+
static rt_mp_t mp_dynamic;
21+
22+
/* Static memory pool test */
23+
static void test_mp_static_init(void)
24+
{
25+
rt_err_t err;
26+
27+
err = rt_mp_init(&mp_static, "mp_static", &mempool_static[0], sizeof(mempool_static), MEMPOOL_BLOCK_SIZE);
28+
uassert_true(err == RT_EOK);
29+
uassert_str_equal(mp_static.parent.name, "mp_static");
30+
uassert_true(mp_static.block_total_count == MEMPOOL_BLOCK_COUNT);
31+
uassert_true(mp_static.block_free_count == MEMPOOL_BLOCK_COUNT);
32+
}
33+
34+
/* Dynamic memory pool test */
35+
static void test_mp_dynamic_create(void)
36+
{
37+
mp_dynamic = rt_mp_create("mp_dynamic", MEMPOOL_BLOCK_COUNT, MEMPOOL_BLOCK_SIZE);
38+
uassert_not_null(mp_dynamic);
39+
uassert_str_equal(mp_dynamic->parent.name, "mp_dynamic");
40+
uassert_true(mp_dynamic->block_total_count == MEMPOOL_BLOCK_COUNT);
41+
uassert_true(mp_dynamic->block_free_count == MEMPOOL_BLOCK_COUNT);
42+
}
43+
44+
/* Allocation and free test for static */
45+
static void test_mp_static_alloc_free(void)
46+
{
47+
void *block1, *block2, *block3;
48+
49+
/* Allocate blocks */
50+
block1 = rt_mp_alloc(&mp_static, 0);
51+
uassert_not_null(block1);
52+
53+
block2 = rt_mp_alloc(&mp_static, 0);
54+
uassert_not_null(block2);
55+
56+
block3 = rt_mp_alloc(&mp_static, 0);
57+
uassert_not_null(block3);
58+
59+
/* Check free count */
60+
uassert_true(mp_static.block_free_count == MEMPOOL_BLOCK_COUNT - 3);
61+
62+
/* Free blocks */
63+
rt_mp_free(block1);
64+
rt_mp_free(block2);
65+
rt_mp_free(block3);
66+
67+
/* Check free count */
68+
uassert_true(mp_static.block_free_count == MEMPOOL_BLOCK_COUNT);
69+
}
70+
71+
/* Allocation and free test for dynamic */
72+
static void test_mp_dynamic_alloc_free(void)
73+
{
74+
void *block1, *block2, *block3;
75+
76+
/* Allocate blocks */
77+
block1 = rt_mp_alloc(mp_dynamic, 0);
78+
uassert_not_null(block1);
79+
80+
block2 = rt_mp_alloc(mp_dynamic, 0);
81+
uassert_not_null(block2);
82+
83+
block3 = rt_mp_alloc(mp_dynamic, 0);
84+
uassert_not_null(block3);
85+
86+
/* Check free count */
87+
uassert_true(mp_dynamic->block_free_count == MEMPOOL_BLOCK_COUNT - 3);
88+
89+
/* Free blocks */
90+
rt_mp_free(block1);
91+
rt_mp_free(block2);
92+
rt_mp_free(block3);
93+
94+
/* Check free count */
95+
uassert_true(mp_dynamic->block_free_count == MEMPOOL_BLOCK_COUNT);
96+
}
97+
98+
/* Boundary test: allocate all blocks and try to allocate more */
99+
static void test_mp_boundary_alloc_exceed(void)
100+
{
101+
void *blocks[MEMPOOL_BLOCK_COUNT];
102+
void *extra_block;
103+
int i;
104+
105+
/* Allocate all blocks */
106+
for (i = 0; i < MEMPOOL_BLOCK_COUNT; i++)
107+
{
108+
blocks[i] = rt_mp_alloc(&mp_static, 0);
109+
uassert_not_null(blocks[i]);
110+
}
111+
112+
/* Check free count */
113+
uassert_true(mp_static.block_free_count == 0);
114+
115+
/* Try to allocate one more (should fail) */
116+
extra_block = rt_mp_alloc(&mp_static, 0);
117+
uassert_null(extra_block);
118+
119+
/* Free all blocks */
120+
for (i = 0; i < MEMPOOL_BLOCK_COUNT; i++)
121+
{
122+
rt_mp_free(blocks[i]);
123+
}
124+
125+
/* Check free count */
126+
uassert_true(mp_static.block_free_count == MEMPOOL_BLOCK_COUNT);
127+
}
128+
129+
/* Boundary test: free invalid block */
130+
static void test_mp_boundary_free_invalid(void)
131+
{
132+
/* Test freeing NULL - should not crash and do nothing */
133+
rt_mp_free(RT_NULL);
134+
135+
/* Check free count remains the same */
136+
uassert_true(mp_static.block_free_count == MEMPOOL_BLOCK_COUNT);
137+
138+
/* Test freeing an arbitrary invalid address that won't cause hardware exception */
139+
/* Note: We avoid using completely invalid addresses that could trigger exceptions */
140+
/* Instead, test with a valid memory address but not a mempool block */
141+
char dummy_data[MEMPOOL_BLOCK_SIZE];
142+
void *invalid_block = dummy_data; /* Not a mempool block */
143+
144+
/* This should not crash, but may not change free count since it's invalid */
145+
rt_mp_free(invalid_block);
146+
147+
/* Check free count remains the same */
148+
uassert_true(mp_static.block_free_count == MEMPOOL_BLOCK_COUNT);
149+
}
150+
151+
/* Stress test: allocate and free repeatedly */
152+
static void test_mp_stress_alloc_free(void)
153+
{
154+
void *blocks[MEMPOOL_BLOCK_COUNT];
155+
int i, j;
156+
157+
for (j = 0; j < 100; j++) /* Repeat 100 times */
158+
{
159+
/* Allocate all blocks */
160+
for (i = 0; i < MEMPOOL_BLOCK_COUNT; i++)
161+
{
162+
blocks[i] = rt_mp_alloc(&mp_static, 0);
163+
uassert_not_null(blocks[i]);
164+
}
165+
166+
/* Check free count */
167+
uassert_true(mp_static.block_free_count == 0);
168+
169+
/* Free all blocks */
170+
for (i = 0; i < MEMPOOL_BLOCK_COUNT; i++)
171+
{
172+
rt_mp_free(blocks[i]);
173+
}
174+
175+
/* Check free count */
176+
uassert_true(mp_static.block_free_count == MEMPOOL_BLOCK_COUNT);
177+
}
178+
}
179+
180+
static rt_err_t utest_tc_init(void)
181+
{
182+
return RT_EOK;
183+
}
184+
185+
static rt_err_t utest_tc_cleanup(void)
186+
{
187+
/* Detach static mempool */
188+
rt_mp_detach(&mp_static);
189+
190+
/* Delete dynamic mempool */
191+
if (mp_dynamic != RT_NULL)
192+
{
193+
rt_mp_delete(mp_dynamic);
194+
}
195+
196+
return RT_EOK;
197+
}
198+
199+
static void testcase(void)
200+
{
201+
UTEST_UNIT_RUN(test_mp_static_init);
202+
UTEST_UNIT_RUN(test_mp_dynamic_create);
203+
UTEST_UNIT_RUN(test_mp_static_alloc_free);
204+
UTEST_UNIT_RUN(test_mp_dynamic_alloc_free);
205+
UTEST_UNIT_RUN(test_mp_boundary_alloc_exceed);
206+
UTEST_UNIT_RUN(test_mp_boundary_free_invalid);
207+
UTEST_UNIT_RUN(test_mp_stress_alloc_free);
208+
}
209+
210+
UTEST_TC_EXPORT(testcase, "testcases.kernel.mempool", utest_tc_init, utest_tc_cleanup, 10);

0 commit comments

Comments
 (0)