robinxx_map 0.1.0

High-performance, thread-safe open-addressing hash map using Robin Hood displacement & xxHash3.
Documentation
//! Integration tests covering 100% of the public API surface.
//!
//! Uses `with_key`/`with_key_mut` closure-based access pattern per thread-safety contract.

#![cfg(test)]

use robinxx_map::RobinHoodMap;
use std::vec::Vec;

#[test]
fn test_entry_api_flow() {
    let map = RobinHoodMap::new();

    // Vacant → Insert via Entry API
    *map.entry(1).or_insert(10) += 5;

    // Read via with_key closure
    let result = map.with_key(&1, |v| *v);
    assert_eq!(result, Some(15));

    // Occupied → Modify via with_key_mut
    let modified = map.with_key_mut(&1, |v| {
        *v *= 2;
        *v
    });
    assert_eq!(modified, Some(30));

    // Duplicate insert prevention
    let inserted = map.insert(1, 0);
    assert!(!inserted);

    // Verify value preserved
    assert_eq!(map.with_key(&1, |v| *v), Some(30));
}

#[test]
fn test_iterators_yield_correct_data() {
    let map = RobinHoodMap::new();
    for i in 0..5 {
        let _ = map.insert(i, i * 100);
    }

    // Keys iterator
    let mut keys: Vec<_> = map.keys().collect();
    keys.sort();
    assert_eq!(keys, vec![&0, &1, &2, &3, &4]);

    // Values iterator
    let mut vals: Vec<_> = map.values().collect();
    vals.sort();
    assert_eq!(vals, vec![&0, &100, &200, &300, &400]);

    // Pairs iterator
    let pairs: Vec<_> = map.iter().collect();
    assert_eq!(pairs.len(), 5);
}

#[test]
fn test_iter_mut_preserves_keys() {
    let map = RobinHoodMap::new();
    let _ = map.insert("a", 10);
    let _ = map.insert("b", 20);

    for v in map.iter_mut() {
        *v += 5;
    }

    assert_eq!(map.with_key(&"a", |v| *v), Some(15));
    assert_eq!(map.with_key(&"b", |v| *v), Some(25));
}

#[test]
fn test_drain_clears_map_completely() {
    let map = RobinHoodMap::new();
    for i in 0..3 {
        let _ = map.insert(i, i);
    }

    let drained: Vec<_> = map.drain().collect();
    assert_eq!(drained.len(), 3);
    assert!(map.is_empty());
    assert!(map.capacity() > 0); // Capacity retained
}

#[test]
fn test_with_key_closure_semantics() {
    let map = RobinHoodMap::new();
    let _ = map.insert("x", 42);

    // with_key returns transformed value without holding reference
    let doubled = map.with_key(&"x", |v| v * 2);
    assert_eq!(doubled, Some(84));

    // Original value unchanged
    assert_eq!(map.with_key(&"x", |v| *v), Some(42));

    // with_key_mut allows in-place mutation
    let result = map.with_key_mut(&"x", |v| {
        *v += 10;
        *v
    });
    assert_eq!(result, Some(52));

    // Verify mutation persisted
    assert_eq!(map.with_key(&"x", |v| *v), Some(52));
}

#[test]
fn test_with_key_on_missing_key() {
    let map: RobinHoodMap<u32, u32> = RobinHoodMap::new();

    // with_key returns None for missing key
    let result = map.with_key(&999, |v| v * 2);
    assert_eq!(result, None);

    // with_key_mut also returns None
    let result = map.with_key_mut(&999, |v| *v += 1);
    assert_eq!(result, None);
}