Add more tests
This commit is contained in:
@@ -1,15 +1,20 @@
|
||||
use crate::board::game::Game;
|
||||
|
||||
pub fn driver(game: &mut Game, nodes: &mut u64, depth: u8) {
|
||||
pub fn driver(game: &mut Game, nodes: &mut u64, depth_nodes: &mut u64, depth: u8) {
|
||||
if depth == 0 {
|
||||
*nodes += 1;
|
||||
*depth_nodes += 1;
|
||||
return;
|
||||
}
|
||||
|
||||
let color = game.current_player();
|
||||
let pseudo_moves = game.board.pseudo_moves_all();
|
||||
let current_depth_nodes = *depth_nodes;
|
||||
|
||||
for mv in pseudo_moves {
|
||||
if depth == MAX_DEPTH {
|
||||
println!("{mv:?}");
|
||||
}
|
||||
game.make_move(&mv);
|
||||
|
||||
if game.board.king_under_check(color) {
|
||||
@@ -17,18 +22,26 @@ pub fn driver(game: &mut Game, nodes: &mut u64, depth: u8) {
|
||||
continue;
|
||||
}
|
||||
|
||||
driver(game, nodes, depth - 1);
|
||||
driver(game, nodes, depth_nodes, depth - 1);
|
||||
game.unmake_move();
|
||||
}
|
||||
|
||||
let nodes_at_current_depth = *depth_nodes - current_depth_nodes;
|
||||
println!("Nodes at depth {depth}: {nodes_at_current_depth}");
|
||||
}
|
||||
|
||||
const MAX_DEPTH: u8 = 3;
|
||||
const MAX_DEPTH: u8 = 2;
|
||||
|
||||
pub fn perftree_script() {
|
||||
let fen = "r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1";
|
||||
let mut game = crate::board::fen::from_fen(fen).expect("Invalid FEN string");
|
||||
let (mut nodes, depth): (u64, u8) = (0, MAX_DEPTH);
|
||||
driver(&mut game, &mut nodes, depth);
|
||||
let mut nodes: u64 = 0;
|
||||
let mut depth_nodes: u64 = 0;
|
||||
let depth: u8 = MAX_DEPTH;
|
||||
|
||||
driver(&mut game, &mut nodes, &mut depth_nodes, depth);
|
||||
|
||||
println!("Total nodes: {nodes}");
|
||||
}
|
||||
|
||||
use std::collections::HashMap;
|
||||
@@ -57,9 +70,9 @@ pub fn _square_to_notation(square: u8) -> &'static str {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::{board::fen::from_fen, movegen::attack_generator::init_attacks};
|
||||
|
||||
use super::driver;
|
||||
use crate::{board::fen::from_fen, movegen::attack_generator::init_attacks};
|
||||
use rstest::rstest;
|
||||
|
||||
// Examples from https://www.chessprogramming.org/Perft_Results
|
||||
const FEN_PERFT: [&str; 6] = [
|
||||
@@ -71,76 +84,20 @@ mod tests {
|
||||
"r4rk1/1pp1qppp/p1np1n2/2b1p1B1/2B1P1b1/P1NP1N2/1PP1QPPP/R4RK1 w - - 0 10",
|
||||
];
|
||||
|
||||
fn perft(fen: &str, depth: u8) -> Result<u64, String> {
|
||||
#[rstest]
|
||||
#[case(0, 4865609)]
|
||||
#[case(1, 193690690)]
|
||||
#[case(2, 674624)]
|
||||
#[case(3, 15833292)]
|
||||
#[case(4, 89941194)]
|
||||
#[case(5, 164075551)]
|
||||
fn test_perft(#[case] num: usize, #[case] expected_nodes: u64) {
|
||||
init_attacks();
|
||||
|
||||
let time_now = std::time::Instant::now();
|
||||
|
||||
let mut game = from_fen(fen)?;
|
||||
let mut game = from_fen(FEN_PERFT[num]).unwrap();
|
||||
let mut nodes = 0;
|
||||
driver(&mut game, &mut nodes, depth);
|
||||
driver(&mut game, &mut nodes, &mut 0, 5);
|
||||
|
||||
dbg!(nodes, time_now.elapsed());
|
||||
|
||||
Ok(nodes)
|
||||
}
|
||||
|
||||
const FEN: &str = "1r2k2r/2P1pq1p/2npb3/1p3ppP/p3P3/P2B1Q2/1P1PNPP1/R3K2R w KQk g6 0 1";
|
||||
|
||||
// #[ignore]
|
||||
#[test]
|
||||
fn test_nodes() -> Result<(), String> {
|
||||
init_attacks();
|
||||
perft(FEN, 4)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_perft_1() -> Result<(), String> {
|
||||
init_attacks();
|
||||
assert_eq!(perft(FEN_PERFT[0], 5)?, 4865609);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_perft_2() -> Result<(), String> {
|
||||
init_attacks();
|
||||
assert_eq!(perft(FEN_PERFT[1], 5)?, 193690690);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_perft_3() -> Result<(), String> {
|
||||
init_attacks();
|
||||
assert_eq!(perft(FEN_PERFT[2], 5)?, 674624);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_perft_4() -> Result<(), String> {
|
||||
init_attacks();
|
||||
assert_eq!(perft(FEN_PERFT[3], 5)?, 15833292);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_perft_5() -> Result<(), String> {
|
||||
init_attacks();
|
||||
assert_eq!(perft(FEN_PERFT[4], 5)?, 89941194);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_perft_6() -> Result<(), String> {
|
||||
init_attacks();
|
||||
assert_eq!(perft(FEN_PERFT[5], 5)?, 164075551);
|
||||
|
||||
Ok(())
|
||||
assert_eq!(nodes, expected_nodes);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user