()
| 581 | |
| 582 | #[test] |
| 583 | fn test_eviction_probability() { |
| 584 | let prob = ProbEviction::new(f16::from_f32_const(0.03125)); |
| 585 | |
| 586 | // Without wraparound |
| 587 | let global_counter = 1000; |
| 588 | let results = (1..=global_counter) |
| 589 | .map(|n| prob.eviction_probability(global_counter, n)) |
| 590 | .collect::<Vec<f32>>(); |
| 591 | // Check that the eviction probability reduces with decrease |
| 592 | // in counter age, i.e. the results vector is sorted in |
| 593 | // descending order. |
| 594 | assert!(results.as_slice().windows(2).all(|w| w[0] >= w[1])); |
| 595 | |
| 596 | // With wraparound |
| 597 | let global_counter = 2147483647_u32; |
| 598 | let mut rng = rand::thread_rng(); |
| 599 | // Generate some counter values before wraparound. These will |
| 600 | // be > global_counter and < u32::MAX |
| 601 | let mut counter_vals: Vec<u32> = gen_rand_nums(&mut rng, 100, global_counter, u32::MAX); |
| 602 | counter_vals.sort(); |
| 603 | // Generate some counter values after wraparound. These will |
| 604 | // be > 0 and < global_counter |
| 605 | let mut after_wraparound: Vec<u32> = gen_rand_nums(&mut rng, 100, 0, global_counter); |
| 606 | after_wraparound.sort(); |
| 607 | // As the global counter is very large, add some known values |
| 608 | // closer to the global counter |
| 609 | let mut recent: Vec<u32> = (1..=100).map(|n| global_counter - n).collect(); |
| 610 | recent.sort(); |
| 611 | |
| 612 | // Concatenate the above inputs in order |
| 613 | counter_vals.append(&mut after_wraparound); |
| 614 | counter_vals.append(&mut recent); |
| 615 | |
| 616 | let results = counter_vals |
| 617 | .into_iter() |
| 618 | .map(|n| prob.eviction_probability(global_counter, n)) |
| 619 | .collect::<Vec<f32>>(); |
| 620 | // Check that the eviction probability reduces with increase |
| 621 | // in counter value, i.e. the results vector is sorted in |
| 622 | // descending order. |
| 623 | assert!(results.as_slice().windows(2).all(|w| w[0] >= w[1])); |
| 624 | } |
| 625 | |
| 626 | #[test] |
| 627 | fn test_eviction_index() { |
nothing calls this directly
no test coverage detected