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