From d2cfd7e08dddd902d99da9846b52d53be9af3d37 Mon Sep 17 00:00:00 2001 From: Rutherther Date: Sat, 1 Nov 2025 10:24:48 +0100 Subject: [PATCH] tests(tsp): add test for verify_solution --- codes/tsp_hw01/src/tsp.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/codes/tsp_hw01/src/tsp.rs b/codes/tsp_hw01/src/tsp.rs index 1f4eb6c15dd6b84afea07f8a5f52f3a359e9fe7f..e1f5f80e4373ca72216157ab4c5aa6b75157c86e 100644 --- a/codes/tsp_hw01/src/tsp.rs +++ b/codes/tsp_hw01/src/tsp.rs @@ -632,6 +632,38 @@ mod tests { } } + #[test] + fn test_verify_solution() { + let mut rng = rand::rng(); + let rng = &mut rng; + let mut chromosome = NodePermutation::> { + permutation: SVector::from_vec(vec![0, 1, 2, 3, 4, 5]) + }; + + for _ in 0..100 { + chromosome.permutation.as_mut_slice().shuffle(rng); + assert!(TSPInstance::verify_solution(&chromosome)); + } + + // Out of bounds + chromosome.permutation[0] = 6; + assert!(!TSPInstance::verify_solution(&chromosome)); + chromosome.permutation[0] = 7; + assert!(!TSPInstance::verify_solution(&chromosome)); + chromosome.permutation[0] = 8; + assert!(!TSPInstance::verify_solution(&chromosome)); + + // Repeating + chromosome.permutation[0] = 5; + chromosome.permutation[1] = 5; + assert!(!TSPInstance::verify_solution(&chromosome)); + + let chromosome = NodePermutation::> { + permutation: SVector::from_vec(vec![0, 1, 2, 3, 1, 5]) + }; + assert!(!TSPInstance::verify_solution(&chromosome)); + } + #[test] fn test_binary_string_representation() { // x 0 1 2 3 4 5