use swamp_slot_map::SlotMap; pub struct TestItem { name: String, } #[test] fn insert_and_get() { let mut slot_map = SlotMap::new(); let asset = TestItem { name: "Test 1".to_string(), }; let id = slot_map.insert(asset); // Retrieve the item by its ID let retrieved_asset = slot_map.get(&id); assert!(retrieved_asset.is_some()); assert_eq!(retrieved_asset.unwrap().name, "Test 1"); } #[test] fn iter() { let mut slot_map = SlotMap::new(); let asset_id1 = slot_map.insert(TestItem { name: "Test 1".to_string(), }); let asset_id2 = slot_map.insert(TestItem { name: "Test 2".to_string(), }); let asset_id3 = slot_map.insert(TestItem { name: "Test 3".to_string(), }); let inserted_ids = [asset_id1, asset_id2, asset_id3]; // Immutable iteration over items, including their ID let mut iter_count = 0; for (id, item) in slot_map.iter() { iter_count += 1; assert!(inserted_ids.contains(&id)); assert!(item.name.starts_with("Test")); } assert_eq!(iter_count, 3); } #[test] fn iter_mut() { let mut slot_map = SlotMap::new(); let asset_id1 = slot_map.insert(TestItem { name: "Test 1".to_string(), }); let asset_id2 = slot_map.insert(TestItem { name: "Test 2".to_string(), }); let asset_id3 = slot_map.insert(TestItem { name: "Test 3".to_string(), }); let inserted_ids = [asset_id1, asset_id2, asset_id3]; for (id, item) in slot_map.iter_mut() { assert!(inserted_ids.contains(&id)); assert!(item.name.starts_with("Test")); item.name += " (Updated)"; } let updated_asset1 = slot_map.get(&asset_id1).unwrap(); let updated_asset2 = slot_map.get(&asset_id2).unwrap(); let updated_asset3 = slot_map.get(&asset_id3).unwrap(); assert_eq!(updated_asset1.name, "Test 1 (Updated)"); assert_eq!(updated_asset2.name, "Test 2 (Updated)"); assert_eq!(updated_asset3.name, "Test 3 (Updated)"); }