| 394 | |
| 395 | #[test] |
| 396 | fn test_pop() { |
| 397 | let mut lru_queue: LruQueue<i32, i32> = LruQueue::new(); |
| 398 | |
| 399 | // empty queue |
| 400 | assert_eq!(lru_queue.pop(), None); |
| 401 | |
| 402 | // simplest case |
| 403 | lru_queue.put(1, 10); |
| 404 | lru_queue.put(2, 20); |
| 405 | lru_queue.put(3, 30); |
| 406 | assert_eq!(lru_queue.pop(), Some((1, 10))); |
| 407 | assert_eq!(lru_queue.pop(), Some((2, 20))); |
| 408 | assert_eq!(lru_queue.pop(), Some((3, 30))); |
| 409 | assert_eq!(lru_queue.pop(), None); |
| 410 | |
| 411 | // 'get' changes the order |
| 412 | lru_queue.put(1, 10); |
| 413 | lru_queue.put(2, 20); |
| 414 | lru_queue.put(3, 30); |
| 415 | lru_queue.get(&2); |
| 416 | assert_eq!(lru_queue.pop(), Some((1, 10))); |
| 417 | assert_eq!(lru_queue.pop(), Some((3, 30))); |
| 418 | assert_eq!(lru_queue.pop(), Some((2, 20))); |
| 419 | assert_eq!(lru_queue.pop(), None); |
| 420 | |
| 421 | // multiple 'gets' |
| 422 | lru_queue.put(1, 10); |
| 423 | lru_queue.put(2, 20); |
| 424 | lru_queue.put(3, 30); |
| 425 | lru_queue.get(&2); |
| 426 | lru_queue.get(&3); |
| 427 | lru_queue.get(&1); |
| 428 | assert_eq!(lru_queue.pop(), Some((2, 20))); |
| 429 | assert_eq!(lru_queue.pop(), Some((3, 30))); |
| 430 | assert_eq!(lru_queue.pop(), Some((1, 10))); |
| 431 | assert_eq!(lru_queue.pop(), None); |
| 432 | |
| 433 | // 'peak' does not change the order |
| 434 | lru_queue.put(1, 10); |
| 435 | lru_queue.put(2, 20); |
| 436 | lru_queue.put(3, 30); |
| 437 | lru_queue.peek(&2); |
| 438 | assert_eq!(lru_queue.pop(), Some((1, 10))); |
| 439 | assert_eq!(lru_queue.pop(), Some((2, 20))); |
| 440 | assert_eq!(lru_queue.pop(), Some((3, 30))); |
| 441 | assert_eq!(lru_queue.pop(), None); |
| 442 | |
| 443 | // 'contains' does not change the order |
| 444 | lru_queue.put(1, 10); |
| 445 | lru_queue.put(2, 20); |
| 446 | lru_queue.put(3, 30); |
| 447 | lru_queue.contains_key(&2); |
| 448 | assert_eq!(lru_queue.pop(), Some((1, 10))); |
| 449 | assert_eq!(lru_queue.pop(), Some((2, 20))); |
| 450 | assert_eq!(lru_queue.pop(), Some((3, 30))); |
| 451 | assert_eq!(lru_queue.pop(), None); |
| 452 | |
| 453 | // 'put' on the same key promotes it |