| 1 |
use super::*; |
| 2 |
use std::string::String; |
| 3 |
|
| 4 |
#[test] |
| 5 |
fn it_works() { |
| 6 |
let mut map = IndexMap::new(); |
| 7 |
assert_eq!(map.is_empty(), true); |
| 8 |
map.insert(1, ()); |
| 9 |
map.insert(1, ()); |
| 10 |
assert_eq!(map.len(), 1); |
| 11 |
assert!(map.get(&1).is_some()); |
| 12 |
assert_eq!(map.is_empty(), false); |
| 13 |
} |
| 14 |
|
| 15 |
#[test] |
| 16 |
fn new() { |
| 17 |
let map = IndexMap::<String, String>::new(); |
| 18 |
println!("{:?}", map); |
| 19 |
assert_eq!(map.capacity(), 0); |
| 20 |
assert_eq!(map.len(), 0); |
| 21 |
assert_eq!(map.is_empty(), true); |
| 22 |
} |
| 23 |
|
| 24 |
#[test] |
| 25 |
fn insert() { |
| 26 |
let insert = [0, 4, 2, 12, 8, 7, 11, 5]; |
| 27 |
let not_present = [1, 3, 6, 9, 10]; |
| 28 |
let mut map = IndexMap::with_capacity(insert.len()); |
| 29 |
|
| 30 |
for (i, &elt) in insert.iter().enumerate() { |
| 31 |
assert_eq!(map.len(), i); |
| 32 |
map.insert(elt, elt); |
| 33 |
assert_eq!(map.len(), i + 1); |
| 34 |
assert_eq!(map.get(&elt), Some(&elt)); |
| 35 |
assert_eq!(map[&elt], elt); |
| 36 |
} |
| 37 |
println!("{:?}", map); |
| 38 |
|
| 39 |
for &elt in ¬_present { |
| 40 |
assert!(map.get(&elt).is_none()); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
#[test] |
| 45 |
fn insert_full() { |
| 46 |
let insert = vec![9, 2, 7, 1, 4, 6, 13]; |
| 47 |
let present = vec![1, 6, 2]; |
| 48 |
let mut map = IndexMap::with_capacity(insert.len()); |
| 49 |
|
| 50 |
for (i, &elt) in insert.iter().enumerate() { |
| 51 |
assert_eq!(map.len(), i); |
| 52 |
let (index, existing) = map.insert_full(elt, elt); |
| 53 |
assert_eq!(existing, None); |
| 54 |
assert_eq!(Some(index), map.get_full(&elt).map(|x| x.0)); |
| 55 |
assert_eq!(map.len(), i + 1); |
| 56 |
} |
| 57 |
|
| 58 |
let len = map.len(); |
| 59 |
for &elt in &present { |
| 60 |
let (index, existing) = map.insert_full(elt, elt); |
| 61 |
assert_eq!(existing, Some(elt)); |
| 62 |
assert_eq!(Some(index), map.get_full(&elt).map(|x| x.0)); |
| 63 |
assert_eq!(map.len(), len); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
#[test] |
| 68 |
fn insert_2() { |
| 69 |
let mut map = IndexMap::with_capacity(16); |
| 70 |
|
| 71 |
let mut keys = vec![]; |
| 72 |
keys.extend(0..16); |
| 73 |
keys.extend(if cfg!(miri) { 32..64 } else { 128..267 }); |
| 74 |
|
| 75 |
for &i in &keys { |
| 76 |
let old_map = map.clone(); |
| 77 |
map.insert(i, ()); |
| 78 |
for key in old_map.keys() { |
| 79 |
if map.get(key).is_none() { |
| 80 |
println!("old_map: {:?}", old_map); |
| 81 |
println!("map: {:?}", map); |
| 82 |
panic!("did not find {} in map", key); |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
for &i in &keys { |
| 88 |
assert!(map.get(&i).is_some(), "did not find {}", i); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
#[test] |
| 93 |
fn insert_order() { |
| 94 |
let insert = [0, 4, 2, 12, 8, 7, 11, 5, 3, 17, 19, 22, 23]; |
| 95 |
let mut map = IndexMap::new(); |
| 96 |
|
| 97 |
for &elt in &insert { |
| 98 |
map.insert(elt, ()); |
| 99 |
} |
| 100 |
|
| 101 |
assert_eq!(map.keys().count(), map.len()); |
| 102 |
assert_eq!(map.keys().count(), insert.len()); |
| 103 |
for (a, b) in insert.iter().zip(map.keys()) { |
| 104 |
assert_eq!(a, b); |
| 105 |
} |
| 106 |
for (i, k) in (0..insert.len()).zip(map.keys()) { |
| 107 |
assert_eq!(map.get_index(i).unwrap().0, k); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
#[test] |
| 112 |
fn shift_insert() { |
| 113 |
let insert = [0, 4, 2, 12, 8, 7, 11, 5, 3, 17, 19, 22, 23]; |
| 114 |
let mut map = IndexMap::new(); |
| 115 |
|
| 116 |
for &elt in &insert { |
| 117 |
map.shift_insert(0, elt, ()); |
| 118 |
} |
| 119 |
|
| 120 |
assert_eq!(map.keys().count(), map.len()); |
| 121 |
assert_eq!(map.keys().count(), insert.len()); |
| 122 |
for (a, b) in insert.iter().rev().zip(map.keys()) { |
| 123 |
assert_eq!(a, b); |
| 124 |
} |
| 125 |
for (i, k) in (0..insert.len()).zip(map.keys()) { |
| 126 |
assert_eq!(map.get_index(i).unwrap().0, k); |
| 127 |
} |
| 128 |
|
| 129 |
// "insert" that moves an existing entry |
| 130 |
map.shift_insert(0, insert[0], ()); |
| 131 |
assert_eq!(map.keys().count(), insert.len()); |
| 132 |
assert_eq!(insert[0], map.keys()[0]); |
| 133 |
for (a, b) in insert[1..].iter().rev().zip(map.keys().skip(1)) { |
| 134 |
assert_eq!(a, b); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
#[test] |
| 139 |
fn insert_sorted_bad() { |
| 140 |
let mut map = IndexMap::new(); |
| 141 |
map.insert(10, ()); |
| 142 |
for i in 0..10 { |
| 143 |
map.insert(i, ()); |
| 144 |
} |
| 145 |
|
| 146 |
// The binary search will want to insert this at the end (index == len()), |
| 147 |
// but that's only possible for *new* inserts. It should still be handled |
| 148 |
// without panicking though, and in this case it's simple enough that we |
| 149 |
// know the exact result. (But don't read this as an API guarantee!) |
| 150 |
assert_eq!(map.first(), Some((&10, &()))); |
| 151 |
map.insert_sorted(10, ()); |
| 152 |
assert_eq!(map.last(), Some((&10, &()))); |
| 153 |
assert!(map.keys().copied().eq(0..=10)); |
| 154 |
|
| 155 |
// Other out-of-order entries can also "insert" to a binary-searched |
| 156 |
// position, moving in either direction. |
| 157 |
map.move_index(5, 0); |
| 158 |
map.move_index(6, 10); |
| 159 |
assert_eq!(map.first(), Some((&5, &()))); |
| 160 |
assert_eq!(map.last(), Some((&6, &()))); |
| 161 |
map.insert_sorted(5, ()); // moves back up |
| 162 |
map.insert_sorted(6, ()); // moves back down |
| 163 |
assert!(map.keys().copied().eq(0..=10)); |
| 164 |
} |
| 165 |
|
| 166 |
#[test] |
| 167 |
fn grow() { |
| 168 |
let insert = [0, 4, 2, 12, 8, 7, 11]; |
| 169 |
let not_present = [1, 3, 6, 9, 10]; |
| 170 |
let mut map = IndexMap::with_capacity(insert.len()); |
| 171 |
|
| 172 |
for (i, &elt) in insert.iter().enumerate() { |
| 173 |
assert_eq!(map.len(), i); |
| 174 |
map.insert(elt, elt); |
| 175 |
assert_eq!(map.len(), i + 1); |
| 176 |
assert_eq!(map.get(&elt), Some(&elt)); |
| 177 |
assert_eq!(map[&elt], elt); |
| 178 |
} |
| 179 |
|
| 180 |
println!("{:?}", map); |
| 181 |
for &elt in &insert { |
| 182 |
map.insert(elt * 10, elt); |
| 183 |
} |
| 184 |
for &elt in &insert { |
| 185 |
map.insert(elt * 100, elt); |
| 186 |
} |
| 187 |
for (i, &elt) in insert.iter().cycle().enumerate().take(100) { |
| 188 |
map.insert(elt * 100 + i as i32, elt); |
| 189 |
} |
| 190 |
println!("{:?}", map); |
| 191 |
for &elt in ¬_present { |
| 192 |
assert!(map.get(&elt).is_none()); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
#[test] |
| 197 |
fn reserve() { |
| 198 |
let mut map = IndexMap::<usize, usize>::new(); |
| 199 |
assert_eq!(map.capacity(), 0); |
| 200 |
map.reserve(100); |
| 201 |
let capacity = map.capacity(); |
| 202 |
assert!(capacity >= 100); |
| 203 |
for i in 0..capacity { |
| 204 |
assert_eq!(map.len(), i); |
| 205 |
map.insert(i, i * i); |
| 206 |
assert_eq!(map.len(), i + 1); |
| 207 |
assert_eq!(map.capacity(), capacity); |
| 208 |
assert_eq!(map.get(&i), Some(&(i * i))); |
| 209 |
} |
| 210 |
map.insert(capacity, std::usize::MAX); |
| 211 |
assert_eq!(map.len(), capacity + 1); |
| 212 |
assert!(map.capacity() > capacity); |
| 213 |
assert_eq!(map.get(&capacity), Some(&std::usize::MAX)); |
| 214 |
} |
| 215 |
|
| 216 |
#[test] |
| 217 |
fn try_reserve() { |
| 218 |
let mut map = IndexMap::<usize, usize>::new(); |
| 219 |
assert_eq!(map.capacity(), 0); |
| 220 |
assert_eq!(map.try_reserve(100), Ok(())); |
| 221 |
assert!(map.capacity() >= 100); |
| 222 |
assert!(map.try_reserve(usize::MAX).is_err()); |
| 223 |
} |
| 224 |
|
| 225 |
#[test] |
| 226 |
fn shrink_to_fit() { |
| 227 |
let mut map = IndexMap::<usize, usize>::new(); |
| 228 |
assert_eq!(map.capacity(), 0); |
| 229 |
for i in 0..100 { |
| 230 |
assert_eq!(map.len(), i); |
| 231 |
map.insert(i, i * i); |
| 232 |
assert_eq!(map.len(), i + 1); |
| 233 |
assert!(map.capacity() >= i + 1); |
| 234 |
assert_eq!(map.get(&i), Some(&(i * i))); |
| 235 |
map.shrink_to_fit(); |
| 236 |
assert_eq!(map.len(), i + 1); |
| 237 |
assert_eq!(map.capacity(), i + 1); |
| 238 |
assert_eq!(map.get(&i), Some(&(i * i))); |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
#[test] |
| 243 |
fn remove() { |
| 244 |
let insert = [0, 4, 2, 12, 8, 7, 11, 5, 3, 17, 19, 22, 23]; |
| 245 |
let mut map = IndexMap::new(); |
| 246 |
|
| 247 |
for &elt in &insert { |
| 248 |
map.insert(elt, elt); |
| 249 |
} |
| 250 |
|
| 251 |
assert_eq!(map.keys().count(), map.len()); |
| 252 |
assert_eq!(map.keys().count(), insert.len()); |
| 253 |
for (a, b) in insert.iter().zip(map.keys()) { |
| 254 |
assert_eq!(a, b); |
| 255 |
} |
| 256 |
|
| 257 |
let remove_fail = [99, 77]; |
| 258 |
let remove = [4, 12, 8, 7]; |
| 259 |
|
| 260 |
for &key in &remove_fail { |
| 261 |
assert!(map.swap_remove_full(&key).is_none()); |
| 262 |
} |
| 263 |
println!("{:?}", map); |
| 264 |
for &key in &remove { |
| 265 |
//println!("{:?}", map); |
| 266 |
let index = map.get_full(&key).unwrap().0; |
| 267 |
assert_eq!(map.swap_remove_full(&key), Some((index, key, key))); |
| 268 |
} |
| 269 |
println!("{:?}", map); |
| 270 |
|
| 271 |
for key in &insert { |
| 272 |
assert_eq!(map.get(key).is_some(), !remove.contains(key)); |
| 273 |
} |
| 274 |
assert_eq!(map.len(), insert.len() - remove.len()); |
| 275 |
assert_eq!(map.keys().count(), insert.len() - remove.len()); |
| 276 |
} |
| 277 |
|
| 278 |
#[test] |
| 279 |
fn remove_to_empty() { |
| 280 |
let mut map = indexmap! { 0 => 0, 4 => 4, 5 => 5 }; |
| 281 |
map.swap_remove(&5).unwrap(); |
| 282 |
map.swap_remove(&4).unwrap(); |
| 283 |
map.swap_remove(&0).unwrap(); |
| 284 |
assert!(map.is_empty()); |
| 285 |
} |
| 286 |
|
| 287 |
#[test] |
| 288 |
fn swap_remove_index() { |
| 289 |
let insert = [0, 4, 2, 12, 8, 7, 11, 5, 3, 17, 19, 22, 23]; |
| 290 |
let mut map = IndexMap::new(); |
| 291 |
|
| 292 |
for &elt in &insert { |
| 293 |
map.insert(elt, elt * 2); |
| 294 |
} |
| 295 |
|
| 296 |
let mut vector = insert.to_vec(); |
| 297 |
let remove_sequence = &[3, 3, 10, 4, 5, 4, 3, 0, 1]; |
| 298 |
|
| 299 |
// check that the same swap remove sequence on vec and map |
| 300 |
// have the same result. |
| 301 |
for &rm in remove_sequence { |
| 302 |
let out_vec = vector.swap_remove(rm); |
| 303 |
let (out_map, _) = map.swap_remove_index(rm).unwrap(); |
| 304 |
assert_eq!(out_vec, out_map); |
| 305 |
} |
| 306 |
assert_eq!(vector.len(), map.len()); |
| 307 |
for (a, b) in vector.iter().zip(map.keys()) { |
| 308 |
assert_eq!(a, b); |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
#[test] |
| 313 |
fn partial_eq_and_eq() { |
| 314 |
let mut map_a = IndexMap::new(); |
| 315 |
map_a.insert(1, "1"); |
| 316 |
map_a.insert(2, "2"); |
| 317 |
let mut map_b = map_a.clone(); |
| 318 |
assert_eq!(map_a, map_b); |
| 319 |
map_b.swap_remove(&1); |
| 320 |
assert_ne!(map_a, map_b); |
| 321 |
|
| 322 |
let map_c: IndexMap<_, String> = map_b.into_iter().map(|(k, v)| (k, v.into())).collect(); |
| 323 |
assert_ne!(map_a, map_c); |
| 324 |
assert_ne!(map_c, map_a); |
| 325 |
} |
| 326 |
|
| 327 |
#[test] |
| 328 |
fn extend() { |
| 329 |
let mut map = IndexMap::new(); |
| 330 |
map.extend(vec![(&1, &2), (&3, &4)]); |
| 331 |
map.extend(vec![(5, 6)]); |
| 332 |
assert_eq!( |
| 333 |
map.into_iter().collect::<Vec<_>>(), |
| 334 |
vec![(1, 2), (3, 4), (5, 6)] |
| 335 |
); |
| 336 |
} |
| 337 |
|
| 338 |
#[test] |
| 339 |
fn entry() { |
| 340 |
let mut map = IndexMap::new(); |
| 341 |
|
| 342 |
map.insert(1, "1"); |
| 343 |
map.insert(2, "2"); |
| 344 |
{ |
| 345 |
let e = map.entry(3); |
| 346 |
assert_eq!(e.index(), 2); |
| 347 |
let e = e.or_insert("3"); |
| 348 |
assert_eq!(e, &"3"); |
| 349 |
} |
| 350 |
|
| 351 |
let e = map.entry(2); |
| 352 |
assert_eq!(e.index(), 1); |
| 353 |
assert_eq!(e.key(), &2); |
| 354 |
match e { |
| 355 |
Entry::Occupied(ref e) => assert_eq!(e.get(), &"2"), |
| 356 |
Entry::Vacant(_) => panic!(), |
| 357 |
} |
| 358 |
assert_eq!(e.or_insert("4"), &"2"); |
| 359 |
} |
| 360 |
|
| 361 |
#[test] |
| 362 |
fn entry_and_modify() { |
| 363 |
let mut map = IndexMap::new(); |
| 364 |
|
| 365 |
map.insert(1, "1"); |
| 366 |
map.entry(1).and_modify(|x| *x = "2"); |
| 367 |
assert_eq!(Some(&"2"), map.get(&1)); |
| 368 |
|
| 369 |
map.entry(2).and_modify(|x| *x = "doesn't exist"); |
| 370 |
assert_eq!(None, map.get(&2)); |
| 371 |
} |
| 372 |
|
| 373 |
#[test] |
| 374 |
fn entry_or_default() { |
| 375 |
let mut map = IndexMap::new(); |
| 376 |
|
| 377 |
#[derive(Debug, PartialEq)] |
| 378 |
enum TestEnum { |
| 379 |
DefaultValue, |
| 380 |
NonDefaultValue, |
| 381 |
} |
| 382 |
|
| 383 |
impl Default for TestEnum { |
| 384 |
fn default() -> Self { |
| 385 |
TestEnum::DefaultValue |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
map.insert(1, TestEnum::NonDefaultValue); |
| 390 |
assert_eq!(&mut TestEnum::NonDefaultValue, map.entry(1).or_default()); |
| 391 |
|
| 392 |
assert_eq!(&mut TestEnum::DefaultValue, map.entry(2).or_default()); |
| 393 |
} |
| 394 |
|
| 395 |
#[test] |
| 396 |
fn occupied_entry_key() { |
| 397 |
// These keys match hash and equality, but their addresses are distinct. |
| 398 |
let (k1, k2) = (&mut 1, &mut 1); |
| 399 |
let k1_ptr = k1 as *const i32; |
| 400 |
let k2_ptr = k2 as *const i32; |
| 401 |
assert_ne!(k1_ptr, k2_ptr); |
| 402 |
|
| 403 |
let mut map = IndexMap::new(); |
| 404 |
map.insert(k1, "value"); |
| 405 |
match map.entry(k2) { |
| 406 |
Entry::Occupied(ref e) => { |
| 407 |
// `OccupiedEntry::key` should reference the key in the map, |
| 408 |
// not the key that was used to find the entry. |
| 409 |
let ptr = *e.key() as *const i32; |
| 410 |
assert_eq!(ptr, k1_ptr); |
| 411 |
assert_ne!(ptr, k2_ptr); |
| 412 |
} |
| 413 |
Entry::Vacant(_) => panic!(), |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
#[test] |
| 418 |
fn get_index_entry() { |
| 419 |
let mut map = IndexMap::new(); |
| 420 |
|
| 421 |
assert!(map.get_index_entry(0).is_none()); |
| 422 |
assert!(map.first_entry().is_none()); |
| 423 |
assert!(map.last_entry().is_none()); |
| 424 |
|
| 425 |
map.insert(0, "0"); |
| 426 |
map.insert(1, "1"); |
| 427 |
map.insert(2, "2"); |
| 428 |
map.insert(3, "3"); |
| 429 |
|
| 430 |
assert!(map.get_index_entry(4).is_none()); |
| 431 |
|
| 432 |
{ |
| 433 |
let e = map.get_index_entry(1).unwrap(); |
| 434 |
assert_eq!(*e.key(), 1); |
| 435 |
assert_eq!(*e.get(), "1"); |
| 436 |
assert_eq!(e.swap_remove(), "1"); |
| 437 |
} |
| 438 |
|
| 439 |
{ |
| 440 |
let mut e = map.get_index_entry(1).unwrap(); |
| 441 |
assert_eq!(*e.key(), 3); |
| 442 |
assert_eq!(*e.get(), "3"); |
| 443 |
assert_eq!(e.insert("4"), "3"); |
| 444 |
} |
| 445 |
|
| 446 |
assert_eq!(*map.get(&3).unwrap(), "4"); |
| 447 |
|
| 448 |
{ |
| 449 |
let e = map.first_entry().unwrap(); |
| 450 |
assert_eq!(*e.key(), 0); |
| 451 |
assert_eq!(*e.get(), "0"); |
| 452 |
} |
| 453 |
|
| 454 |
{ |
| 455 |
let e = map.last_entry().unwrap(); |
| 456 |
assert_eq!(*e.key(), 2); |
| 457 |
assert_eq!(*e.get(), "2"); |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
#[test] |
| 462 |
fn from_entries() { |
| 463 |
let mut map = IndexMap::from([(1, "1"), (2, "2"), (3, "3")]); |
| 464 |
|
| 465 |
{ |
| 466 |
let e = match map.entry(1) { |
| 467 |
Entry::Occupied(e) => IndexedEntry::from(e), |
| 468 |
Entry::Vacant(_) => panic!(), |
| 469 |
}; |
| 470 |
assert_eq!(e.index(), 0); |
| 471 |
assert_eq!(*e.key(), 1); |
| 472 |
assert_eq!(*e.get(), "1"); |
| 473 |
} |
| 474 |
|
| 475 |
{ |
| 476 |
let e = match map.get_index_entry(1) { |
| 477 |
Some(e) => OccupiedEntry::from(e), |
| 478 |
None => panic!(), |
| 479 |
}; |
| 480 |
assert_eq!(e.index(), 1); |
| 481 |
assert_eq!(*e.key(), 2); |
| 482 |
assert_eq!(*e.get(), "2"); |
| 483 |
} |
| 484 |
} |
| 485 |
|
| 486 |
#[test] |
| 487 |
fn keys() { |
| 488 |
let vec = vec![(1, 'a'), (2, 'b'), (3, 'c')]; |
| 489 |
let map: IndexMap<_, _> = vec.into_iter().collect(); |
| 490 |
let keys: Vec<_> = map.keys().copied().collect(); |
| 491 |
assert_eq!(keys.len(), 3); |
| 492 |
assert!(keys.contains(&1)); |
| 493 |
assert!(keys.contains(&2)); |
| 494 |
assert!(keys.contains(&3)); |
| 495 |
} |
| 496 |
|
| 497 |
#[test] |
| 498 |
fn into_keys() { |
| 499 |
let vec = vec![(1, 'a'), (2, 'b'), (3, 'c')]; |
| 500 |
let map: IndexMap<_, _> = vec.into_iter().collect(); |
| 501 |
let keys: Vec<i32> = map.into_keys().collect(); |
| 502 |
assert_eq!(keys.len(), 3); |
| 503 |
assert!(keys.contains(&1)); |
| 504 |
assert!(keys.contains(&2)); |
| 505 |
assert!(keys.contains(&3)); |
| 506 |
} |
| 507 |
|
| 508 |
#[test] |
| 509 |
fn values() { |
| 510 |
let vec = vec![(1, 'a'), (2, 'b'), (3, 'c')]; |
| 511 |
let map: IndexMap<_, _> = vec.into_iter().collect(); |
| 512 |
let values: Vec<_> = map.values().copied().collect(); |
| 513 |
assert_eq!(values.len(), 3); |
| 514 |
assert!(values.contains(&'a')); |
| 515 |
assert!(values.contains(&'b')); |
| 516 |
assert!(values.contains(&'c')); |
| 517 |
} |
| 518 |
|
| 519 |
#[test] |
| 520 |
fn values_mut() { |
| 521 |
let vec = vec![(1, 1), (2, 2), (3, 3)]; |
| 522 |
let mut map: IndexMap<_, _> = vec.into_iter().collect(); |
| 523 |
for value in map.values_mut() { |
| 524 |
*value *= 2 |
| 525 |
} |
| 526 |
let values: Vec<_> = map.values().copied().collect(); |
| 527 |
assert_eq!(values.len(), 3); |
| 528 |
assert!(values.contains(&2)); |
| 529 |
assert!(values.contains(&4)); |
| 530 |
assert!(values.contains(&6)); |
| 531 |
} |
| 532 |
|
| 533 |
#[test] |
| 534 |
fn into_values() { |
| 535 |
let vec = vec![(1, 'a'), (2, 'b'), (3, 'c')]; |
| 536 |
let map: IndexMap<_, _> = vec.into_iter().collect(); |
| 537 |
let values: Vec<char> = map.into_values().collect(); |
| 538 |
assert_eq!(values.len(), 3); |
| 539 |
assert!(values.contains(&'a')); |
| 540 |
assert!(values.contains(&'b')); |
| 541 |
assert!(values.contains(&'c')); |
| 542 |
} |
| 543 |
|
| 544 |
#[test] |
| 545 |
fn drain_range() { |
| 546 |
// Test the various heuristics of `erase_indices` |
| 547 |
for range in [ |
| 548 |
0..0, // nothing erased |
| 549 |
10..90, // reinsert the few kept (..10 and 90..) |
| 550 |
80..90, // update the few to adjust (80..) |
| 551 |
20..30, // sweep everything |
| 552 |
] { |
| 553 |
let mut vec = Vec::from_iter(0..100); |
| 554 |
let mut map: IndexMap<i32, ()> = (0..100).map(|i| (i, ())).collect(); |
| 555 |
drop(vec.drain(range.clone())); |
| 556 |
drop(map.drain(range)); |
| 557 |
assert!(vec.iter().eq(map.keys())); |
| 558 |
for (i, x) in vec.iter().enumerate() { |
| 559 |
assert_eq!(map.get_index_of(x), Some(i)); |
| 560 |
} |
| 561 |
} |
| 562 |
} |
| 563 |
|
| 564 |
#[test] |
| 565 |
#[cfg(feature = "std")] |
| 566 |
fn from_array() { |
| 567 |
let map = IndexMap::from([(1, 2), (3, 4)]); |
| 568 |
let mut expected = IndexMap::new(); |
| 569 |
expected.insert(1, 2); |
| 570 |
expected.insert(3, 4); |
| 571 |
|
| 572 |
assert_eq!(map, expected) |
| 573 |
} |
| 574 |
|
| 575 |
#[test] |
| 576 |
fn iter_default() { |
| 577 |
struct K; |
| 578 |
struct V; |
| 579 |
fn assert_default<T>() |
| 580 |
where |
| 581 |
T: Default + Iterator, |
| 582 |
{ |
| 583 |
assert!(T::default().next().is_none()); |
| 584 |
} |
| 585 |
assert_default::<Iter<'static, K, V>>(); |
| 586 |
assert_default::<IterMut<'static, K, V>>(); |
| 587 |
assert_default::<IterMut2<'static, K, V>>(); |
| 588 |
assert_default::<IntoIter<K, V>>(); |
| 589 |
assert_default::<Keys<'static, K, V>>(); |
| 590 |
assert_default::<IntoKeys<K, V>>(); |
| 591 |
assert_default::<Values<'static, K, V>>(); |
| 592 |
assert_default::<ValuesMut<'static, K, V>>(); |
| 593 |
assert_default::<IntoValues<K, V>>(); |
| 594 |
} |
| 595 |
|
| 596 |
#[test] |
| 597 |
fn test_binary_search_by() { |
| 598 |
// adapted from std's test for binary_search |
| 599 |
let b: IndexMap<_, i32> = [] |
| 600 |
.into_iter() |
| 601 |
.enumerate() |
| 602 |
.map(|(i, x)| (i + 100, x)) |
| 603 |
.collect(); |
| 604 |
assert_eq!(b.binary_search_by(|_, x| x.cmp(&5)), Err(0)); |
| 605 |
|
| 606 |
let b: IndexMap<_, i32> = [4] |
| 607 |
.into_iter() |
| 608 |
.enumerate() |
| 609 |
.map(|(i, x)| (i + 100, x)) |
| 610 |
.collect(); |
| 611 |
assert_eq!(b.binary_search_by(|_, x| x.cmp(&3)), Err(0)); |
| 612 |
assert_eq!(b.binary_search_by(|_, x| x.cmp(&4)), Ok(0)); |
| 613 |
assert_eq!(b.binary_search_by(|_, x| x.cmp(&5)), Err(1)); |
| 614 |
|
| 615 |
let b: IndexMap<_, i32> = [1, 2, 4, 6, 8, 9] |
| 616 |
.into_iter() |
| 617 |
.enumerate() |
| 618 |
.map(|(i, x)| (i + 100, x)) |
| 619 |
.collect(); |
| 620 |
assert_eq!(b.binary_search_by(|_, x| x.cmp(&5)), Err(3)); |
| 621 |
assert_eq!(b.binary_search_by(|_, x| x.cmp(&6)), Ok(3)); |
| 622 |
assert_eq!(b.binary_search_by(|_, x| x.cmp(&7)), Err(4)); |
| 623 |
assert_eq!(b.binary_search_by(|_, x| x.cmp(&8)), Ok(4)); |
| 624 |
|
| 625 |
let b: IndexMap<_, i32> = [1, 2, 4, 5, 6, 8] |
| 626 |
.into_iter() |
| 627 |
.enumerate() |
| 628 |
.map(|(i, x)| (i + 100, x)) |
| 629 |
.collect(); |
| 630 |
assert_eq!(b.binary_search_by(|_, x| x.cmp(&9)), Err(6)); |
| 631 |
|
| 632 |
let b: IndexMap<_, i32> = [1, 2, 4, 6, 7, 8, 9] |
| 633 |
.into_iter() |
| 634 |
.enumerate() |
| 635 |
.map(|(i, x)| (i + 100, x)) |
| 636 |
.collect(); |
| 637 |
assert_eq!(b.binary_search_by(|_, x| x.cmp(&6)), Ok(3)); |
| 638 |
assert_eq!(b.binary_search_by(|_, x| x.cmp(&5)), Err(3)); |
| 639 |
assert_eq!(b.binary_search_by(|_, x| x.cmp(&8)), Ok(5)); |
| 640 |
|
| 641 |
let b: IndexMap<_, i32> = [1, 2, 4, 5, 6, 8, 9] |
| 642 |
.into_iter() |
| 643 |
.enumerate() |
| 644 |
.map(|(i, x)| (i + 100, x)) |
| 645 |
.collect(); |
| 646 |
assert_eq!(b.binary_search_by(|_, x| x.cmp(&7)), Err(5)); |
| 647 |
assert_eq!(b.binary_search_by(|_, x| x.cmp(&0)), Err(0)); |
| 648 |
|
| 649 |
let b: IndexMap<_, i32> = [1, 3, 3, 3, 7] |
| 650 |
.into_iter() |
| 651 |
.enumerate() |
| 652 |
.map(|(i, x)| (i + 100, x)) |
| 653 |
.collect(); |
| 654 |
assert_eq!(b.binary_search_by(|_, x| x.cmp(&0)), Err(0)); |
| 655 |
assert_eq!(b.binary_search_by(|_, x| x.cmp(&1)), Ok(0)); |
| 656 |
assert_eq!(b.binary_search_by(|_, x| x.cmp(&2)), Err(1)); |
| 657 |
assert!(match b.binary_search_by(|_, x| x.cmp(&3)) { |
| 658 |
Ok(1..=3) => true, |
| 659 |
_ => false, |
| 660 |
}); |
| 661 |
assert!(match b.binary_search_by(|_, x| x.cmp(&3)) { |
| 662 |
Ok(1..=3) => true, |
| 663 |
_ => false, |
| 664 |
}); |
| 665 |
assert_eq!(b.binary_search_by(|_, x| x.cmp(&4)), Err(4)); |
| 666 |
assert_eq!(b.binary_search_by(|_, x| x.cmp(&5)), Err(4)); |
| 667 |
assert_eq!(b.binary_search_by(|_, x| x.cmp(&6)), Err(4)); |
| 668 |
assert_eq!(b.binary_search_by(|_, x| x.cmp(&7)), Ok(4)); |
| 669 |
assert_eq!(b.binary_search_by(|_, x| x.cmp(&8)), Err(5)); |
| 670 |
} |
| 671 |
|
| 672 |
#[test] |
| 673 |
fn test_binary_search_by_key() { |
| 674 |
// adapted from std's test for binary_search |
| 675 |
let b: IndexMap<_, i32> = [] |
| 676 |
.into_iter() |
| 677 |
.enumerate() |
| 678 |
.map(|(i, x)| (i + 100, x)) |
| 679 |
.collect(); |
| 680 |
assert_eq!(b.binary_search_by_key(&5, |_, &x| x), Err(0)); |
| 681 |
|
| 682 |
let b: IndexMap<_, i32> = [4] |
| 683 |
.into_iter() |
| 684 |
.enumerate() |
| 685 |
.map(|(i, x)| (i + 100, x)) |
| 686 |
.collect(); |
| 687 |
assert_eq!(b.binary_search_by_key(&3, |_, &x| x), Err(0)); |
| 688 |
assert_eq!(b.binary_search_by_key(&4, |_, &x| x), Ok(0)); |
| 689 |
assert_eq!(b.binary_search_by_key(&5, |_, &x| x), Err(1)); |
| 690 |
|
| 691 |
let b: IndexMap<_, i32> = [1, 2, 4, 6, 8, 9] |
| 692 |
.into_iter() |
| 693 |
.enumerate() |
| 694 |
.map(|(i, x)| (i + 100, x)) |
| 695 |
.collect(); |
| 696 |
assert_eq!(b.binary_search_by_key(&5, |_, &x| x), Err(3)); |
| 697 |
assert_eq!(b.binary_search_by_key(&6, |_, &x| x), Ok(3)); |
| 698 |
assert_eq!(b.binary_search_by_key(&7, |_, &x| x), Err(4)); |
| 699 |
assert_eq!(b.binary_search_by_key(&8, |_, &x| x), Ok(4)); |
| 700 |
|
| 701 |
let b: IndexMap<_, i32> = [1, 2, 4, 5, 6, 8] |
| 702 |
.into_iter() |
| 703 |
.enumerate() |
| 704 |
.map(|(i, x)| (i + 100, x)) |
| 705 |
.collect(); |
| 706 |
assert_eq!(b.binary_search_by_key(&9, |_, &x| x), Err(6)); |
| 707 |
|
| 708 |
let b: IndexMap<_, i32> = [1, 2, 4, 6, 7, 8, 9] |
| 709 |
.into_iter() |
| 710 |
.enumerate() |
| 711 |
.map(|(i, x)| (i + 100, x)) |
| 712 |
.collect(); |
| 713 |
assert_eq!(b.binary_search_by_key(&6, |_, &x| x), Ok(3)); |
| 714 |
assert_eq!(b.binary_search_by_key(&5, |_, &x| x), Err(3)); |
| 715 |
assert_eq!(b.binary_search_by_key(&8, |_, &x| x), Ok(5)); |
| 716 |
|
| 717 |
let b: IndexMap<_, i32> = [1, 2, 4, 5, 6, 8, 9] |
| 718 |
.into_iter() |
| 719 |
.enumerate() |
| 720 |
.map(|(i, x)| (i + 100, x)) |
| 721 |
.collect(); |
| 722 |
assert_eq!(b.binary_search_by_key(&7, |_, &x| x), Err(5)); |
| 723 |
assert_eq!(b.binary_search_by_key(&0, |_, &x| x), Err(0)); |
| 724 |
|
| 725 |
let b: IndexMap<_, i32> = [1, 3, 3, 3, 7] |
| 726 |
.into_iter() |
| 727 |
.enumerate() |
| 728 |
.map(|(i, x)| (i + 100, x)) |
| 729 |
.collect(); |
| 730 |
assert_eq!(b.binary_search_by_key(&0, |_, &x| x), Err(0)); |
| 731 |
assert_eq!(b.binary_search_by_key(&1, |_, &x| x), Ok(0)); |
| 732 |
assert_eq!(b.binary_search_by_key(&2, |_, &x| x), Err(1)); |
| 733 |
assert!(match b.binary_search_by_key(&3, |_, &x| x) { |
| 734 |
Ok(1..=3) => true, |
| 735 |
_ => false, |
| 736 |
}); |
| 737 |
assert!(match b.binary_search_by_key(&3, |_, &x| x) { |
| 738 |
Ok(1..=3) => true, |
| 739 |
_ => false, |
| 740 |
}); |
| 741 |
assert_eq!(b.binary_search_by_key(&4, |_, &x| x), Err(4)); |
| 742 |
assert_eq!(b.binary_search_by_key(&5, |_, &x| x), Err(4)); |
| 743 |
assert_eq!(b.binary_search_by_key(&6, |_, &x| x), Err(4)); |
| 744 |
assert_eq!(b.binary_search_by_key(&7, |_, &x| x), Ok(4)); |
| 745 |
assert_eq!(b.binary_search_by_key(&8, |_, &x| x), Err(5)); |
| 746 |
} |
| 747 |
|
| 748 |
#[test] |
| 749 |
fn test_partition_point() { |
| 750 |
// adapted from std's test for partition_point |
| 751 |
let b: IndexMap<_, i32> = [] |
| 752 |
.into_iter() |
| 753 |
.enumerate() |
| 754 |
.map(|(i, x)| (i + 100, x)) |
| 755 |
.collect(); |
| 756 |
assert_eq!(b.partition_point(|_, &x| x < 5), 0); |
| 757 |
|
| 758 |
let b: IndexMap<_, i32> = [4] |
| 759 |
.into_iter() |
| 760 |
.enumerate() |
| 761 |
.map(|(i, x)| (i + 100, x)) |
| 762 |
.collect(); |
| 763 |
assert_eq!(b.partition_point(|_, &x| x < 3), 0); |
| 764 |
assert_eq!(b.partition_point(|_, &x| x < 4), 0); |
| 765 |
assert_eq!(b.partition_point(|_, &x| x < 5), 1); |
| 766 |
|
| 767 |
let b: IndexMap<_, i32> = [1, 2, 4, 6, 8, 9] |
| 768 |
.into_iter() |
| 769 |
.enumerate() |
| 770 |
.map(|(i, x)| (i + 100, x)) |
| 771 |
.collect(); |
| 772 |
assert_eq!(b.partition_point(|_, &x| x < 5), 3); |
| 773 |
assert_eq!(b.partition_point(|_, &x| x < 6), 3); |
| 774 |
assert_eq!(b.partition_point(|_, &x| x < 7), 4); |
| 775 |
assert_eq!(b.partition_point(|_, &x| x < 8), 4); |
| 776 |
|
| 777 |
let b: IndexMap<_, i32> = [1, 2, 4, 5, 6, 8] |
| 778 |
.into_iter() |
| 779 |
.enumerate() |
| 780 |
.map(|(i, x)| (i + 100, x)) |
| 781 |
.collect(); |
| 782 |
assert_eq!(b.partition_point(|_, &x| x < 9), 6); |
| 783 |
|
| 784 |
let b: IndexMap<_, i32> = [1, 2, 4, 6, 7, 8, 9] |
| 785 |
.into_iter() |
| 786 |
.enumerate() |
| 787 |
.map(|(i, x)| (i + 100, x)) |
| 788 |
.collect(); |
| 789 |
assert_eq!(b.partition_point(|_, &x| x < 6), 3); |
| 790 |
assert_eq!(b.partition_point(|_, &x| x < 5), 3); |
| 791 |
assert_eq!(b.partition_point(|_, &x| x < 8), 5); |
| 792 |
|
| 793 |
let b: IndexMap<_, i32> = [1, 2, 4, 5, 6, 8, 9] |
| 794 |
.into_iter() |
| 795 |
.enumerate() |
| 796 |
.map(|(i, x)| (i + 100, x)) |
| 797 |
.collect(); |
| 798 |
assert_eq!(b.partition_point(|_, &x| x < 7), 5); |
| 799 |
assert_eq!(b.partition_point(|_, &x| x < 0), 0); |
| 800 |
|
| 801 |
let b: IndexMap<_, i32> = [1, 3, 3, 3, 7] |
| 802 |
.into_iter() |
| 803 |
.enumerate() |
| 804 |
.map(|(i, x)| (i + 100, x)) |
| 805 |
.collect(); |
| 806 |
assert_eq!(b.partition_point(|_, &x| x < 0), 0); |
| 807 |
assert_eq!(b.partition_point(|_, &x| x < 1), 0); |
| 808 |
assert_eq!(b.partition_point(|_, &x| x < 2), 1); |
| 809 |
assert_eq!(b.partition_point(|_, &x| x < 3), 1); |
| 810 |
assert_eq!(b.partition_point(|_, &x| x < 4), 4); |
| 811 |
assert_eq!(b.partition_point(|_, &x| x < 5), 4); |
| 812 |
assert_eq!(b.partition_point(|_, &x| x < 6), 4); |
| 813 |
assert_eq!(b.partition_point(|_, &x| x < 7), 4); |
| 814 |
assert_eq!(b.partition_point(|_, &x| x < 8), 5); |
| 815 |
} |
| 816 |
|
| 817 |
macro_rules! move_index_oob { |
| 818 |
($test:ident, $from:expr, $to:expr) => { |
| 819 |
#[test] |
| 820 |
#[should_panic(expected = "index out of bounds")] |
| 821 |
fn $test() { |
| 822 |
let mut map: IndexMap<i32, ()> = (0..10).map(|k| (k, ())).collect(); |
| 823 |
map.move_index($from, $to); |
| 824 |
} |
| 825 |
}; |
| 826 |
} |
| 827 |
move_index_oob!(test_move_index_out_of_bounds_0_10, 0, 10); |
| 828 |
move_index_oob!(test_move_index_out_of_bounds_0_max, 0, usize::MAX); |
| 829 |
move_index_oob!(test_move_index_out_of_bounds_10_0, 10, 0); |
| 830 |
move_index_oob!(test_move_index_out_of_bounds_max_0, usize::MAX, 0); |