|
6 | 6 | * Change Logs: |
7 | 7 | * Date Author Notes |
8 | 8 | * 2021-10-14 tyx the first version |
| 9 | + * 2025-11-11 CYFS Add standardized utest documentation block |
| 10 | + */ |
| 11 | + |
| 12 | +/** |
| 13 | + * Test Case Name: Kernel Core Small Memory Management Test |
| 14 | + * |
| 15 | + * Test Objectives: |
| 16 | + * - Validates the core kernel small memory management module functionality |
| 17 | + * - Verify core APIs: rt_smem_init, rt_smem_alloc, rt_smem_free, rt_smem_realloc, rt_smem_detach |
| 18 | + * |
| 19 | + * Test Scenarios: |
| 20 | + * - **Scenario 1 (Functional Test / mem_functional_test):** |
| 21 | + * 1. Initialize small memory heap with 1024 bytes test buffer |
| 22 | + * 2. Allocate entire heap at once and verify complete deallocation |
| 23 | + * 3. Sequential allocation of multiple blocks followed by sequential release to test forward merging |
| 24 | + * 4. Interval allocation and release pattern to test bidirectional block merging |
| 25 | + * 5. Reallocation from small size to large size (requires memory move and data preservation) |
| 26 | + * 6. Reallocation from large size to small size (in-place shrink verification) |
| 27 | + * 7. Reallocation with equal size (no-op verification) |
| 28 | + * - **Scenario 2 (Random Allocation Stress Test / mem_alloc_test):** |
| 29 | + * 1. Perform random allocation/deallocation operations with 60% allocation probability |
| 30 | + * 2. Memory size randomization (2-5 blocks of mem_alloc_context size) |
| 31 | + * 3. Memory exhaustion handling: automatic release of half allocated blocks when allocation fails |
| 32 | + * 4. Long-duration stress test (5 seconds by default) |
| 33 | + * 5. Memory content verification using magic number patterns during operations |
| 34 | + * - **Scenario 3 (Random Reallocation Stress Test / mem_realloc_test):** |
| 35 | + * 1. Perform random reallocation of multiple memory blocks with size variations (0-5 blocks) |
| 36 | + * 2. Random selection of target block from context table for reallocation |
| 37 | + * 3. Memory exhaustion handling: random freeing of other blocks when realloc fails |
| 38 | + * 4. Reallocation to zero size (free operation) verification |
| 39 | + * 5. Memory content integrity verification before and after reallocation |
| 40 | + * |
| 41 | + * Verification Metrics: |
| 42 | + * - **Pass (Scenario 1):** All allocation operations return non-NULL pointers |
| 43 | + * - **Pass (Scenario 1):** Allocated memory is properly aligned (RT_ALIGN_SIZE) |
| 44 | + * - **Pass (Scenario 1):** Memory content integrity verified using magic number pattern |
| 45 | + * - **Pass (Scenario 1):** Maximum free block size equals initial total size after all deallocations |
| 46 | + * - **Pass (Scenario 1):** Memory block merging verified after sequential and interval releases |
| 47 | + * - **Pass (Scenario 1):** Realloc operations preserve existing data when expanding |
| 48 | + * - **Pass (Scenario 1):** Realloc operations maintain pointer when shrinking in-place |
| 49 | + * - **Pass (Scenario 2):** Allocated memory pointers are properly aligned |
| 50 | + * - **Pass (Scenario 2):** Memory content integrity maintained throughout random operations |
| 51 | + * - **Pass (Scenario 2):** Memory heap fully recovered (max_block equals initial total_size) after test |
| 52 | + * - **Pass (Scenario 2):** Test count tracking accurate (head.count equals 0 at end) |
| 53 | + * - **Pass (Scenario 3):** Realloc operations preserve existing data when size increases or decreases |
| 54 | + * - **Pass (Scenario 3):** Magic number patterns maintained correctly after reallocation |
| 55 | + * - **Pass (Scenario 3):** Realloc to zero size properly frees memory blocks |
| 56 | + * |
| 57 | + * Dependencies: |
| 58 | + * - No specific hardware requirements, runs on any RT-Thread supported platform |
| 59 | + * - Software configuration (e.g., kernel options, driver initialization) |
| 60 | + * - `RT_USING_UTEST` must be enabled (`RT-Thread Utestcases`). |
| 61 | + * - `Memory Test` must be enabled (`RT-Thread Utestcases` -> `Kernel Core` -> 'Memory Test'). |
| 62 | + * - RT-Thread kernel with small memory management enabled |
| 63 | + * - rt_malloc and rt_free functions available for test buffer allocation |
| 64 | + * - RT_ALIGN_SIZE macro defined for memory alignment |
| 65 | + * - Random number generator (rand function) available for stress tests |
| 66 | + * - System tick timer (rt_tick_get) for test duration control |
| 67 | + * - Environmental assumptions |
| 68 | + * - TEST_MEM_SIZE (1024 bytes) sufficient for test operations |
| 69 | + * - System can handle high-frequency memory operations for stress tests |
| 70 | + * - Run the test case from the msh prompt: |
| 71 | + * `utest_run core.mem` |
| 72 | + * |
| 73 | + * Expected Results: |
| 74 | + * - The test case completes without errors or failed assertions. |
| 75 | + * - Memory heap fully recovered to initial state after functional test |
| 76 | + * - No memory leaks or corruption detected |
| 77 | + * - The utest framework prints: |
| 78 | + * `[ PASSED ] [ result ] testcase (core.mem)` |
| 79 | + * - Progress indicators (#) printed at regular intervals during stress tests |
9 | 80 | */ |
10 | 81 |
|
11 | 82 | #include <rtthread.h> |
|
0 commit comments