From 1686eeb35a643e86896e5b04f8a73ee5a1a5b4aa Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Fri, 6 Jun 2025 21:45:48 -0700 Subject: [PATCH] [board] A small set of unit tests for Zobrist hashes on Board A few tests to ensure the zobrist hash changes when changes are made to the Board. This set isn't exhaustive. --- board/src/board.rs | 53 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) 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)); + } }