oxedyne/fe2o3/fe2o3_data/tests/gen_map.rs
4.3 KiB, 1 run
created by r1870400018:68815, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! Integration tests for the generation-swept map. |
| 2 | |
| 3 | use oxedyne_fe2o3_data::gen_map::GenMap; |
| 4 | |
| 5 | |
| 6 | #[test] |
| 7 | fn an_entry_untouched_for_two_generations_goes_at_the_sweep() { |
| 8 | let mut m: GenMap<u32, &str> = GenMap::unbounded(); |
| 9 | m.begin(); |
| 10 | assert!(m.insert(1, "kept", 10)); |
| 11 | assert!(m.insert(2, "lost", 10)); |
| 12 | m.sweep(); |
| 13 | assert_eq!(m.len(), 2); |
| 14 | |
| 15 | // The next compile reads one of them and not the other. Both are within the last two generations. |
| 16 | m.begin(); |
| 17 | assert_eq!(m.get(&1), Some(&"kept")); |
| 18 | assert_eq!(m.sweep(), 0); |
| 19 | assert_eq!(m.len(), 2); |
| 20 | |
| 21 | // The compile after reads neither generation's untouched entry: 2 was last touched two compiles ago. |
| 22 | m.begin(); |
| 23 | assert_eq!(m.get(&1), Some(&"kept")); |
| 24 | assert_eq!(m.sweep(), 1); |
| 25 | assert!(m.contains(&1)); |
| 26 | assert!(!m.contains(&2)); |
| 27 | assert_eq!(m.bytes(), 10); |
| 28 | } |
| 29 | |
| 30 | #[test] |
| 31 | fn a_read_in_every_compile_keeps_an_entry_for_ever() { |
| 32 | let mut m: GenMap<u32, u32> = GenMap::unbounded(); |
| 33 | m.begin(); |
| 34 | m.insert(7, 70, 4); |
| 35 | for _ in 0..20 { |
| 36 | m.begin(); |
| 37 | assert_eq!(m.get(&7), Some(&70)); |
| 38 | m.sweep(); |
| 39 | } |
| 40 | assert_eq!(m.len(), 1); |
| 41 | assert_eq!(m.bytes(), 4); |
| 42 | } |
| 43 | |
| 44 | #[test] |
| 45 | fn a_look_does_not_keep_an_entry_from_the_sweep() { |
| 46 | let mut m: GenMap<u32, u32> = GenMap::unbounded(); |
| 47 | m.begin(); |
| 48 | m.insert(1, 10, 1); |
| 49 | m.begin(); |
| 50 | m.begin(); |
| 51 | assert_eq!(m.peek(&1), Some(&10)); |
| 52 | assert_eq!(m.sweep(), 1); |
| 53 | assert!(m.is_empty()); |
| 54 | } |
| 55 | |
| 56 | #[test] |
| 57 | fn the_ceiling_stops_inserts_and_evicts_nothing() { |
| 58 | let mut m: GenMap<u32, u32> = GenMap::new(100); |
| 59 | m.begin(); |
| 60 | assert!(m.insert(1, 1, 60)); |
| 61 | assert!(m.insert(2, 2, 40)); |
| 62 | assert_eq!(m.bytes(), 100); |
| 63 | |
| 64 | // Full: a third entry is refused, and the two held are still there to read. |
| 65 | assert!(!m.insert(3, 3, 1)); |
| 66 | assert_eq!(m.refused(), 1); |
| 67 | assert_eq!(m.len(), 2); |
| 68 | assert_eq!(m.get(&1), Some(&1)); |
| 69 | assert_eq!(m.get(&2), Some(&2)); |
| 70 | assert_eq!(m.get(&3), None); |
| 71 | |
| 72 | // An entry larger than the whole ceiling is refused on an empty map as well. |
| 73 | let mut e: GenMap<u32, u32> = GenMap::new(100); |
| 74 | e.begin(); |
| 75 | assert!(!e.insert(1, 1, 101)); |
| 76 | assert!(e.is_empty()); |
| 77 | assert_eq!(e.bytes(), 0); |
| 78 | } |
| 79 | |
| 80 | #[test] |
| 81 | fn a_sweep_makes_room_and_inserts_resume() { |
| 82 | let mut m: GenMap<u32, u32> = GenMap::new(100); |
| 83 | m.begin(); |
| 84 | assert!(m.insert(1, 1, 100)); |
| 85 | assert!(!m.insert(2, 2, 1)); |
| 86 | m.sweep(); |
| 87 | m.begin(); |
| 88 | m.sweep(); |
| 89 | m.begin(); |
| 90 | assert_eq!(m.sweep(), 1); |
| 91 | assert_eq!(m.bytes(), 0); |
| 92 | assert!(m.insert(2, 2, 100)); |
| 93 | } |
| 94 | |
| 95 | #[test] |
| 96 | fn a_replacement_is_counted_by_its_growth_and_may_be_refused() { |
| 97 | let mut m: GenMap<u32, &str> = GenMap::new(100); |
| 98 | m.begin(); |
| 99 | assert!(m.insert(1, "old", 70)); |
| 100 | assert!(m.insert(2, "other", 30)); |
| 101 | // The same key at the same size fits, though the map is full. |
| 102 | assert!(m.insert(1, "same", 70)); |
| 103 | assert_eq!(m.bytes(), 100); |
| 104 | // A shrunken replacement frees the difference. |
| 105 | assert!(m.insert(1, "small", 20)); |
| 106 | assert_eq!(m.bytes(), 50); |
| 107 | // A grown one that would pass the ceiling is refused, and the old entry stands, touched. |
| 108 | assert!(!m.insert(1, "big", 71)); |
| 109 | assert_eq!(m.peek(&1), Some(&"small")); |
| 110 | assert_eq!(m.bytes(), 50); |
| 111 | m.begin(); |
| 112 | m.begin(); |
| 113 | assert!(!m.insert(1, "big", 71)); |
| 114 | assert_eq!(m.sweep(), 1); |
| 115 | assert!(m.contains(&1)); |
| 116 | assert!(!m.contains(&2)); |
| 117 | } |
| 118 | |
| 119 | #[test] |
| 120 | fn a_ceiling_set_below_the_held_bytes_evicts_nothing_and_refuses_until_it_is_met() { |
| 121 | let mut m: GenMap<u32, u32> = GenMap::new(100); |
| 122 | m.begin(); |
| 123 | m.insert(1, 1, 80); |
| 124 | m.set_ceiling(10); |
| 125 | assert_eq!(m.len(), 1); |
| 126 | assert_eq!(m.get(&1), Some(&1)); |
| 127 | assert!(!m.insert(2, 2, 1)); |
| 128 | assert!(!m.insert(1, 1, 80)); |
| 129 | m.begin(); |
| 130 | m.begin(); |
| 131 | m.sweep(); |
| 132 | assert!(m.insert(2, 2, 10)); |
| 133 | } |
| 134 | |
| 135 | #[test] |
| 136 | fn clear_empties_the_map_and_keeps_its_generation_and_ceiling() { |
| 137 | let mut m: GenMap<String, Vec<u8>> = GenMap::new(64); |
| 138 | m.begin(); |
| 139 | m.begin(); |
| 140 | m.insert("a".to_string(), vec![0; 8], 8); |
| 141 | assert!(!m.insert("b".to_string(), vec![0; 65], 65)); |
| 142 | m.clear(); |
| 143 | assert!(m.is_empty()); |
| 144 | assert_eq!(m.bytes(), 0); |
| 145 | assert_eq!(m.epoch(), 2); |
| 146 | assert_eq!(m.ceiling(), 64); |
| 147 | assert_eq!(m.refused(), 1); |
| 148 | // A borrowed key reads an owned one. |
| 149 | assert!(m.insert("c".to_string(), vec![1], 1)); |
| 150 | assert_eq!(m.get("c"), Some(&vec![1])); |
| 151 | } |
| 152 | |
| 153 | #[test] |
| 154 | fn an_unbounded_map_counts_sizes_without_overflowing() { |
| 155 | let mut m: GenMap<u32, u32> = GenMap::default(); |
| 156 | m.begin(); |
| 157 | assert!(m.insert(1, 1, usize::MAX - 1)); |
| 158 | assert!(!m.insert(2, 2, 2)); |
| 159 | assert_eq!(m.refused(), 1); |
| 160 | assert!(m.insert(2, 2, 1)); |
| 161 | assert_eq!(m.bytes(), usize::MAX); |
| 162 | } |