|
| 1 | +use crate::models::misc::Partition; |
| 2 | +use crate::solvers::{BruteForce, Solver}; |
| 3 | +use crate::traits::Problem; |
| 4 | + |
| 5 | +#[test] |
| 6 | +fn test_partition_basic() { |
| 7 | + let problem = Partition::new(vec![3, 1, 1, 2, 2, 1]); |
| 8 | + assert_eq!(problem.num_elements(), 6); |
| 9 | + assert_eq!(problem.sizes(), &[3, 1, 1, 2, 2, 1]); |
| 10 | + assert_eq!(problem.total_sum(), 10); |
| 11 | + assert_eq!(problem.dims(), vec![2; 6]); |
| 12 | +} |
| 13 | + |
| 14 | +#[test] |
| 15 | +fn test_partition_evaluate_satisfying() { |
| 16 | + let problem = Partition::new(vec![3, 1, 1, 2, 2, 1]); |
| 17 | + // A' = {3, 2} (indices 0, 3), sum = 5 = 10/2 |
| 18 | + assert!(problem.evaluate(&[1, 0, 0, 1, 0, 0])); |
| 19 | +} |
| 20 | + |
| 21 | +#[test] |
| 22 | +fn test_partition_evaluate_unsatisfying() { |
| 23 | + let problem = Partition::new(vec![3, 1, 1, 2, 2, 1]); |
| 24 | + // All in first subset, selected sum = 0 != 5 |
| 25 | + assert!(!problem.evaluate(&[0, 0, 0, 0, 0, 0])); |
| 26 | + // All in second subset, selected sum = 10 != 5 |
| 27 | + assert!(!problem.evaluate(&[1, 1, 1, 1, 1, 1])); |
| 28 | +} |
| 29 | + |
| 30 | +#[test] |
| 31 | +fn test_partition_evaluate_wrong_length() { |
| 32 | + let problem = Partition::new(vec![3, 1, 1, 2, 2, 1]); |
| 33 | + assert!(!problem.evaluate(&[1, 0, 0])); |
| 34 | + assert!(!problem.evaluate(&[1, 0, 0, 1, 0, 0, 0])); |
| 35 | +} |
| 36 | + |
| 37 | +#[test] |
| 38 | +fn test_partition_evaluate_invalid_value() { |
| 39 | + let problem = Partition::new(vec![3, 1, 1, 2, 2, 1]); |
| 40 | + assert!(!problem.evaluate(&[2, 0, 0, 0, 0, 0])); |
| 41 | +} |
| 42 | + |
| 43 | +#[test] |
| 44 | +fn test_partition_odd_total() { |
| 45 | + // Total = 7 (odd), no equal partition possible |
| 46 | + let problem = Partition::new(vec![3, 1, 2, 1]); |
| 47 | + let solver = BruteForce::new(); |
| 48 | + assert!(solver.find_satisfying(&problem).is_none()); |
| 49 | +} |
| 50 | + |
| 51 | +#[test] |
| 52 | +fn test_partition_solver() { |
| 53 | + let problem = Partition::new(vec![3, 1, 1, 2, 2, 1]); |
| 54 | + let solver = BruteForce::new(); |
| 55 | + let solution = solver.find_satisfying(&problem); |
| 56 | + assert!(solution.is_some()); |
| 57 | + assert!(problem.evaluate(&solution.unwrap())); |
| 58 | +} |
| 59 | + |
| 60 | +#[test] |
| 61 | +fn test_partition_solver_all() { |
| 62 | + let problem = Partition::new(vec![3, 1, 1, 2, 2, 1]); |
| 63 | + let solver = BruteForce::new(); |
| 64 | + let solutions = solver.find_all_satisfying(&problem); |
| 65 | + // 10 satisfying configs for {3,1,1,2,2,1} with target half-sum 5 |
| 66 | + assert_eq!(solutions.len(), 10); |
| 67 | + for sol in &solutions { |
| 68 | + assert!(problem.evaluate(sol)); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +#[test] |
| 73 | +fn test_partition_single_element() { |
| 74 | + // Single element can never be partitioned equally |
| 75 | + let problem = Partition::new(vec![5]); |
| 76 | + let solver = BruteForce::new(); |
| 77 | + assert!(solver.find_satisfying(&problem).is_none()); |
| 78 | +} |
| 79 | + |
| 80 | +#[test] |
| 81 | +fn test_partition_two_equal_elements() { |
| 82 | + let problem = Partition::new(vec![4, 4]); |
| 83 | + let solver = BruteForce::new(); |
| 84 | + let solution = solver.find_satisfying(&problem); |
| 85 | + assert!(solution.is_some()); |
| 86 | + assert!(problem.evaluate(&solution.unwrap())); |
| 87 | +} |
| 88 | + |
| 89 | +#[test] |
| 90 | +fn test_partition_serialization() { |
| 91 | + let problem = Partition::new(vec![3, 1, 1, 2, 2, 1]); |
| 92 | + let json = serde_json::to_value(&problem).unwrap(); |
| 93 | + let restored: Partition = serde_json::from_value(json).unwrap(); |
| 94 | + assert_eq!(restored.sizes(), problem.sizes()); |
| 95 | + assert_eq!(restored.num_elements(), problem.num_elements()); |
| 96 | +} |
| 97 | + |
| 98 | +#[test] |
| 99 | +#[should_panic(expected = "All sizes must be positive")] |
| 100 | +fn test_partition_zero_size_panics() { |
| 101 | + Partition::new(vec![3, 0, 1]); |
| 102 | +} |
| 103 | + |
| 104 | +#[test] |
| 105 | +#[should_panic(expected = "at least one element")] |
| 106 | +fn test_partition_empty_panics() { |
| 107 | + Partition::new(vec![]); |
| 108 | +} |
0 commit comments