[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.
This commit is contained in:
Eryn Wells 2025-06-06 21:45:48 -07:00
parent 651c982ead
commit 1686eeb35a

View file

@ -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));
}
}