|
| 1 | +use super::*; |
| 2 | +use crate::solvers::{BruteForce, Solver}; |
| 3 | +use crate::topology::SimpleGraph; |
| 4 | + |
| 5 | +#[test] |
| 6 | +fn test_partitionintotriangles_basic() { |
| 7 | + use crate::traits::Problem; |
| 8 | + |
| 9 | + // 9-vertex YES instance: three disjoint triangles |
| 10 | + // Triangle 1: 0-1-2, Triangle 2: 3-4-5, Triangle 3: 6-7-8 |
| 11 | + let graph = SimpleGraph::new( |
| 12 | + 9, |
| 13 | + vec![ |
| 14 | + (0, 1), |
| 15 | + (1, 2), |
| 16 | + (0, 2), |
| 17 | + (3, 4), |
| 18 | + (4, 5), |
| 19 | + (3, 5), |
| 20 | + (6, 7), |
| 21 | + (7, 8), |
| 22 | + (6, 8), |
| 23 | + ], |
| 24 | + ); |
| 25 | + let problem = PartitionIntoTriangles::new(graph); |
| 26 | + |
| 27 | + assert_eq!(problem.num_vertices(), 9); |
| 28 | + assert_eq!(problem.num_edges(), 9); |
| 29 | + assert_eq!(problem.dims(), vec![3; 9]); |
| 30 | + |
| 31 | + // Valid partition: vertices 0,1,2 in group 0; 3,4,5 in group 1; 6,7,8 in group 2 |
| 32 | + assert!(problem.evaluate(&[0, 0, 0, 1, 1, 1, 2, 2, 2])); |
| 33 | + |
| 34 | + // Invalid: wrong grouping (vertices 0,1,3 are not a triangle) |
| 35 | + assert!(!problem.evaluate(&[0, 0, 1, 0, 1, 1, 2, 2, 2])); |
| 36 | + |
| 37 | + // Invalid: group sizes wrong (4 in group 0, 2 in group 1) |
| 38 | + assert!(!problem.evaluate(&[0, 0, 0, 0, 1, 1, 2, 2, 2])); |
| 39 | +} |
| 40 | + |
| 41 | +#[test] |
| 42 | +fn test_partitionintotriangles_no_solution() { |
| 43 | + use crate::traits::Problem; |
| 44 | + |
| 45 | + // 6-vertex NO instance: path graph has no triangles at all |
| 46 | + let graph = SimpleGraph::new(6, vec![(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]); |
| 47 | + let problem = PartitionIntoTriangles::new(graph); |
| 48 | + |
| 49 | + assert_eq!(problem.num_vertices(), 6); |
| 50 | + assert_eq!(problem.dims(), vec![2; 6]); |
| 51 | + |
| 52 | + // No valid partition exists since there are no triangles |
| 53 | + let solver = BruteForce::new(); |
| 54 | + let solution = solver.find_satisfying(&problem); |
| 55 | + assert!(solution.is_none()); |
| 56 | +} |
| 57 | + |
| 58 | +#[test] |
| 59 | +fn test_partitionintotriangles_solver() { |
| 60 | + use crate::traits::Problem; |
| 61 | + |
| 62 | + // Single triangle |
| 63 | + let graph = SimpleGraph::new(3, vec![(0, 1), (1, 2), (0, 2)]); |
| 64 | + let problem = PartitionIntoTriangles::new(graph); |
| 65 | + |
| 66 | + let solver = BruteForce::new(); |
| 67 | + let solution = solver.find_satisfying(&problem); |
| 68 | + assert!(solution.is_some()); |
| 69 | + let sol = solution.unwrap(); |
| 70 | + assert!(problem.evaluate(&sol)); |
| 71 | + |
| 72 | + // All solutions should be valid |
| 73 | + let all = solver.find_all_satisfying(&problem); |
| 74 | + assert!(!all.is_empty()); |
| 75 | + for s in &all { |
| 76 | + assert!(problem.evaluate(s)); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +#[test] |
| 81 | +fn test_partitionintotriangles_serialization() { |
| 82 | + let graph = SimpleGraph::new(3, vec![(0, 1), (1, 2), (0, 2)]); |
| 83 | + let problem = PartitionIntoTriangles::new(graph); |
| 84 | + |
| 85 | + let json = serde_json::to_string(&problem).unwrap(); |
| 86 | + let deserialized: PartitionIntoTriangles<SimpleGraph> = serde_json::from_str(&json).unwrap(); |
| 87 | + |
| 88 | + assert_eq!(deserialized.num_vertices(), 3); |
| 89 | + assert_eq!(deserialized.num_edges(), 3); |
| 90 | +} |
| 91 | + |
| 92 | +#[test] |
| 93 | +#[should_panic(expected = "must be divisible by 3")] |
| 94 | +fn test_partitionintotriangles_invalid_vertex_count() { |
| 95 | + let graph = SimpleGraph::new(4, vec![(0, 1), (1, 2), (2, 3)]); |
| 96 | + let _ = PartitionIntoTriangles::new(graph); |
| 97 | +} |
| 98 | + |
| 99 | +#[test] |
| 100 | +fn test_partitionintotriangles_config_out_of_range() { |
| 101 | + use crate::traits::Problem; |
| 102 | + |
| 103 | + let graph = SimpleGraph::new(3, vec![(0, 1), (1, 2), (0, 2)]); |
| 104 | + let problem = PartitionIntoTriangles::new(graph); |
| 105 | + |
| 106 | + // q = 1, so only group 0 is valid; group 1 is out of range |
| 107 | + assert!(!problem.evaluate(&[0, 0, 1])); |
| 108 | +} |
| 109 | + |
| 110 | +#[test] |
| 111 | +fn test_partitionintotriangles_wrong_config_length() { |
| 112 | + use crate::traits::Problem; |
| 113 | + |
| 114 | + let graph = SimpleGraph::new(3, vec![(0, 1), (1, 2), (0, 2)]); |
| 115 | + let problem = PartitionIntoTriangles::new(graph); |
| 116 | + |
| 117 | + assert!(!problem.evaluate(&[0, 0])); |
| 118 | + assert!(!problem.evaluate(&[0, 0, 0, 0])); |
| 119 | +} |
| 120 | + |
| 121 | +#[test] |
| 122 | +fn test_partitionintotriangles_size_getters() { |
| 123 | + let graph = SimpleGraph::new(6, vec![(0, 1), (1, 2), (0, 2), (3, 4), (4, 5), (3, 5)]); |
| 124 | + let problem = PartitionIntoTriangles::new(graph); |
| 125 | + assert_eq!(problem.num_vertices(), 6); |
| 126 | + assert_eq!(problem.num_edges(), 6); |
| 127 | +} |
0 commit comments