| 1 | use super::*; |
| 2 | use std::string::String; |
| 3 | use std::vec::Vec; |
| 4 | |
| 5 | #[test] |
| 6 | fn it_works() { |
| 7 | let mut map = OrderMap::new(); |
| 8 | assert_eq!(map.is_empty(), true); |
| 9 | map.insert(1, ()); |
| 10 | map.insert(1, ()); |
| 11 | assert_eq!(map.len(), 1); |
| 12 | assert!(map.get(&1).is_some()); |
| 13 | assert_eq!(map.is_empty(), false); |
| 14 | } |
| 15 | |
| 16 | #[test] |
| 17 | fn new() { |
| 18 | let map = OrderMap::<String, String>::new(); |
| 19 | println!("{:?}", map); |
| 20 | assert_eq!(map.capacity(), 0); |
| 21 | assert_eq!(map.len(), 0); |
| 22 | assert_eq!(map.is_empty(), true); |
| 23 | } |
| 24 | |
| 25 | #[test] |
| 26 | fn insert() { |
| 27 | let insert = [0, 4, 2, 12, 8, 7, 11, 5]; |
| 28 | let not_present = [1, 3, 6, 9, 10]; |
| 29 | let mut map = OrderMap::with_capacity(insert.len()); |
| 30 | |
| 31 | for (i, &elt) in insert.iter().enumerate() { |
| 32 | assert_eq!(map.len(), i); |
| 33 | map.insert(elt, elt); |
| 34 | assert_eq!(map.len(), i + 1); |
| 35 | assert_eq!(map.get(&elt), Some(&elt)); |
| 36 | assert_eq!(map[&elt], elt); |
| 37 | } |
| 38 | println!("{:?}", map); |
| 39 | |
| 40 | for &elt in ¬_present { |
| 41 | assert!(map.get(&elt).is_none()); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | #[test] |
| 46 | fn insert_full() { |
| 47 | let insert = vec![9, 2, 7, 1, 4, 6, 13]; |
| 48 | let present = vec![1, 6, 2]; |
| 49 | let mut map = OrderMap::with_capacity(insert.len()); |
| 50 | |
| 51 | for (i, &elt) in insert.iter().enumerate() { |
| 52 | assert_eq!(map.len(), i); |
| 53 | let (index, existing) = map.insert_full(elt, elt); |
| 54 | assert_eq!(existing, None); |
| 55 | assert_eq!(Some(index), map.get_full(&elt).map(|x| x.0)); |
| 56 | assert_eq!(map.len(), i + 1); |
| 57 | } |
| 58 | |
| 59 | let len = map.len(); |
| 60 | for &elt in &present { |
| 61 | let (index, existing) = map.insert_full(elt, elt); |
| 62 | assert_eq!(existing, Some(elt)); |
| 63 | assert_eq!(Some(index), map.get_full(&elt).map(|x| x.0)); |
| 64 | assert_eq!(map.len(), len); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | #[test] |
| 69 | fn insert_2() { |
| 70 | let mut map = OrderMap::with_capacity(16); |
| 71 | |
| 72 | let mut keys = vec![]; |
| 73 | keys.extend(0..16); |
| 74 | keys.extend(if cfg!(miri) { 32..64 } else { 128..267 }); |
| 75 | |
| 76 | for &i in &keys { |
| 77 | let old_map = map.clone(); |
| 78 | map.insert(i, ()); |
| 79 | for key in old_map.keys() { |
| 80 | if map.get(key).is_none() { |
| 81 | println!("old_map: {:?}", old_map); |
| 82 | println!("map: {:?}", map); |
| 83 | panic!("did not find {} in map", key); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | for &i in &keys { |
| 89 | assert!(map.get(&i).is_some(), "did not find {}", i); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | #[test] |
| 94 | fn insert_order() { |
| 95 | let insert = [0, 4, 2, 12, 8, 7, 11, 5, 3, 17, 19, 22, 23]; |
| 96 | let mut map = OrderMap::new(); |
| 97 | |
| 98 | for &elt in &insert { |
| 99 | map.insert(elt, ()); |
| 100 | } |
| 101 | |
| 102 | assert_eq!(map.keys().count(), map.len()); |
| 103 | assert_eq!(map.keys().count(), insert.len()); |
| 104 | for (a, b) in insert.iter().zip(map.keys()) { |
| 105 | assert_eq!(a, b); |
| 106 | } |
| 107 | for (i, k) in (0..insert.len()).zip(map.keys()) { |
| 108 | assert_eq!(map.get_index(i).unwrap().0, k); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | #[test] |
| 113 | fn shift_insert() { |
| 114 | let insert = [0, 4, 2, 12, 8, 7, 11, 5, 3, 17, 19, 22, 23]; |
| 115 | let mut map = OrderMap::new(); |
| 116 | |
| 117 | for &elt in &insert { |
| 118 | map.shift_insert(0, elt, ()); |
| 119 | } |
| 120 | |
| 121 | assert_eq!(map.keys().count(), map.len()); |
| 122 | assert_eq!(map.keys().count(), insert.len()); |
| 123 | for (a, b) in insert.iter().rev().zip(map.keys()) { |
| 124 | assert_eq!(a, b); |
| 125 | } |
| 126 | for (i, k) in (0..insert.len()).zip(map.keys()) { |
| 127 | assert_eq!(map.get_index(i).unwrap().0, k); |
| 128 | } |
| 129 | |
| 130 | // "insert" that moves an existing entry |
| 131 | map.shift_insert(0, insert[0], ()); |
| 132 | assert_eq!(map.keys().count(), insert.len()); |
| 133 | assert_eq!(insert[0], map.keys()[0]); |
| 134 | for (a, b) in insert[1..].iter().rev().zip(map.keys().skip(1)) { |
| 135 | assert_eq!(a, b); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | #[test] |
| 140 | fn insert_sorted_bad() { |
| 141 | let mut map = OrderMap::new(); |
| 142 | map.insert(10, ()); |
| 143 | for i in 0..10 { |
| 144 | map.insert(i, ()); |
| 145 | } |
| 146 | |
| 147 | // The binary search will want to insert this at the end (index == len()), |
| 148 | // but that's only possible for *new* inserts. It should still be handled |
| 149 | // without panicking though, and in this case it's simple enough that we |
| 150 | // know the exact result. (But don't read this as an API guarantee!) |
| 151 | assert_eq!(map.first(), Some((&10, &()))); |
| 152 | map.insert_sorted(10, ()); |
| 153 | assert_eq!(map.last(), Some((&10, &()))); |
| 154 | assert!(map.keys().copied().eq(0..=10)); |
| 155 | |
| 156 | // Other out-of-order entries can also "insert" to a binary-searched |
| 157 | // position, moving in either direction. |
| 158 | map.move_index(5, 0); |
| 159 | map.move_index(6, 10); |
| 160 | assert_eq!(map.first(), Some((&5, &()))); |
| 161 | assert_eq!(map.last(), Some((&6, &()))); |
| 162 | map.insert_sorted(5, ()); // moves back up |
| 163 | map.insert_sorted(6, ()); // moves back down |
| 164 | assert!(map.keys().copied().eq(0..=10)); |
| 165 | } |
| 166 | |
| 167 | #[test] |
| 168 | fn grow() { |
| 169 | let insert = [0, 4, 2, 12, 8, 7, 11]; |
| 170 | let not_present = [1, 3, 6, 9, 10]; |
| 171 | let mut map = OrderMap::with_capacity(insert.len()); |
| 172 | |
| 173 | for (i, &elt) in insert.iter().enumerate() { |
| 174 | assert_eq!(map.len(), i); |
| 175 | map.insert(elt, elt); |
| 176 | assert_eq!(map.len(), i + 1); |
| 177 | assert_eq!(map.get(&elt), Some(&elt)); |
| 178 | assert_eq!(map[&elt], elt); |
| 179 | } |
| 180 | |
| 181 | println!("{:?}", map); |
| 182 | for &elt in &insert { |
| 183 | map.insert(elt * 10, elt); |
| 184 | } |
| 185 | for &elt in &insert { |
| 186 | map.insert(elt * 100, elt); |
| 187 | } |
| 188 | for (i, &elt) in insert.iter().cycle().enumerate().take(100) { |
| 189 | map.insert(elt * 100 + i as i32, elt); |
| 190 | } |
| 191 | println!("{:?}", map); |
| 192 | for &elt in ¬_present { |
| 193 | assert!(map.get(&elt).is_none()); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | #[test] |
| 198 | fn reserve() { |
| 199 | let mut map = OrderMap::<usize, usize>::new(); |
| 200 | assert_eq!(map.capacity(), 0); |
| 201 | map.reserve(100); |
| 202 | let capacity = map.capacity(); |
| 203 | assert!(capacity >= 100); |
| 204 | for i in 0..capacity { |
| 205 | assert_eq!(map.len(), i); |
| 206 | map.insert(i, i * i); |
| 207 | assert_eq!(map.len(), i + 1); |
| 208 | assert_eq!(map.capacity(), capacity); |
| 209 | assert_eq!(map.get(&i), Some(&(i * i))); |
| 210 | } |
| 211 | map.insert(capacity, std::usize::MAX); |
| 212 | assert_eq!(map.len(), capacity + 1); |
| 213 | assert!(map.capacity() > capacity); |
| 214 | assert_eq!(map.get(&capacity), Some(&std::usize::MAX)); |
| 215 | } |
| 216 | |
| 217 | #[test] |
| 218 | fn try_reserve() { |
| 219 | let mut map = OrderMap::<usize, usize>::new(); |
| 220 | assert_eq!(map.capacity(), 0); |
| 221 | assert_eq!(map.try_reserve(100), Ok(())); |
| 222 | assert!(map.capacity() >= 100); |
| 223 | assert!(map.try_reserve(usize::MAX).is_err()); |
| 224 | } |
| 225 | |
| 226 | #[test] |
| 227 | fn shrink_to_fit() { |
| 228 | let mut map = OrderMap::<usize, usize>::new(); |
| 229 | assert_eq!(map.capacity(), 0); |
| 230 | for i in 0..100 { |
| 231 | assert_eq!(map.len(), i); |
| 232 | map.insert(i, i * i); |
| 233 | assert_eq!(map.len(), i + 1); |
| 234 | assert!(map.capacity() >= i + 1); |
| 235 | assert_eq!(map.get(&i), Some(&(i * i))); |
| 236 | map.shrink_to_fit(); |
| 237 | assert_eq!(map.len(), i + 1); |
| 238 | assert_eq!(map.capacity(), i + 1); |
| 239 | assert_eq!(map.get(&i), Some(&(i * i))); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | #[test] |
| 244 | fn remove() { |
| 245 | let insert = [0, 4, 2, 12, 8, 7, 11, 5, 3, 17, 19, 22, 23]; |
| 246 | let mut map = OrderMap::new(); |
| 247 | |
| 248 | for &elt in &insert { |
| 249 | map.insert(elt, elt); |
| 250 | } |
| 251 | |
| 252 | assert_eq!(map.keys().count(), map.len()); |
| 253 | assert_eq!(map.keys().count(), insert.len()); |
| 254 | for (a, b) in insert.iter().zip(map.keys()) { |
| 255 | assert_eq!(a, b); |
| 256 | } |
| 257 | |
| 258 | let remove_fail = [99, 77]; |
| 259 | let remove = [4, 12, 8, 7]; |
| 260 | |
| 261 | for &key in &remove_fail { |
| 262 | assert!(map.swap_remove_full(&key).is_none()); |
| 263 | } |
| 264 | println!("{:?}", map); |
| 265 | for &key in &remove { |
| 266 | //println!("{:?}", map); |
| 267 | let index = map.get_full(&key).unwrap().0; |
| 268 | assert_eq!(map.swap_remove_full(&key), Some((index, key, key))); |
| 269 | } |
| 270 | println!("{:?}", map); |
| 271 | |
| 272 | for key in &insert { |
| 273 | assert_eq!(map.get(key).is_some(), !remove.contains(key)); |
| 274 | } |
| 275 | assert_eq!(map.len(), insert.len() - remove.len()); |
| 276 | assert_eq!(map.keys().count(), insert.len() - remove.len()); |
| 277 | } |
| 278 | |
| 279 | #[test] |
| 280 | fn remove_to_empty() { |
| 281 | let mut map = ordermap! { 0 => 0, 4 => 4, 5 => 5 }; |
| 282 | map.swap_remove(&5).unwrap(); |
| 283 | map.swap_remove(&4).unwrap(); |
| 284 | map.swap_remove(&0).unwrap(); |
| 285 | assert!(map.is_empty()); |
| 286 | } |
| 287 | |
| 288 | #[test] |
| 289 | fn swap_remove_index() { |
| 290 | let insert = [0, 4, 2, 12, 8, 7, 11, 5, 3, 17, 19, 22, 23]; |
| 291 | let mut map = OrderMap::new(); |
| 292 | |
| 293 | for &elt in &insert { |
| 294 | map.insert(elt, elt * 2); |
| 295 | } |
| 296 | |
| 297 | let mut vector = insert.to_vec(); |
| 298 | let remove_sequence = &[3, 3, 10, 4, 5, 4, 3, 0, 1]; |
| 299 | |
| 300 | // check that the same swap remove sequence on vec and map |
| 301 | // have the same result. |
| 302 | for &rm in remove_sequence { |
| 303 | let out_vec = vector.swap_remove(rm); |
| 304 | let (out_map, _) = map.swap_remove_index(rm).unwrap(); |
| 305 | assert_eq!(out_vec, out_map); |
| 306 | } |
| 307 | assert_eq!(vector.len(), map.len()); |
| 308 | for (a, b) in vector.iter().zip(map.keys()) { |
| 309 | assert_eq!(a, b); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | #[test] |
| 314 | fn partial_eq_and_eq() { |
| 315 | let mut map_a = OrderMap::new(); |
| 316 | map_a.insert(1, "1"); |
| 317 | map_a.insert(2, "2"); |
| 318 | let mut map_b = map_a.clone(); |
| 319 | assert_eq!(map_a, map_b); |
| 320 | map_b.swap_remove(&1); |
| 321 | assert_ne!(map_a, map_b); |
| 322 | } |
| 323 | |
| 324 | #[test] |
| 325 | fn extend() { |
| 326 | let mut map = OrderMap::new(); |
| 327 | map.extend(vec![(&1, &2), (&3, &4)]); |
| 328 | map.extend(vec![(5, 6)]); |
| 329 | assert_eq!( |
| 330 | map.into_iter().collect::<Vec<_>>(), |
| 331 | vec![(1, 2), (3, 4), (5, 6)] |
| 332 | ); |
| 333 | } |
| 334 | |
| 335 | #[test] |
| 336 | fn entry() { |
| 337 | let mut map = OrderMap::new(); |
| 338 | |
| 339 | map.insert(1, "1"); |
| 340 | map.insert(2, "2"); |
| 341 | { |
| 342 | let e = map.entry(3); |
| 343 | assert_eq!(e.index(), 2); |
| 344 | let e = e.or_insert("3"); |
| 345 | assert_eq!(e, &"3"); |
| 346 | } |
| 347 | |
| 348 | let e = map.entry(2); |
| 349 | assert_eq!(e.index(), 1); |
| 350 | assert_eq!(e.key(), &2); |
| 351 | match e { |
| 352 | Entry::Occupied(ref e) => assert_eq!(e.get(), &"2"), |
| 353 | Entry::Vacant(_) => panic!(), |
| 354 | } |
| 355 | assert_eq!(e.or_insert("4"), &"2"); |
| 356 | } |
| 357 | |
| 358 | #[test] |
| 359 | fn entry_and_modify() { |
| 360 | let mut map = OrderMap::new(); |
| 361 | |
| 362 | map.insert(1, "1"); |
| 363 | map.entry(1).and_modify(|x| *x = "2"); |
| 364 | assert_eq!(Some(&"2"), map.get(&1)); |
| 365 | |
| 366 | map.entry(2).and_modify(|x| *x = "doesn't exist"); |
| 367 | assert_eq!(None, map.get(&2)); |
| 368 | } |
| 369 | |
| 370 | #[test] |
| 371 | fn entry_or_default() { |
| 372 | let mut map = OrderMap::new(); |
| 373 | |
| 374 | #[derive(Debug, PartialEq)] |
| 375 | enum TestEnum { |
| 376 | DefaultValue, |
| 377 | NonDefaultValue, |
| 378 | } |
| 379 | |
| 380 | impl Default for TestEnum { |
| 381 | fn default() -> Self { |
| 382 | TestEnum::DefaultValue |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | map.insert(1, TestEnum::NonDefaultValue); |
| 387 | assert_eq!(&mut TestEnum::NonDefaultValue, map.entry(1).or_default()); |
| 388 | |
| 389 | assert_eq!(&mut TestEnum::DefaultValue, map.entry(2).or_default()); |
| 390 | } |
| 391 | |
| 392 | #[test] |
| 393 | fn occupied_entry_key() { |
| 394 | // These keys match hash and equality, but their addresses are distinct. |
| 395 | let (k1, k2) = (&mut 1, &mut 1); |
| 396 | let k1_ptr = k1 as *const i32; |
| 397 | let k2_ptr = k2 as *const i32; |
| 398 | assert_ne!(k1_ptr, k2_ptr); |
| 399 | |
| 400 | let mut map = OrderMap::new(); |
| 401 | map.insert(k1, "value"); |
| 402 | match map.entry(k2) { |
| 403 | Entry::Occupied(ref e) => { |
| 404 | // `OccupiedEntry::key` should reference the key in the map, |
| 405 | // not the key that was used to find the entry. |
| 406 | let ptr = *e.key() as *const i32; |
| 407 | assert_eq!(ptr, k1_ptr); |
| 408 | assert_ne!(ptr, k2_ptr); |
| 409 | } |
| 410 | Entry::Vacant(_) => panic!(), |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | #[test] |
| 415 | fn get_index_entry() { |
| 416 | let mut map = OrderMap::new(); |
| 417 | |
| 418 | assert!(map.get_index_entry(0).is_none()); |
| 419 | assert!(map.first_entry().is_none()); |
| 420 | assert!(map.last_entry().is_none()); |
| 421 | |
| 422 | map.insert(0, "0"); |
| 423 | map.insert(1, "1"); |
| 424 | map.insert(2, "2"); |
| 425 | map.insert(3, "3"); |
| 426 | |
| 427 | assert!(map.get_index_entry(4).is_none()); |
| 428 | |
| 429 | { |
| 430 | let e = map.get_index_entry(1).unwrap(); |
| 431 | assert_eq!(*e.key(), 1); |
| 432 | assert_eq!(*e.get(), "1"); |
| 433 | assert_eq!(e.swap_remove(), "1"); |
| 434 | } |
| 435 | |
| 436 | { |
| 437 | let mut e = map.get_index_entry(1).unwrap(); |
| 438 | assert_eq!(*e.key(), 3); |
| 439 | assert_eq!(*e.get(), "3"); |
| 440 | assert_eq!(e.insert("4"), "3"); |
| 441 | } |
| 442 | |
| 443 | assert_eq!(*map.get(&3).unwrap(), "4"); |
| 444 | |
| 445 | { |
| 446 | let e = map.first_entry().unwrap(); |
| 447 | assert_eq!(*e.key(), 0); |
| 448 | assert_eq!(*e.get(), "0"); |
| 449 | } |
| 450 | |
| 451 | { |
| 452 | let e = map.last_entry().unwrap(); |
| 453 | assert_eq!(*e.key(), 2); |
| 454 | assert_eq!(*e.get(), "2"); |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | #[test] |
| 459 | fn from_entries() { |
| 460 | let mut map = OrderMap::from([(1, "1"), (2, "2"), (3, "3")]); |
| 461 | |
| 462 | { |
| 463 | let e = match map.entry(1) { |
| 464 | Entry::Occupied(e) => IndexedEntry::from(e), |
| 465 | Entry::Vacant(_) => panic!(), |
| 466 | }; |
| 467 | assert_eq!(e.index(), 0); |
| 468 | assert_eq!(*e.key(), 1); |
| 469 | assert_eq!(*e.get(), "1"); |
| 470 | } |
| 471 | |
| 472 | { |
| 473 | let e = match map.get_index_entry(1) { |
| 474 | Some(e) => OccupiedEntry::from(e), |
| 475 | None => panic!(), |
| 476 | }; |
| 477 | assert_eq!(e.index(), 1); |
| 478 | assert_eq!(*e.key(), 2); |
| 479 | assert_eq!(*e.get(), "2"); |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | #[test] |
| 484 | fn keys() { |
| 485 | let vec = vec![(1, 'a'), (2, 'b'), (3, 'c')]; |
| 486 | let map: OrderMap<_, _> = vec.into_iter().collect(); |
| 487 | let keys: Vec<_> = map.keys().copied().collect(); |
| 488 | assert_eq!(keys.len(), 3); |
| 489 | assert!(keys.contains(&1)); |
| 490 | assert!(keys.contains(&2)); |
| 491 | assert!(keys.contains(&3)); |
| 492 | } |
| 493 | |
| 494 | #[test] |
| 495 | fn into_keys() { |
| 496 | let vec = vec![(1, 'a'), (2, 'b'), (3, 'c')]; |
| 497 | let map: OrderMap<_, _> = vec.into_iter().collect(); |
| 498 | let keys: Vec<i32> = map.into_keys().collect(); |
| 499 | assert_eq!(keys.len(), 3); |
| 500 | assert!(keys.contains(&1)); |
| 501 | assert!(keys.contains(&2)); |
| 502 | assert!(keys.contains(&3)); |
| 503 | } |
| 504 | |
| 505 | #[test] |
| 506 | fn values() { |
| 507 | let vec = vec![(1, 'a'), (2, 'b'), (3, 'c')]; |
| 508 | let map: OrderMap<_, _> = vec.into_iter().collect(); |
| 509 | let values: Vec<_> = map.values().copied().collect(); |
| 510 | assert_eq!(values.len(), 3); |
| 511 | assert!(values.contains(&'a')); |
| 512 | assert!(values.contains(&'b')); |
| 513 | assert!(values.contains(&'c')); |
| 514 | } |
| 515 | |
| 516 | #[test] |
| 517 | fn values_mut() { |
| 518 | let vec = vec![(1, 1), (2, 2), (3, 3)]; |
| 519 | let mut map: OrderMap<_, _> = vec.into_iter().collect(); |
| 520 | for value in map.values_mut() { |
| 521 | *value *= 2 |
| 522 | } |
| 523 | let values: Vec<_> = map.values().copied().collect(); |
| 524 | assert_eq!(values.len(), 3); |
| 525 | assert!(values.contains(&2)); |
| 526 | assert!(values.contains(&4)); |
| 527 | assert!(values.contains(&6)); |
| 528 | } |
| 529 | |
| 530 | #[test] |
| 531 | fn into_values() { |
| 532 | let vec = vec![(1, 'a'), (2, 'b'), (3, 'c')]; |
| 533 | let map: OrderMap<_, _> = vec.into_iter().collect(); |
| 534 | let values: Vec<char> = map.into_values().collect(); |
| 535 | assert_eq!(values.len(), 3); |
| 536 | assert!(values.contains(&'a')); |
| 537 | assert!(values.contains(&'b')); |
| 538 | assert!(values.contains(&'c')); |
| 539 | } |
| 540 | |
| 541 | #[test] |
| 542 | fn drain_range() { |
| 543 | // Test the various heuristics of `erase_indices` |
| 544 | for range in [ |
| 545 | 0..0, // nothing erased |
| 546 | 10..90, // reinsert the few kept (..10 and 90..) |
| 547 | 80..90, // update the few to adjust (80..) |
| 548 | 20..30, // sweep everything |
| 549 | ] { |
| 550 | let mut vec = Vec::from_iter(0..100); |
| 551 | let mut map: OrderMap<i32, ()> = (0..100).map(|i| (i, ())).collect(); |
| 552 | drop(vec.drain(range.clone())); |
| 553 | drop(map.drain(range)); |
| 554 | assert!(vec.iter().eq(map.keys())); |
| 555 | for (i, x) in vec.iter().enumerate() { |
| 556 | assert_eq!(map.get_index_of(x), Some(i)); |
| 557 | } |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | #[test] |
| 562 | #[cfg(feature = "std")] |
| 563 | fn from_array() { |
| 564 | let map = OrderMap::from([(1, 2), (3, 4)]); |
| 565 | let mut expected = OrderMap::new(); |
| 566 | expected.insert(1, 2); |
| 567 | expected.insert(3, 4); |
| 568 | |
| 569 | assert_eq!(map, expected) |
| 570 | } |
| 571 | |
| 572 | #[test] |
| 573 | fn iter_default() { |
| 574 | struct K; |
| 575 | struct V; |
| 576 | fn assert_default<T>() |
| 577 | where |
| 578 | T: Default + Iterator, |
| 579 | { |
| 580 | assert!(T::default().next().is_none()); |
| 581 | } |
| 582 | assert_default::<Iter<'static, K, V>>(); |
| 583 | assert_default::<IterMut<'static, K, V>>(); |
| 584 | assert_default::<IterMut2<'static, K, V>>(); |
| 585 | assert_default::<IntoIter<K, V>>(); |
| 586 | assert_default::<Keys<'static, K, V>>(); |
| 587 | assert_default::<IntoKeys<K, V>>(); |
| 588 | assert_default::<Values<'static, K, V>>(); |
| 589 | assert_default::<ValuesMut<'static, K, V>>(); |
| 590 | assert_default::<IntoValues<K, V>>(); |
| 591 | } |
| 592 | |
| 593 | #[test] |
| 594 | fn get_index_mut2() { |
| 595 | let mut map: OrderMap<i32, i32> = OrderMap::new(); |
| 596 | map.insert(1, 2); |
| 597 | map.insert(3, 4); |
| 598 | map.insert(5, 6); |
| 599 | |
| 600 | { |
| 601 | let (key, value) = map.get_index_mut2(0).unwrap(); |
| 602 | assert_eq!(*key, 1); |
| 603 | assert_eq!(*value, 2); |
| 604 | |
| 605 | *value = 7; |
| 606 | } |
| 607 | assert_eq!(map[0], 7); |
| 608 | |
| 609 | { |
| 610 | let (key, _) = map.get_index_mut2(0).unwrap(); |
| 611 | *key = 8; |
| 612 | } |
| 613 | assert_eq!(map.get_index(0).unwrap().0, &8); |
| 614 | } |
| 615 | |
| 616 | #[test] |
| 617 | fn shift_shift_remove_index() { |
| 618 | let mut map: OrderMap<i32, i32> = OrderMap::new(); |
| 619 | map.insert(1, 2); |
| 620 | map.insert(3, 4); |
| 621 | map.insert(5, 6); |
| 622 | map.insert(7, 8); |
| 623 | map.insert(9, 10); |
| 624 | |
| 625 | let result = map.remove_index(1); |
| 626 | assert_eq!(result, Some((3, 4))); |
| 627 | assert_eq!(map.len(), 4); |
| 628 | assert_eq!(map.as_slice(), &[(1, 2), (5, 6), (7, 8), (9, 10)]); |
| 629 | |
| 630 | let result = map.remove_index(1); |
| 631 | assert_eq!(result, Some((5, 6))); |
| 632 | assert_eq!(map.len(), 3); |
| 633 | assert_eq!(map.as_slice(), &[(1, 2), (7, 8), (9, 10)]); |
| 634 | |
| 635 | let result = map.remove_index(2); |
| 636 | assert_eq!(result, Some((9, 10))); |
| 637 | assert_eq!(map.len(), 2); |
| 638 | assert_eq!(map.as_slice(), &[(1, 2), (7, 8)]); |
| 639 | |
| 640 | let result = map.remove_index(2); |
| 641 | assert_eq!(result, None); |
| 642 | assert_eq!(map.len(), 2); |
| 643 | assert_eq!(map.as_slice(), &[(1, 2), (7, 8)]); |
| 644 | } |
| 645 | |
| 646 | #[test] |
| 647 | fn shift_remove_entry() { |
| 648 | let mut map: OrderMap<i32, i32> = OrderMap::new(); |
| 649 | map.insert(1, 2); |
| 650 | map.insert(3, 4); |
| 651 | map.insert(5, 6); |
| 652 | map.insert(7, 8); |
| 653 | map.insert(9, 10); |
| 654 | |
| 655 | let result = map.remove_entry(&3); |
| 656 | assert_eq!(result, Some((3, 4))); |
| 657 | assert_eq!(map.len(), 4); |
| 658 | assert_eq!(map.as_slice(), &[(1, 2), (5, 6), (7, 8), (9, 10)]); |
| 659 | |
| 660 | let result = map.remove_entry(&9); |
| 661 | assert_eq!(result, Some((9, 10))); |
| 662 | assert_eq!(map.len(), 3); |
| 663 | assert_eq!(map.as_slice(), &[(1, 2), (5, 6), (7, 8)]); |
| 664 | |
| 665 | let result = map.remove_entry(&9); |
| 666 | assert_eq!(result, None); |
| 667 | assert_eq!(map.len(), 3); |
| 668 | assert_eq!(map.as_slice(), &[(1, 2), (5, 6), (7, 8)]); |
| 669 | } |
| 670 | |
| 671 | #[test] |
| 672 | fn shift_remove_full() { |
| 673 | let mut map: OrderMap<i32, i32> = OrderMap::new(); |
| 674 | map.insert(1, 2); |
| 675 | map.insert(3, 4); |
| 676 | map.insert(5, 6); |
| 677 | map.insert(7, 8); |
| 678 | map.insert(9, 10); |
| 679 | |
| 680 | let result = map.remove_full(&3); |
| 681 | assert_eq!(result, Some((1, 3, 4))); |
| 682 | assert_eq!(map.len(), 4); |
| 683 | assert_eq!(map.as_slice(), &[(1, 2), (5, 6), (7, 8), (9, 10)]); |
| 684 | |
| 685 | let result = map.remove_full(&9); |
| 686 | assert_eq!(result, Some((3, 9, 10))); |
| 687 | assert_eq!(map.len(), 3); |
| 688 | assert_eq!(map.as_slice(), &[(1, 2), (5, 6), (7, 8)]); |
| 689 | |
| 690 | let result = map.remove_full(&9); |
| 691 | assert_eq!(result, None); |
| 692 | assert_eq!(map.len(), 3); |
| 693 | assert_eq!(map.as_slice(), &[(1, 2), (5, 6), (7, 8)]); |
| 694 | } |
| 695 | |
| 696 | #[test] |
| 697 | fn sorted_unstable_by() { |
| 698 | let mut map: OrderMap<i32, i32> = OrderMap::new(); |
| 699 | map.extend(vec![(1, 10), (2, 20), (3, 30), (4, 40), (5, 50)]); |
| 700 | let sorted = map.sorted_unstable_by(|_a, b, _c, d| d.cmp(&b)); |
| 701 | |
| 702 | assert_eq!( |
| 703 | sorted.as_slice(), |
| 704 | &[(5, 50), (4, 40), (3, 30), (2, 20), (1, 10)] |
| 705 | ); |
| 706 | } |
| 707 | |
| 708 | #[test] |
| 709 | fn into_boxed_slice() { |
| 710 | let mut map: OrderMap<i32, i32> = OrderMap::new(); |
| 711 | for i in 0..5 { |
| 712 | map.insert(i, i * 10); |
| 713 | } |
| 714 | let boxed_slice: Box<Slice<i32, i32>> = map.into_boxed_slice(); |
| 715 | assert_eq!(boxed_slice.len(), 5); |
| 716 | assert_eq!( |
| 717 | boxed_slice.as_ref(), |
| 718 | &[(0, 0), (1, 10), (2, 20), (3, 30), (4, 40)] |
| 719 | ); |
| 720 | } |
| 721 | |
| 722 | #[test] |
| 723 | fn last_mut() { |
| 724 | let mut map: OrderMap<&str, i32> = OrderMap::new(); |
| 725 | |
| 726 | let last_entry = map.last_mut(); |
| 727 | assert_eq!(last_entry, None); |
| 728 | |
| 729 | map.insert("key1", 1); |
| 730 | map.insert("key2", 2); |
| 731 | map.insert("key3", 3); |
| 732 | let last_entry = map.last_mut(); |
| 733 | assert_eq!(last_entry, Some((&"key3", &mut 3))); |
| 734 | |
| 735 | *last_entry.unwrap().1 = 4; |
| 736 | assert_eq!(map.get("key3"), Some(&4)); |
| 737 | } |
| 738 | |
| 739 | #[test] |
| 740 | #[should_panic = "index out of bounds"] |
| 741 | fn insert_before_oob() { |
| 742 | let mut map: OrderMap<char, ()> = OrderMap::new(); |
| 743 | let _ = map.insert_before(0, 'a', ()); |
| 744 | let _ = map.insert_before(1, 'b', ()); |
| 745 | map.insert_before(3, 'd', ()); |
| 746 | } |
| 747 | |
| 748 | #[test] |
| 749 | fn clear() { |
| 750 | let mut map: OrderMap<i32, i32> = OrderMap::new(); |
| 751 | map.extend(vec![(1, 10), (2, 20), (3, 30), (4, 40), (5, 50)]); |
| 752 | map.clear(); |
| 753 | assert_eq!(map.len(), 0); |
| 754 | } |
| 755 | |
| 756 | #[test] |
| 757 | fn get_range() { |
| 758 | let mut index_map: OrderMap<i32, i32> = OrderMap::new(); |
| 759 | index_map.insert(1, 10); |
| 760 | index_map.insert(2, 20); |
| 761 | index_map.insert(3, 30); |
| 762 | index_map.insert(4, 40); |
| 763 | index_map.insert(5, 50); |
| 764 | |
| 765 | let result = index_map.get_range(2..2); |
| 766 | assert!(result.unwrap().is_empty()); |
| 767 | |
| 768 | let result = index_map.get_range(4..2); |
| 769 | assert!(result.is_none()); |
| 770 | |
| 771 | let result = index_map.get_range(2..4); |
| 772 | let slice: &Slice<i32, i32> = result.unwrap(); |
| 773 | assert_eq!(slice.len(), 2); |
| 774 | assert_eq!(slice, &[(3, 30), (4, 40)]); |
| 775 | } |
| 776 | |
| 777 | #[test] |
| 778 | fn get_range_mut() { |
| 779 | let mut index_map: OrderMap<i32, i32> = OrderMap::new(); |
| 780 | index_map.insert(1, 10); |
| 781 | index_map.insert(2, 20); |
| 782 | index_map.insert(3, 30); |
| 783 | index_map.insert(4, 40); |
| 784 | index_map.insert(5, 50); |
| 785 | |
| 786 | let result = index_map.get_range_mut(2..2); |
| 787 | assert!(result.unwrap().is_empty()); |
| 788 | |
| 789 | let result = index_map.get_range_mut(4..2); |
| 790 | assert!(result.is_none()); |
| 791 | |
| 792 | let result = index_map.get_range_mut(2..4); |
| 793 | let slice: &mut Slice<i32, i32> = result.unwrap(); |
| 794 | assert_eq!(slice.len(), 2); |
| 795 | assert_eq!(slice, &mut [(3, 30), (4, 40)]); |
| 796 | |
| 797 | for i in 0..slice.len() { |
| 798 | slice[i] += 1; |
| 799 | } |
| 800 | assert_eq!(slice, &mut [(3, 31), (4, 41)]); |
| 801 | } |
| 802 | |
| 803 | #[test] |
| 804 | #[should_panic = "index out of bounds"] |
| 805 | fn shift_insert_oob() { |
| 806 | let mut map: OrderMap<u32, u32> = OrderMap::new(); |
| 807 | map.shift_insert(0, 1, 10); |
| 808 | map.shift_insert(1, 2, 20); |
| 809 | map.shift_insert(2, 3, 30); |
| 810 | map.shift_insert(5, 4, 40); |
| 811 | } |
| 812 | |
| 813 | #[test] |
| 814 | fn test_binary_search_by() { |
| 815 | // adapted from std's test for binary_search |
| 816 | let b: OrderMap<_, i32> = [] |
| 817 | .into_iter() |
| 818 | .enumerate() |
| 819 | .map(|(i, x)| (i + 100, x)) |
| 820 | .collect(); |
| 821 | assert_eq!(b.binary_search_by(|_, x| x.cmp(&5)), Err(0)); |
| 822 | |
| 823 | let b: OrderMap<_, i32> = [4] |
| 824 | .into_iter() |
| 825 | .enumerate() |
| 826 | .map(|(i, x)| (i + 100, x)) |
| 827 | .collect(); |
| 828 | assert_eq!(b.binary_search_by(|_, x| x.cmp(&3)), Err(0)); |
| 829 | assert_eq!(b.binary_search_by(|_, x| x.cmp(&4)), Ok(0)); |
| 830 | assert_eq!(b.binary_search_by(|_, x| x.cmp(&5)), Err(1)); |
| 831 | |
| 832 | let b: OrderMap<_, i32> = [1, 2, 4, 6, 8, 9] |
| 833 | .into_iter() |
| 834 | .enumerate() |
| 835 | .map(|(i, x)| (i + 100, x)) |
| 836 | .collect(); |
| 837 | assert_eq!(b.binary_search_by(|_, x| x.cmp(&5)), Err(3)); |
| 838 | assert_eq!(b.binary_search_by(|_, x| x.cmp(&6)), Ok(3)); |
| 839 | assert_eq!(b.binary_search_by(|_, x| x.cmp(&7)), Err(4)); |
| 840 | assert_eq!(b.binary_search_by(|_, x| x.cmp(&8)), Ok(4)); |
| 841 | |
| 842 | let b: OrderMap<_, i32> = [1, 2, 4, 5, 6, 8] |
| 843 | .into_iter() |
| 844 | .enumerate() |
| 845 | .map(|(i, x)| (i + 100, x)) |
| 846 | .collect(); |
| 847 | assert_eq!(b.binary_search_by(|_, x| x.cmp(&9)), Err(6)); |
| 848 | |
| 849 | let b: OrderMap<_, i32> = [1, 2, 4, 6, 7, 8, 9] |
| 850 | .into_iter() |
| 851 | .enumerate() |
| 852 | .map(|(i, x)| (i + 100, x)) |
| 853 | .collect(); |
| 854 | assert_eq!(b.binary_search_by(|_, x| x.cmp(&6)), Ok(3)); |
| 855 | assert_eq!(b.binary_search_by(|_, x| x.cmp(&5)), Err(3)); |
| 856 | assert_eq!(b.binary_search_by(|_, x| x.cmp(&8)), Ok(5)); |
| 857 | |
| 858 | let b: OrderMap<_, i32> = [1, 2, 4, 5, 6, 8, 9] |
| 859 | .into_iter() |
| 860 | .enumerate() |
| 861 | .map(|(i, x)| (i + 100, x)) |
| 862 | .collect(); |
| 863 | assert_eq!(b.binary_search_by(|_, x| x.cmp(&7)), Err(5)); |
| 864 | assert_eq!(b.binary_search_by(|_, x| x.cmp(&0)), Err(0)); |
| 865 | |
| 866 | let b: OrderMap<_, i32> = [1, 3, 3, 3, 7] |
| 867 | .into_iter() |
| 868 | .enumerate() |
| 869 | .map(|(i, x)| (i + 100, x)) |
| 870 | .collect(); |
| 871 | assert_eq!(b.binary_search_by(|_, x| x.cmp(&0)), Err(0)); |
| 872 | assert_eq!(b.binary_search_by(|_, x| x.cmp(&1)), Ok(0)); |
| 873 | assert_eq!(b.binary_search_by(|_, x| x.cmp(&2)), Err(1)); |
| 874 | assert!(match b.binary_search_by(|_, x| x.cmp(&3)) { |
| 875 | Ok(1..=3) => true, |
| 876 | _ => false, |
| 877 | }); |
| 878 | assert!(match b.binary_search_by(|_, x| x.cmp(&3)) { |
| 879 | Ok(1..=3) => true, |
| 880 | _ => false, |
| 881 | }); |
| 882 | assert_eq!(b.binary_search_by(|_, x| x.cmp(&4)), Err(4)); |
| 883 | assert_eq!(b.binary_search_by(|_, x| x.cmp(&5)), Err(4)); |
| 884 | assert_eq!(b.binary_search_by(|_, x| x.cmp(&6)), Err(4)); |
| 885 | assert_eq!(b.binary_search_by(|_, x| x.cmp(&7)), Ok(4)); |
| 886 | assert_eq!(b.binary_search_by(|_, x| x.cmp(&8)), Err(5)); |
| 887 | } |
| 888 | |
| 889 | #[test] |
| 890 | fn test_binary_search_by_key() { |
| 891 | // adapted from std's test for binary_search |
| 892 | let b: OrderMap<_, i32> = [] |
| 893 | .into_iter() |
| 894 | .enumerate() |
| 895 | .map(|(i, x)| (i + 100, x)) |
| 896 | .collect(); |
| 897 | assert_eq!(b.binary_search_by_key(&5, |_, &x| x), Err(0)); |
| 898 | |
| 899 | let b: OrderMap<_, i32> = [4] |
| 900 | .into_iter() |
| 901 | .enumerate() |
| 902 | .map(|(i, x)| (i + 100, x)) |
| 903 | .collect(); |
| 904 | assert_eq!(b.binary_search_by_key(&3, |_, &x| x), Err(0)); |
| 905 | assert_eq!(b.binary_search_by_key(&4, |_, &x| x), Ok(0)); |
| 906 | assert_eq!(b.binary_search_by_key(&5, |_, &x| x), Err(1)); |
| 907 | |
| 908 | let b: OrderMap<_, i32> = [1, 2, 4, 6, 8, 9] |
| 909 | .into_iter() |
| 910 | .enumerate() |
| 911 | .map(|(i, x)| (i + 100, x)) |
| 912 | .collect(); |
| 913 | assert_eq!(b.binary_search_by_key(&5, |_, &x| x), Err(3)); |
| 914 | assert_eq!(b.binary_search_by_key(&6, |_, &x| x), Ok(3)); |
| 915 | assert_eq!(b.binary_search_by_key(&7, |_, &x| x), Err(4)); |
| 916 | assert_eq!(b.binary_search_by_key(&8, |_, &x| x), Ok(4)); |
| 917 | |
| 918 | let b: OrderMap<_, i32> = [1, 2, 4, 5, 6, 8] |
| 919 | .into_iter() |
| 920 | .enumerate() |
| 921 | .map(|(i, x)| (i + 100, x)) |
| 922 | .collect(); |
| 923 | assert_eq!(b.binary_search_by_key(&9, |_, &x| x), Err(6)); |
| 924 | |
| 925 | let b: OrderMap<_, i32> = [1, 2, 4, 6, 7, 8, 9] |
| 926 | .into_iter() |
| 927 | .enumerate() |
| 928 | .map(|(i, x)| (i + 100, x)) |
| 929 | .collect(); |
| 930 | assert_eq!(b.binary_search_by_key(&6, |_, &x| x), Ok(3)); |
| 931 | assert_eq!(b.binary_search_by_key(&5, |_, &x| x), Err(3)); |
| 932 | assert_eq!(b.binary_search_by_key(&8, |_, &x| x), Ok(5)); |
| 933 | |
| 934 | let b: OrderMap<_, i32> = [1, 2, 4, 5, 6, 8, 9] |
| 935 | .into_iter() |
| 936 | .enumerate() |
| 937 | .map(|(i, x)| (i + 100, x)) |
| 938 | .collect(); |
| 939 | assert_eq!(b.binary_search_by_key(&7, |_, &x| x), Err(5)); |
| 940 | assert_eq!(b.binary_search_by_key(&0, |_, &x| x), Err(0)); |
| 941 | |
| 942 | let b: OrderMap<_, i32> = [1, 3, 3, 3, 7] |
| 943 | .into_iter() |
| 944 | .enumerate() |
| 945 | .map(|(i, x)| (i + 100, x)) |
| 946 | .collect(); |
| 947 | assert_eq!(b.binary_search_by_key(&0, |_, &x| x), Err(0)); |
| 948 | assert_eq!(b.binary_search_by_key(&1, |_, &x| x), Ok(0)); |
| 949 | assert_eq!(b.binary_search_by_key(&2, |_, &x| x), Err(1)); |
| 950 | assert!(match b.binary_search_by_key(&3, |_, &x| x) { |
| 951 | Ok(1..=3) => true, |
| 952 | _ => false, |
| 953 | }); |
| 954 | assert!(match b.binary_search_by_key(&3, |_, &x| x) { |
| 955 | Ok(1..=3) => true, |
| 956 | _ => false, |
| 957 | }); |
| 958 | assert_eq!(b.binary_search_by_key(&4, |_, &x| x), Err(4)); |
| 959 | assert_eq!(b.binary_search_by_key(&5, |_, &x| x), Err(4)); |
| 960 | assert_eq!(b.binary_search_by_key(&6, |_, &x| x), Err(4)); |
| 961 | assert_eq!(b.binary_search_by_key(&7, |_, &x| x), Ok(4)); |
| 962 | assert_eq!(b.binary_search_by_key(&8, |_, &x| x), Err(5)); |
| 963 | } |
| 964 | |
| 965 | #[test] |
| 966 | fn test_partition_point() { |
| 967 | // adapted from std's test for partition_point |
| 968 | let b: OrderMap<_, i32> = [] |
| 969 | .into_iter() |
| 970 | .enumerate() |
| 971 | .map(|(i, x)| (i + 100, x)) |
| 972 | .collect(); |
| 973 | assert_eq!(b.partition_point(|_, &x| x < 5), 0); |
| 974 | |
| 975 | let b: OrderMap<_, i32> = [4] |
| 976 | .into_iter() |
| 977 | .enumerate() |
| 978 | .map(|(i, x)| (i + 100, x)) |
| 979 | .collect(); |
| 980 | assert_eq!(b.partition_point(|_, &x| x < 3), 0); |
| 981 | assert_eq!(b.partition_point(|_, &x| x < 4), 0); |
| 982 | assert_eq!(b.partition_point(|_, &x| x < 5), 1); |
| 983 | |
| 984 | let b: OrderMap<_, i32> = [1, 2, 4, 6, 8, 9] |
| 985 | .into_iter() |
| 986 | .enumerate() |
| 987 | .map(|(i, x)| (i + 100, x)) |
| 988 | .collect(); |
| 989 | assert_eq!(b.partition_point(|_, &x| x < 5), 3); |
| 990 | assert_eq!(b.partition_point(|_, &x| x < 6), 3); |
| 991 | assert_eq!(b.partition_point(|_, &x| x < 7), 4); |
| 992 | assert_eq!(b.partition_point(|_, &x| x < 8), 4); |
| 993 | |
| 994 | let b: OrderMap<_, i32> = [1, 2, 4, 5, 6, 8] |
| 995 | .into_iter() |
| 996 | .enumerate() |
| 997 | .map(|(i, x)| (i + 100, x)) |
| 998 | .collect(); |
| 999 | assert_eq!(b.partition_point(|_, &x| x < 9), 6); |
| 1000 | |
| 1001 | let b: OrderMap<_, i32> = [1, 2, 4, 6, 7, 8, 9] |
| 1002 | .into_iter() |
| 1003 | .enumerate() |
| 1004 | .map(|(i, x)| (i + 100, x)) |
| 1005 | .collect(); |
| 1006 | assert_eq!(b.partition_point(|_, &x| x < 6), 3); |
| 1007 | assert_eq!(b.partition_point(|_, &x| x < 5), 3); |
| 1008 | assert_eq!(b.partition_point(|_, &x| x < 8), 5); |
| 1009 | |
| 1010 | let b: OrderMap<_, i32> = [1, 2, 4, 5, 6, 8, 9] |
| 1011 | .into_iter() |
| 1012 | .enumerate() |
| 1013 | .map(|(i, x)| (i + 100, x)) |
| 1014 | .collect(); |
| 1015 | assert_eq!(b.partition_point(|_, &x| x < 7), 5); |
| 1016 | assert_eq!(b.partition_point(|_, &x| x < 0), 0); |
| 1017 | |
| 1018 | let b: OrderMap<_, i32> = [1, 3, 3, 3, 7] |
| 1019 | .into_iter() |
| 1020 | .enumerate() |
| 1021 | .map(|(i, x)| (i + 100, x)) |
| 1022 | .collect(); |
| 1023 | assert_eq!(b.partition_point(|_, &x| x < 0), 0); |
| 1024 | assert_eq!(b.partition_point(|_, &x| x < 1), 0); |
| 1025 | assert_eq!(b.partition_point(|_, &x| x < 2), 1); |
| 1026 | assert_eq!(b.partition_point(|_, &x| x < 3), 1); |
| 1027 | assert_eq!(b.partition_point(|_, &x| x < 4), 4); |
| 1028 | assert_eq!(b.partition_point(|_, &x| x < 5), 4); |
| 1029 | assert_eq!(b.partition_point(|_, &x| x < 6), 4); |
| 1030 | assert_eq!(b.partition_point(|_, &x| x < 7), 4); |
| 1031 | assert_eq!(b.partition_point(|_, &x| x < 8), 5); |
| 1032 | } |
| 1033 | |
| 1034 | #[test] |
| 1035 | fn disjoint_mut_empty_map() { |
| 1036 | let mut map: OrderMap<u32, u32> = OrderMap::default(); |
| 1037 | assert_eq!( |
| 1038 | map.get_disjoint_mut([&0, &1, &2, &3]), |
| 1039 | [None, None, None, None] |
| 1040 | ); |
| 1041 | } |
| 1042 | |
| 1043 | #[test] |
| 1044 | fn disjoint_mut_empty_param() { |
| 1045 | let mut map: OrderMap<u32, u32> = OrderMap::default(); |
| 1046 | map.insert(1, 10); |
| 1047 | assert_eq!(map.get_disjoint_mut([] as [&u32; 0]), []); |
| 1048 | } |
| 1049 | |
| 1050 | #[test] |
| 1051 | fn disjoint_mut_single_fail() { |
| 1052 | let mut map: OrderMap<u32, u32> = OrderMap::default(); |
| 1053 | map.insert(1, 10); |
| 1054 | assert_eq!(map.get_disjoint_mut([&0]), [None]); |
| 1055 | } |
| 1056 | |
| 1057 | #[test] |
| 1058 | fn disjoint_mut_single_success() { |
| 1059 | let mut map: OrderMap<u32, u32> = OrderMap::default(); |
| 1060 | map.insert(1, 10); |
| 1061 | assert_eq!(map.get_disjoint_mut([&1]), [Some(&mut 10)]); |
| 1062 | } |
| 1063 | |
| 1064 | #[test] |
| 1065 | fn disjoint_mut_multi_success() { |
| 1066 | let mut map: OrderMap<u32, u32> = OrderMap::default(); |
| 1067 | map.insert(1, 100); |
| 1068 | map.insert(2, 200); |
| 1069 | map.insert(3, 300); |
| 1070 | map.insert(4, 400); |
| 1071 | assert_eq!( |
| 1072 | map.get_disjoint_mut([&1, &2]), |
| 1073 | [Some(&mut 100), Some(&mut 200)] |
| 1074 | ); |
| 1075 | assert_eq!( |
| 1076 | map.get_disjoint_mut([&1, &3]), |
| 1077 | [Some(&mut 100), Some(&mut 300)] |
| 1078 | ); |
| 1079 | assert_eq!( |
| 1080 | map.get_disjoint_mut([&3, &1, &4, &2]), |
| 1081 | [ |
| 1082 | Some(&mut 300), |
| 1083 | Some(&mut 100), |
| 1084 | Some(&mut 400), |
| 1085 | Some(&mut 200) |
| 1086 | ] |
| 1087 | ); |
| 1088 | } |
| 1089 | |
| 1090 | #[test] |
| 1091 | fn disjoint_mut_multi_success_unsized_key() { |
| 1092 | let mut map: OrderMap<&'static str, u32> = OrderMap::default(); |
| 1093 | map.insert("1", 100); |
| 1094 | map.insert("2", 200); |
| 1095 | map.insert("3", 300); |
| 1096 | map.insert("4", 400); |
| 1097 | |
| 1098 | assert_eq!( |
| 1099 | map.get_disjoint_mut(["1", "2"]), |
| 1100 | [Some(&mut 100), Some(&mut 200)] |
| 1101 | ); |
| 1102 | assert_eq!( |
| 1103 | map.get_disjoint_mut(["1", "3"]), |
| 1104 | [Some(&mut 100), Some(&mut 300)] |
| 1105 | ); |
| 1106 | assert_eq!( |
| 1107 | map.get_disjoint_mut(["3", "1", "4", "2"]), |
| 1108 | [ |
| 1109 | Some(&mut 300), |
| 1110 | Some(&mut 100), |
| 1111 | Some(&mut 400), |
| 1112 | Some(&mut 200) |
| 1113 | ] |
| 1114 | ); |
| 1115 | } |
| 1116 | |
| 1117 | #[test] |
| 1118 | fn disjoint_mut_multi_success_borrow_key() { |
| 1119 | let mut map: OrderMap<String, u32> = OrderMap::default(); |
| 1120 | map.insert("1".into(), 100); |
| 1121 | map.insert("2".into(), 200); |
| 1122 | map.insert("3".into(), 300); |
| 1123 | map.insert("4".into(), 400); |
| 1124 | |
| 1125 | assert_eq!( |
| 1126 | map.get_disjoint_mut(["1", "2"]), |
| 1127 | [Some(&mut 100), Some(&mut 200)] |
| 1128 | ); |
| 1129 | assert_eq!( |
| 1130 | map.get_disjoint_mut(["1", "3"]), |
| 1131 | [Some(&mut 100), Some(&mut 300)] |
| 1132 | ); |
| 1133 | assert_eq!( |
| 1134 | map.get_disjoint_mut(["3", "1", "4", "2"]), |
| 1135 | [ |
| 1136 | Some(&mut 300), |
| 1137 | Some(&mut 100), |
| 1138 | Some(&mut 400), |
| 1139 | Some(&mut 200) |
| 1140 | ] |
| 1141 | ); |
| 1142 | } |
| 1143 | |
| 1144 | #[test] |
| 1145 | fn disjoint_mut_multi_fail_missing() { |
| 1146 | let mut map: OrderMap<u32, u32> = OrderMap::default(); |
| 1147 | map.insert(1, 100); |
| 1148 | map.insert(2, 200); |
| 1149 | map.insert(3, 300); |
| 1150 | map.insert(4, 400); |
| 1151 | |
| 1152 | assert_eq!(map.get_disjoint_mut([&1, &5]), [Some(&mut 100), None]); |
| 1153 | assert_eq!(map.get_disjoint_mut([&5, &6]), [None, None]); |
| 1154 | assert_eq!( |
| 1155 | map.get_disjoint_mut([&1, &5, &4]), |
| 1156 | [Some(&mut 100), None, Some(&mut 400)] |
| 1157 | ); |
| 1158 | } |
| 1159 | |
| 1160 | #[test] |
| 1161 | #[should_panic] |
| 1162 | fn disjoint_mut_multi_fail_duplicate_panic() { |
| 1163 | let mut map: OrderMap<u32, u32> = OrderMap::default(); |
| 1164 | map.insert(1, 100); |
| 1165 | map.get_disjoint_mut([&1, &2, &1]); |
| 1166 | } |
| 1167 | |
| 1168 | #[test] |
| 1169 | fn disjoint_indices_mut_fail_oob() { |
| 1170 | let mut map: OrderMap<u32, u32> = OrderMap::default(); |
| 1171 | map.insert(1, 10); |
| 1172 | map.insert(321, 20); |
| 1173 | assert_eq!( |
| 1174 | map.get_disjoint_indices_mut([1, 3]), |
| 1175 | Err(crate::GetDisjointMutError::IndexOutOfBounds) |
| 1176 | ); |
| 1177 | } |
| 1178 | |
| 1179 | #[test] |
| 1180 | fn disjoint_indices_mut_empty() { |
| 1181 | let mut map: OrderMap<u32, u32> = OrderMap::default(); |
| 1182 | map.insert(1, 10); |
| 1183 | map.insert(321, 20); |
| 1184 | assert_eq!(map.get_disjoint_indices_mut([]), Ok([])); |
| 1185 | } |
| 1186 | |
| 1187 | #[test] |
| 1188 | fn disjoint_indices_mut_success() { |
| 1189 | let mut map: OrderMap<u32, u32> = OrderMap::default(); |
| 1190 | map.insert(1, 10); |
| 1191 | map.insert(321, 20); |
| 1192 | assert_eq!(map.get_disjoint_indices_mut([0]), Ok([(&1, &mut 10)])); |
| 1193 | |
| 1194 | assert_eq!(map.get_disjoint_indices_mut([1]), Ok([(&321, &mut 20)])); |
| 1195 | assert_eq!( |
| 1196 | map.get_disjoint_indices_mut([0, 1]), |
| 1197 | Ok([(&1, &mut 10), (&321, &mut 20)]) |
| 1198 | ); |
| 1199 | } |
| 1200 | |
| 1201 | #[test] |
| 1202 | fn disjoint_indices_mut_fail_duplicate() { |
| 1203 | let mut map: OrderMap<u32, u32> = OrderMap::default(); |
| 1204 | map.insert(1, 10); |
| 1205 | map.insert(321, 20); |
| 1206 | assert_eq!( |
| 1207 | map.get_disjoint_indices_mut([1, 0, 1]), |
| 1208 | Err(crate::GetDisjointMutError::OverlappingIndices) |
| 1209 | ); |
| 1210 | } |
| 1211 | |
| 1212 | #[test] |
| 1213 | fn insert_sorted_by_key() { |
| 1214 | let mut values = [(-1, 8), (3, 18), (-27, 2), (-2, 5)]; |
| 1215 | let mut map: OrderMap<i32, i32> = OrderMap::new(); |
| 1216 | for (key, value) in values { |
| 1217 | let (_, old) = map.insert_sorted_by_key(key, value, |k, _| k.abs()); |
| 1218 | assert_eq!(old, None); |
| 1219 | } |
| 1220 | values.sort_by_key(|(key, _)| key.abs()); |
| 1221 | assert_eq!(values, *map.as_slice()); |
| 1222 | |
| 1223 | for (key, value) in &mut values { |
| 1224 | let (_, old) = map.insert_sorted_by_key(*key, -*value, |k, _| k.abs()); |
| 1225 | assert_eq!(old, Some(*value)); |
| 1226 | *value = -*value; |
| 1227 | } |
| 1228 | assert_eq!(values, *map.as_slice()); |
| 1229 | } |
| 1230 | |
| 1231 | #[test] |
| 1232 | fn insert_sorted_by() { |
| 1233 | let mut values = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5)]; |
| 1234 | let mut map: OrderMap<i32, i32> = OrderMap::new(); |
| 1235 | for (key, value) in values { |
| 1236 | let (_, old) = map.insert_sorted_by(key, value, |key1, _, key2, _| key2.cmp(key1)); |
| 1237 | assert_eq!(old, None); |
| 1238 | } |
| 1239 | values.reverse(); |
| 1240 | assert_eq!(values, *map.as_slice()); |
| 1241 | |
| 1242 | for (key, value) in &mut values { |
| 1243 | let (_, old) = map.insert_sorted_by(*key, -*value, |key1, _, key2, _| key2.cmp(key1)); |
| 1244 | assert_eq!(old, Some(*value)); |
| 1245 | *value = -*value; |
| 1246 | } |
| 1247 | assert_eq!(values, *map.as_slice()); |
| 1248 | } |
| 1249 | |
| 1250 | #[test] |
| 1251 | fn is_sorted() { |
| 1252 | fn expect(map: &OrderMap<i32, i32>, e: [bool; 7]) { |
| 1253 | assert_eq!(e[0], map.is_sorted()); |
| 1254 | assert_eq!(e[1], map.is_sorted_by(|k1, _, k2, _| k1 < k2)); |
| 1255 | assert_eq!(e[2], map.is_sorted_by(|k1, _, k2, _| k1 > k2)); |
| 1256 | assert_eq!(e[3], map.is_sorted_by(|_, v1, _, v2| v1 < v2)); |
| 1257 | assert_eq!(e[4], map.is_sorted_by(|_, v1, _, v2| v1 > v2)); |
| 1258 | assert_eq!(e[5], map.is_sorted_by_key(|k, _| k)); |
| 1259 | assert_eq!(e[6], map.is_sorted_by_key(|_, v| v)); |
| 1260 | } |
| 1261 | |
| 1262 | let mut map = OrderMap::from_iter((0..10).map(|i| (i, i * i))); |
| 1263 | expect(&map, [true, true, false, true, false, true, true]); |
| 1264 | |
| 1265 | map[5] = -1; |
| 1266 | expect(&map, [true, true, false, false, false, true, false]); |
| 1267 | |
| 1268 | map[5] = 25; |
| 1269 | map.replace_index(5, -1).unwrap(); |
| 1270 | expect(&map, [false, false, false, true, false, false, true]); |
| 1271 | } |
| 1272 | |
| 1273 | #[test] |
| 1274 | fn is_sorted_trivial() { |
| 1275 | fn expect(map: &OrderMap<i32, i32>, e: [bool; 5]) { |
| 1276 | assert_eq!(e[0], map.is_sorted()); |
| 1277 | assert_eq!(e[1], map.is_sorted_by(|_, _, _, _| true)); |
| 1278 | assert_eq!(e[2], map.is_sorted_by(|_, _, _, _| false)); |
| 1279 | assert_eq!(e[3], map.is_sorted_by_key(|_, _| 0f64)); |
| 1280 | assert_eq!(e[4], map.is_sorted_by_key(|_, _| f64::NAN)); |
| 1281 | } |
| 1282 | |
| 1283 | let mut map = OrderMap::new(); |
| 1284 | expect(&map, [true, true, true, true, true]); |
| 1285 | |
| 1286 | map.insert(0, 0); |
| 1287 | expect(&map, [true, true, true, true, true]); |
| 1288 | |
| 1289 | map.insert(1, 1); |
| 1290 | expect(&map, [true, true, false, true, false]); |
| 1291 | |
| 1292 | map.reverse(); |
| 1293 | expect(&map, [false, true, false, true, false]); |
| 1294 | } |