use std::hash::Hash; use priority_container::{unique::max::UniquePrioContainerMax, StableUniquePrioContainer}; fn make_invariant_test(len: usize, max: usize) -> UniquePrioContainerMax> { let mut heap = UniquePrioContainerMax::new(max); let to_add = (0..len) .into_iter() .map(|i| UniqueItem::new(i, i as u32)) .collect::>(); heap.extend(to_add); heap } #[test] fn test1() { for i in (10..100).step_by(10) { let container = make_invariant_test(i, 100); let expected = (0..i).collect::>(); assert_eq!( container.into_iter().map(|i| i.item).collect::>(), expected ); } } struct UniqueItem { item: T, val: u32, } impl Hash for UniqueItem { fn hash(&self, state: &mut H) { self.item.hash(state); } } impl UniqueItem { fn new(item: T, val: u32) -> Self { Self { item, val } } } impl PartialEq for UniqueItem { #[inline] fn eq(&self, other: &Self) -> bool { self.item == other.item } } impl Eq for UniqueItem {} impl PartialOrd for UniqueItem { fn partial_cmp(&self, other: &Self) -> Option { self.val.partial_cmp(&other.val) } } impl Ord for UniqueItem { fn cmp(&self, other: &Self) -> std::cmp::Ordering { self.val.cmp(&other.val) } } impl Clone for UniqueItem { #[inline] fn clone(&self) -> Self { Self { item: self.item.clone(), val: self.val, } } } #[test] fn test_stability_simple_unique() { for _ in 0..1000 { let mut heap = StableUniquePrioContainer::new(10); for _ in 0..20 { heap.insert(UniqueItem::new("9", 2)); } assert_eq!(heap.len(), 1); heap.insert(UniqueItem::new("9", 2)); heap.insert(UniqueItem::new("9", 2)); assert_eq!(heap.len(), 1); heap.insert(UniqueItem::new("8", 2)); assert_eq!(heap.len(), 2); for _ in 0..4 { heap.insert(UniqueItem::new("7", 2)); heap.insert(UniqueItem::new("a", 1)); heap.insert(UniqueItem::new("b", 1)); heap.insert(UniqueItem::new("c", 1)); heap.insert(UniqueItem::new("d", 1)); } heap.insert(UniqueItem::new("7", 2)); heap.insert(UniqueItem::new("e", 0)); heap.insert(UniqueItem::new("e", 0)); let out = heap.into_iter().map(|i| i.item).collect::>(); assert_eq!(out, vec!["9", "8", "7", "a", "b", "c", "d", "e"]); } }