Debian Codegraph @

debian/missing-sources/litemap/src/testing.rs

raw
1 // This file is part of ICU4X. For terms of use, please see the file
2 // called LICENSE at the top level of the ICU4X source tree
3 // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4
5 //! Test utilities, primarily targeted to custom LiteMap stores.
6
7 use crate::store::*;
8 use crate::LiteMap;
9 use alloc::vec::Vec;
10 use core::fmt::Debug;
11
12 // Test code
13 #[expect(clippy::expect_used)]
14 fn check_equivalence<'a, K, V, S0, S1>(mut a: S0, mut b: S1)
15 where
16 K: Ord + Debug + PartialEq + 'a,
17 V: Debug + PartialEq + 'a,
18 S0: StoreMut<K, V> + StoreIterable<'a, K, V>,
19 S1: StoreMut<K, V> + StoreIterable<'a, K, V>,
20 {
21 let len = a.lm_len();
22 assert_eq!(len, b.lm_len());
23 if len == 0 {
24 assert!(a.lm_is_empty());
25 assert!(b.lm_is_empty());
26 }
27 for i in 0..len {
28 let a_kv = a.lm_get(i);
29 let b_kv = b.lm_get(i);
30 assert!(a_kv.is_some());
31 assert_eq!(a_kv, b_kv);
32 let a_kv_mut = a.lm_get_mut(i);
33 let b_kv_mut = b.lm_get_mut(i);
34 assert!(a_kv_mut.is_some());
35 assert_eq!(a_kv_mut, b_kv_mut);
36 }
37 for j in 0..len {
38 let needle = a.lm_get(j).expect("j is in range").0;
39 let a_binary = a.lm_binary_search_by(|k| k.cmp(needle));
40 let b_binary = a.lm_binary_search_by(|k| k.cmp(needle));
41 assert_eq!(Ok(j), a_binary);
42 assert_eq!(Ok(j), b_binary);
43 }
44 assert!(a.lm_get(len).is_none());
45 assert!(b.lm_get(len).is_none());
46 assert_eq!(a.lm_last(), b.lm_last());
47 }
48
49 // Test code
50 fn check_into_iter_equivalence<K, V, S0, S1>(a: S0, b: S1)
51 where
52 K: Ord + Debug + PartialEq,
53 V: Debug + PartialEq,
54 S0: StoreIntoIterator<K, V>,
55 S1: StoreIntoIterator<K, V>,
56 {
57 let a_vec = a.lm_into_iter().collect::<Vec<_>>();
58 let b_vec = b.lm_into_iter().collect::<Vec<_>>();
59 assert_eq!(a_vec, b_vec);
60 }
61
62 const SORTED_DATA: &[(u32, u64)] = &[
63 (106, 4816),
64 (147, 9864),
65 (188, 8588),
66 (252, 6031),
67 (434, 2518),
68 (574, 8500),
69 (607, 3756),
70 (619, 4965),
71 (663, 2669),
72 (724, 9211),
73 ];
74
75 const RANDOM_DATA: &[(u32, u64)] = &[
76 (546, 7490),
77 (273, 4999),
78 (167, 8078),
79 (176, 2101),
80 (373, 1304),
81 (339, 9613),
82 (561, 3620),
83 (301, 1214),
84 (483, 4453),
85 (704, 5359),
86 ];
87
88 // Test code
89 #[expect(clippy::panic)]
90 fn populate_litemap<S>(map: &mut LiteMap<u32, u64, S>)
91 where
92 S: StoreMut<u32, u64> + Debug,
93 {
94 assert_eq!(0, map.len());
95 assert!(map.is_empty());
96 for (k, v) in SORTED_DATA.iter() {
97 if map.try_append(*k, *v).is_some() {
98 panic!("appending sorted data: {k:?} to {map:?}");
99 };
100 }
101 assert_eq!(10, map.len());
102 for (k, v) in RANDOM_DATA.iter() {
103 match map.try_append(*k, *v) {
104 Some(_) => (), // OK
105 None => panic!("cannot append random data: {k:?} to{map:?}"),
106 };
107 }
108 assert_eq!(10, map.len());
109 for (k, v) in RANDOM_DATA.iter() {
110 map.insert(*k, *v);
111 }
112 assert_eq!(20, map.len());
113 }
114
115 /// Tests that a litemap that uses the given store as backend has behavior consistent with the
116 /// reference impl.
117 ///
118 /// Call this function in a test with the store impl to test as a valid backend for LiteMap.
119 // Test code
120 #[expect(clippy::expect_used)]
121 pub fn check_store<'a, S>()
122 where
123 S: StoreConstEmpty<u32, u64>
124 + StoreMut<u32, u64>
125 + StoreIterable<'a, u32, u64>
126 + StoreFromIterator<u32, u64>
127 + Clone
128 + Debug
129 + PartialEq
130 + 'a,
131 {
132 let mut litemap_test: LiteMap<u32, u64, S> = LiteMap::new();
133 assert!(litemap_test.is_empty());
134 let mut litemap_std = LiteMap::<u32, u64>::new();
135 populate_litemap(&mut litemap_test);
136 populate_litemap(&mut litemap_std);
137 check_equivalence(litemap_test.clone().values, litemap_std.clone().values);
138
139 litemap_test
140 .remove(&175)
141 .ok_or(())
142 .expect_err("does not exist");
143 litemap_test.remove(&147).ok_or(()).expect("exists");
144 litemap_std
145 .remove(&175)
146 .ok_or(())
147 .expect_err("does not exist");
148 litemap_std.remove(&147).ok_or(()).expect("exists");
149
150 assert_eq!(19, litemap_test.len());
151 assert_eq!(19, litemap_std.len());
152 check_equivalence(litemap_test.clone().values, litemap_std.clone().values);
153
154 litemap_test.clear();
155 litemap_std.clear();
156 check_equivalence(litemap_test.values, litemap_std.values);
157 }
158
159 /// Similar to [`check_store`] function, but also checks the validitiy of [`StoreIterableMut`]
160 /// and [`StoreBulkMut`] traits.
161 // Test code
162 #[expect(clippy::expect_used)]
163 pub fn check_store_full<'a, S>()
164 where
165 S: StoreConstEmpty<u32, u64>
166 + StoreIterableMut<'a, u32, u64>
167 + StoreIntoIterator<u32, u64>
168 + StoreFromIterator<u32, u64>
169 + StoreBulkMut<u32, u64>
170 + Clone
171 + Debug
172 + PartialEq
173 + 'a,
174 {
175 let mut litemap_test: LiteMap<u32, u64, S> = LiteMap::new();
176 assert!(litemap_test.is_empty());
177 let mut litemap_std = LiteMap::<u32, u64>::new();
178 populate_litemap(&mut litemap_test);
179 populate_litemap(&mut litemap_std);
180 check_equivalence(litemap_test.clone().values, litemap_std.clone().values);
181 check_into_iter_equivalence(litemap_test.clone().values, litemap_std.clone().values);
182
183 let extras_test = litemap_test.clone();
184 let extras_test = litemap_test
185 .extend_from_litemap(extras_test)
186 .expect("duplicates");
187 assert_eq!(extras_test, litemap_test);
188 let extras_std = litemap_std.clone();
189 check_equivalence(litemap_test.clone().values, litemap_std.clone().values);
190 check_into_iter_equivalence(litemap_test.clone().values, litemap_std.clone().values);
191
192 assert_eq!(20, litemap_test.len());
193 litemap_test.retain(|_, v| v % 2 == 0);
194 litemap_std.retain(|_, v| v % 2 == 0);
195 assert_eq!(11, litemap_test.len());
196 assert_eq!(11, litemap_std.len());
197 check_equivalence(litemap_test.clone().values, litemap_std.clone().values);
198 check_into_iter_equivalence(litemap_test.clone().values, litemap_std.clone().values);
199
200 let extras_test = litemap_test
201 .extend_from_litemap(extras_test)
202 .expect("duplicates");
203 let extras_std = litemap_std
204 .extend_from_litemap(extras_std)
205 .expect("duplicates");
206 assert_eq!(11, extras_test.len());
207 assert_eq!(11, extras_std.len());
208 assert_eq!(20, litemap_test.len());
209 assert_eq!(20, litemap_std.len());
210 check_equivalence(litemap_test.clone().values, litemap_std.clone().values);
211
212 assert_eq!(20, litemap_test.len());
213 litemap_test.retain(|_, v| v % 2 == 0);
214 litemap_std.retain(|_, v| v % 2 == 0);
215 assert_eq!(11, litemap_test.len());
216 assert_eq!(11, litemap_std.len());
217 let mut extras = LiteMap::<u32, u64>::new();
218 populate_litemap(&mut extras);
219 litemap_test.extend(extras.clone());
220 litemap_std.extend(extras);
221 assert_eq!(20, litemap_test.len());
222 assert_eq!(20, litemap_std.len());
223
224 check_into_iter_equivalence(litemap_test.clone().values, litemap_std.clone().values);
225 litemap_test
226 .remove(&175)
227 .ok_or(())
228 .expect_err("does not exist");
229 litemap_test.remove(&176).ok_or(()).expect("exists");
230 litemap_std
231 .remove(&175)
232 .ok_or(())
233 .expect_err("does not exist");
234 litemap_std.remove(&176).ok_or(()).expect("exists");
235 assert_eq!(19, litemap_test.len());
236 assert_eq!(19, litemap_std.len());
237 check_equivalence(litemap_test.clone().values, litemap_std.clone().values);
238 check_into_iter_equivalence(litemap_test.clone().values, litemap_std.clone().values);
239
240 litemap_test.clear();
241 litemap_std.clear();
242 check_equivalence(litemap_test.clone().values, litemap_std.clone().values);
243 check_into_iter_equivalence(litemap_test.values, litemap_std.values);
244
245 test_extend::<S>();
246 }
247
248 fn test_extend<'a, S>()
249 where
250 S: StoreConstEmpty<u32, u64>
251 + StoreIterableMut<'a, u32, u64>
252 + StoreIntoIterator<u32, u64>
253 + StoreFromIterator<u32, u64>
254 + StoreBulkMut<u32, u64>
255 + Clone
256 + Debug
257 + PartialEq
258 + 'a,
259 {
260 // Extend an empty BTreeMap with initial entries.
261 let mut map: LiteMap<u32, u64, S> = LiteMap::new();
262 let initial_entries = [(1, 1), (2, 2), (3, 3)];
263 map.extend(initial_entries);
264 assert_eq!(map.len(), 3);
265 assert_eq!(map.get(&1), Some(&1));
266 assert_eq!(map.get(&2), Some(&2));
267 assert_eq!(map.get(&3), Some(&3));
268
269 // Extend with entries that contain keys already present.
270 // For repeated keys, the last value should remain.
271 let overlapping_entries = [(2, 22), (4, 44), (1, 11)];
272 map.extend(overlapping_entries);
273 assert_eq!(map.len(), 4);
274 assert_eq!(map.get(&1), Some(&11));
275 assert_eq!(map.get(&2), Some(&22));
276 assert_eq!(map.get(&3), Some(&3));
277 assert_eq!(map.get(&4), Some(&44));
278
279 // Extend with an iterator that includes duplicate key entries.
280 // The very last occurrence for a key should be the final value.
281 let duplicate_entries = [(3, 333), (3, 3333), (5, 5)];
282 map.extend(duplicate_entries);
283 assert_eq!(map.len(), 5);
284 assert_eq!(map.get(&3), Some(&3333));
285 assert_eq!(map.get(&5), Some(&5));
286
287 // Extend with an empty iterator: the map should remain unchanged.
288 let empty_entries: Vec<(u32, u64)> = Vec::new();
289 let map_clone = map.clone();
290 map.extend(empty_entries);
291 check_equivalence(map.values.clone(), map_clone.values.clone());
292
293 // Extend with the same values: the map should remain unchanged.
294 map.extend(map_clone.clone());
295 check_equivalence(map.values.clone(), map_clone.values);
296 }