diff --git a/board/src/board.rs b/board/src/board.rs index 67f7f81..a3afd64 100644 --- a/board/src/board.rs +++ b/board/src/board.rs @@ -325,7 +325,7 @@ impl Board { mod tests { use super::*; use crate::test_board; - use chessfriend_core::piece; + use chessfriend_core::{piece, random::RandomNumberGenerator}; #[test] fn get_piece_on_square() { @@ -335,4 +335,55 @@ mod tests { assert_eq!(board.get_piece(Square::F7), Some(piece!(Black Bishop))); } + + // MARK: - Zobrist Hashing + + fn test_state() -> ZobristState { + let mut rng = RandomNumberGenerator::default(); + ZobristState::new(&mut rng) + } + + #[test] + fn zobrist_hash_set_for_empty_board() { + let state = Arc::new(test_state()); + let board = Board::empty(Some(state.clone())); + let hash = board.zobrist_hash(); + assert_eq!(hash, Some(0)); + } + + #[test] + fn zobrist_hash_set_for_starting_position_board() { + let state = Arc::new(test_state()); + let board = Board::starting(Some(state.clone())); + let hash = board.zobrist_hash(); + assert!(hash.is_some()); + } + + #[test] + fn zobrist_hash_updated_when_changing_active_color() { + let state = Arc::new(test_state()); + + let mut board = Board::empty(Some(state.clone())); + board.set_active_color(Color::Black); + + // Just verify that the value is real and has changed. The actual value + // computation is covered by the tests in zobrist.rs. + let hash = board.zobrist_hash(); + assert!(hash.is_some()); + assert_ne!(hash, Some(0)); + } + + #[test] + fn zobrist_hash_updated_when_changing_en_passant_target() { + let state = Arc::new(test_state()); + + let mut board = Board::empty(Some(state.clone())); + board.set_en_passant_target(Square::C3); + + // Just verify that the value is real and has changed. The actual value + // computation is covered by the tests in zobrist.rs. + let hash = board.zobrist_hash(); + assert!(hash.is_some()); + assert_ne!(hash, Some(0)); + } }