Fuzzy test using an hashmap as the base to check the methods.
()
| 503 | #[test] |
| 504 | /// Fuzzy test using an hashmap as the base to check the methods. |
| 505 | fn test_fuzzy() { |
| 506 | let mut lru_queue: LruQueue<i32, i32> = LruQueue::new(); |
| 507 | let mut map: HashMap<i32, i32> = HashMap::new(); |
| 508 | let max_keys = 1_000; |
| 509 | let methods = ["get", "put", "remove", "pop", "contains", "len"]; |
| 510 | let mut rng = rand::rng(); |
| 511 | |
| 512 | for i in 0..1_000_000 { |
| 513 | match *methods.choose(&mut rng).unwrap() { |
| 514 | "get" => { |
| 515 | assert_eq!(lru_queue.get(&(i % max_keys)), map.get(&(i % max_keys))) |
| 516 | } |
| 517 | "put" => assert_eq!( |
| 518 | lru_queue.put(i % max_keys, i), |
| 519 | map.insert(i % max_keys, i) |
| 520 | ), |
| 521 | "remove" => assert_eq!( |
| 522 | lru_queue.remove(&(i % max_keys)), |
| 523 | map.remove(&(i % max_keys)) |
| 524 | ), |
| 525 | "pop" => { |
| 526 | let removed = lru_queue.pop(); |
| 527 | if let Some((k, v)) = removed { |
| 528 | assert_eq!(Some(v), map.remove(&k)) |
| 529 | } |
| 530 | } |
| 531 | "contains" => { |
| 532 | assert_eq!( |
| 533 | lru_queue.contains_key(&(i % max_keys)), |
| 534 | map.contains_key(&(i % max_keys)) |
| 535 | ) |
| 536 | } |
| 537 | "len" => assert_eq!(lru_queue.len(), map.len()), |
| 538 | _ => unreachable!(), |
| 539 | } |
| 540 | } |
| 541 | } |
| 542 | } |