Skip to content

Commit

Permalink
refactor: move tests (#103)
Browse files Browse the repository at this point in the history
* refactor: move tests

---------

Signed-off-by: Thomas Mauran <thomasmauran@yahoo.com>
  • Loading branch information
thomas-mauran authored Nov 20, 2024
1 parent f89b852 commit 9575727
Show file tree
Hide file tree
Showing 21 changed files with 3,818 additions and 3,811 deletions.
1,001 changes: 1 addition & 1,000 deletions src/board.rs

Large diffs are not rendered by default.

376 changes: 0 additions & 376 deletions src/pieces/bishop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,379 +202,3 @@ impl Bishop {
}
}
}

#[cfg(test)]
mod tests {
use crate::{
board::{Board, Coord},
pieces::{bishop::Bishop, PieceColor, PieceType, Position},
utils::is_getting_checked,
};

#[test]
fn piece_move_no_enemies() {
let custom_board = [
[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::Bishop, PieceColor::White)),
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],
];
let mut board = Board::default();
board.set_board(custom_board);

let mut right_positions = vec![
Coord::new(0, 0),
Coord::new(1, 1),
Coord::new(2, 2),
Coord::new(3, 3),
Coord::new(5, 5),
Coord::new(6, 6),
Coord::new(7, 7),
Coord::new(1, 7),
Coord::new(2, 6),
Coord::new(3, 5),
Coord::new(5, 3),
Coord::new(6, 2),
Coord::new(7, 1),
];
right_positions.sort();

let mut positions = Bishop::authorized_positions(
&Coord::new(4, 4),
PieceColor::White,
board.board,
&[],
false,
);
positions.sort();

assert_eq!(right_positions, positions);
}

#[test]
fn piece_move_one_enemies_top_right() {
let custom_board = [
[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,
],
[
None,
None,
None,
None,
Some((PieceType::Rook, PieceColor::White)),
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],
];
let mut board = Board::default();
board.set_board(custom_board);

let mut right_positions = vec![
Coord::new(0, 0),
Coord::new(1, 1),
Coord::new(2, 2),
Coord::new(3, 3),
Coord::new(5, 5),
Coord::new(6, 6),
Coord::new(7, 7),
Coord::new(3, 5),
Coord::new(5, 3),
Coord::new(6, 2),
Coord::new(7, 1),
];
right_positions.sort();

let mut positions = Bishop::authorized_positions(
&Coord::new(4, 4),
PieceColor::White,
board.board,
&[],
false,
);
positions.sort();

assert_eq!(right_positions, positions);
}

#[test]
fn piece_move_multiple_enemies_and_ally_front() {
let custom_board = [
[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,
None,
None,
],
[
None,
None,
None,
None,
Some((PieceType::Bishop, PieceColor::White)),
None,
None,
None,
],
[
None,
None,
None,
None,
None,
Some((PieceType::Pawn, PieceColor::Black)),
None,
None,
],
[None, None, None, None, None, None, None, None],
[
None,
Some((PieceType::Bishop, PieceColor::White)),
None,
None,
None,
None,
None,
None,
],
];
let mut board = Board::default();
board.set_board(custom_board);

let mut right_positions = vec![
Coord::new(3, 3),
Coord::new(5, 5),
Coord::new(3, 5),
Coord::new(2, 6),
Coord::new(1, 7),
Coord::new(5, 3),
Coord::new(6, 2),
];
right_positions.sort();

let mut positions = Bishop::authorized_positions(
&Coord::new(4, 4),
PieceColor::White,
board.board,
&[],
false,
);
positions.sort();

assert_eq!(right_positions, positions);
}

#[test]
fn king_checked_can_resolve() {
let custom_board = [
[None, None, None, None, 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,
Some((PieceType::Bishop, PieceColor::White)),
None,
None,
None,
],
[
None,
None,
None,
None,
None,
Some((PieceType::Bishop, PieceColor::Black)),
None,
None,
],
[None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None],
];
let mut board = Board::new(custom_board, PieceColor::Black, vec![]);
board.set_board(custom_board);

let is_king_checked =
is_getting_checked(board.board, board.player_turn, &board.move_history);

let mut right_positions = vec![Coord::new(4, 4)];
right_positions.sort();

let mut positions = Bishop::authorized_positions(
&Coord::new(5, 5),
PieceColor::Black,
board.board,
&[],
is_king_checked,
);
positions.sort();

assert_eq!(right_positions, positions);
}

#[test]
fn king_checked_cant_resolve() {
let custom_board = [
[None, None, None, None, 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,
Some((PieceType::Bishop, PieceColor::White)),
None,
None,
None,
],
[
None,
None,
None,
None,
None,
None,
Some((PieceType::Bishop, PieceColor::Black)),
None,
],
[None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None],
];
let mut board = Board::new(custom_board, PieceColor::Black, vec![]);
board.set_board(custom_board);

let is_king_checked =
is_getting_checked(board.board, board.player_turn, &board.move_history);

let mut right_positions: Vec<Coord> = vec![];
right_positions.sort();

let mut positions = Bishop::authorized_positions(
&Coord::new(5, 6),
PieceColor::Black,
board.board,
&[],
is_king_checked,
);
positions.sort();

assert_eq!(right_positions, positions);
}

#[test]
fn nailing() {
let custom_board = [
[
None,
None,
None,
None,
Some((PieceType::King, PieceColor::Black)),
None,
None,
None,
],
[
None,
None,
None,
None,
None,
Some((PieceType::Bishop, PieceColor::Black)),
None,
None,
],
[None, None, None, None, None, None, None, None],
[
None,
None,
None,
None,
None,
None,
None,
Some((PieceType::Queen, PieceColor::White)),
],
[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],
];
let mut board = Board::new(custom_board, PieceColor::Black, vec![]);
board.set_board(custom_board);

let is_king_checked =
is_getting_checked(board.board, board.player_turn, &board.move_history);

let mut right_positions = vec![Coord::new(2, 6), Coord::new(3, 7)];
right_positions.sort();

let mut positions = Bishop::authorized_positions(
&Coord::new(1, 5),
PieceColor::Black,
board.board,
&[],
is_king_checked,
);
positions.sort();

assert_eq!(right_positions, positions);
}
}
Loading

0 comments on commit 9575727

Please sign in to comment.