heap_map/
tests.rs

1use super::*;
2
3impl<K: hash::Hash + Eq + Copy + Debug, V: Eq + Ord + Clone> Clone
4    for HeapMap<K, V>
5{
6    fn clone(&self) -> Self {
7        Self {
8            data: self.data.clone(),
9            mapping: self.mapping.clone(),
10        }
11    }
12}
13
14impl<K: hash::Hash + Eq + Copy + Debug, V: Eq + Ord + Clone> HeapMap<K, V> {
15    #[allow(dead_code)]
16    fn check_mono(&self) -> bool {
17        let mut me = self.clone();
18        let mut last_value = if let Some((_k, v)) = me.pop() {
19            v
20        } else {
21            return true;
22        };
23
24        while let Some((_k, v)) = me.pop() {
25            if v > last_value {
26                return false;
27            }
28            last_value = v;
29        }
30
31        true
32    }
33}
34
35#[test]
36fn test_simple() {
37    let mut map = HeapMap::<usize, usize>::new();
38    map.insert(&1, 1);
39    map.insert(&2, 2);
40    assert_eq!(Some((2, 2)), map.pop());
41    assert_eq!(Some((1, 1)), map.pop());
42    assert_eq!(None, map.pop());
43}
44
45#[test]
46fn test_random() {
47    const SIZE: usize = 10000usize;
48    let key = || rand::random::<usize>() % (SIZE * 2);
49    let mut map = HeapMap::<usize, usize>::new();
50
51    for _round in 0..10 {
52        for _ in 0..SIZE {
53            map.insert(&key(), rand::random());
54        }
55        for _iter in 0..1000 {
56            map.remove(&key());
57            map.insert(&key(), rand::random());
58            if _iter % 10 == 9 {
59                assert!(map.check_mono());
60            }
61        }
62    }
63}