use crate::{DeferredMap, Key};
#[test]
fn test_basic_insertion() {
let mut map = DeferredMap::new();
let handle = map.allocate_handle();
let key = handle.key();
map.insert(handle, 42);
assert_eq!(map.get(key), Some(&42));
assert_eq!(map.len(), 1);
}
#[test]
fn test_multiple_sequential_insertions() {
let mut map = DeferredMap::new();
let mut keys = Vec::new();
for i in 0..100 {
let handle = map.allocate_handle();
let key = handle.key();
map.insert(handle, i);
keys.push(key);
}
assert_eq!(map.len(), 100);
for (i, &key) in keys.iter().enumerate() {
assert_eq!(map.get(key), Some(&i));
}
}
#[test]
fn test_insertion_with_different_types() {
let mut map_string = DeferredMap::new();
let h = map_string.allocate_handle();
let k = h.key();
map_string.insert(h, "Hello".to_string());
assert_eq!(map_string.get(k), Some(&"Hello".to_string()));
let mut map_vec = DeferredMap::new();
let h = map_vec.allocate_handle();
let k = h.key();
map_vec.insert(h, vec![1, 2, 3]);
assert_eq!(map_vec.get(k), Some(&vec![1, 2, 3]));
let mut map_option = DeferredMap::new();
let h = map_option.allocate_handle();
let k = h.key();
map_option.insert(h, Some(42));
assert_eq!(map_option.get(k), Some(&Some(42)));
}
#[test]
fn test_insertion_with_zero_sized_type() {
let mut map = DeferredMap::new();
let handle = map.allocate_handle();
let key = handle.key();
map.insert(handle, ());
assert_eq!(map.get(key), Some(&()));
assert_eq!(map.len(), 1);
}
#[test]
fn test_insertion_returns_correct_key() {
let mut map = DeferredMap::new();
let handle = map.allocate_handle();
let handle_key = handle.key();
let handle_index = handle.index();
map.insert(handle, 42);
let key_index = handle_key.index();
assert_eq!(key_index, handle_index);
assert_eq!(map.get(handle_key), Some(&42));
}
#[test]
fn test_insertion_extends_slots_vec() {
let mut map = DeferredMap::new();
assert_eq!(map.capacity(), 0);
let h1 = map.allocate_handle();
map.insert(h1, 1);
assert!(map.capacity() >= 1);
for i in 2..=10 {
let h = map.allocate_handle();
map.insert(h, i);
}
assert!(map.capacity() >= 10);
assert_eq!(map.len(), 10);
}
#[test]
fn test_insertion_after_removal_reuses_slot() {
let mut map = DeferredMap::new();
let h1 = map.allocate_handle();
let k1 = h1.key();
map.insert(h1, 42);
let index1 = k1.index();
map.remove(k1);
let h2 = map.allocate_handle();
let k2 = h2.key();
map.insert(h2, 100);
let index2 = k2.index();
assert_eq!(index1, index2);
assert_ne!(k1, k2);
}
#[test]
fn test_insertion_with_preallocated_capacity() {
let mut map: DeferredMap<i32> = DeferredMap::with_capacity(100);
for i in 0..50 {
let h = map.allocate_handle();
map.insert(h, i);
}
assert_eq!(map.len(), 50);
}
#[test]
fn test_insertion_preserves_previous_values() {
let mut map = DeferredMap::new();
let mut keys = Vec::new();
for i in 0..10 {
let h = map.allocate_handle();
let k = h.key();
map.insert(h, i * 10);
keys.push(k);
}
for (i, &key) in keys.iter().enumerate() {
assert_eq!(map.get(key), Some(&(i * 10)));
}
}
#[test]
fn test_insertion_with_large_values() {
let mut map = DeferredMap::new();
let large_string = "a".repeat(10000);
let h = map.allocate_handle();
let k = h.key();
map.insert(h, large_string.clone());
assert_eq!(map.get(k), Some(&large_string));
}
#[test]
fn test_insertion_maintains_len_count() {
let mut map = DeferredMap::new();
assert_eq!(map.len(), 0);
let h1 = map.allocate_handle();
map.insert(h1, 1);
assert_eq!(map.len(), 1);
let h2 = map.allocate_handle();
map.insert(h2, 2);
assert_eq!(map.len(), 2);
let h3 = map.allocate_handle();
map.insert(h3, 3);
assert_eq!(map.len(), 3);
}
#[test]
fn test_insertion_with_custom_struct() {
#[derive(Debug, PartialEq)]
struct CustomStruct {
id: u32,
name: String,
values: Vec<i32>,
}
let mut map = DeferredMap::new();
let custom = CustomStruct {
id: 1,
name: "Test".to_string(),
values: vec![1, 2, 3],
};
let h = map.allocate_handle();
let k = h.key();
map.insert(h, custom);
if let Some(value) = map.get(k) {
assert_eq!(value.id, 1);
assert_eq!(value.name, "Test");
assert_eq!(value.values, vec![1, 2, 3]);
} else {
panic!("Value not found");
}
}
#[test]
fn test_insertion_interleaved_with_allocations() {
let mut map = DeferredMap::new();
let h1 = map.allocate_handle();
let k1 = h1.key();
let h2 = map.allocate_handle();
let k2 = h2.key();
let h3 = map.allocate_handle();
let k3 = h3.key();
map.insert(h2, 2);
map.insert(h1, 1);
map.insert(h3, 3);
assert_eq!(map.get(k1), Some(&1));
assert_eq!(map.get(k2), Some(&2));
assert_eq!(map.get(k3), Some(&3));
}
#[test]
fn test_insertion_with_box() {
let mut map = DeferredMap::new();
let boxed_value = Box::new(42);
let h = map.allocate_handle();
let k = h.key();
map.insert(h, boxed_value);
assert_eq!(map.get(k), Some(&Box::new(42)));
}