//! Ensures that a custom config behaves as the default config, until limits are reached. //! Prevents regression after #80. use crate::{cfg::CfgPrivate, Config, Slab}; struct CustomConfig; #[cfg(target_pointer_width = "64")] impl Config for CustomConfig { const INITIAL_PAGE_SIZE: usize = 32; const MAX_PAGES: usize = 15; const MAX_THREADS: usize = 256; const RESERVED_BITS: usize = 24; } #[cfg(not(target_pointer_width = "64"))] impl Config for CustomConfig { const INITIAL_PAGE_SIZE: usize = 16; const MAX_PAGES: usize = 6; const MAX_THREADS: usize = 128; const RESERVED_BITS: usize = 12; } // We should repeat actions several times to detect invalid lifecycle changes. const ITERS: u64 = 5; #[track_caller] fn slab_eq(mut lhs: Slab, mut rhs: Slab) { let mut lhs_vec = lhs.unique_iter().collect::>(); lhs_vec.sort_unstable(); let mut rhs_vec = rhs.unique_iter().collect::>(); rhs_vec.sort_unstable(); assert_eq!(lhs_vec, rhs_vec); } /// Calls `insert(); remove()` multiple times to detect invalid releasing. /// Initially, it revealed bugs in the `Slot::release_with()` implementation. #[test] fn insert_remove() { eprintln!("bits={}; config={:#?}", usize::BITS, CustomConfig::debug()); let default_slab = Slab::::new(); let custom_slab = Slab::::new_with_config::(); for i in 0..=ITERS { let idx = default_slab.insert(i).unwrap(); assert!(default_slab.remove(idx)); let idx = custom_slab.insert(i).unwrap(); assert!(custom_slab.remove(idx)); } slab_eq(custom_slab, default_slab); } /// Calls `get()` multiple times to detect invalid ref counting. /// Initially, it revealed bugs in the `Slot::get()` implementation. #[test] fn double_get() { eprintln!("bits={}; config={:#?}", usize::BITS, CustomConfig::debug()); let default_slab = Slab::::new(); let custom_slab = Slab::::new_with_config::(); for i in 0..=ITERS { let idx = default_slab.insert(i).unwrap(); assert!(default_slab.get(idx).is_some()); assert!(default_slab.get(idx).is_some()); assert!(default_slab.remove(idx)); let idx = custom_slab.insert(i).unwrap(); assert!(custom_slab.get(idx).is_some()); assert!(custom_slab.get(idx).is_some()); assert!(custom_slab.remove(idx)); } slab_eq(custom_slab, default_slab); }