Module test

Module test 

Source
Expand description

Test template for ManagerTrait implementations.

This module provides a comprehensive test suite that can be used to validate any implementation of the ManagerTrait. The tests cover all the core functionality including allocation, deallocation, reallocation, and cache management.

§Usage

The easiest way to test your memory manager implementation is to use the implement_memory_manager_tests! macro, which generates individual test functions for each test category:

use memory::implement_memory_manager_tests;

#[cfg(test)]
mod tests {
    use super::*;
    use memory::{Manager, ManagerTrait};
     
    struct MyAllocator;
    impl ManagerTrait for MyAllocator {
        // ... implementation ...
    }
     
    static ALLOCATOR: MyAllocator = MyAllocator;
     
    implement_memory_manager_tests! {
        Manager::new(&ALLOCATOR)
    }
}

This will generate 14 individual #[test] functions, each testing a specific aspect of your memory manager implementation.

§Using Individual Test Functions

You can also call individual test functions directly:

use memory::{Manager, test_template};

#[cfg(test)]
mod tests {
    use super::*;

    fn get_test_manager() -> Manager<'static> {
        static ALLOCATOR: YourAllocatorType = YourAllocatorType::new();
        Manager::new(&ALLOCATOR)
    }

    #[test]
    fn test_basic_allocation() {
        let manager = get_test_manager();
        test_template::test_basic_allocation($manager);
    }

    #[test]
    fn test_all() {
        let manager = get_test_manager();
        test_template::run_all_tests($manager);
    }
}

§Test Categories

§Basic Allocation Tests

  • test_basic_allocation - Basic allocation and deallocation
  • test_zero_sized_allocation - Zero-size allocation handling
  • test_aligned_allocation - Various alignment requirements
  • test_allocation_with_capabilities - Capability-based allocation

§Deallocation & Reallocation Tests

  • test_deallocation - Memory deallocation and usage tracking
  • test_reallocation_grow - Growing allocations
  • test_reallocation_shrink - Shrinking allocations
  • test_reallocation_same_size - Same-size reallocation

§Complex Operations Tests

  • test_multiple_allocations - Multiple simultaneous allocations
  • test_allocation_pattern - Complex allocation/deallocation patterns
  • test_large_allocation - Large memory allocations

§Manager Features Tests

  • test_memory_statistics - Memory usage statistics
  • test_page_size - Page size reporting
  • test_cache_flush_operations - Cache management operations

Functions§

test_aligned_allocation
Tests allocation with specific alignment requirements.
test_allocation_pattern
Tests allocation and deallocation pattern.
test_allocation_with_capabilities
Tests allocation with different capability flags.
test_basic_allocation
Tests basic memory allocation.
test_cache_flush_operations
Tests cache flush operations.
test_deallocation
Tests memory deallocation.
test_large_allocation
Tests large memory allocation.
test_memory_statistics
Tests memory usage statistics.
test_multiple_allocations
Tests multiple simultaneous allocations.
test_page_size
Tests the page size reporting.
test_reallocation_grow
Tests memory reallocation to a larger size.
test_reallocation_same_size
Tests reallocation with the same size.
test_reallocation_shrink
Tests memory reallocation to a smaller size.
test_zero_sized_allocation
Tests zero-sized allocation behavior.