|
6 | 6 | * Change Logs: |
7 | 7 | * Date Author Notes |
8 | 8 | * 2021-10-14 tyx the first version |
| 9 | + * 2025-11-13 CYFS Add standardized utest documentation block |
| 10 | + */ |
| 11 | + |
| 12 | +/** |
| 13 | + * Test Case Name: Kernel Core Slab Memory Management Test |
| 14 | + * |
| 15 | + * Test Objectives: |
| 16 | + * - Validate RT-Thread slab allocator behavior under deterministic and randomized workloads |
| 17 | + * - Verify core APIs: rt_slab_init, rt_slab_alloc, rt_slab_free, rt_slab_realloc, rt_slab_detach |
| 18 | + * |
| 19 | + * Test Scenarios: |
| 20 | + * - **Scenario 1 (Random Allocation Stress Test / slab_alloc_test):** |
| 21 | + * 1. Initialize a 1 MB slab pool backed by dynamic memory. |
| 22 | + * 2. Perform mixed random-sized allocations with ~60% allocation probability. |
| 23 | + * 3. Reclaim half the outstanding allocations when the pool exhausts resources. |
| 24 | + * 4. Validate slab payload integrity using magic bytes before every free. |
| 25 | + * 5. Drain remaining allocations, ensuring list bookkeeping reaches zero. |
| 26 | + * - **Scenario 2 (Random Reallocation Stress Test / slab_realloc_test):** |
| 27 | + * 1. Initialize the slab pool and allocate a context table within it. |
| 28 | + * 2. Randomly reallocate tracked blocks with size adjustments between 0 and 5 units. |
| 29 | + * 3. On realloc failures, free random existing blocks to relieve pressure. |
| 30 | + * 4. Confirm in-place data integrity after size changes using magic byte comparisons. |
| 31 | + * 5. Reallocate to zero size to validate free semantics, then release all resources. |
| 32 | + * |
| 33 | + * Verification Metrics: |
| 34 | + * - **Scenario 1:** Allocations return aligned pointers; data regions retain magic patterns until freed; head.count returns to 0 before teardown. |
| 35 | + * - **Scenario 2:** Reallocated blocks preserve prior data up to the min(old, new) length; zero-size realloc frees memory; final sweep leaves no outstanding allocations. |
| 36 | + * |
| 37 | + * Dependencies: |
| 38 | + * - Requires dynamic memory support (rt_malloc/rt_free) to back the slab pool. |
| 39 | + * - `RT_USING_UTEST` enabled with test registered under `core.slab`. |
| 40 | + * - System tick (`rt_tick_get`) and random number generator (`rand`) available for timing and stochastic operations. |
| 41 | + * |
| 42 | + * Expected Results: |
| 43 | + * - Test case completes without assertion failures. |
| 44 | + * - Console prints progress markers (`#`) during stress loops and `[ PASSED ] [ result ] testcase (core.slab)` when run via `utest_run core.slab`. |
9 | 45 | */ |
10 | 46 |
|
11 | 47 | #include <rtthread.h> |
|
0 commit comments