microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
source/resource_estimator/src/estimates/optimization/population/tests.rs
94lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | use super::{Point, Point2D, Population}; |
| 5 | |
| 6 | #[test] |
| 7 | fn test_dominates_should_dominate_by_first_dimension() { |
| 8 | let p = Point2D::new(0, 1.0, 1); |
| 9 | let other = Point2D::new(1, 1.5, 1); |
| 10 | assert!(p.dominates(&other)); |
| 11 | } |
| 12 | |
| 13 | #[test] |
| 14 | fn test_dominates_should_dominate_by_second_dimension() { |
| 15 | let p = Point2D::new(0, 1.0, 1); |
| 16 | let other = Point2D::new(1, 1.0, 5); |
| 17 | assert!(p.dominates(&other)); |
| 18 | } |
| 19 | |
| 20 | #[test] |
| 21 | fn test_dominates_should_dominate_by_both_dimensions() { |
| 22 | let p = Point2D::new(0, 1.0, 1); |
| 23 | let other = Point2D::new(1, 2.0, 5); |
| 24 | assert!(p.dominates(&other)); |
| 25 | } |
| 26 | |
| 27 | #[test] |
| 28 | fn test_dominates_should_not_dominate_first_less_second_greater() { |
| 29 | let p = Point2D::new(0, 1.0, 1); |
| 30 | let other = Point2D::new(1, 3.0, 0); |
| 31 | assert!(!p.dominates(&other)); |
| 32 | } |
| 33 | |
| 34 | #[test] |
| 35 | fn test_dominates_should_not_dominate_first_greater_second_less() { |
| 36 | let p = Point2D::new(0, 1.0, 1); |
| 37 | let other = Point2D::new(1, 0.0, 2); |
| 38 | assert!(!p.dominates(&other)); |
| 39 | } |
| 40 | |
| 41 | #[test] |
| 42 | fn test_dominates_should_not_dominate_itself() { |
| 43 | let p = Point2D::new(0, 1.0, 1); |
| 44 | let other = Point2D::new(1, 1.0, 1); |
| 45 | assert!(!p.dominates(&other)); |
| 46 | } |
| 47 | |
| 48 | #[test] |
| 49 | fn fill() { |
| 50 | let mut population = Population::<Point2D<i32>>::new(); |
| 51 | let p1 = Point2D::new(0, 1.0, 1); |
| 52 | let p2 = Point2D::new(1, 0.5, 1); |
| 53 | let p3 = Point2D::new(2, 1.1, 0); |
| 54 | population.push(p1); |
| 55 | population.push(p2); |
| 56 | population.push(p3); |
| 57 | let items = population.items(); |
| 58 | assert_eq!(items.len(), 3); |
| 59 | assert_eq!(items[0].item, 0); |
| 60 | assert_eq!(items[1].item, 1); |
| 61 | assert_eq!(items[2].item, 2); |
| 62 | } |
| 63 | |
| 64 | #[test] |
| 65 | fn fill_and_filter() { |
| 66 | let mut population = Population::<Point2D<i32>>::new(); |
| 67 | let p1 = Point2D::new(0, 1.0, 1); |
| 68 | let p2 = Point2D::new(1, 0.5, 1); |
| 69 | let p3 = Point2D::new(2, 1.1, 0); |
| 70 | population.push(p1); |
| 71 | population.push(p2); |
| 72 | population.push(p3); |
| 73 | population.filter_out_dominated(); |
| 74 | let items = population.items(); |
| 75 | assert_eq!(items.len(), 2); |
| 76 | assert_eq!(items[0].item + items[1].item, 3); |
| 77 | } |
| 78 | |
| 79 | #[test] |
| 80 | fn fill_filter_and_sort() { |
| 81 | let mut population = Population::<Point2D<i32>>::new(); |
| 82 | let p1 = Point2D::new(0, 1.0, 1); |
| 83 | let p2 = Point2D::new(1, 0.5, 1); |
| 84 | let p3 = Point2D::new(2, 1.1, 0); |
| 85 | population.push(p1); |
| 86 | population.push(p2); |
| 87 | population.push(p3); |
| 88 | population.filter_out_dominated(); |
| 89 | population.sort_items(); |
| 90 | let items = population.items(); |
| 91 | assert_eq!(items.len(), 2); |
| 92 | assert_eq!(items[0].item, 2); |
| 93 | assert_eq!(items[1].item, 1); |
| 94 | } |
| 95 | |