-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhash_map.rs
212 lines (171 loc) · 5.46 KB
/
hash_map.rs
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
#![allow(clippy::cast_possible_truncation)]
#![allow(clippy::manual_find_map)]
#![allow(clippy::must_use_candidate)]
#![allow(clippy::option_if_let_else)]
use honggfuzz::fuzz;
use rutenspitz::arbitrary_stateful_operations;
use hashbrown::HashMap;
use std::fmt::Debug;
use std::hash::{BuildHasher, Hash};
pub struct BuildAHasher {
seed: u128,
}
impl BuildAHasher {
pub fn new(seed: u128) -> Self {
Self { seed }
}
}
impl BuildHasher for BuildAHasher {
type Hasher = ahash::AHasher;
fn build_hasher(&self) -> Self::Hasher {
ahash::RandomState::with_seed(self.seed as usize).build_hasher()
}
}
#[derive(Default)]
pub struct ModelHashMap<K, V>
where
K: Eq + Hash,
{
data: Vec<(K, V)>,
}
impl<K, V> ModelHashMap<K, V>
where
K: Eq + Hash,
{
pub fn clear(&mut self) {
self.data.clear();
}
pub fn contains_key(&self, k: &K) -> bool {
self.data.iter().any(|probe| probe.0 == *k)
}
pub fn get(&self, k: &K) -> Option<&V> {
self.data.iter().find(|probe| probe.0 == *k).map(|e| &e.1)
}
pub fn get_key_value(&self, k: &K) -> Option<(&K, &V)> {
self.data
.iter()
.find(|probe| probe.0 == *k)
.map(|e| (&e.0, &e.1))
}
pub fn get_mut(&mut self, k: &K) -> Option<&mut V> {
self.data
.iter_mut()
.find(|probe| probe.0 == *k)
.map(|e| &mut e.1)
}
pub fn insert(&mut self, k: K, v: V) -> Option<V> {
if let Some(e) = self.data.iter_mut().find(|probe| probe.0 == k) {
Some(std::mem::replace(&mut e.1, v))
} else {
self.data.push((k, v));
None
}
}
pub fn is_empty(&self) -> bool {
self.data.is_empty()
}
pub fn len(&self) -> usize {
self.data.len()
}
pub fn remove(&mut self, k: &K) -> Option<V> {
let pos = self.data.iter().position(|probe| probe.0 == *k);
pos.map(|idx| self.data.swap_remove(idx).1)
}
pub fn shrink_to(&mut self, min_capacity: usize) {
self.data.shrink_to(std::cmp::min(
self.data.capacity(),
std::cmp::max(min_capacity, self.data.len()),
));
}
pub fn shrink_to_fit(&mut self) {
self.data.shrink_to_fit();
}
pub fn drain(&mut self) -> impl Iterator<Item = (K, V)> + '_ {
self.data.drain(..)
}
pub fn iter(&self) -> impl Iterator<Item = (&K, &V)> {
self.data.iter().map(|e| (&e.0, &e.1))
}
pub fn iter_mut(&mut self) -> impl Iterator<Item = (&K, &mut V)> {
self.data.iter_mut().map(|e| (&e.0, &mut e.1))
}
pub fn keys(&self) -> impl Iterator<Item = &K> {
self.data.iter().map(|e| &e.0)
}
pub fn values(&self) -> impl Iterator<Item = &V> {
self.data.iter().map(|e| &e.1)
}
pub fn values_mut(&mut self) -> impl Iterator<Item = &mut V> {
self.data.iter_mut().map(|e| &mut e.1)
}
}
fn sort_iterator<T: Ord, I: Iterator<Item = T>>(i: I) -> Vec<T> {
let mut v: Vec<_> = i.collect::<Vec<_>>();
v.sort();
v
}
arbitrary_stateful_operations! {
model = ModelHashMap<K, V>,
tested = HashMap<K, V, BuildAHasher>,
type_parameters = <
K: Clone + Debug + Eq + Hash + Ord,
V: Clone + Debug + Eq + Ord
>,
methods {
equal {
fn clear(&mut self);
fn contains_key(&self, k: &K) -> bool;
fn get(&self, k: &K) -> Option<&V>;
fn get_key_value(&self, k: &K) -> Option<(&K, &V)>;
fn get_mut(&mut self, k: &K) -> Option<&mut V>;
fn insert(&mut self, k: K, v: V) -> Option<V>;
fn remove(&mut self, k: &K) -> Option<V>;
fn shrink_to(&mut self, min_capacity: usize);
fn shrink_to_fit(&mut self);
}
equal_with(sort_iterator) {
fn drain(&mut self) -> impl Iterator<Item = (K, V)>;
fn iter(&self) -> impl Iterator<Item = (&K, &V)>;
fn iter_mut(&self) -> impl Iterator<Item = (&K, &mut V)>;
fn keys(&self) -> impl Iterator<Item = &K>;
fn values(&self) -> impl Iterator<Item = &V>;
fn values_mut(&mut self) -> impl Iterator<Item = &mut V>;
}
}
pre {
let prev_capacity = tested.capacity();
}
post {
if op_name == "clear" {
assert_eq!(tested.capacity(), prev_capacity,
"capacity: {}, previous: {}",
tested.capacity(), prev_capacity);
}
assert!(tested.capacity() >= model.len());
assert_eq!(tested.is_empty(), model.is_empty());
assert_eq!(tested.len(), model.len());
}
}
fn fuzz_cycle(data: &[u8]) -> arbitrary::Result<()> {
use arbitrary::{Arbitrary, Unstructured};
let mut ring = Unstructured::new(data);
let capacity: u16 = Arbitrary::arbitrary(&mut ring)?;
let hash_seed: u128 = Arbitrary::arbitrary(&mut ring)?;
let mut model = ModelHashMap::<u16, u16>::default();
let mut tested: HashMap<u16, u16, BuildAHasher> =
HashMap::with_capacity_and_hasher(capacity as usize, BuildAHasher::new(hash_seed));
let mut op_trace = String::new();
while let Ok(op) = <op::Op<u16, u16> as Arbitrary>::arbitrary(&mut ring) {
op.append_to_trace(&mut op_trace);
op.execute_and_compare(&mut model, &mut tested);
}
Ok(())
}
fn main() -> Result<(), ()> {
better_panic::install();
loop {
fuzz!(|data: &[u8]| {
let _ = fuzz_cycle(data);
});
}
}