From 05197181d045ef10fc775a2bf410e6a63cbf1b5e Mon Sep 17 00:00:00 2001 From: "Christoph J. Scherr" Date: Fri, 2 Aug 2024 17:37:50 +0200 Subject: [PATCH] test(game): test the evaluation --- tests/game.rs | 57 +++++++++++++++++++++++++++++++++++++++++++ tests/solver.rs | 64 ++++++++++++++++++++++++------------------------- 2 files changed, 88 insertions(+), 33 deletions(-) create mode 100644 tests/game.rs diff --git a/tests/game.rs b/tests/game.rs new file mode 100644 index 0000000..dd658b1 --- /dev/null +++ b/tests/game.rs @@ -0,0 +1,57 @@ +use wordle_analyzer::error::GameError; +use wordle_analyzer::game::evaluation::Evaluation; +use wordle_analyzer::game::response::GuessResponse; +use wordle_analyzer::wlist::builtin::BuiltinWList; +use wordle_analyzer::wlist::WordList; + +use wordle_analyzer::wlist::word::Word; +use wordle_analyzer::{self, game}; + +fn wordlist() -> impl WordList { + BuiltinWList::default() +} + +#[test] +fn test_eval_simple() -> anyhow::Result<()> { + let wl = wordlist(); + let builder = game::Game::builder(&wl) + .length(5) + .max_steps(6) + .solution(Some(wl.get_word(&Word::from("crate")).unwrap())) + .precompute(false); + + let mut game = builder.build()?; + let guess = Word::from("slate"); + game.guess(&guess, None)?; + assert_eq!( + *game.last_response().unwrap().evaluation(), + Evaluation::build(&guess, "xxccc")? + ); + Ok(()) +} + +#[test] +fn test_eval_reoccuring_char() -> anyhow::Result<()> { + let wl = wordlist(); + let builder = game::Game::builder(&wl) + .solution(Some(wl.get_word(&Word::from("nines")).unwrap())) + .precompute(false); + + let mut game = builder.build()?; + let guess = Word::from("pines"); + game.guess(&guess, None)?; + assert_eq!( + *game.last_response().unwrap().evaluation(), + Evaluation::build(&guess, "xcccc")? + ); + + let mut game = builder.build()?; + let guess = Word::from("sides"); + game.guess(&guess, None)?; + assert_eq!( + *game.last_response().unwrap().evaluation(), + Evaluation::build(&guess, "xcxcc")? + ); + + Ok(()) +} diff --git a/tests/solver.rs b/tests/solver.rs index 3aa2634..ac93ef5 100644 --- a/tests/solver.rs +++ b/tests/solver.rs @@ -36,22 +36,17 @@ fn test_naive_play_predetermined_game() -> anyhow::Result<()> { sl.make_a_move(&mut game)?; assert_eq!( game.responses().last().unwrap().guess(), - &Word::from("their") + &Word::from("first") ); sl.make_a_move(&mut game)?; assert_eq!( game.responses().last().unwrap().guess(), - &Word::from("being") + &Word::from("lives") ); sl.make_a_move(&mut game)?; assert_eq!( game.responses().last().unwrap().guess(), - &Word::from("since") - ); - sl.make_a_move(&mut game)?; - assert_eq!( - game.responses().last().unwrap().guess(), - &Word::from("lines") + &Word::from("sides") ); sl.make_a_move(&mut game)?; assert_eq!( @@ -61,12 +56,16 @@ fn test_naive_play_predetermined_game() -> anyhow::Result<()> { sl.make_a_move(&mut game)?; assert_eq!( game.responses().last().unwrap().guess(), - &Word::from("wines") + &Word::from("pines") + ); + sl.make_a_move(&mut game)?; + assert_eq!( + game.responses().last().unwrap().guess(), + &Word::from("nines") ); - // naive is at the moment too bad to solve "nines" assert!(game.finished()); - assert!(!game.won()); + assert!(game.won()); Ok(()) } @@ -85,55 +84,54 @@ fn test_naive_play_predetermined_game_manually() -> anyhow::Result<()> { next_guess = sl.guess_for(&game)?; assert_eq!(next_guess, Word::from("which")); game.guess( - next_guess.clone(), + &next_guess.clone(), Some(Evaluation::build(&next_guess, "xxfxx")?), )?; next_guess = sl.guess_for(&game)?; - assert_eq!(next_guess, Word::from("their")); + assert_eq!(next_guess, Word::from("first")); game.guess( - next_guess.clone(), - Some(Evaluation::build(&next_guess, "xxffx")?), + &next_guess.clone(), + Some(Evaluation::build(&next_guess, "xcxfx")?), )?; next_guess = sl.guess_for(&game)?; - assert_eq!(next_guess, Word::from("being")); + assert_eq!(next_guess, Word::from("lives")); game.guess( - next_guess.clone(), - Some(Evaluation::build(&next_guess, "xfffx")?), + &next_guess.clone(), + Some(Evaluation::build(&next_guess, "xcxcc")?), )?; next_guess = sl.guess_for(&game)?; - assert_eq!(next_guess, Word::from("since")); + assert_eq!(next_guess, Word::from("sides")); game.guess( - next_guess.clone(), - Some(Evaluation::build(&next_guess, "fcfxf")?), - )?; - - next_guess = sl.guess_for(&game)?; - assert_eq!(next_guess, Word::from("lines")); - game.guess( - next_guess.clone(), - Some(Evaluation::build(&next_guess, "xcccc")?), + &next_guess.clone(), + Some(Evaluation::build(&next_guess, "xcxcc")?), )?; next_guess = sl.guess_for(&game)?; assert_eq!(next_guess, Word::from("mines")); game.guess( - next_guess.clone(), + &next_guess.clone(), Some(Evaluation::build(&next_guess, "xcccc")?), )?; next_guess = sl.guess_for(&game)?; - assert_eq!(next_guess, Word::from("wines")); + assert_eq!(next_guess, Word::from("pines")); game.guess( - next_guess.clone(), + &next_guess.clone(), Some(Evaluation::build(&next_guess, "xcccc")?), )?; - // naive is at the moment too bad to solve "nines" + next_guess = sl.guess_for(&game)?; + assert_eq!(next_guess, Word::from("nines")); + game.guess( + &next_guess.clone(), + Some(Evaluation::build(&next_guess, "ccccc")?), + )?; + assert!(game.finished()); - assert!(!game.won()); + assert!(game.won()); Ok(()) }