Skip to content

Commit a5e8f40

Browse files
committed
test: add indexmap as sample to show its substitutability.
1 parent fb249c1 commit a5e8f40

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

tests/random.rs

+42
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use seq_macro::seq;
88
use std::hash::Hash;
99
use std::vec;
1010

11+
use indexmap::IndexMap;
1112
use micromap::Map;
1213
use rand::rngs::SmallRng;
1314
use rand::Rng;
@@ -36,6 +37,11 @@ fn each_capacity() {
3637
let hash_map: HashMap<Uuid, [u8; 16]> = HashMap::new();
3738
let hashmap_results = for_n::<N>(hash_map);
3839
assert_eq!(micromap_results, hashmap_results);
40+
// Or use IndexMap instead of HashMap if you want, the final
41+
// result sequence is consistent.
42+
// let index_map: IndexMap<Uuid, [u8; 16]> = IndexMap::new();
43+
// let indexmap_results = for_n::<N>(index_map);
44+
// assert_eq!(indexmap_results, micromap_results);
3945
for result in micromap_results {
4046
let hash_val = make_hash(&result, ROUNDS as u64);
4147
xor_hash ^= hash_val;
@@ -135,6 +141,42 @@ where
135141
}
136142
}
137143

144+
impl<K, V, const N: usize> IsMap<K, V, N> for IndexMap<K, V>
145+
where
146+
K: PartialEq + Eq + Hash,
147+
{
148+
fn get(&self, key: &K) -> Option<&V> {
149+
self.get(key)
150+
}
151+
152+
fn insert(&mut self, key: K, value: V) -> Option<V> {
153+
self.insert(key, value)
154+
}
155+
156+
fn remove(&mut self, key: &K) -> Option<V> {
157+
self.swap_remove(key)
158+
}
159+
160+
fn retain<F>(&mut self, f: F)
161+
where
162+
F: FnMut(&K, &mut V) -> bool,
163+
{
164+
self.retain(f)
165+
}
166+
167+
fn len(&self) -> usize {
168+
self.len()
169+
}
170+
171+
fn contains_key(&self, key: &K) -> bool {
172+
self.contains_key(key)
173+
}
174+
175+
fn clear(&mut self) {
176+
self.clear()
177+
}
178+
}
179+
138180
enum MapOp<K, V> {
139181
Get(K),
140182
Insert(K, V),

0 commit comments

Comments
 (0)