From b0b91232d517c1261cc822379f0446c2bb0746b8 Mon Sep 17 00:00:00 2001 From: dogeystamp Date: Mon, 21 Oct 2024 14:23:47 -0400 Subject: [PATCH] test: test augmenting with color flips --- src/movegen.rs | 84 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 61 insertions(+), 23 deletions(-) diff --git a/src/movegen.rs b/src/movegen.rs index 65ae136..eeae82f 100644 --- a/src/movegen.rs +++ b/src/movegen.rs @@ -626,6 +626,48 @@ mod tests { use super::*; use crate::fen::{ToFen, START_POSITION}; + /// Helper to produce test cases. + fn decondense_moves( + test_case: (&str, Vec<(&str, Vec<&str>, MoveType)>), + ) -> (BoardState, Vec) { + let (fen, expected) = test_case; + let board = BoardState::from_fen(fen).unwrap(); + + let mut expected_moves = expected + .iter() + .map(|(src, dests, move_type)| { + let src = src.parse::().unwrap(); + let dests = dests + .iter() + .map(|x| x.parse::()) + .map(|x| x.unwrap()); + dests.map(move |dest| Move { + src, + dest, + move_type: *move_type, + }) + }) + .flatten() + .collect::>(); + + expected_moves.sort_unstable(); + (board, expected_moves) + } + + /// Generate new test cases by flipping colors on existing ones. + fn flip_test_case(board: BoardState, moves: &Vec) -> (BoardState, Vec) { + let mut move_vec = moves.iter().map(|mv| Move { + src: mv.src.mirror_vert(), + dest: mv.dest.mirror_vert(), + move_type: mv.move_type, + }).collect::>(); + move_vec.sort_unstable(); + ( + board.flip_colors(), + move_vec, + ) + } + /// Test movegen through contrived positions. #[test] fn test_movegen() { @@ -851,34 +893,21 @@ mod tests { ), ]; - for (fen, expected) in test_cases { - let board = BoardState::from_fen(fen).unwrap(); + for tc in test_cases { + let decondensed = decondense_moves(tc); + let (board, expected_moves) = decondensed; + + let (anti_board, anti_expected_moves) = flip_test_case(board, &expected_moves); let mut moves: Vec = board.gen_pseudo_moves().into_iter().collect(); + let mut anti_moves: Vec = anti_board.gen_pseudo_moves().into_iter().collect(); moves.sort_unstable(); + anti_moves.sort_unstable(); let moves = moves; + let anti_moves = anti_moves; - let mut expected_moves = expected - .iter() - .map(|(src, dests, move_type)| { - let src = src.parse::().unwrap(); - let dests = dests - .iter() - .map(|x| x.parse::()) - .map(|x| x.unwrap()); - dests.map(move |dest| Move { - src, - dest, - move_type: *move_type, - }) - }) - .flatten() - .collect::>(); - - expected_moves.sort_unstable(); - let expected_moves = expected_moves; - - assert_eq!(moves, expected_moves); + assert_eq!(moves, expected_moves, "failed tc {}", board.to_fen()); + assert_eq!(anti_moves, anti_expected_moves, "failed anti-tc '{}' (originally '{}')", anti_board.to_fen(), board.to_fen()); } } @@ -917,6 +946,15 @@ mod tests { "failed on {}", fen ); + + let board_anti = board.flip_colors(); + assert_eq!( + is_check(&board_anti, Color::Black), + *expected, + "failed on anti-version of {} ({})", + fen, + board_anti.to_fen() + ); } }