use crate::{DeferredMap, Handle, Key};
#[test]
fn test_handle_key() {
let mut map = DeferredMap::<i32>::new();
let handle = map.allocate_handle();
let key = handle.key();
assert!(key.index() > 0);
}
#[test]
fn test_handle_index_extraction() {
let mut map = DeferredMap::<i32>::new();
let handle = map.allocate_handle();
let index = handle.index();
assert_eq!(index, 1);
}
#[test]
fn test_handle_generation_extraction() {
let mut map = DeferredMap::<i32>::new();
let handle = map.allocate_handle();
let generation = handle.generation();
assert_eq!(generation.get(), 1);
}
#[test]
fn test_handle_encoding_consistency() {
let mut map = DeferredMap::<i32>::new();
let handle = map.allocate_handle();
let key = handle.key();
let index = handle.index();
let generation = handle.generation();
assert_eq!(key.index() as u64, index as u64);
assert_eq!(key.generation(), generation);
}
#[test]
fn test_multiple_handles_different_indices() {
let mut map = DeferredMap::<i32>::new();
let handle1 = map.allocate_handle();
let handle2 = map.allocate_handle();
let handle3 = map.allocate_handle();
let index1 = handle1.index();
let index2 = handle2.index();
let index3 = handle3.index();
assert_ne!(index1, index2);
assert_ne!(index2, index3);
assert_ne!(index1, index3);
assert_eq!(index1 + 1, index2);
assert_eq!(index2 + 1, index3);
}
#[test]
fn test_handle_generation_after_reuse() {
let mut map = DeferredMap::new();
let handle1 = map.allocate_handle();
let gen1 = handle1.generation();
let key1 = handle1.key();
map.insert(handle1, 42);
map.remove(key1);
let handle2 = map.allocate_handle();
let gen2 = handle2.generation();
assert_eq!(gen2.get(), gen1.get() + 1);
}
#[test]
fn test_handle_cannot_be_cloned() {
let mut map = DeferredMap::<i32>::new();
let handle = map.allocate_handle();
map.insert(handle, 42);
}
#[test]
fn test_handle_equality() {
let mut map = DeferredMap::<i32>::new();
let handle1 = map.allocate_handle();
let handle2 = map.allocate_handle();
assert_ne!(handle1, handle2);
let key = handle1.key();
let handle3 = Handle::new(key);
assert_eq!(handle1, handle3);
}
#[test]
fn test_handle_debug_format() {
let mut map = DeferredMap::<i32>::new();
let handle = map.allocate_handle();
let debug_str = format!("{:?}", handle);
assert!(debug_str.contains("Handle"));
}
#[test]
fn test_handle_with_large_index() {
let mut map = DeferredMap::<i32>::new();
let mut handles = Vec::new();
for _ in 0..1000 {
handles.push(map.allocate_handle());
}
let last_handle = handles.last().unwrap();
let index = last_handle.index();
assert!(index >= 1000);
}
#[test]
fn test_handle_generation_consistency_after_multiple_reuses() {
let mut map = DeferredMap::new();
let handle1 = map.allocate_handle();
let index = handle1.index();
let mut prev_gen = handle1.generation();
let mut current_key = handle1.key();
map.insert(handle1, 1);
for i in 2..=10 {
map.remove(current_key);
let handle = map.allocate_handle();
assert_eq!(handle.index(), index);
let curr_gen = handle.generation();
assert_eq!(curr_gen.get(), prev_gen.get() + 1); prev_gen = curr_gen;
current_key = handle.key();
map.insert(handle, i);
}
}
#[test]
fn test_handle_with_max_index() {
let max_index = u32::MAX;
let generation = 1u32;
let key = (generation as u64) << 32 | max_index as u64;
#[cfg(debug_assertions)]
let key = crate::DefaultKey {
raw: key,
#[cfg(debug_assertions)]
map_id: 0,
};
let handle = Handle::new(key);
assert_eq!(handle.index(), max_index);
assert_eq!(handle.generation().get(), generation); }
#[test]
fn test_handle_with_max_generation() {
let index = 1u32;
let max_generation = u32::MAX; let key = (max_generation as u64) << 32 | index as u64;
#[cfg(debug_assertions)]
let key = crate::DefaultKey {
raw: key,
#[cfg(debug_assertions)]
map_id: 0,
};
let handle = Handle::new(key);
assert_eq!(handle.index(), index);
assert_eq!(handle.generation().get(), max_generation);
}
#[test]
fn test_release_handle_basic() {
let mut map = DeferredMap::<i32>::new();
let handle = map.allocate_handle();
map.release_handle(handle);
assert_eq!(map.len(), 0);
}
#[test]
fn test_release_handle_allows_reuse() {
let mut map = DeferredMap::<i32>::new();
let handle1 = map.allocate_handle();
let index1 = handle1.index();
map.release_handle(handle1);
let handle2 = map.allocate_handle();
let index2 = handle2.index();
assert_eq!(index1, index2);
}
#[test]
fn test_release_handle_increments_generation() {
let mut map = DeferredMap::<i32>::new();
let handle1 = map.allocate_handle();
let gen1 = handle1.generation();
map.release_handle(handle1);
let handle2 = map.allocate_handle();
let gen2 = handle2.generation();
assert_eq!(gen2.get(), gen1.get() + 1);
}
#[test]
fn test_release_handle_multiple_handles() {
let mut map = DeferredMap::<i32>::new();
let handles: Vec<_> = (0..10).map(|_| map.allocate_handle()).collect();
for handle in handles {
map.release_handle(handle);
}
assert_eq!(map.len(), 0);
}
#[test]
fn test_release_handle_doesnt_affect_len() {
let mut map = DeferredMap::new();
let h1 = map.allocate_handle();
map.insert(h1, 1);
let h2 = map.allocate_handle();
map.insert(h2, 2);
assert_eq!(map.len(), 2);
let h3 = map.allocate_handle();
map.release_handle(h3);
assert_eq!(map.len(), 2);
}
#[test]
fn test_release_handle_interleaved_with_insertions() {
let mut map = DeferredMap::new();
let h1 = map.allocate_handle();
let k1 = h1.key();
let h2 = map.allocate_handle();
let h3 = map.allocate_handle();
let k3 = h3.key();
let h4 = map.allocate_handle();
map.insert(h1, 1);
map.release_handle(h2); map.insert(h3, 3);
map.release_handle(h4);
assert_eq!(map.get(k1), Some(&1));
assert_eq!(map.get(k3), Some(&3));
assert_eq!(map.len(), 2);
}
#[test]
fn test_release_handle_then_insert_at_same_slot() {
let mut map = DeferredMap::new();
let handle1 = map.allocate_handle();
let index1 = handle1.index();
map.release_handle(handle1);
let handle2 = map.allocate_handle();
let index2 = handle2.index();
let key2 = handle2.key();
map.insert(handle2, 42);
assert_eq!(index1, index2);
assert_eq!(map.get(key2), Some(&42));
}
#[test]
fn test_release_handle_lifo_order() {
let mut map = DeferredMap::<i32>::new();
let h1 = map.allocate_handle();
let idx1 = h1.index();
let h2 = map.allocate_handle();
let idx2 = h2.index();
let h3 = map.allocate_handle();
let idx3 = h3.index();
map.release_handle(h1);
map.release_handle(h2);
map.release_handle(h3);
let h4 = map.allocate_handle();
assert_eq!(h4.index(), idx3);
let h5 = map.allocate_handle();
assert_eq!(h5.index(), idx2);
let h6 = map.allocate_handle();
assert_eq!(h6.index(), idx1);
}
#[test]
fn test_release_handle_with_capacity_check() {
let mut map = DeferredMap::<i32>::new();
let handles: Vec<_> = (0..10).map(|_| map.allocate_handle()).collect();
assert!(map.capacity() >= 10);
for h in handles {
map.release_handle(h);
}
assert!(map.capacity() >= 10);
assert_eq!(map.len(), 0);
}
#[test]
fn test_release_handle_mixed_with_removal() {
let mut map = DeferredMap::new();
let h1 = map.allocate_handle();
let k1 = h1.key();
map.insert(h1, 1);
let h2 = map.allocate_handle();
map.release_handle(h2);
map.remove(k1);
let h3 = map.allocate_handle();
let k3 = h3.key();
let h4 = map.allocate_handle();
let k4 = h4.key();
map.insert(h3, 3);
map.insert(h4, 4);
assert_eq!(map.get(k3), Some(&3));
assert_eq!(map.get(k4), Some(&4));
assert_eq!(map.len(), 2);
}