| 1 |
use indexmap::{IndexMap, IndexSet}; |
| 2 |
use itertools::Itertools; |
| 3 |
|
| 4 |
use quickcheck::Arbitrary; |
| 5 |
use quickcheck::Gen; |
| 6 |
use quickcheck::QuickCheck; |
| 7 |
use quickcheck::TestResult; |
| 8 |
|
| 9 |
use fnv::FnvHasher; |
| 10 |
use std::hash::{BuildHasher, BuildHasherDefault}; |
| 11 |
type FnvBuilder = BuildHasherDefault<FnvHasher>; |
| 12 |
type IndexMapFnv<K, V> = IndexMap<K, V, FnvBuilder>; |
| 13 |
|
| 14 |
use std::cmp::min; |
| 15 |
use std::collections::HashMap; |
| 16 |
use std::collections::HashSet; |
| 17 |
use std::fmt::Debug; |
| 18 |
use std::hash::Hash; |
| 19 |
use std::ops::Bound; |
| 20 |
use std::ops::Deref; |
| 21 |
|
| 22 |
use indexmap::map::Entry; |
| 23 |
use std::collections::hash_map::Entry as StdEntry; |
| 24 |
|
| 25 |
fn set<'a, T: 'a, I>(iter: I) -> HashSet<T> |
| 26 |
where |
| 27 |
I: IntoIterator<Item = &'a T>, |
| 28 |
T: Copy + Hash + Eq, |
| 29 |
{ |
| 30 |
iter.into_iter().copied().collect() |
| 31 |
} |
| 32 |
|
| 33 |
fn indexmap<'a, T: 'a, I>(iter: I) -> IndexMap<T, ()> |
| 34 |
where |
| 35 |
I: IntoIterator<Item = &'a T>, |
| 36 |
T: Copy + Hash + Eq, |
| 37 |
{ |
| 38 |
IndexMap::from_iter(iter.into_iter().copied().map(|k| (k, ()))) |
| 39 |
} |
| 40 |
|
| 41 |
// Helper macro to allow us to use smaller quickcheck limits under miri. |
| 42 |
macro_rules! quickcheck_limit { |
| 43 |
(@as_items $($i:item)*) => ($($i)*); |
| 44 |
{ |
| 45 |
$( |
| 46 |
$(#[$m:meta])* |
| 47 |
fn $fn_name:ident($($arg_name:ident : $arg_ty:ty),*) -> $ret:ty { |
| 48 |
$($code:tt)* |
| 49 |
} |
| 50 |
)* |
| 51 |
} => ( |
| 52 |
quickcheck::quickcheck! { |
| 53 |
@as_items |
| 54 |
$( |
| 55 |
#[test] |
| 56 |
$(#[$m])* |
| 57 |
fn $fn_name() { |
| 58 |
fn prop($($arg_name: $arg_ty),*) -> $ret { |
| 59 |
$($code)* |
| 60 |
} |
| 61 |
let mut quickcheck = QuickCheck::new(); |
| 62 |
if cfg!(miri) { |
| 63 |
quickcheck = quickcheck |
| 64 |
.gen(Gen::new(10)) |
| 65 |
.tests(10) |
| 66 |
.max_tests(100); |
| 67 |
} |
| 68 |
|
| 69 |
quickcheck.quickcheck(prop as fn($($arg_ty),*) -> $ret); |
| 70 |
} |
| 71 |
)* |
| 72 |
} |
| 73 |
) |
| 74 |
} |
| 75 |
|
| 76 |
quickcheck_limit! { |
| 77 |
fn contains(insert: Vec<u32>) -> bool { |
| 78 |
let mut map = IndexMap::new(); |
| 79 |
for &key in &insert { |
| 80 |
map.insert(key, ()); |
| 81 |
} |
| 82 |
insert.iter().all(|&key| map.get(&key).is_some()) |
| 83 |
} |
| 84 |
|
| 85 |
fn contains_not(insert: Vec<u8>, not: Vec<u8>) -> bool { |
| 86 |
let mut map = IndexMap::new(); |
| 87 |
for &key in &insert { |
| 88 |
map.insert(key, ()); |
| 89 |
} |
| 90 |
let nots = &set(¬) - &set(&insert); |
| 91 |
nots.iter().all(|&key| map.get(&key).is_none()) |
| 92 |
} |
| 93 |
|
| 94 |
fn insert_remove(insert: Vec<u8>, remove: Vec<u8>) -> bool { |
| 95 |
let mut map = IndexMap::new(); |
| 96 |
for &key in &insert { |
| 97 |
map.insert(key, ()); |
| 98 |
} |
| 99 |
for &key in &remove { |
| 100 |
map.swap_remove(&key); |
| 101 |
} |
| 102 |
let elements = &set(&insert) - &set(&remove); |
| 103 |
map.len() == elements.len() && map.iter().count() == elements.len() && |
| 104 |
elements.iter().all(|k| map.get(k).is_some()) |
| 105 |
} |
| 106 |
|
| 107 |
fn insertion_order(insert: Vec<u32>) -> bool { |
| 108 |
let mut map = IndexMap::new(); |
| 109 |
for &key in &insert { |
| 110 |
map.insert(key, ()); |
| 111 |
} |
| 112 |
itertools::assert_equal(insert.iter().unique(), map.keys()); |
| 113 |
true |
| 114 |
} |
| 115 |
|
| 116 |
fn insert_sorted(insert: Vec<(u32, u32)>) -> bool { |
| 117 |
let mut hmap = HashMap::new(); |
| 118 |
let mut map = IndexMap::new(); |
| 119 |
let mut map2 = IndexMap::new(); |
| 120 |
for &(key, value) in &insert { |
| 121 |
hmap.insert(key, value); |
| 122 |
map.insert_sorted(key, value); |
| 123 |
match map2.entry(key) { |
| 124 |
Entry::Occupied(e) => *e.into_mut() = value, |
| 125 |
Entry::Vacant(e) => { e.insert_sorted(value); } |
| 126 |
} |
| 127 |
} |
| 128 |
itertools::assert_equal(hmap.iter().sorted(), &map); |
| 129 |
itertools::assert_equal(&map, &map2); |
| 130 |
true |
| 131 |
} |
| 132 |
|
| 133 |
fn insert_sorted_by(insert: Vec<(u32, u32)>) -> bool { |
| 134 |
let mut hmap = HashMap::new(); |
| 135 |
let mut map = IndexMap::new(); |
| 136 |
let mut map2 = IndexMap::new(); |
| 137 |
for &(key, value) in &insert { |
| 138 |
hmap.insert(key, value); |
| 139 |
map.insert_sorted_by(key, value, |key1, _, key2, _| key2.cmp(key1)); |
| 140 |
match map2.entry(key) { |
| 141 |
Entry::Occupied(e) => *e.into_mut() = value, |
| 142 |
Entry::Vacant(e) => { |
| 143 |
e.insert_sorted_by(value, |key1, _, key2, _| key2.cmp(key1)); |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
let hsorted = hmap.iter().sorted_by(|(key1, _), (key2, _)| key2.cmp(key1)); |
| 148 |
itertools::assert_equal(hsorted, &map); |
| 149 |
itertools::assert_equal(&map, &map2); |
| 150 |
true |
| 151 |
} |
| 152 |
|
| 153 |
fn insert_sorted_by_key(insert: Vec<(i32, u32)>) -> bool { |
| 154 |
let mut hmap = HashMap::new(); |
| 155 |
let mut map = IndexMap::new(); |
| 156 |
let mut map2 = IndexMap::new(); |
| 157 |
for &(key, value) in &insert { |
| 158 |
hmap.insert(key, value); |
| 159 |
map.insert_sorted_by_key(key, value, |&k, _| (k.unsigned_abs(), k)); |
| 160 |
match map2.entry(key) { |
| 161 |
Entry::Occupied(e) => *e.into_mut() = value, |
| 162 |
Entry::Vacant(e) => { |
| 163 |
e.insert_sorted_by_key(value, |&k, _| (k.unsigned_abs(), k)); |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
let hsorted = hmap.iter().sorted_by_key(|(&k, _)| (k.unsigned_abs(), k)); |
| 168 |
itertools::assert_equal(hsorted, &map); |
| 169 |
itertools::assert_equal(&map, &map2); |
| 170 |
true |
| 171 |
} |
| 172 |
|
| 173 |
fn replace_index(insert: Vec<u8>, index: u8, new_key: u8) -> TestResult { |
| 174 |
if insert.is_empty() { |
| 175 |
return TestResult::discard(); |
| 176 |
} |
| 177 |
let mut map = IndexMap::new(); |
| 178 |
for &key in &insert { |
| 179 |
map.insert(key, ()); |
| 180 |
} |
| 181 |
let mut index = usize::from(index); |
| 182 |
if index < map.len() { |
| 183 |
match map.replace_index(index, new_key) { |
| 184 |
Ok(old_key) => { |
| 185 |
assert!(old_key == new_key || !map.contains_key(&old_key)); |
| 186 |
} |
| 187 |
Err((i, key)) => { |
| 188 |
assert_eq!(key, new_key); |
| 189 |
index = i; |
| 190 |
} |
| 191 |
} |
| 192 |
assert_eq!(map.get_index_of(&new_key), Some(index)); |
| 193 |
assert_eq!(map.get_index(index), Some((&new_key, &()))); |
| 194 |
TestResult::passed() |
| 195 |
} else { |
| 196 |
TestResult::must_fail(move || map.replace_index(index, new_key)) |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
fn vacant_replace_index(insert: Vec<u8>, index: u8, new_key: u8) -> TestResult { |
| 201 |
if insert.is_empty() { |
| 202 |
return TestResult::discard(); |
| 203 |
} |
| 204 |
let mut map = IndexMap::new(); |
| 205 |
for &key in &insert { |
| 206 |
map.insert(key, ()); |
| 207 |
} |
| 208 |
let index = usize::from(index); |
| 209 |
if let Some((&old_key, &())) = map.get_index(index) { |
| 210 |
match map.entry(new_key) { |
| 211 |
Entry::Occupied(_) => return TestResult::discard(), |
| 212 |
Entry::Vacant(entry) => { |
| 213 |
let (replaced_key, entry) = entry.replace_index(index); |
| 214 |
assert_eq!(old_key, replaced_key); |
| 215 |
assert_eq!(*entry.key(), new_key); |
| 216 |
} |
| 217 |
}; |
| 218 |
assert!(!map.contains_key(&old_key)); |
| 219 |
assert_eq!(map.get_index_of(&new_key), Some(index)); |
| 220 |
assert_eq!(map.get_index(index), Some((&new_key, &()))); |
| 221 |
TestResult::passed() |
| 222 |
} else { |
| 223 |
TestResult::must_fail(move || map.replace_index(index, new_key)) |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
fn pop(insert: Vec<u8>) -> bool { |
| 228 |
let mut map = IndexMap::new(); |
| 229 |
for &key in &insert { |
| 230 |
map.insert(key, ()); |
| 231 |
} |
| 232 |
let mut pops = Vec::new(); |
| 233 |
while let Some((key, _v)) = map.pop() { |
| 234 |
pops.push(key); |
| 235 |
} |
| 236 |
pops.reverse(); |
| 237 |
|
| 238 |
itertools::assert_equal(insert.iter().unique(), &pops); |
| 239 |
true |
| 240 |
} |
| 241 |
|
| 242 |
fn with_cap(template: Vec<()>) -> bool { |
| 243 |
let cap = template.len(); |
| 244 |
let map: IndexMap<u8, u8> = IndexMap::with_capacity(cap); |
| 245 |
println!("wish: {}, got: {} (diff: {})", cap, map.capacity(), map.capacity() as isize - cap as isize); |
| 246 |
map.capacity() >= cap |
| 247 |
} |
| 248 |
|
| 249 |
fn drain_full(insert: Vec<u8>) -> bool { |
| 250 |
let mut map = IndexMap::new(); |
| 251 |
for &key in &insert { |
| 252 |
map.insert(key, ()); |
| 253 |
} |
| 254 |
let mut clone = map.clone(); |
| 255 |
let drained = clone.drain(..); |
| 256 |
for (key, _) in drained { |
| 257 |
map.swap_remove(&key); |
| 258 |
} |
| 259 |
map.is_empty() |
| 260 |
} |
| 261 |
|
| 262 |
fn drain_bounds(insert: Vec<u8>, range: (Bound<usize>, Bound<usize>)) -> TestResult { |
| 263 |
let mut map = IndexMap::new(); |
| 264 |
for &key in &insert { |
| 265 |
map.insert(key, ()); |
| 266 |
} |
| 267 |
|
| 268 |
// First see if `Vec::drain` is happy with this range. |
| 269 |
let result = std::panic::catch_unwind(|| { |
| 270 |
let mut keys: Vec<u8> = map.keys().copied().collect(); |
| 271 |
keys.drain(range); |
| 272 |
keys |
| 273 |
}); |
| 274 |
|
| 275 |
if let Ok(keys) = result { |
| 276 |
map.drain(range); |
| 277 |
// Check that our `drain` matches the same key order. |
| 278 |
assert!(map.keys().eq(&keys)); |
| 279 |
// Check that hash lookups all work too. |
| 280 |
assert!(keys.iter().all(|key| map.contains_key(key))); |
| 281 |
TestResult::passed() |
| 282 |
} else { |
| 283 |
// If `Vec::drain` panicked, so should we. |
| 284 |
TestResult::must_fail(move || { map.drain(range); }) |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
fn extract_if_odd(insert: Vec<u8>) -> bool { |
| 289 |
let mut map = IndexMap::new(); |
| 290 |
for &x in &insert { |
| 291 |
map.insert(x, x.to_string()); |
| 292 |
} |
| 293 |
|
| 294 |
let (odd, even): (Vec<_>, Vec<_>) = map.keys().copied().partition(|k| k % 2 == 1); |
| 295 |
|
| 296 |
let extracted: Vec<_> = map |
| 297 |
.extract_if(.., |k, _| k % 2 == 1) |
| 298 |
.map(|(k, _)| k) |
| 299 |
.collect(); |
| 300 |
|
| 301 |
even.iter().all(|k| map.contains_key(k)) |
| 302 |
&& map.keys().eq(&even) |
| 303 |
&& extracted == odd |
| 304 |
} |
| 305 |
|
| 306 |
fn extract_if_odd_limit(insert: Vec<u8>, limit: usize) -> bool { |
| 307 |
let mut map = IndexMap::new(); |
| 308 |
for &x in &insert { |
| 309 |
map.insert(x, x.to_string()); |
| 310 |
} |
| 311 |
let limit = limit % (map.len() + 1); |
| 312 |
|
| 313 |
let mut i = 0; |
| 314 |
let (odd, other): (Vec<_>, Vec<_>) = map.keys().copied().partition(|k| { |
| 315 |
k % 2 == 1 && i < limit && { i += 1; true } |
| 316 |
}); |
| 317 |
|
| 318 |
let extracted: Vec<_> = map |
| 319 |
.extract_if(.., |k, _| k % 2 == 1) |
| 320 |
.map(|(k, _)| k) |
| 321 |
.take(limit) |
| 322 |
.collect(); |
| 323 |
|
| 324 |
other.iter().all(|k| map.contains_key(k)) |
| 325 |
&& map.keys().eq(&other) |
| 326 |
&& extracted == odd |
| 327 |
} |
| 328 |
|
| 329 |
fn shift_remove(insert: Vec<u8>, remove: Vec<u8>) -> bool { |
| 330 |
let mut map = IndexMap::new(); |
| 331 |
for &key in &insert { |
| 332 |
map.insert(key, ()); |
| 333 |
} |
| 334 |
for &key in &remove { |
| 335 |
map.shift_remove(&key); |
| 336 |
} |
| 337 |
let elements = &set(&insert) - &set(&remove); |
| 338 |
|
| 339 |
// Check that order is preserved after removals |
| 340 |
let mut iter = map.keys(); |
| 341 |
for &key in insert.iter().unique() { |
| 342 |
if elements.contains(&key) { |
| 343 |
assert_eq!(Some(&key), iter.next()); |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
map.len() == elements.len() && map.iter().count() == elements.len() && |
| 348 |
elements.iter().all(|k| map.get(k).is_some()) |
| 349 |
} |
| 350 |
|
| 351 |
fn indexing(insert: Vec<u8>) -> bool { |
| 352 |
let mut map: IndexMap<_, _> = insert.into_iter().map(|x| (x, x)).collect(); |
| 353 |
let set: IndexSet<_> = map.keys().copied().collect(); |
| 354 |
assert_eq!(map.len(), set.len()); |
| 355 |
|
| 356 |
for (i, &key) in set.iter().enumerate() { |
| 357 |
assert_eq!(map.get_index(i), Some((&key, &key))); |
| 358 |
assert_eq!(set.get_index(i), Some(&key)); |
| 359 |
assert_eq!(map[i], key); |
| 360 |
assert_eq!(set[i], key); |
| 361 |
|
| 362 |
*map.get_index_mut(i).unwrap().1 >>= 1; |
| 363 |
map[i] <<= 1; |
| 364 |
} |
| 365 |
|
| 366 |
set.iter().enumerate().all(|(i, &key)| { |
| 367 |
let value = key & !1; |
| 368 |
map[&key] == value && map[i] == value |
| 369 |
}) |
| 370 |
} |
| 371 |
|
| 372 |
// Use `u8` test indices so quickcheck is less likely to go out of bounds. |
| 373 |
fn set_swap_indices(vec: Vec<u8>, a: u8, b: u8) -> TestResult { |
| 374 |
let mut set = IndexSet::<u8>::from_iter(vec); |
| 375 |
let a = usize::from(a); |
| 376 |
let b = usize::from(b); |
| 377 |
|
| 378 |
if a >= set.len() || b >= set.len() { |
| 379 |
return TestResult::discard(); |
| 380 |
} |
| 381 |
|
| 382 |
let mut vec = Vec::from_iter(set.iter().cloned()); |
| 383 |
vec.swap(a, b); |
| 384 |
|
| 385 |
set.swap_indices(a, b); |
| 386 |
|
| 387 |
// Check both iteration order and hash lookups |
| 388 |
assert!(set.iter().eq(vec.iter())); |
| 389 |
assert!(vec.iter().enumerate().all(|(i, x)| { |
| 390 |
set.get_index_of(x) == Some(i) |
| 391 |
})); |
| 392 |
TestResult::passed() |
| 393 |
} |
| 394 |
|
| 395 |
fn map_swap_indices(vec: Vec<u8>, from: u8, to: u8) -> TestResult { |
| 396 |
test_map_swap_indices(vec, from, to, IndexMap::swap_indices) |
| 397 |
} |
| 398 |
|
| 399 |
fn occupied_entry_swap_indices(vec: Vec<u8>, from: u8, to: u8) -> TestResult { |
| 400 |
test_map_swap_indices(vec, from, to, |map, from, to| { |
| 401 |
let key = map.keys()[from]; |
| 402 |
match map.entry(key) { |
| 403 |
Entry::Occupied(entry) => entry.swap_indices(to), |
| 404 |
_ => unreachable!(), |
| 405 |
} |
| 406 |
}) |
| 407 |
} |
| 408 |
|
| 409 |
fn indexed_entry_swap_indices(vec: Vec<u8>, from: u8, to: u8) -> TestResult { |
| 410 |
test_map_swap_indices(vec, from, to, |map, from, to| { |
| 411 |
map.get_index_entry(from).unwrap().swap_indices(to); |
| 412 |
}) |
| 413 |
} |
| 414 |
|
| 415 |
fn raw_occupied_entry_swap_indices(vec: Vec<u8>, from: u8, to: u8) -> TestResult { |
| 416 |
use indexmap::map::raw_entry_v1::{RawEntryApiV1, RawEntryMut}; |
| 417 |
test_map_swap_indices(vec, from, to, |map, from, to| { |
| 418 |
let key = map.keys()[from]; |
| 419 |
match map.raw_entry_mut_v1().from_key(&key) { |
| 420 |
RawEntryMut::Occupied(entry) => entry.swap_indices(to), |
| 421 |
_ => unreachable!(), |
| 422 |
} |
| 423 |
}) |
| 424 |
} |
| 425 |
|
| 426 |
// Use `u8` test indices so quickcheck is less likely to go out of bounds. |
| 427 |
fn set_move_index(vec: Vec<u8>, from: u8, to: u8) -> TestResult { |
| 428 |
let mut set = IndexSet::<u8>::from_iter(vec); |
| 429 |
let from = usize::from(from); |
| 430 |
let to = usize::from(to); |
| 431 |
|
| 432 |
if from >= set.len() || to >= set.len() { |
| 433 |
return TestResult::discard(); |
| 434 |
} |
| 435 |
|
| 436 |
let mut vec = Vec::from_iter(set.iter().cloned()); |
| 437 |
let x = vec.remove(from); |
| 438 |
vec.insert(to, x); |
| 439 |
|
| 440 |
set.move_index(from, to); |
| 441 |
|
| 442 |
// Check both iteration order and hash lookups |
| 443 |
assert!(set.iter().eq(vec.iter())); |
| 444 |
assert!(vec.iter().enumerate().all(|(i, x)| { |
| 445 |
set.get_index_of(x) == Some(i) |
| 446 |
})); |
| 447 |
TestResult::passed() |
| 448 |
} |
| 449 |
|
| 450 |
fn map_move_index(vec: Vec<u8>, from: u8, to: u8) -> TestResult { |
| 451 |
test_map_move_index(vec, from, to, IndexMap::move_index) |
| 452 |
} |
| 453 |
|
| 454 |
fn occupied_entry_move_index(vec: Vec<u8>, from: u8, to: u8) -> TestResult { |
| 455 |
test_map_move_index(vec, from, to, |map, from, to| { |
| 456 |
let key = map.keys()[from]; |
| 457 |
match map.entry(key) { |
| 458 |
Entry::Occupied(entry) => entry.move_index(to), |
| 459 |
_ => unreachable!(), |
| 460 |
} |
| 461 |
}) |
| 462 |
} |
| 463 |
|
| 464 |
fn indexed_entry_move_index(vec: Vec<u8>, from: u8, to: u8) -> TestResult { |
| 465 |
test_map_move_index(vec, from, to, |map, from, to| { |
| 466 |
map.get_index_entry(from).unwrap().move_index(to); |
| 467 |
}) |
| 468 |
} |
| 469 |
|
| 470 |
fn raw_occupied_entry_move_index(vec: Vec<u8>, from: u8, to: u8) -> TestResult { |
| 471 |
use indexmap::map::raw_entry_v1::{RawEntryApiV1, RawEntryMut}; |
| 472 |
test_map_move_index(vec, from, to, |map, from, to| { |
| 473 |
let key = map.keys()[from]; |
| 474 |
match map.raw_entry_mut_v1().from_key(&key) { |
| 475 |
RawEntryMut::Occupied(entry) => entry.move_index(to), |
| 476 |
_ => unreachable!(), |
| 477 |
} |
| 478 |
}) |
| 479 |
} |
| 480 |
|
| 481 |
fn occupied_entry_shift_insert(vec: Vec<u8>, i: u8) -> TestResult { |
| 482 |
test_map_shift_insert(vec, i, |map, i, key| { |
| 483 |
match map.entry(key) { |
| 484 |
Entry::Vacant(entry) => entry.shift_insert(i, ()), |
| 485 |
_ => unreachable!(), |
| 486 |
}; |
| 487 |
}) |
| 488 |
} |
| 489 |
|
| 490 |
fn raw_occupied_entry_shift_insert(vec: Vec<u8>, i: u8) -> TestResult { |
| 491 |
use indexmap::map::raw_entry_v1::{RawEntryApiV1, RawEntryMut}; |
| 492 |
test_map_shift_insert(vec, i, |map, i, key| { |
| 493 |
match map.raw_entry_mut_v1().from_key(&key) { |
| 494 |
RawEntryMut::Vacant(entry) => entry.shift_insert(i, key, ()), |
| 495 |
_ => unreachable!(), |
| 496 |
}; |
| 497 |
}) |
| 498 |
} |
| 499 |
} |
| 500 |
|
| 501 |
fn test_map_swap_indices<F>(vec: Vec<u8>, a: u8, b: u8, swap_indices: F) -> TestResult |
| 502 |
where |
| 503 |
F: FnOnce(&mut IndexMap<u8, ()>, usize, usize), |
| 504 |
{ |
| 505 |
let mut map = IndexMap::<u8, ()>::from_iter(vec.into_iter().map(|k| (k, ()))); |
| 506 |
let a = usize::from(a); |
| 507 |
let b = usize::from(b); |
| 508 |
|
| 509 |
if a >= map.len() || b >= map.len() { |
| 510 |
return TestResult::discard(); |
| 511 |
} |
| 512 |
|
| 513 |
let mut vec = Vec::from_iter(map.keys().copied()); |
| 514 |
vec.swap(a, b); |
| 515 |
|
| 516 |
swap_indices(&mut map, a, b); |
| 517 |
|
| 518 |
// Check both iteration order and hash lookups |
| 519 |
assert!(map.keys().eq(vec.iter())); |
| 520 |
assert!(vec |
| 521 |
.iter() |
| 522 |
.enumerate() |
| 523 |
.all(|(i, x)| { map.get_index_of(x) == Some(i) })); |
| 524 |
TestResult::passed() |
| 525 |
} |
| 526 |
|
| 527 |
fn test_map_move_index<F>(vec: Vec<u8>, from: u8, to: u8, move_index: F) -> TestResult |
| 528 |
where |
| 529 |
F: FnOnce(&mut IndexMap<u8, ()>, usize, usize), |
| 530 |
{ |
| 531 |
let mut map = IndexMap::<u8, ()>::from_iter(vec.into_iter().map(|k| (k, ()))); |
| 532 |
let from = usize::from(from); |
| 533 |
let to = usize::from(to); |
| 534 |
|
| 535 |
if from >= map.len() || to >= map.len() { |
| 536 |
return TestResult::discard(); |
| 537 |
} |
| 538 |
|
| 539 |
let mut vec = Vec::from_iter(map.keys().copied()); |
| 540 |
let x = vec.remove(from); |
| 541 |
vec.insert(to, x); |
| 542 |
|
| 543 |
move_index(&mut map, from, to); |
| 544 |
|
| 545 |
// Check both iteration order and hash lookups |
| 546 |
assert!(map.keys().eq(vec.iter())); |
| 547 |
assert!(vec |
| 548 |
.iter() |
| 549 |
.enumerate() |
| 550 |
.all(|(i, x)| { map.get_index_of(x) == Some(i) })); |
| 551 |
TestResult::passed() |
| 552 |
} |
| 553 |
|
| 554 |
fn test_map_shift_insert<F>(vec: Vec<u8>, i: u8, shift_insert: F) -> TestResult |
| 555 |
where |
| 556 |
F: FnOnce(&mut IndexMap<u8, ()>, usize, u8), |
| 557 |
{ |
| 558 |
let mut map = IndexMap::<u8, ()>::from_iter(vec.into_iter().map(|k| (k, ()))); |
| 559 |
let i = usize::from(i); |
| 560 |
if i >= map.len() { |
| 561 |
return TestResult::discard(); |
| 562 |
} |
| 563 |
|
| 564 |
let mut vec = Vec::from_iter(map.keys().copied()); |
| 565 |
let x = vec.pop().unwrap(); |
| 566 |
vec.insert(i, x); |
| 567 |
|
| 568 |
let (last, ()) = map.pop().unwrap(); |
| 569 |
assert_eq!(x, last); |
| 570 |
map.shrink_to_fit(); // so we might have to grow and rehash the table |
| 571 |
|
| 572 |
shift_insert(&mut map, i, last); |
| 573 |
|
| 574 |
// Check both iteration order and hash lookups |
| 575 |
assert!(map.keys().eq(vec.iter())); |
| 576 |
assert!(vec |
| 577 |
.iter() |
| 578 |
.enumerate() |
| 579 |
.all(|(i, x)| { map.get_index_of(x) == Some(i) })); |
| 580 |
TestResult::passed() |
| 581 |
} |
| 582 |
|
| 583 |
use crate::Op::*; |
| 584 |
#[derive(Copy, Clone, Debug)] |
| 585 |
enum Op<K, V> { |
| 586 |
Add(K, V), |
| 587 |
Remove(K), |
| 588 |
AddEntry(K, V), |
| 589 |
RemoveEntry(K), |
| 590 |
} |
| 591 |
|
| 592 |
impl<K, V> Arbitrary for Op<K, V> |
| 593 |
where |
| 594 |
K: Arbitrary, |
| 595 |
V: Arbitrary, |
| 596 |
{ |
| 597 |
fn arbitrary(g: &mut Gen) -> Self { |
| 598 |
match u32::arbitrary(g) % 4 { |
| 599 |
0 => Add(K::arbitrary(g), V::arbitrary(g)), |
| 600 |
1 => AddEntry(K::arbitrary(g), V::arbitrary(g)), |
| 601 |
2 => Remove(K::arbitrary(g)), |
| 602 |
_ => RemoveEntry(K::arbitrary(g)), |
| 603 |
} |
| 604 |
} |
| 605 |
} |
| 606 |
|
| 607 |
fn do_ops<K, V, S>(ops: &[Op<K, V>], a: &mut IndexMap<K, V, S>, b: &mut HashMap<K, V>) |
| 608 |
where |
| 609 |
K: Hash + Eq + Clone, |
| 610 |
V: Clone, |
| 611 |
S: BuildHasher, |
| 612 |
{ |
| 613 |
for op in ops { |
| 614 |
match *op { |
| 615 |
Add(ref k, ref v) => { |
| 616 |
a.insert(k.clone(), v.clone()); |
| 617 |
b.insert(k.clone(), v.clone()); |
| 618 |
} |
| 619 |
AddEntry(ref k, ref v) => { |
| 620 |
a.entry(k.clone()).or_insert_with(|| v.clone()); |
| 621 |
b.entry(k.clone()).or_insert_with(|| v.clone()); |
| 622 |
} |
| 623 |
Remove(ref k) => { |
| 624 |
a.swap_remove(k); |
| 625 |
b.remove(k); |
| 626 |
} |
| 627 |
RemoveEntry(ref k) => { |
| 628 |
if let Entry::Occupied(ent) = a.entry(k.clone()) { |
| 629 |
ent.swap_remove_entry(); |
| 630 |
} |
| 631 |
if let StdEntry::Occupied(ent) = b.entry(k.clone()) { |
| 632 |
ent.remove_entry(); |
| 633 |
} |
| 634 |
} |
| 635 |
} |
| 636 |
//println!("{:?}", a); |
| 637 |
} |
| 638 |
} |
| 639 |
|
| 640 |
fn assert_maps_equivalent<K, V>(a: &IndexMap<K, V>, b: &HashMap<K, V>) -> bool |
| 641 |
where |
| 642 |
K: Hash + Eq + Debug, |
| 643 |
V: Eq + Debug, |
| 644 |
{ |
| 645 |
assert_eq!(a.len(), b.len()); |
| 646 |
assert_eq!(a.iter().next().is_some(), b.iter().next().is_some()); |
| 647 |
for key in a.keys() { |
| 648 |
assert!(b.contains_key(key), "b does not contain {:?}", key); |
| 649 |
} |
| 650 |
for key in b.keys() { |
| 651 |
assert!(a.get(key).is_some(), "a does not contain {:?}", key); |
| 652 |
} |
| 653 |
for key in a.keys() { |
| 654 |
assert_eq!(a[key], b[key]); |
| 655 |
} |
| 656 |
true |
| 657 |
} |
| 658 |
|
| 659 |
quickcheck_limit! { |
| 660 |
fn operations_i8(ops: Large<Vec<Op<i8, i8>>>) -> bool { |
| 661 |
let mut map = IndexMap::new(); |
| 662 |
let mut reference = HashMap::new(); |
| 663 |
do_ops(&ops, &mut map, &mut reference); |
| 664 |
assert_maps_equivalent(&map, &reference) |
| 665 |
} |
| 666 |
|
| 667 |
fn operations_string(ops: Vec<Op<Alpha, i8>>) -> bool { |
| 668 |
let mut map = IndexMap::new(); |
| 669 |
let mut reference = HashMap::new(); |
| 670 |
do_ops(&ops, &mut map, &mut reference); |
| 671 |
assert_maps_equivalent(&map, &reference) |
| 672 |
} |
| 673 |
|
| 674 |
fn keys_values(ops: Large<Vec<Op<i8, i8>>>) -> bool { |
| 675 |
let mut map = IndexMap::new(); |
| 676 |
let mut reference = HashMap::new(); |
| 677 |
do_ops(&ops, &mut map, &mut reference); |
| 678 |
let mut visit = IndexMap::new(); |
| 679 |
for (k, v) in map.keys().zip(map.values()) { |
| 680 |
assert_eq!(&map[k], v); |
| 681 |
assert!(!visit.contains_key(k)); |
| 682 |
visit.insert(*k, *v); |
| 683 |
} |
| 684 |
assert_eq!(visit.len(), reference.len()); |
| 685 |
true |
| 686 |
} |
| 687 |
|
| 688 |
fn keys_values_mut(ops: Large<Vec<Op<i8, i8>>>) -> bool { |
| 689 |
let mut map = IndexMap::new(); |
| 690 |
let mut reference = HashMap::new(); |
| 691 |
do_ops(&ops, &mut map, &mut reference); |
| 692 |
let mut visit = IndexMap::new(); |
| 693 |
let keys = Vec::from_iter(map.keys().copied()); |
| 694 |
for (k, v) in keys.iter().zip(map.values_mut()) { |
| 695 |
assert_eq!(&reference[k], v); |
| 696 |
assert!(!visit.contains_key(k)); |
| 697 |
visit.insert(*k, *v); |
| 698 |
} |
| 699 |
assert_eq!(visit.len(), reference.len()); |
| 700 |
true |
| 701 |
} |
| 702 |
|
| 703 |
fn equality(ops1: Vec<Op<i8, i8>>, removes: Vec<usize>) -> bool { |
| 704 |
let mut map = IndexMap::new(); |
| 705 |
let mut reference = HashMap::new(); |
| 706 |
do_ops(&ops1, &mut map, &mut reference); |
| 707 |
let mut ops2 = ops1.clone(); |
| 708 |
for &r in &removes { |
| 709 |
if !ops2.is_empty() { |
| 710 |
let i = r % ops2.len(); |
| 711 |
ops2.remove(i); |
| 712 |
} |
| 713 |
} |
| 714 |
let mut map2 = IndexMapFnv::default(); |
| 715 |
let mut reference2 = HashMap::new(); |
| 716 |
do_ops(&ops2, &mut map2, &mut reference2); |
| 717 |
assert_eq!(map == map2, reference == reference2); |
| 718 |
true |
| 719 |
} |
| 720 |
|
| 721 |
fn retain_ordered(keys: Large<Vec<i8>>, remove: Large<Vec<i8>>) -> () { |
| 722 |
let mut map = indexmap(keys.iter()); |
| 723 |
let initial_map = map.clone(); // deduplicated in-order input |
| 724 |
let remove_map = indexmap(remove.iter()); |
| 725 |
let keys_s = set(keys.iter()); |
| 726 |
let remove_s = set(remove.iter()); |
| 727 |
let answer = &keys_s - &remove_s; |
| 728 |
map.retain(|k, _| !remove_map.contains_key(k)); |
| 729 |
|
| 730 |
// check the values |
| 731 |
assert_eq!(map.len(), answer.len()); |
| 732 |
for key in &answer { |
| 733 |
assert!(map.contains_key(key)); |
| 734 |
} |
| 735 |
// check the order |
| 736 |
itertools::assert_equal(map.keys(), initial_map.keys().filter(|&k| !remove_map.contains_key(k))); |
| 737 |
} |
| 738 |
|
| 739 |
fn sort_1(keyvals: Large<Vec<(i8, i8)>>) -> () { |
| 740 |
let mut map: IndexMap<_, _> = IndexMap::from_iter(keyvals.to_vec()); |
| 741 |
let mut answer = keyvals.0; |
| 742 |
answer.sort_by_key(|t| t.0); |
| 743 |
|
| 744 |
// reverse dedup: Because IndexMap::from_iter keeps the last value for |
| 745 |
// identical keys |
| 746 |
answer.reverse(); |
| 747 |
answer.dedup_by_key(|t| t.0); |
| 748 |
answer.reverse(); |
| 749 |
|
| 750 |
map.sort_by(|k1, _, k2, _| Ord::cmp(k1, k2)); |
| 751 |
|
| 752 |
// check it contains all the values it should |
| 753 |
for &(key, val) in &answer { |
| 754 |
assert_eq!(map[&key], val); |
| 755 |
} |
| 756 |
|
| 757 |
// check the order |
| 758 |
|
| 759 |
let mapv = Vec::from_iter(map); |
| 760 |
assert_eq!(answer, mapv); |
| 761 |
|
| 762 |
} |
| 763 |
|
| 764 |
fn sort_2(keyvals: Large<Vec<(i8, i8)>>) -> () { |
| 765 |
let mut map: IndexMap<_, _> = IndexMap::from_iter(keyvals.to_vec()); |
| 766 |
map.sort_by(|_, v1, _, v2| Ord::cmp(v1, v2)); |
| 767 |
assert_sorted_by_key(map, |t| t.1); |
| 768 |
} |
| 769 |
|
| 770 |
fn sort_3(keyvals: Large<Vec<(i8, i8)>>) -> () { |
| 771 |
let mut map: IndexMap<_, _> = IndexMap::from_iter(keyvals.to_vec()); |
| 772 |
map.sort_by_cached_key(|&k, _| std::cmp::Reverse(k)); |
| 773 |
assert_sorted_by_key(map, |t| std::cmp::Reverse(t.0)); |
| 774 |
} |
| 775 |
|
| 776 |
fn reverse(keyvals: Large<Vec<(i8, i8)>>) -> () { |
| 777 |
let mut map: IndexMap<_, _> = IndexMap::from_iter(keyvals.to_vec()); |
| 778 |
|
| 779 |
fn generate_answer(input: &Vec<(i8, i8)>) -> Vec<(i8, i8)> { |
| 780 |
// to mimic what `IndexMap::from_iter` does: |
| 781 |
// need to get (A) the unique keys in forward order, and (B) the |
| 782 |
// last value of each of those keys. |
| 783 |
|
| 784 |
// create (A): an iterable that yields the unique keys in ltr order |
| 785 |
let mut seen_keys = HashSet::new(); |
| 786 |
let unique_keys_forward = input.iter().filter_map(move |(k, _)| { |
| 787 |
if seen_keys.contains(k) { None } |
| 788 |
else { seen_keys.insert(*k); Some(*k) } |
| 789 |
}); |
| 790 |
|
| 791 |
// create (B): a mapping of keys to the last value seen for that key |
| 792 |
// this is the same as reversing the input and taking the first |
| 793 |
// value seen for that key! |
| 794 |
let mut last_val_per_key = HashMap::new(); |
| 795 |
for &(k, v) in input.iter().rev() { |
| 796 |
if !last_val_per_key.contains_key(&k) { |
| 797 |
last_val_per_key.insert(k, v); |
| 798 |
} |
| 799 |
} |
| 800 |
|
| 801 |
// iterate over the keys in (A) in order, and match each one with |
| 802 |
// the corresponding last value from (B) |
| 803 |
let mut ans: Vec<_> = unique_keys_forward |
| 804 |
.map(|k| (k, *last_val_per_key.get(&k).unwrap())) |
| 805 |
.collect(); |
| 806 |
|
| 807 |
// finally, since this test is testing `.reverse()`, reverse the |
| 808 |
// answer in-place |
| 809 |
ans.reverse(); |
| 810 |
|
| 811 |
ans |
| 812 |
} |
| 813 |
|
| 814 |
let answer = generate_answer(&keyvals.0); |
| 815 |
|
| 816 |
// perform the work |
| 817 |
map.reverse(); |
| 818 |
|
| 819 |
// check it contains all the values it should |
| 820 |
for &(key, val) in &answer { |
| 821 |
assert_eq!(map[&key], val); |
| 822 |
} |
| 823 |
|
| 824 |
// check the order |
| 825 |
let mapv = Vec::from_iter(map); |
| 826 |
assert_eq!(answer, mapv); |
| 827 |
} |
| 828 |
} |
| 829 |
|
| 830 |
fn assert_sorted_by_key<I, Key, X>(iterable: I, key: Key) |
| 831 |
where |
| 832 |
I: IntoIterator<Item: Ord + Clone + Debug>, |
| 833 |
Key: Fn(&I::Item) -> X, |
| 834 |
X: Ord, |
| 835 |
{ |
| 836 |
let input = Vec::from_iter(iterable); |
| 837 |
let mut sorted = input.clone(); |
| 838 |
sorted.sort_by_key(key); |
| 839 |
assert_eq!(input, sorted); |
| 840 |
} |
| 841 |
|
| 842 |
#[derive(Clone, Debug, Hash, PartialEq, Eq)] |
| 843 |
struct Alpha(String); |
| 844 |
|
| 845 |
impl Deref for Alpha { |
| 846 |
type Target = String; |
| 847 |
fn deref(&self) -> &String { |
| 848 |
&self.0 |
| 849 |
} |
| 850 |
} |
| 851 |
|
| 852 |
const ALPHABET: &[u8] = b"abcdefghijklmnopqrstuvwxyz"; |
| 853 |
|
| 854 |
impl Arbitrary for Alpha { |
| 855 |
fn arbitrary(g: &mut Gen) -> Self { |
| 856 |
let len = usize::arbitrary(g) % g.size(); |
| 857 |
let len = min(len, 16); |
| 858 |
Alpha( |
| 859 |
(0..len) |
| 860 |
.map(|_| ALPHABET[usize::arbitrary(g) % ALPHABET.len()] as char) |
| 861 |
.collect(), |
| 862 |
) |
| 863 |
} |
| 864 |
|
| 865 |
fn shrink(&self) -> Box<dyn Iterator<Item = Self>> { |
| 866 |
Box::new((**self).shrink().map(Alpha)) |
| 867 |
} |
| 868 |
} |
| 869 |
|
| 870 |
/// quickcheck Arbitrary adaptor -- make a larger vec |
| 871 |
#[derive(Clone, Debug)] |
| 872 |
struct Large<T>(T); |
| 873 |
|
| 874 |
impl<T> Deref for Large<T> { |
| 875 |
type Target = T; |
| 876 |
fn deref(&self) -> &T { |
| 877 |
&self.0 |
| 878 |
} |
| 879 |
} |
| 880 |
|
| 881 |
impl<T> Arbitrary for Large<Vec<T>> |
| 882 |
where |
| 883 |
T: Arbitrary, |
| 884 |
{ |
| 885 |
fn arbitrary(g: &mut Gen) -> Self { |
| 886 |
let len = usize::arbitrary(g) % (g.size() * 10); |
| 887 |
Large((0..len).map(|_| T::arbitrary(g)).collect()) |
| 888 |
} |
| 889 |
|
| 890 |
fn shrink(&self) -> Box<dyn Iterator<Item = Self>> { |
| 891 |
Box::new((**self).shrink().map(Large)) |
| 892 |
} |
| 893 |
} |