1
0
Fork 0

update interface for text records

This commit is contained in:
Paul-Nicolas Madelaine 2025-10-23 23:34:31 +02:00
parent 0d8b9cc9cf
commit 149aa841c9
5 changed files with 198 additions and 179 deletions

View file

@ -13,13 +13,7 @@ static P6: &'static str = "r4rk1/1pp1qppp/p1np1n2/2b1p1B1/2B1P1b1/P1NP1N2/1PP1QP
fn recursive_check_aux(position: Position, depth: usize) {
assert_eq!(
position,
position
.as_setup()
.to_string()
.parse::<Setup>()
.unwrap()
.into_position()
.unwrap(),
Position::from_text_record(&position.as_setup().to_text_record()).unwrap(),
);
if let Some(passed) = position.pass() {
@ -75,7 +69,7 @@ fn recursive_check_aux(position: Position, depth: usize) {
}
}
fn recursive_check(record: &str) {
recursive_check_aux(record.parse::<Setup>().unwrap().into_position().unwrap(), 4);
recursive_check_aux(Position::from_text_record(record).unwrap(), 4);
}
#[test]
fn recursive_check_1() {
@ -104,34 +98,29 @@ fn recursive_check_6() {
#[test]
fn setup() {
assert_eq!(Position::new().as_setup().to_string(), P1);
assert_eq!(Setup::new().to_string(), "8/8/8/8/8/8/8/8 w - -");
assert_eq!(Position::new().as_setup().to_text_record(), P1);
assert_eq!(Setup::new().to_text_record(), "8/8/8/8/8/8/8/8 w - -");
assert_eq!(
"8/8/8/8/1Pp5/8/R1k5/K7 w - b3"
.parse::<Setup>()
Setup::from_text_record("8/8/8/8/1Pp5/8/R1k5/K7 w - b3")
.unwrap()
.to_string(),
.to_text_record(),
"8/8/8/8/1Pp5/8/R1k5/K7 w - b3",
);
for (record, err) in [
("", ParseSetupError::InvalidBoard),
(" w - -", ParseSetupError::InvalidBoard),
("8/8/8/8/8/8/8 w - -", ParseSetupError::InvalidBoard),
("1/1/1/1/1/1/1/1 w - -", ParseSetupError::InvalidBoard),
(
"44/44/44/44/44/44/44/44 w - -",
ParseSetupError::InvalidBoard,
),
("8/8/8/8/8/8/8/8/8 w - -", ParseSetupError::InvalidBoard),
("p8/8/8/8/8/8/8/8 w - -", ParseSetupError::InvalidBoard),
("8/8/8/8/8/8/8/8 - - - ", ParseSetupError::InvalidTurn),
(
"8/8/8/8/8/8/8/8 w QQQQ -",
ParseSetupError::InvalidCastlingRights,
),
for (record, byte) in [
("", 0),
(" w - -", 1),
("8/8/8/8/8/8/8 w - -", 14),
("1/1/1/1/1/1/1/1 w - -", 2),
("44/44/44/44/44/44/44/44 w - -", 2),
("8/8/8/8/8/8/8/8/8 w - -", 16),
("p8/8/8/8/8/8/8/8 w - -", 2),
("8/8/8/8/8/8/8/8 - - - ", 17),
("8/8/8/8/8/8/8/8 w QQQQ -", 20),
] {
assert_eq!(record.parse::<Setup>(), Err(err), "{record}");
let res = Setup::from_text_record(record);
assert!(res.is_err());
assert_eq!(res.unwrap_err().byte, byte, "{record}");
}
for (record, reason) in [
(
@ -192,12 +181,12 @@ fn setup() {
),
] {
assert!(
record.parse::<Setup>().map(|record| record.to_string()) == Ok(record.to_string()),
Setup::from_text_record(record).map(|setup| setup.to_text_record())
== Ok(record.to_string()),
"{record}",
);
assert!(
record
.parse::<Setup>()
Setup::from_text_record(record)
.unwrap()
.into_position()
.is_err_and(|e| e.reasons.contains(reason)),
@ -212,28 +201,34 @@ fn setup() {
"3kr3/8/8/8/4P3/8/8/4K3 b - e3",
"8/8/8/3k4/3P4/8/8/3RK3 b - d3",
] {
assert!(record.parse::<Setup>().is_ok(), "{record}");
assert!(Setup::from_text_record(record).is_ok(), "{record}");
}
assert_eq!(
Setup::from_text_record(
"p1p1p1p1/p1p1p1p1/p1p1p1p1/p1p1p1p1/p1p1p1p1/p1p1p1p1/p1p1p1p1/p1p1p1p1 w KQkq a1"
)
.unwrap()
.to_text_record()
.len(),
81
);
}
#[test]
fn mirror() {
assert_eq!(Position::new().pass(), Some(Position::new().mirror()));
let position = "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b Kq e3"
.parse::<Setup>()
.unwrap()
.into_position()
.unwrap();
let mirror = "rnbqkbnr/pppp1ppp/8/4p3/8/8/PPPPPPPP/RNBQKBNR w Qk e6"
.parse::<Setup>()
.unwrap()
.into_position()
.unwrap();
let position =
Position::from_text_record("rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b Kq e3")
.unwrap();
let mirror =
Position::from_text_record("rnbqkbnr/pppp1ppp/8/4p3/8/8/PPPPPPPP/RNBQKBNR w Qk e6")
.unwrap();
assert_eq!(mirror, position.mirror());
}
fn perft_aux(record: &str, tests: &[u128]) {
let position = record.parse::<Setup>().unwrap().into_position().unwrap();
let position = Position::from_text_record(record).unwrap();
for (depth, value) in tests.iter().copied().enumerate() {
assert_eq!(
position.perft(depth),
@ -272,11 +267,7 @@ fn perft_6() {
#[test]
fn san() {
let position = "8/2KN1p2/5p2/3N1B1k/5PNp/7P/7P/8 w - -"
.parse::<Setup>()
.unwrap()
.into_position()
.unwrap();
let position = Position::from_text_record("8/2KN1p2/5p2/3N1B1k/5PNp/7P/7P/8 w - -").unwrap();
let san1 = "N7xf6#".parse::<San>().unwrap();
let m1 = san1.to_move(&position).unwrap();
let san2 = "N5xf6#".parse::<San>().unwrap();