| 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 get_index_mut2() { |
| 598 |
let mut map: IndexMap<i32, i32> = IndexMap::new(); |
| 599 |
map.insert(1, 2); |
| 600 |
map.insert(3, 4); |
| 601 |
map.insert(5, 6); |
| 602 |
|
| 603 |
{ |
| 604 |
let (key, value) = map.get_index_mut2(0).unwrap(); |
| 605 |
assert_eq!(*key, 1); |
| 606 |
assert_eq!(*value, 2); |
| 607 |
|
| 608 |
*value = 7; |
| 609 |
} |
| 610 |
assert_eq!(map[0], 7); |
| 611 |
|
| 612 |
{ |
| 613 |
let (key, _) = map.get_index_mut2(0).unwrap(); |
| 614 |
*key = 8; |
| 615 |
} |
| 616 |
assert_eq!(map.get_index(0).unwrap().0, &8); |
| 617 |
} |
| 618 |
|
| 619 |
#[test] |
| 620 |
fn shift_shift_remove_index() { |
| 621 |
let mut map: IndexMap<i32, i32> = IndexMap::new(); |
| 622 |
map.insert(1, 2); |
| 623 |
map.insert(3, 4); |
| 624 |
map.insert(5, 6); |
| 625 |
map.insert(7, 8); |
| 626 |
map.insert(9, 10); |
| 627 |
|
| 628 |
let result = map.shift_remove_index(1); |
| 629 |
assert_eq!(result, Some((3, 4))); |
| 630 |
assert_eq!(map.len(), 4); |
| 631 |
assert_eq!(map.as_slice(), &[(1, 2), (5, 6), (7, 8), (9, 10)]); |
| 632 |
|
| 633 |
let result = map.shift_remove_index(1); |
| 634 |
assert_eq!(result, Some((5, 6))); |
| 635 |
assert_eq!(map.len(), 3); |
| 636 |
assert_eq!(map.as_slice(), &[(1, 2), (7, 8), (9, 10)]); |
| 637 |
|
| 638 |
let result = map.shift_remove_index(2); |
| 639 |
assert_eq!(result, Some((9, 10))); |
| 640 |
assert_eq!(map.len(), 2); |
| 641 |
assert_eq!(map.as_slice(), &[(1, 2), (7, 8)]); |
| 642 |
|
| 643 |
let result = map.shift_remove_index(2); |
| 644 |
assert_eq!(result, None); |
| 645 |
assert_eq!(map.len(), 2); |
| 646 |
assert_eq!(map.as_slice(), &[(1, 2), (7, 8)]); |
| 647 |
} |
| 648 |
|
| 649 |
#[test] |
| 650 |
fn shift_remove_entry() { |
| 651 |
let mut map: IndexMap<i32, i32> = IndexMap::new(); |
| 652 |
map.insert(1, 2); |
| 653 |
map.insert(3, 4); |
| 654 |
map.insert(5, 6); |
| 655 |
map.insert(7, 8); |
| 656 |
map.insert(9, 10); |
| 657 |
|
| 658 |
let result = map.shift_remove_entry(&3); |
| 659 |
assert_eq!(result, Some((3, 4))); |
| 660 |
assert_eq!(map.len(), 4); |
| 661 |
assert_eq!(map.as_slice(), &[(1, 2), (5, 6), (7, 8), (9, 10)]); |
| 662 |
|
| 663 |
let result = map.shift_remove_entry(&9); |
| 664 |
assert_eq!(result, Some((9, 10))); |
| 665 |
assert_eq!(map.len(), 3); |
| 666 |
assert_eq!(map.as_slice(), &[(1, 2), (5, 6), (7, 8)]); |
| 667 |
|
| 668 |
let result = map.shift_remove_entry(&9); |
| 669 |
assert_eq!(result, None); |
| 670 |
assert_eq!(map.len(), 3); |
| 671 |
assert_eq!(map.as_slice(), &[(1, 2), (5, 6), (7, 8)]); |
| 672 |
} |
| 673 |
|
| 674 |
#[test] |
| 675 |
fn shift_remove_full() { |
| 676 |
let mut map: IndexMap<i32, i32> = IndexMap::new(); |
| 677 |
map.insert(1, 2); |
| 678 |
map.insert(3, 4); |
| 679 |
map.insert(5, 6); |
| 680 |
map.insert(7, 8); |
| 681 |
map.insert(9, 10); |
| 682 |
|
| 683 |
let result = map.shift_remove_full(&3); |
| 684 |
assert_eq!(result, Some((1, 3, 4))); |
| 685 |
assert_eq!(map.len(), 4); |
| 686 |
assert_eq!(map.as_slice(), &[(1, 2), (5, 6), (7, 8), (9, 10)]); |
| 687 |
|
| 688 |
let result = map.shift_remove_full(&9); |
| 689 |
assert_eq!(result, Some((3, 9, 10))); |
| 690 |
assert_eq!(map.len(), 3); |
| 691 |
assert_eq!(map.as_slice(), &[(1, 2), (5, 6), (7, 8)]); |
| 692 |
|
| 693 |
let result = map.shift_remove_full(&9); |
| 694 |
assert_eq!(result, None); |
| 695 |
assert_eq!(map.len(), 3); |
| 696 |
assert_eq!(map.as_slice(), &[(1, 2), (5, 6), (7, 8)]); |
| 697 |
} |
| 698 |
|
| 699 |
#[test] |
| 700 |
fn sorted_unstable_by() { |
| 701 |
let mut map: IndexMap<i32, i32> = IndexMap::new(); |
| 702 |
map.extend(vec![(1, 10), (2, 20), (3, 30), (4, 40), (5, 50)]); |
| 703 |
let sorted = map.sorted_unstable_by(|_a, b, _c, d| d.cmp(&b)); |
| 704 |
|
| 705 |
assert_eq!( |
| 706 |
sorted.as_slice(), |
| 707 |
&[(5, 50), (4, 40), (3, 30), (2, 20), (1, 10)] |
| 708 |
); |
| 709 |
} |
| 710 |
|
| 711 |
#[test] |
| 712 |
fn into_boxed_slice() { |
| 713 |
let mut map: IndexMap<i32, i32> = IndexMap::new(); |
| 714 |
for i in 0..5 { |
| 715 |
map.insert(i, i * 10); |
| 716 |
} |
| 717 |
let boxed_slice: Box<Slice<i32, i32>> = map.into_boxed_slice(); |
| 718 |
assert_eq!(boxed_slice.len(), 5); |
| 719 |
assert_eq!( |
| 720 |
boxed_slice.as_ref(), |
| 721 |
&[(0, 0), (1, 10), (2, 20), (3, 30), (4, 40)] |
| 722 |
); |
| 723 |
} |
| 724 |
|
| 725 |
#[test] |
| 726 |
fn last_mut() { |
| 727 |
let mut map: IndexMap<&str, i32> = IndexMap::new(); |
| 728 |
|
| 729 |
let last_entry = map.last_mut(); |
| 730 |
assert_eq!(last_entry, None); |
| 731 |
|
| 732 |
map.insert("key1", 1); |
| 733 |
map.insert("key2", 2); |
| 734 |
map.insert("key3", 3); |
| 735 |
let last_entry = map.last_mut(); |
| 736 |
assert_eq!(last_entry, Some((&"key3", &mut 3))); |
| 737 |
|
| 738 |
*last_entry.unwrap().1 = 4; |
| 739 |
assert_eq!(map.get("key3"), Some(&4)); |
| 740 |
} |
| 741 |
|
| 742 |
#[test] |
| 743 |
#[should_panic = "index out of bounds"] |
| 744 |
fn insert_before_oob() { |
| 745 |
let mut map: IndexMap<char, ()> = IndexMap::new(); |
| 746 |
let _ = map.insert_before(0, 'a', ()); |
| 747 |
let _ = map.insert_before(1, 'b', ()); |
| 748 |
map.insert_before(3, 'd', ()); |
| 749 |
} |
| 750 |
|
| 751 |
#[test] |
| 752 |
fn clear() { |
| 753 |
let mut map: IndexMap<i32, i32> = IndexMap::new(); |
| 754 |
map.extend(vec![(1, 10), (2, 20), (3, 30), (4, 40), (5, 50)]); |
| 755 |
map.clear(); |
| 756 |
assert_eq!(map.len(), 0); |
| 757 |
} |
| 758 |
|
| 759 |
#[test] |
| 760 |
fn get_range() { |
| 761 |
let mut index_map: IndexMap<i32, i32> = IndexMap::new(); |
| 762 |
index_map.insert(1, 10); |
| 763 |
index_map.insert(2, 20); |
| 764 |
index_map.insert(3, 30); |
| 765 |
index_map.insert(4, 40); |
| 766 |
index_map.insert(5, 50); |
| 767 |
|
| 768 |
let result = index_map.get_range(2..2); |
| 769 |
assert!(result.unwrap().is_empty()); |
| 770 |
|
| 771 |
let result = index_map.get_range(4..2); |
| 772 |
assert!(result.is_none()); |
| 773 |
|
| 774 |
let result = index_map.get_range(2..4); |
| 775 |
let slice: &Slice<i32, i32> = result.unwrap(); |
| 776 |
assert_eq!(slice.len(), 2); |
| 777 |
assert_eq!(slice, &[(3, 30), (4, 40)]); |
| 778 |
} |
| 779 |
|
| 780 |
#[test] |
| 781 |
fn get_range_mut() { |
| 782 |
let mut index_map: IndexMap<i32, i32> = IndexMap::new(); |
| 783 |
index_map.insert(1, 10); |
| 784 |
index_map.insert(2, 20); |
| 785 |
index_map.insert(3, 30); |
| 786 |
index_map.insert(4, 40); |
| 787 |
index_map.insert(5, 50); |
| 788 |
|
| 789 |
let result = index_map.get_range_mut(2..2); |
| 790 |
assert!(result.unwrap().is_empty()); |
| 791 |
|
| 792 |
let result = index_map.get_range_mut(4..2); |
| 793 |
assert!(result.is_none()); |
| 794 |
|
| 795 |
let result = index_map.get_range_mut(2..4); |
| 796 |
let slice: &mut Slice<i32, i32> = result.unwrap(); |
| 797 |
assert_eq!(slice.len(), 2); |
| 798 |
assert_eq!(slice, &mut [(3, 30), (4, 40)]); |
| 799 |
|
| 800 |
for i in 0..slice.len() { |
| 801 |
slice[i] += 1; |
| 802 |
} |
| 803 |
assert_eq!(slice, &mut [(3, 31), (4, 41)]); |
| 804 |
} |
| 805 |
|
| 806 |
#[test] |
| 807 |
#[should_panic = "index out of bounds"] |
| 808 |
fn shift_insert_oob() { |
| 809 |
let mut map: IndexMap<u32, u32> = IndexMap::new(); |
| 810 |
map.shift_insert(0, 1, 10); |
| 811 |
map.shift_insert(1, 2, 20); |
| 812 |
map.shift_insert(2, 3, 30); |
| 813 |
map.shift_insert(5, 4, 40); |
| 814 |
} |
| 815 |
|
| 816 |
#[test] |
| 817 |
fn test_binary_search_by() { |
| 818 |
// adapted from std's test for binary_search |
| 819 |
let b: IndexMap<_, i32> = [] |
| 820 |
.into_iter() |
| 821 |
.enumerate() |
| 822 |
.map(|(i, x)| (i + 100, x)) |
| 823 |
.collect(); |
| 824 |
assert_eq!(b.binary_search_by(|_, x| x.cmp(&5)), Err(0)); |
| 825 |
|
| 826 |
let b: IndexMap<_, i32> = [4] |
| 827 |
.into_iter() |
| 828 |
.enumerate() |
| 829 |
.map(|(i, x)| (i + 100, x)) |
| 830 |
.collect(); |
| 831 |
assert_eq!(b.binary_search_by(|_, x| x.cmp(&3)), Err(0)); |
| 832 |
assert_eq!(b.binary_search_by(|_, x| x.cmp(&4)), Ok(0)); |
| 833 |
assert_eq!(b.binary_search_by(|_, x| x.cmp(&5)), Err(1)); |
| 834 |
|
| 835 |
let b: IndexMap<_, i32> = [1, 2, 4, 6, 8, 9] |
| 836 |
.into_iter() |
| 837 |
.enumerate() |
| 838 |
.map(|(i, x)| (i + 100, x)) |
| 839 |
.collect(); |
| 840 |
assert_eq!(b.binary_search_by(|_, x| x.cmp(&5)), Err(3)); |
| 841 |
assert_eq!(b.binary_search_by(|_, x| x.cmp(&6)), Ok(3)); |
| 842 |
assert_eq!(b.binary_search_by(|_, x| x.cmp(&7)), Err(4)); |
| 843 |
assert_eq!(b.binary_search_by(|_, x| x.cmp(&8)), Ok(4)); |
| 844 |
|
| 845 |
let b: IndexMap<_, i32> = [1, 2, 4, 5, 6, 8] |
| 846 |
.into_iter() |
| 847 |
.enumerate() |
| 848 |
.map(|(i, x)| (i + 100, x)) |
| 849 |
.collect(); |
| 850 |
assert_eq!(b.binary_search_by(|_, x| x.cmp(&9)), Err(6)); |
| 851 |
|
| 852 |
let b: IndexMap<_, i32> = [1, 2, 4, 6, 7, 8, 9] |
| 853 |
.into_iter() |
| 854 |
.enumerate() |
| 855 |
.map(|(i, x)| (i + 100, x)) |
| 856 |
.collect(); |
| 857 |
assert_eq!(b.binary_search_by(|_, x| x.cmp(&6)), Ok(3)); |
| 858 |
assert_eq!(b.binary_search_by(|_, x| x.cmp(&5)), Err(3)); |
| 859 |
assert_eq!(b.binary_search_by(|_, x| x.cmp(&8)), Ok(5)); |
| 860 |
|
| 861 |
let b: IndexMap<_, i32> = [1, 2, 4, 5, 6, 8, 9] |
| 862 |
.into_iter() |
| 863 |
.enumerate() |
| 864 |
.map(|(i, x)| (i + 100, x)) |
| 865 |
.collect(); |
| 866 |
assert_eq!(b.binary_search_by(|_, x| x.cmp(&7)), Err(5)); |
| 867 |
assert_eq!(b.binary_search_by(|_, x| x.cmp(&0)), Err(0)); |
| 868 |
|
| 869 |
let b: IndexMap<_, i32> = [1, 3, 3, 3, 7] |
| 870 |
.into_iter() |
| 871 |
.enumerate() |
| 872 |
.map(|(i, x)| (i + 100, x)) |
| 873 |
.collect(); |
| 874 |
assert_eq!(b.binary_search_by(|_, x| x.cmp(&0)), Err(0)); |
| 875 |
assert_eq!(b.binary_search_by(|_, x| x.cmp(&1)), Ok(0)); |
| 876 |
assert_eq!(b.binary_search_by(|_, x| x.cmp(&2)), Err(1)); |
| 877 |
assert!(match b.binary_search_by(|_, x| x.cmp(&3)) { |
| 878 |
Ok(1..=3) => true, |
| 879 |
_ => false, |
| 880 |
}); |
| 881 |
assert!(match b.binary_search_by(|_, x| x.cmp(&3)) { |
| 882 |
Ok(1..=3) => true, |
| 883 |
_ => false, |
| 884 |
}); |
| 885 |
assert_eq!(b.binary_search_by(|_, x| x.cmp(&4)), Err(4)); |
| 886 |
assert_eq!(b.binary_search_by(|_, x| x.cmp(&5)), Err(4)); |
| 887 |
assert_eq!(b.binary_search_by(|_, x| x.cmp(&6)), Err(4)); |
| 888 |
assert_eq!(b.binary_search_by(|_, x| x.cmp(&7)), Ok(4)); |
| 889 |
assert_eq!(b.binary_search_by(|_, x| x.cmp(&8)), Err(5)); |
| 890 |
} |
| 891 |
|
| 892 |
#[test] |
| 893 |
fn test_binary_search_by_key() { |
| 894 |
// adapted from std's test for binary_search |
| 895 |
let b: IndexMap<_, i32> = [] |
| 896 |
.into_iter() |
| 897 |
.enumerate() |
| 898 |
.map(|(i, x)| (i + 100, x)) |
| 899 |
.collect(); |
| 900 |
assert_eq!(b.binary_search_by_key(&5, |_, &x| x), Err(0)); |
| 901 |
|
| 902 |
let b: IndexMap<_, i32> = [4] |
| 903 |
.into_iter() |
| 904 |
.enumerate() |
| 905 |
.map(|(i, x)| (i + 100, x)) |
| 906 |
.collect(); |
| 907 |
assert_eq!(b.binary_search_by_key(&3, |_, &x| x), Err(0)); |
| 908 |
assert_eq!(b.binary_search_by_key(&4, |_, &x| x), Ok(0)); |
| 909 |
assert_eq!(b.binary_search_by_key(&5, |_, &x| x), Err(1)); |
| 910 |
|
| 911 |
let b: IndexMap<_, i32> = [1, 2, 4, 6, 8, 9] |
| 912 |
.into_iter() |
| 913 |
.enumerate() |
| 914 |
.map(|(i, x)| (i + 100, x)) |
| 915 |
.collect(); |
| 916 |
assert_eq!(b.binary_search_by_key(&5, |_, &x| x), Err(3)); |
| 917 |
assert_eq!(b.binary_search_by_key(&6, |_, &x| x), Ok(3)); |
| 918 |
assert_eq!(b.binary_search_by_key(&7, |_, &x| x), Err(4)); |
| 919 |
assert_eq!(b.binary_search_by_key(&8, |_, &x| x), Ok(4)); |
| 920 |
|
| 921 |
let b: IndexMap<_, i32> = [1, 2, 4, 5, 6, 8] |
| 922 |
.into_iter() |
| 923 |
.enumerate() |
| 924 |
.map(|(i, x)| (i + 100, x)) |
| 925 |
.collect(); |
| 926 |
assert_eq!(b.binary_search_by_key(&9, |_, &x| x), Err(6)); |
| 927 |
|
| 928 |
let b: IndexMap<_, i32> = [1, 2, 4, 6, 7, 8, 9] |
| 929 |
.into_iter() |
| 930 |
.enumerate() |
| 931 |
.map(|(i, x)| (i + 100, x)) |
| 932 |
.collect(); |
| 933 |
assert_eq!(b.binary_search_by_key(&6, |_, &x| x), Ok(3)); |
| 934 |
assert_eq!(b.binary_search_by_key(&5, |_, &x| x), Err(3)); |
| 935 |
assert_eq!(b.binary_search_by_key(&8, |_, &x| x), Ok(5)); |
| 936 |
|
| 937 |
let b: IndexMap<_, i32> = [1, 2, 4, 5, 6, 8, 9] |
| 938 |
.into_iter() |
| 939 |
.enumerate() |
| 940 |
.map(|(i, x)| (i + 100, x)) |
| 941 |
.collect(); |
| 942 |
assert_eq!(b.binary_search_by_key(&7, |_, &x| x), Err(5)); |
| 943 |
assert_eq!(b.binary_search_by_key(&0, |_, &x| x), Err(0)); |
| 944 |
|
| 945 |
let b: IndexMap<_, i32> = [1, 3, 3, 3, 7] |
| 946 |
.into_iter() |
| 947 |
.enumerate() |
| 948 |
.map(|(i, x)| (i + 100, x)) |
| 949 |
.collect(); |
| 950 |
assert_eq!(b.binary_search_by_key(&0, |_, &x| x), Err(0)); |
| 951 |
assert_eq!(b.binary_search_by_key(&1, |_, &x| x), Ok(0)); |
| 952 |
assert_eq!(b.binary_search_by_key(&2, |_, &x| x), Err(1)); |
| 953 |
assert!(match b.binary_search_by_key(&3, |_, &x| x) { |
| 954 |
Ok(1..=3) => true, |
| 955 |
_ => false, |
| 956 |
}); |
| 957 |
assert!(match b.binary_search_by_key(&3, |_, &x| x) { |
| 958 |
Ok(1..=3) => true, |
| 959 |
_ => false, |
| 960 |
}); |
| 961 |
assert_eq!(b.binary_search_by_key(&4, |_, &x| x), Err(4)); |
| 962 |
assert_eq!(b.binary_search_by_key(&5, |_, &x| x), Err(4)); |
| 963 |
assert_eq!(b.binary_search_by_key(&6, |_, &x| x), Err(4)); |
| 964 |
assert_eq!(b.binary_search_by_key(&7, |_, &x| x), Ok(4)); |
| 965 |
assert_eq!(b.binary_search_by_key(&8, |_, &x| x), Err(5)); |
| 966 |
} |
| 967 |
|
| 968 |
#[test] |
| 969 |
fn test_partition_point() { |
| 970 |
// adapted from std's test for partition_point |
| 971 |
let b: IndexMap<_, i32> = [] |
| 972 |
.into_iter() |
| 973 |
.enumerate() |
| 974 |
.map(|(i, x)| (i + 100, x)) |
| 975 |
.collect(); |
| 976 |
assert_eq!(b.partition_point(|_, &x| x < 5), 0); |
| 977 |
|
| 978 |
let b: IndexMap<_, i32> = [4] |
| 979 |
.into_iter() |
| 980 |
.enumerate() |
| 981 |
.map(|(i, x)| (i + 100, x)) |
| 982 |
.collect(); |
| 983 |
assert_eq!(b.partition_point(|_, &x| x < 3), 0); |
| 984 |
assert_eq!(b.partition_point(|_, &x| x < 4), 0); |
| 985 |
assert_eq!(b.partition_point(|_, &x| x < 5), 1); |
| 986 |
|
| 987 |
let b: IndexMap<_, i32> = [1, 2, 4, 6, 8, 9] |
| 988 |
.into_iter() |
| 989 |
.enumerate() |
| 990 |
.map(|(i, x)| (i + 100, x)) |
| 991 |
.collect(); |
| 992 |
assert_eq!(b.partition_point(|_, &x| x < 5), 3); |
| 993 |
assert_eq!(b.partition_point(|_, &x| x < 6), 3); |
| 994 |
assert_eq!(b.partition_point(|_, &x| x < 7), 4); |
| 995 |
assert_eq!(b.partition_point(|_, &x| x < 8), 4); |
| 996 |
|
| 997 |
let b: IndexMap<_, i32> = [1, 2, 4, 5, 6, 8] |
| 998 |
.into_iter() |
| 999 |
.enumerate() |
| 1000 |
.map(|(i, x)| (i + 100, x)) |
| 1001 |
.collect(); |
| 1002 |
assert_eq!(b.partition_point(|_, &x| x < 9), 6); |
| 1003 |
|
| 1004 |
let b: IndexMap<_, i32> = [1, 2, 4, 6, 7, 8, 9] |
| 1005 |
.into_iter() |
| 1006 |
.enumerate() |
| 1007 |
.map(|(i, x)| (i + 100, x)) |
| 1008 |
.collect(); |
| 1009 |
assert_eq!(b.partition_point(|_, &x| x < 6), 3); |
| 1010 |
assert_eq!(b.partition_point(|_, &x| x < 5), 3); |
| 1011 |
assert_eq!(b.partition_point(|_, &x| x < 8), 5); |
| 1012 |
|
| 1013 |
let b: IndexMap<_, i32> = [1, 2, 4, 5, 6, 8, 9] |
| 1014 |
.into_iter() |
| 1015 |
.enumerate() |
| 1016 |
.map(|(i, x)| (i + 100, x)) |
| 1017 |
.collect(); |
| 1018 |
assert_eq!(b.partition_point(|_, &x| x < 7), 5); |
| 1019 |
assert_eq!(b.partition_point(|_, &x| x < 0), 0); |
| 1020 |
|
| 1021 |
let b: IndexMap<_, i32> = [1, 3, 3, 3, 7] |
| 1022 |
.into_iter() |
| 1023 |
.enumerate() |
| 1024 |
.map(|(i, x)| (i + 100, x)) |
| 1025 |
.collect(); |
| 1026 |
assert_eq!(b.partition_point(|_, &x| x < 0), 0); |
| 1027 |
assert_eq!(b.partition_point(|_, &x| x < 1), 0); |
| 1028 |
assert_eq!(b.partition_point(|_, &x| x < 2), 1); |
| 1029 |
assert_eq!(b.partition_point(|_, &x| x < 3), 1); |
| 1030 |
assert_eq!(b.partition_point(|_, &x| x < 4), 4); |
| 1031 |
assert_eq!(b.partition_point(|_, &x| x < 5), 4); |
| 1032 |
assert_eq!(b.partition_point(|_, &x| x < 6), 4); |
| 1033 |
assert_eq!(b.partition_point(|_, &x| x < 7), 4); |
| 1034 |
assert_eq!(b.partition_point(|_, &x| x < 8), 5); |
| 1035 |
} |
| 1036 |
|
| 1037 |
macro_rules! move_index_oob { |
| 1038 |
($test:ident, $from:expr, $to:expr) => { |
| 1039 |
#[test] |
| 1040 |
#[should_panic(expected = "index out of bounds")] |
| 1041 |
fn $test() { |
| 1042 |
let mut map: IndexMap<i32, ()> = (0..10).map(|k| (k, ())).collect(); |
| 1043 |
map.move_index($from, $to); |
| 1044 |
} |
| 1045 |
}; |
| 1046 |
} |
| 1047 |
move_index_oob!(test_move_index_out_of_bounds_0_10, 0, 10); |
| 1048 |
move_index_oob!(test_move_index_out_of_bounds_0_max, 0, usize::MAX); |
| 1049 |
move_index_oob!(test_move_index_out_of_bounds_10_0, 10, 0); |
| 1050 |
move_index_oob!(test_move_index_out_of_bounds_max_0, usize::MAX, 0); |
| 1051 |
|
| 1052 |
#[test] |
| 1053 |
fn disjoint_mut_empty_map() { |
| 1054 |
let mut map: IndexMap<u32, u32> = IndexMap::default(); |
| 1055 |
assert_eq!( |
| 1056 |
map.get_disjoint_mut([&0, &1, &2, &3]), |
| 1057 |
[None, None, None, None] |
| 1058 |
); |
| 1059 |
} |
| 1060 |
|
| 1061 |
#[test] |
| 1062 |
fn disjoint_mut_empty_param() { |
| 1063 |
let mut map: IndexMap<u32, u32> = IndexMap::default(); |
| 1064 |
map.insert(1, 10); |
| 1065 |
assert_eq!(map.get_disjoint_mut([] as [&u32; 0]), []); |
| 1066 |
} |
| 1067 |
|
| 1068 |
#[test] |
| 1069 |
fn disjoint_mut_single_fail() { |
| 1070 |
let mut map: IndexMap<u32, u32> = IndexMap::default(); |
| 1071 |
map.insert(1, 10); |
| 1072 |
assert_eq!(map.get_disjoint_mut([&0]), [None]); |
| 1073 |
} |
| 1074 |
|
| 1075 |
#[test] |
| 1076 |
fn disjoint_mut_single_success() { |
| 1077 |
let mut map: IndexMap<u32, u32> = IndexMap::default(); |
| 1078 |
map.insert(1, 10); |
| 1079 |
assert_eq!(map.get_disjoint_mut([&1]), [Some(&mut 10)]); |
| 1080 |
} |
| 1081 |
|
| 1082 |
#[test] |
| 1083 |
fn disjoint_mut_multi_success() { |
| 1084 |
let mut map: IndexMap<u32, u32> = IndexMap::default(); |
| 1085 |
map.insert(1, 100); |
| 1086 |
map.insert(2, 200); |
| 1087 |
map.insert(3, 300); |
| 1088 |
map.insert(4, 400); |
| 1089 |
assert_eq!( |
| 1090 |
map.get_disjoint_mut([&1, &2]), |
| 1091 |
[Some(&mut 100), Some(&mut 200)] |
| 1092 |
); |
| 1093 |
assert_eq!( |
| 1094 |
map.get_disjoint_mut([&1, &3]), |
| 1095 |
[Some(&mut 100), Some(&mut 300)] |
| 1096 |
); |
| 1097 |
assert_eq!( |
| 1098 |
map.get_disjoint_mut([&3, &1, &4, &2]), |
| 1099 |
[ |
| 1100 |
Some(&mut 300), |
| 1101 |
Some(&mut 100), |
| 1102 |
Some(&mut 400), |
| 1103 |
Some(&mut 200) |
| 1104 |
] |
| 1105 |
); |
| 1106 |
} |
| 1107 |
|
| 1108 |
#[test] |
| 1109 |
fn disjoint_mut_multi_success_unsized_key() { |
| 1110 |
let mut map: IndexMap<&'static str, u32> = IndexMap::default(); |
| 1111 |
map.insert("1", 100); |
| 1112 |
map.insert("2", 200); |
| 1113 |
map.insert("3", 300); |
| 1114 |
map.insert("4", 400); |
| 1115 |
|
| 1116 |
assert_eq!( |
| 1117 |
map.get_disjoint_mut(["1", "2"]), |
| 1118 |
[Some(&mut 100), Some(&mut 200)] |
| 1119 |
); |
| 1120 |
assert_eq!( |
| 1121 |
map.get_disjoint_mut(["1", "3"]), |
| 1122 |
[Some(&mut 100), Some(&mut 300)] |
| 1123 |
); |
| 1124 |
assert_eq!( |
| 1125 |
map.get_disjoint_mut(["3", "1", "4", "2"]), |
| 1126 |
[ |
| 1127 |
Some(&mut 300), |
| 1128 |
Some(&mut 100), |
| 1129 |
Some(&mut 400), |
| 1130 |
Some(&mut 200) |
| 1131 |
] |
| 1132 |
); |
| 1133 |
} |
| 1134 |
|
| 1135 |
#[test] |
| 1136 |
fn disjoint_mut_multi_success_borrow_key() { |
| 1137 |
let mut map: IndexMap<String, u32> = IndexMap::default(); |
| 1138 |
map.insert("1".into(), 100); |
| 1139 |
map.insert("2".into(), 200); |
| 1140 |
map.insert("3".into(), 300); |
| 1141 |
map.insert("4".into(), 400); |
| 1142 |
|
| 1143 |
assert_eq!( |
| 1144 |
map.get_disjoint_mut(["1", "2"]), |
| 1145 |
[Some(&mut 100), Some(&mut 200)] |
| 1146 |
); |
| 1147 |
assert_eq!( |
| 1148 |
map.get_disjoint_mut(["1", "3"]), |
| 1149 |
[Some(&mut 100), Some(&mut 300)] |
| 1150 |
); |
| 1151 |
assert_eq!( |
| 1152 |
map.get_disjoint_mut(["3", "1", "4", "2"]), |
| 1153 |
[ |
| 1154 |
Some(&mut 300), |
| 1155 |
Some(&mut 100), |
| 1156 |
Some(&mut 400), |
| 1157 |
Some(&mut 200) |
| 1158 |
] |
| 1159 |
); |
| 1160 |
} |
| 1161 |
|
| 1162 |
#[test] |
| 1163 |
fn disjoint_mut_multi_fail_missing() { |
| 1164 |
let mut map: IndexMap<u32, u32> = IndexMap::default(); |
| 1165 |
map.insert(1, 100); |
| 1166 |
map.insert(2, 200); |
| 1167 |
map.insert(3, 300); |
| 1168 |
map.insert(4, 400); |
| 1169 |
|
| 1170 |
assert_eq!(map.get_disjoint_mut([&1, &5]), [Some(&mut 100), None]); |
| 1171 |
assert_eq!(map.get_disjoint_mut([&5, &6]), [None, None]); |
| 1172 |
assert_eq!( |
| 1173 |
map.get_disjoint_mut([&1, &5, &4]), |
| 1174 |
[Some(&mut 100), None, Some(&mut 400)] |
| 1175 |
); |
| 1176 |
} |
| 1177 |
|
| 1178 |
#[test] |
| 1179 |
#[should_panic] |
| 1180 |
fn disjoint_mut_multi_fail_duplicate_panic() { |
| 1181 |
let mut map: IndexMap<u32, u32> = IndexMap::default(); |
| 1182 |
map.insert(1, 100); |
| 1183 |
map.get_disjoint_mut([&1, &2, &1]); |
| 1184 |
} |
| 1185 |
|
| 1186 |
#[test] |
| 1187 |
fn disjoint_indices_mut_fail_oob() { |
| 1188 |
let mut map: IndexMap<u32, u32> = IndexMap::default(); |
| 1189 |
map.insert(1, 10); |
| 1190 |
map.insert(321, 20); |
| 1191 |
assert_eq!( |
| 1192 |
map.get_disjoint_indices_mut([1, 3]), |
| 1193 |
Err(crate::GetDisjointMutError::IndexOutOfBounds) |
| 1194 |
); |
| 1195 |
} |
| 1196 |
|
| 1197 |
#[test] |
| 1198 |
fn disjoint_indices_mut_empty() { |
| 1199 |
let mut map: IndexMap<u32, u32> = IndexMap::default(); |
| 1200 |
map.insert(1, 10); |
| 1201 |
map.insert(321, 20); |
| 1202 |
assert_eq!(map.get_disjoint_indices_mut([]), Ok([])); |
| 1203 |
} |
| 1204 |
|
| 1205 |
#[test] |
| 1206 |
fn disjoint_indices_mut_success() { |
| 1207 |
let mut map: IndexMap<u32, u32> = IndexMap::default(); |
| 1208 |
map.insert(1, 10); |
| 1209 |
map.insert(321, 20); |
| 1210 |
assert_eq!(map.get_disjoint_indices_mut([0]), Ok([(&1, &mut 10)])); |
| 1211 |
|
| 1212 |
assert_eq!(map.get_disjoint_indices_mut([1]), Ok([(&321, &mut 20)])); |
| 1213 |
assert_eq!( |
| 1214 |
map.get_disjoint_indices_mut([0, 1]), |
| 1215 |
Ok([(&1, &mut 10), (&321, &mut 20)]) |
| 1216 |
); |
| 1217 |
} |
| 1218 |
|
| 1219 |
#[test] |
| 1220 |
fn disjoint_indices_mut_fail_duplicate() { |
| 1221 |
let mut map: IndexMap<u32, u32> = IndexMap::default(); |
| 1222 |
map.insert(1, 10); |
| 1223 |
map.insert(321, 20); |
| 1224 |
assert_eq!( |
| 1225 |
map.get_disjoint_indices_mut([1, 0, 1]), |
| 1226 |
Err(crate::GetDisjointMutError::OverlappingIndices) |
| 1227 |
); |
| 1228 |
} |
| 1229 |
|
| 1230 |
#[test] |
| 1231 |
fn insert_sorted_by_key() { |
| 1232 |
let mut values = [(-1, 8), (3, 18), (-27, 2), (-2, 5)]; |
| 1233 |
let mut map: IndexMap<i32, i32> = IndexMap::new(); |
| 1234 |
for (key, value) in values { |
| 1235 |
let (_, old) = map.insert_sorted_by_key(key, value, |k, _| k.abs()); |
| 1236 |
assert_eq!(old, None); |
| 1237 |
} |
| 1238 |
values.sort_by_key(|(key, _)| key.abs()); |
| 1239 |
assert_eq!(values, *map.as_slice()); |
| 1240 |
|
| 1241 |
for (key, value) in &mut values { |
| 1242 |
let (_, old) = map.insert_sorted_by_key(*key, -*value, |k, _| k.abs()); |
| 1243 |
assert_eq!(old, Some(*value)); |
| 1244 |
*value = -*value; |
| 1245 |
} |
| 1246 |
assert_eq!(values, *map.as_slice()); |
| 1247 |
} |
| 1248 |
|
| 1249 |
#[test] |
| 1250 |
fn insert_sorted_by() { |
| 1251 |
let mut values = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5)]; |
| 1252 |
let mut map: IndexMap<i32, i32> = IndexMap::new(); |
| 1253 |
for (key, value) in values { |
| 1254 |
let (_, old) = map.insert_sorted_by(key, value, |key1, _, key2, _| key2.cmp(key1)); |
| 1255 |
assert_eq!(old, None); |
| 1256 |
} |
| 1257 |
values.reverse(); |
| 1258 |
assert_eq!(values, *map.as_slice()); |
| 1259 |
|
| 1260 |
for (key, value) in &mut values { |
| 1261 |
let (_, old) = map.insert_sorted_by(*key, -*value, |key1, _, key2, _| key2.cmp(key1)); |
| 1262 |
assert_eq!(old, Some(*value)); |
| 1263 |
*value = -*value; |
| 1264 |
} |
| 1265 |
assert_eq!(values, *map.as_slice()); |
| 1266 |
} |
| 1267 |
|
| 1268 |
#[test] |
| 1269 |
fn is_sorted() { |
| 1270 |
fn expect(map: &IndexMap<i32, i32>, e: [bool; 7]) { |
| 1271 |
assert_eq!(e[0], map.is_sorted()); |
| 1272 |
assert_eq!(e[1], map.is_sorted_by(|k1, _, k2, _| k1 < k2)); |
| 1273 |
assert_eq!(e[2], map.is_sorted_by(|k1, _, k2, _| k1 > k2)); |
| 1274 |
assert_eq!(e[3], map.is_sorted_by(|_, v1, _, v2| v1 < v2)); |
| 1275 |
assert_eq!(e[4], map.is_sorted_by(|_, v1, _, v2| v1 > v2)); |
| 1276 |
assert_eq!(e[5], map.is_sorted_by_key(|k, _| k)); |
| 1277 |
assert_eq!(e[6], map.is_sorted_by_key(|_, v| v)); |
| 1278 |
} |
| 1279 |
|
| 1280 |
let mut map = IndexMap::from_iter((0..10).map(|i| (i, i * i))); |
| 1281 |
expect(&map, [true, true, false, true, false, true, true]); |
| 1282 |
|
| 1283 |
map[5] = -1; |
| 1284 |
expect(&map, [true, true, false, false, false, true, false]); |
| 1285 |
|
| 1286 |
map[5] = 25; |
| 1287 |
map.replace_index(5, -1).unwrap(); |
| 1288 |
expect(&map, [false, false, false, true, false, false, true]); |
| 1289 |
} |
| 1290 |
|
| 1291 |
#[test] |
| 1292 |
fn is_sorted_trivial() { |
| 1293 |
fn expect(map: &IndexMap<i32, i32>, e: [bool; 5]) { |
| 1294 |
assert_eq!(e[0], map.is_sorted()); |
| 1295 |
assert_eq!(e[1], map.is_sorted_by(|_, _, _, _| true)); |
| 1296 |
assert_eq!(e[2], map.is_sorted_by(|_, _, _, _| false)); |
| 1297 |
assert_eq!(e[3], map.is_sorted_by_key(|_, _| 0f64)); |
| 1298 |
assert_eq!(e[4], map.is_sorted_by_key(|_, _| f64::NAN)); |
| 1299 |
} |
| 1300 |
|
| 1301 |
let mut map = IndexMap::new(); |
| 1302 |
expect(&map, [true, true, true, true, true]); |
| 1303 |
|
| 1304 |
map.insert(0, 0); |
| 1305 |
expect(&map, [true, true, true, true, true]); |
| 1306 |
|
| 1307 |
map.insert(1, 1); |
| 1308 |
expect(&map, [true, true, false, true, false]); |
| 1309 |
|
| 1310 |
map.reverse(); |
| 1311 |
expect(&map, [false, true, false, true, false]); |
| 1312 |
} |