From efa81da13beb802a4a1ee7d5ab733f73a75b01f8 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Thu, 12 Sep 2024 12:24:09 -0700 Subject: [PATCH] Test the various heuristics of `erase_indices` This is a targeted test to make sure we cover all the heuristic edge cases in `erase_indices`, used by `drain` and other methods. I found a failure from `cargo mutants` where we still passed tests after `erase_indices_sweep` was replaced with an empty body. I was concerned because that function contains `unsafe` code, so we *really* need it tested. It turns out that we do *sometimes* hit that in `quickcheck` tests, but might miss that if we're randomly unlucky, so this PR adds a new test that will hit all the edge cases every time. --- src/map/tests.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/map/tests.rs b/src/map/tests.rs index cbd4b4f2..d2707a44 100644 --- a/src/map/tests.rs +++ b/src/map/tests.rs @@ -541,6 +541,26 @@ fn into_values() { assert!(values.contains(&'c')); } +#[test] +fn drain_range() { + // Test the various heuristics of `erase_indices` + for range in [ + 0..0, // nothing erased + 10..90, // reinsert the few kept (..10 and 90..) + 80..90, // update the few to adjust (80..) + 20..30, // sweep everything + ] { + let mut vec = Vec::from_iter(0..100); + let mut map: IndexMap = (0..100).map(|i| (i, ())).collect(); + drop(vec.drain(range.clone())); + drop(map.drain(range)); + assert!(vec.iter().eq(map.keys())); + for (i, x) in vec.iter().enumerate() { + assert_eq!(map.get_index_of(x), Some(i)); + } + } +} + #[test] #[cfg(feature = "std")] fn from_array() {