|
| 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