Skip to main content

Module testing

Module testing 

Source
Expand description

Conformance helpers for downstream impls of FixedRange and Allocator.

Drop these into a #[test] to validate that a custom implementation meets the trait contracts that wrappers in the forge-alloc family rely on. Helpers panic on failure with a human-readable diagnostic, so they compose with the standard #[test] harness without ceremony.

§Example

use forge_alloc_core::testing::{
    assert_allocator_basic_round_trip,
    assert_fixed_range_invariants,
};

#[test]
fn my_allocator_meets_contract() {
    let a = MyAllocator::new(4096);
    assert_fixed_range_invariants(&a);
    assert_allocator_basic_round_trip(&a);
}

These helpers cover the structural and basic behavioral parts of the contract — pointer non-null-ness, alignment, non-overlap, address-range stability, single-threaded write-then-read round-trip, contains membership for issued pointers. They do NOT cover:

  • Concurrent-access invariants (Inv 3 — “allocator doesn’t concurrently write user-visible bytes”). Verifying that requires a multi-threaded probe with a happens-before discipline, which is allocator-specific.
  • grow / shrink semantics (Inv 5). Add helpers for these as they’re needed.
  • Statistical / performance properties (no benchmarking) or hardening-specific behavior (no MAC verification, no canary checks). Use crate-specific tests for those.

On failure, helpers panic at the call site (each is #[track_caller]) so the test report points at your test, not at this file.

Functions§

assert_allocator_basic_round_trip
Verify the basic round-trip invariants of Allocator:
assert_allocator_respects_alignment
Verify that allocate returns pointers aligned to the requested alignment, across every power-of-two alignment from 1 to 512. (Covers up through CACHE_LINE = 128 on every supported target plus a 4-KiB-page-aligned slot.)
assert_combined_invariants
Verify that an allocator implements both FixedRange and Allocator consistently:
assert_fixed_range_invariants
Verify the structural invariants of FixedRange: