|
1 | 1 | use std::collections::btree_map::Entry::{Occupied, Vacant};
|
2 | 2 | use std::collections::BTreeMap;
|
| 3 | +use std::convert::TryFrom; |
| 4 | +use std::fmt::Debug; |
3 | 5 | use std::iter::FromIterator;
|
4 | 6 | use std::ops::Bound::{self, Excluded, Included, Unbounded};
|
5 | 7 | use std::rc::Rc;
|
@@ -57,36 +59,65 @@ fn test_basic_large() {
|
57 | 59 | #[test]
|
58 | 60 | fn test_basic_small() {
|
59 | 61 | let mut map = BTreeMap::new();
|
| 62 | + // Empty, shared root: |
60 | 63 | assert_eq!(map.remove(&1), None);
|
61 | 64 | assert_eq!(map.len(), 0);
|
| 65 | + assert_eq!(map.get(&1), None); |
| 66 | + assert_eq!(map.get_mut(&1), None); |
62 | 67 | assert_eq!(map.first_key_value(), None);
|
63 | 68 | assert_eq!(map.last_key_value(), None);
|
64 |
| - assert_eq!(map.get(&1), None); |
| 69 | + assert_eq!(map.keys().count(), 0); |
| 70 | + assert_eq!(map.values().count(), 0); |
65 | 71 | assert_eq!(map.insert(1, 1), None);
|
| 72 | + |
| 73 | + // 1 key-value pair: |
66 | 74 | assert_eq!(map.len(), 1);
|
67 | 75 | assert_eq!(map.get(&1), Some(&1));
|
| 76 | + assert_eq!(map.get_mut(&1), Some(&mut 1)); |
68 | 77 | assert_eq!(map.first_key_value(), Some((&1, &1)));
|
69 | 78 | assert_eq!(map.last_key_value(), Some((&1, &1)));
|
| 79 | + assert_eq!(map.keys().collect::<Vec<_>>(), vec![&1]); |
| 80 | + assert_eq!(map.values().collect::<Vec<_>>(), vec![&1]); |
70 | 81 | assert_eq!(map.insert(1, 2), Some(1));
|
71 | 82 | assert_eq!(map.len(), 1);
|
72 | 83 | assert_eq!(map.get(&1), Some(&2));
|
| 84 | + assert_eq!(map.get_mut(&1), Some(&mut 2)); |
73 | 85 | assert_eq!(map.first_key_value(), Some((&1, &2)));
|
74 | 86 | assert_eq!(map.last_key_value(), Some((&1, &2)));
|
| 87 | + assert_eq!(map.keys().collect::<Vec<_>>(), vec![&1]); |
| 88 | + assert_eq!(map.values().collect::<Vec<_>>(), vec![&2]); |
75 | 89 | assert_eq!(map.insert(2, 4), None);
|
| 90 | + |
| 91 | + // 2 key-value pairs: |
76 | 92 | assert_eq!(map.len(), 2);
|
77 | 93 | assert_eq!(map.get(&2), Some(&4));
|
| 94 | + assert_eq!(map.get_mut(&2), Some(&mut 4)); |
78 | 95 | assert_eq!(map.first_key_value(), Some((&1, &2)));
|
79 | 96 | assert_eq!(map.last_key_value(), Some((&2, &4)));
|
| 97 | + assert_eq!(map.keys().collect::<Vec<_>>(), vec![&1, &2]); |
| 98 | + assert_eq!(map.values().collect::<Vec<_>>(), vec![&2, &4]); |
80 | 99 | assert_eq!(map.remove(&1), Some(2));
|
| 100 | + |
| 101 | + // 1 key-value pair: |
81 | 102 | assert_eq!(map.len(), 1);
|
82 | 103 | assert_eq!(map.get(&1), None);
|
| 104 | + assert_eq!(map.get_mut(&1), None); |
83 | 105 | assert_eq!(map.get(&2), Some(&4));
|
| 106 | + assert_eq!(map.get_mut(&2), Some(&mut 4)); |
84 | 107 | assert_eq!(map.first_key_value(), Some((&2, &4)));
|
85 | 108 | assert_eq!(map.last_key_value(), Some((&2, &4)));
|
| 109 | + assert_eq!(map.keys().collect::<Vec<_>>(), vec![&2]); |
| 110 | + assert_eq!(map.values().collect::<Vec<_>>(), vec![&4]); |
86 | 111 | assert_eq!(map.remove(&2), Some(4));
|
| 112 | + |
| 113 | + // Empty but private root: |
87 | 114 | assert_eq!(map.len(), 0);
|
| 115 | + assert_eq!(map.get(&1), None); |
| 116 | + assert_eq!(map.get_mut(&1), None); |
88 | 117 | assert_eq!(map.first_key_value(), None);
|
89 | 118 | assert_eq!(map.last_key_value(), None);
|
| 119 | + assert_eq!(map.keys().count(), 0); |
| 120 | + assert_eq!(map.values().count(), 0); |
90 | 121 | assert_eq!(map.remove(&1), None);
|
91 | 122 | }
|
92 | 123 |
|
@@ -142,6 +173,87 @@ fn test_iter_rev() {
|
142 | 173 | test(size, map.into_iter().rev());
|
143 | 174 | }
|
144 | 175 |
|
| 176 | +/// Specifically tests iter_mut's ability to mutate the value of pairs in-line |
| 177 | +fn do_test_iter_mut_mutation<T>(size: usize) |
| 178 | +where |
| 179 | + T: Copy + Debug + Ord + TryFrom<usize>, |
| 180 | + <T as std::convert::TryFrom<usize>>::Error: std::fmt::Debug, |
| 181 | +{ |
| 182 | + let zero = T::try_from(0).unwrap(); |
| 183 | + let mut map: BTreeMap<T, T> = (0..size).map(|i| (T::try_from(i).unwrap(), zero)).collect(); |
| 184 | + |
| 185 | + // Forward and backward iteration sees enough pairs (also tested elsewhere) |
| 186 | + assert_eq!(map.iter_mut().count(), size); |
| 187 | + assert_eq!(map.iter_mut().rev().count(), size); |
| 188 | + |
| 189 | + // Iterate forwards, trying to mutate to unique values |
| 190 | + for (i, (k, v)) in map.iter_mut().enumerate() { |
| 191 | + assert_eq!(*k, T::try_from(i).unwrap()); |
| 192 | + assert_eq!(*v, zero); |
| 193 | + *v = T::try_from(i + 1).unwrap(); |
| 194 | + } |
| 195 | + |
| 196 | + // Iterate backwards, checking that mutations succeeded and trying to mutate again |
| 197 | + for (i, (k, v)) in map.iter_mut().rev().enumerate() { |
| 198 | + assert_eq!(*k, T::try_from(size - i - 1).unwrap()); |
| 199 | + assert_eq!(*v, T::try_from(size - i).unwrap()); |
| 200 | + *v = T::try_from(2 * size - i).unwrap(); |
| 201 | + } |
| 202 | + |
| 203 | + // Check that backward mutations succeeded |
| 204 | + for (i, (k, v)) in map.iter_mut().enumerate() { |
| 205 | + assert_eq!(*k, T::try_from(i).unwrap()); |
| 206 | + assert_eq!(*v, T::try_from(size + i + 1).unwrap()); |
| 207 | + } |
| 208 | +} |
| 209 | + |
| 210 | +#[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd, Ord)] |
| 211 | +#[repr(align(32))] |
| 212 | +struct Align32(usize); |
| 213 | + |
| 214 | +impl TryFrom<usize> for Align32 { |
| 215 | + type Error = (); |
| 216 | + |
| 217 | + fn try_from(s: usize) -> Result<Align32, ()> { |
| 218 | + Ok(Align32(s)) |
| 219 | + } |
| 220 | +} |
| 221 | + |
| 222 | +#[test] |
| 223 | +fn test_iter_mut_mutation() { |
| 224 | + // Check many alignments because various fields precede array in NodeHeader. |
| 225 | + // Check with size 0 which should not iterate at all. |
| 226 | + // Check with size 1 for a tree with one kind of node (root = leaf). |
| 227 | + // Check with size 12 for a tree with two kinds of nodes (root and leaves). |
| 228 | + // Check with size 144 for a tree with all kinds of nodes (root, internals and leaves). |
| 229 | + do_test_iter_mut_mutation::<u8>(0); |
| 230 | + do_test_iter_mut_mutation::<u8>(1); |
| 231 | + do_test_iter_mut_mutation::<u8>(12); |
| 232 | + do_test_iter_mut_mutation::<u8>(127); // not enough unique values to test 144 |
| 233 | + do_test_iter_mut_mutation::<u16>(1); |
| 234 | + do_test_iter_mut_mutation::<u16>(12); |
| 235 | + do_test_iter_mut_mutation::<u16>(144); |
| 236 | + do_test_iter_mut_mutation::<u32>(1); |
| 237 | + do_test_iter_mut_mutation::<u32>(12); |
| 238 | + do_test_iter_mut_mutation::<u32>(144); |
| 239 | + do_test_iter_mut_mutation::<u64>(1); |
| 240 | + do_test_iter_mut_mutation::<u64>(12); |
| 241 | + do_test_iter_mut_mutation::<u64>(144); |
| 242 | + do_test_iter_mut_mutation::<u128>(1); |
| 243 | + do_test_iter_mut_mutation::<u128>(12); |
| 244 | + do_test_iter_mut_mutation::<u128>(144); |
| 245 | + do_test_iter_mut_mutation::<Align32>(1); |
| 246 | + do_test_iter_mut_mutation::<Align32>(12); |
| 247 | + do_test_iter_mut_mutation::<Align32>(144); |
| 248 | +} |
| 249 | + |
| 250 | +#[test] |
| 251 | +fn test_into_key_slice_with_shared_root_past_bounds() { |
| 252 | + let mut map: BTreeMap<Align32, ()> = BTreeMap::new(); |
| 253 | + assert_eq!(map.get(&Align32(1)), None); |
| 254 | + assert_eq!(map.get_mut(&Align32(1)), None); |
| 255 | +} |
| 256 | + |
145 | 257 | #[test]
|
146 | 258 | fn test_values_mut() {
|
147 | 259 | let mut a = BTreeMap::new();
|
|
0 commit comments