From c87fdfe3dfaa2d20ddb6d3b35c873dcd00e149bc Mon Sep 17 00:00:00 2001 From: Nick Mello Date: Sun, 30 Jun 2024 01:38:23 -0500 Subject: [PATCH] Add tests to history movement features Signed-off-by: Nicholas Mello --- src/board.rs | 374 +++++++++++++++++++++++++++++++++++++++++++++ src/pieces/king.rs | 9 ++ src/pieces/pawn.rs | 9 ++ 3 files changed, 392 insertions(+) diff --git a/src/board.rs b/src/board.rs index a339b29..5594b96 100644 --- a/src/board.rs +++ b/src/board.rs @@ -1585,6 +1585,9 @@ mod tests { from_x: 3, to_y: 6, to_x: 3, + is_en_passant: false, + piece_captured: None, + promotion_piece: None, }), ], ); @@ -1640,6 +1643,9 @@ mod tests { from_x: 4, to_y: 0, to_x: 4, + is_en_passant: false, + piece_captured: None, + promotion_piece: Some(PieceType::Queen), }), ], ); @@ -1751,6 +1757,9 @@ mod tests { from_x: 4, to_y: 7, to_x: 4, + is_en_passant: false, + piece_captured: None, + promotion_piece: Some(PieceType::Queen), }), ], ); @@ -1882,6 +1891,9 @@ mod tests { from_x: 2, to_y: 0, to_x: 1, + is_en_passant: false, + piece_captured: None, + promotion_piece: None, }), (PieceMove { piece_type: PieceType::King, @@ -1889,6 +1901,9 @@ mod tests { from_x: 6, to_y: 0, to_x: 5, + is_en_passant: false, + piece_captured: None, + promotion_piece: None, }), (PieceMove { piece_type: PieceType::King, @@ -1896,6 +1911,9 @@ mod tests { from_x: 1, to_y: 0, to_x: 2, + is_en_passant: false, + piece_captured: None, + promotion_piece: None, }), (PieceMove { piece_type: PieceType::King, @@ -1903,6 +1921,9 @@ mod tests { from_x: 5, to_y: 0, to_x: 6, + is_en_passant: false, + piece_captured: None, + promotion_piece: None, }), (PieceMove { piece_type: PieceType::King, @@ -1910,6 +1931,9 @@ mod tests { from_x: 2, to_y: 0, to_x: 1, + is_en_passant: false, + piece_captured: None, + promotion_piece: None, }), (PieceMove { piece_type: PieceType::King, @@ -1917,6 +1941,9 @@ mod tests { from_x: 6, to_y: 0, to_x: 5, + is_en_passant: false, + piece_captured: None, + promotion_piece: None, }), (PieceMove { piece_type: PieceType::King, @@ -1924,6 +1951,9 @@ mod tests { from_x: 1, to_y: 0, to_x: 2, + is_en_passant: false, + piece_captured: None, + promotion_piece: None, }), (PieceMove { piece_type: PieceType::King, @@ -1931,6 +1961,9 @@ mod tests { from_x: 5, to_y: 0, to_x: 6, + is_en_passant: false, + piece_captured: None, + promotion_piece: None, }), ], ); @@ -2029,6 +2062,9 @@ mod tests { from_x: 2, to_y: 4, to_x: 2, + is_en_passant: false, + piece_captured: None, + promotion_piece: None, }), ], ); @@ -2093,4 +2129,342 @@ mod tests { "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR b kq - 0 0" ); } + + #[test] + fn history_forward_backward() { + let custom_board = [ + [ + Some((PieceType::Rook, PieceColor::Black)), + Some((PieceType::Knight, PieceColor::Black)), + Some((PieceType::Bishop, PieceColor::Black)), + Some((PieceType::Queen, PieceColor::Black)), + Some((PieceType::King, PieceColor::Black)), + Some((PieceType::Bishop, PieceColor::Black)), + Some((PieceType::Knight, PieceColor::Black)), + Some((PieceType::Rook, PieceColor::Black)), + ], + [ + Some((PieceType::Pawn, PieceColor::Black)), + Some((PieceType::Pawn, PieceColor::Black)), + Some((PieceType::Pawn, PieceColor::Black)), + Some((PieceType::Pawn, PieceColor::Black)), + Some((PieceType::Pawn, PieceColor::Black)), + Some((PieceType::Pawn, PieceColor::Black)), + Some((PieceType::Pawn, PieceColor::Black)), + Some((PieceType::Pawn, PieceColor::Black)), + ], + [None, None, None, None, None, None, None, None], + [None, None, None, None, None, None, None, None], + [None, None, None, None, None, None, None, None], + [None, None, None, None, None, None, None, None], + [ + Some((PieceType::Pawn, PieceColor::White)), + Some((PieceType::Pawn, PieceColor::White)), + Some((PieceType::Pawn, PieceColor::White)), + Some((PieceType::Pawn, PieceColor::White)), + Some((PieceType::Pawn, PieceColor::White)), + Some((PieceType::Pawn, PieceColor::White)), + Some((PieceType::Pawn, PieceColor::White)), + Some((PieceType::Pawn, PieceColor::White)), + ], + [ + Some((PieceType::Rook, PieceColor::White)), + Some((PieceType::Knight, PieceColor::White)), + Some((PieceType::Bishop, PieceColor::White)), + Some((PieceType::Queen, PieceColor::White)), + Some((PieceType::King, PieceColor::White)), + Some((PieceType::Bishop, PieceColor::White)), + Some((PieceType::Knight, PieceColor::White)), + Some((PieceType::Rook, PieceColor::White)), + ], + ]; + + let mut board = Board::new(custom_board, PieceColor::White, vec![]); + board.move_piece_on_the_board([6, 0], [5, 0]); + board.history_backward(); + board.history_forward(); + } + + // Checking overflow and underflow cases by going back and forth through + // history in many directions + #[test] + fn history_forward_backward_overflow_underflow() { + let custom_board = [ + [ + Some((PieceType::Rook, PieceColor::Black)), + Some((PieceType::Knight, PieceColor::Black)), + Some((PieceType::Bishop, PieceColor::Black)), + Some((PieceType::Queen, PieceColor::Black)), + Some((PieceType::King, PieceColor::Black)), + Some((PieceType::Bishop, PieceColor::Black)), + Some((PieceType::Knight, PieceColor::Black)), + Some((PieceType::Rook, PieceColor::Black)), + ], + [ + Some((PieceType::Pawn, PieceColor::Black)), + Some((PieceType::Pawn, PieceColor::Black)), + Some((PieceType::Pawn, PieceColor::Black)), + Some((PieceType::Pawn, PieceColor::Black)), + Some((PieceType::Pawn, PieceColor::Black)), + Some((PieceType::Pawn, PieceColor::Black)), + Some((PieceType::Pawn, PieceColor::Black)), + Some((PieceType::Pawn, PieceColor::Black)), + ], + [None, None, None, None, None, None, None, None], + [None, None, None, None, None, None, None, None], + [None, None, None, None, None, None, None, None], + [None, None, None, None, None, None, None, None], + [ + Some((PieceType::Pawn, PieceColor::White)), + Some((PieceType::Pawn, PieceColor::White)), + Some((PieceType::Pawn, PieceColor::White)), + Some((PieceType::Pawn, PieceColor::White)), + Some((PieceType::Pawn, PieceColor::White)), + Some((PieceType::Pawn, PieceColor::White)), + Some((PieceType::Pawn, PieceColor::White)), + Some((PieceType::Pawn, PieceColor::White)), + ], + [ + Some((PieceType::Rook, PieceColor::White)), + Some((PieceType::Knight, PieceColor::White)), + Some((PieceType::Bishop, PieceColor::White)), + Some((PieceType::Queen, PieceColor::White)), + Some((PieceType::King, PieceColor::White)), + Some((PieceType::Bishop, PieceColor::White)), + Some((PieceType::Knight, PieceColor::White)), + Some((PieceType::Rook, PieceColor::White)), + ], + ]; + + let mut board = Board::new(custom_board, PieceColor::White, vec![]); + board.move_piece_on_the_board([6, 0], [5, 0]); + board.move_piece_on_the_board([1, 0], [2, 0]); + board.history_backward(); + board.history_forward(); + board.history_backward(); + board.history_backward(); + board.history_backward(); + board.history_backward(); + board.history_forward(); + board.history_forward(); + board.history_forward(); + board.history_forward(); + } + + #[test] + fn history_promition_white() { + let custom_board = [ + [None, None, None, None, None, None, None, None], + [ + None, + Some((PieceType::Pawn, PieceColor::White)), + None, + None, + None, + None, + Some((PieceType::King, PieceColor::Black)), + None, + ], + [None, None, None, None, None, None, None, None], + [None, None, None, None, None, None, None, None], + [None, None, None, None, None, None, None, None], + [None, None, None, None, None, None, None, None], + [ + None, + None, + None, + None, + Some((PieceType::King, PieceColor::White)), + None, + None, + None, + ], + [None, None, None, None, None, None, None, None], + ]; + let mut board = Board::new(custom_board, PieceColor::White, vec![]); + board.move_piece_on_the_board([1, 1], [0, 1]); + board.promotion_cursor = 0; + board.promote_piece(); + board.history_backward(); + board.history_backward(); + board.history_forward(); + board.history_forward(); + assert_eq!(board.display_board, board.board); + } + + #[test] + fn history_promition_black() { + let custom_board = [ + [None, None, None, None, None, None, None, None], + [ + None, + None, + None, + None, + None, + None, + Some((PieceType::King, PieceColor::Black)), + None, + ], + [None, None, None, None, None, None, None, None], + [None, None, None, None, None, None, None, None], + [None, None, None, None, None, None, None, None], + [None, None, None, None, None, None, None, None], + [ + None, + Some((PieceType::Pawn, PieceColor::Black)), + None, + None, + Some((PieceType::King, PieceColor::White)), + None, + None, + None, + ], + [None, None, None, None, None, None, None, None], + ]; + let mut board = Board::new(custom_board, PieceColor::Black, vec![]); + board.move_piece_on_the_board([6, 1], [7, 1]); + board.promotion_cursor = 0; + board.promote_piece(); + board.history_backward(); + board.history_backward(); + board.history_forward(); + board.history_forward(); + assert_eq!(board.display_board, board.board); + } + + #[test] + fn history_en_passant() { + let custom_board = [ + [ + Some((PieceType::Rook, PieceColor::Black)), + Some((PieceType::Knight, PieceColor::Black)), + Some((PieceType::Bishop, PieceColor::Black)), + Some((PieceType::Queen, PieceColor::Black)), + Some((PieceType::King, PieceColor::Black)), + Some((PieceType::Bishop, PieceColor::Black)), + Some((PieceType::Knight, PieceColor::Black)), + Some((PieceType::Rook, PieceColor::Black)), + ], + [ + None, + Some((PieceType::Pawn, PieceColor::Black)), + Some((PieceType::Pawn, PieceColor::Black)), + Some((PieceType::Pawn, PieceColor::Black)), + Some((PieceType::Pawn, PieceColor::Black)), + Some((PieceType::Pawn, PieceColor::Black)), + Some((PieceType::Pawn, PieceColor::Black)), + Some((PieceType::Pawn, PieceColor::Black)), + ], + [None, None, None, None, None, None, None, None], + [None, None, None, None, None, None, None, None], + [ + Some((PieceType::Pawn, PieceColor::Black)), + None, + None, + None, + None, + None, + None, + None, + ], + [None, None, None, None, None, None, None, None], + [ + Some((PieceType::Pawn, PieceColor::White)), + Some((PieceType::Pawn, PieceColor::White)), + Some((PieceType::Pawn, PieceColor::White)), + Some((PieceType::Pawn, PieceColor::White)), + Some((PieceType::Pawn, PieceColor::White)), + Some((PieceType::Pawn, PieceColor::White)), + Some((PieceType::Pawn, PieceColor::White)), + Some((PieceType::Pawn, PieceColor::White)), + ], + [ + Some((PieceType::Rook, PieceColor::White)), + Some((PieceType::Knight, PieceColor::White)), + Some((PieceType::Bishop, PieceColor::White)), + Some((PieceType::Queen, PieceColor::White)), + Some((PieceType::King, PieceColor::White)), + Some((PieceType::Bishop, PieceColor::White)), + Some((PieceType::Knight, PieceColor::White)), + Some((PieceType::Rook, PieceColor::White)), + ], + ]; + + let mut board = Board::new(custom_board, PieceColor::White, vec![]); + board.move_piece_on_the_board([6, 1], [4, 1]); + board.move_piece_on_the_board([4, 0], [3, 1]); + board.move_piece_on_the_board([2, 2], [3, 1]); + board.history_backward(); + board.history_backward(); + board.history_backward(); + board.history_backward(); + board.history_forward(); + board.history_forward(); + board.history_forward(); + board.history_forward(); + assert_eq!(board.display_board, board.board); + } + + #[test] + fn history_castling() { + let custom_board = [ + [ + Some((PieceType::Rook, PieceColor::Black)), + Some((PieceType::Knight, PieceColor::Black)), + Some((PieceType::Bishop, PieceColor::Black)), + Some((PieceType::Queen, PieceColor::Black)), + Some((PieceType::King, PieceColor::Black)), + Some((PieceType::Bishop, PieceColor::Black)), + Some((PieceType::Knight, PieceColor::Black)), + Some((PieceType::Rook, PieceColor::Black)), + ], + [ + Some((PieceType::Pawn, PieceColor::Black)), + Some((PieceType::Pawn, PieceColor::Black)), + Some((PieceType::Pawn, PieceColor::Black)), + Some((PieceType::Pawn, PieceColor::Black)), + Some((PieceType::Pawn, PieceColor::Black)), + Some((PieceType::Pawn, PieceColor::Black)), + Some((PieceType::Pawn, PieceColor::Black)), + Some((PieceType::Pawn, PieceColor::Black)), + ], + [None, None, None, None, None, None, None, None], + [None, None, None, None, None, None, None, None], + [None, None, None, None, None, None, None, None], + [None, None, None, None, None, None, None, None], + [ + Some((PieceType::Pawn, PieceColor::White)), + Some((PieceType::Pawn, PieceColor::White)), + Some((PieceType::Pawn, PieceColor::White)), + Some((PieceType::Pawn, PieceColor::White)), + Some((PieceType::Pawn, PieceColor::White)), + Some((PieceType::Pawn, PieceColor::White)), + Some((PieceType::Pawn, PieceColor::White)), + Some((PieceType::Pawn, PieceColor::White)), + ], + [ + Some((PieceType::Rook, PieceColor::White)), + Some((PieceType::Knight, PieceColor::White)), + Some((PieceType::Bishop, PieceColor::White)), + Some((PieceType::Queen, PieceColor::White)), + Some((PieceType::King, PieceColor::White)), + None, + None, + Some((PieceType::Rook, PieceColor::White)), + ], + ]; + + let mut board = Board::new(custom_board, PieceColor::White, vec![]); + board.move_piece_on_the_board([7, 4], [7, 7]); + board.history_backward(); + board.history_backward(); + board.history_backward(); + board.history_backward(); + board.history_forward(); + board.history_forward(); + board.history_forward(); + board.history_forward(); + assert_eq!(board.display_board, board.board); + } } diff --git a/src/pieces/king.rs b/src/pieces/king.rs index b9cbb4f..4a9e004 100644 --- a/src/pieces/king.rs +++ b/src/pieces/king.rs @@ -658,6 +658,9 @@ mod tests { from_x: 7, to_y: 4, to_x: 7, + is_en_passant: false, + piece_captured: None, + promotion_piece: None, }), (PieceMove { piece_type: PieceType::Pawn, @@ -665,6 +668,9 @@ mod tests { from_x: 2, to_y: 5, to_x: 2, + is_en_passant: false, + piece_captured: None, + promotion_piece: None, }), (PieceMove { piece_type: PieceType::Rook, @@ -672,6 +678,9 @@ mod tests { from_x: 7, to_y: 0, to_x: 7, + is_en_passant: false, + piece_captured: None, + promotion_piece: None, }), ], false, diff --git a/src/pieces/pawn.rs b/src/pieces/pawn.rs index d0d50a0..1390474 100644 --- a/src/pieces/pawn.rs +++ b/src/pieces/pawn.rs @@ -356,6 +356,9 @@ mod tests { from_x: 2, to_y: 3, to_x: 2, + is_en_passant: false, + piece_captured: None, + promotion_piece: None, })], false, ); @@ -400,6 +403,9 @@ mod tests { from_x: 3, to_y: 4, to_x: 3, + is_en_passant: false, + piece_captured: None, + promotion_piece: None, })], false, ); @@ -453,6 +459,9 @@ mod tests { from_x: 3, to_y: 4, to_x: 3, + is_en_passant: false, + piece_captured: None, + promotion_piece: None, })], false, );