~ruther/ctu-fee-eoa

d2cfd7e08dddd902d99da9846b52d53be9af3d37 — Rutherther a month ago dd037eb
tests(tsp): add test for verify_solution
1 files changed, 32 insertions(+), 0 deletions(-)

M codes/tsp_hw01/src/tsp.rs
M codes/tsp_hw01/src/tsp.rs => codes/tsp_hw01/src/tsp.rs +32 -0
@@ 633,6 633,38 @@ mod tests {
    }

    #[test]
    fn test_verify_solution() {
        let mut rng = rand::rng();
        let rng = &mut rng;
        let mut chromosome = NodePermutation::<Const<6>> {
            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::<Const<6>> {
            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
        // 0 0 0 0 0 0 0