From 32034c9c07eaf291d37594b13b15635cec35fc18 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Wed, 7 Aug 2024 14:44:56 +0200 Subject: [PATCH 1/2] fix: regression fixes and clippy findings --- src/game/mod.rs | 8 ++++---- src/solve/naive/mod.rs | 2 +- src/solve/naive/states.rs | 1 - src/wlist/mod.rs | 2 +- tests/solver.rs | 14 +++++++------- 5 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/game/mod.rs b/src/game/mod.rs index 9b34f65..ead7ee9 100644 --- a/src/game/mod.rs +++ b/src/game/mod.rs @@ -108,16 +108,16 @@ impl<'wl, WL: WordList> Game<'wl, WL> { if self.finished() || self.step > self.max_steps { return Err(GameError::TryingToPlayAFinishedGame); } - if self.wordlist.get_word(&guess).is_none() { + if self.wordlist.get_word(guess).is_none() { return Err(GameError::WordNotInWordlist(guess.to_string())); } self.step += 1; let response; if eval.is_some() && self.solution.is_none() { - response = GuessResponse::new(&guess, eval.unwrap(), self); + response = GuessResponse::new(guess, eval.unwrap(), self); } else if let Some(solution) = self.solution.clone() { - response = GuessResponse::new(&guess, Self::evaluate(solution, &guess), self); + response = GuessResponse::new(guess, Self::evaluate(solution, guess), self); } else { panic!("there is neither an evaluation nor a predefined solution for this guess"); } @@ -126,7 +126,7 @@ impl<'wl, WL: WordList> Game<'wl, WL> { } /// Generates an [Evaluation] for a given solution and guess. - pub(crate) fn evaluate(mut solution: WordData, guess: &Word) -> Evaluation { + pub(crate) fn evaluate(solution: WordData, guess: &Word) -> Evaluation { let mut evaluation = Vec::new(); let mut status: Status; let mut buf = solution.0.clone(); diff --git a/src/solve/naive/mod.rs b/src/solve/naive/mod.rs index e824dde..f3d94a2 100644 --- a/src/solve/naive/mod.rs +++ b/src/solve/naive/mod.rs @@ -3,7 +3,7 @@ use std::collections::HashMap; use libpt::log::{debug, error, info, trace}; use crate::error::{SolverError, WResult}; -use crate::game::evaluation::{Evaluation, EvaluationUnit}; +use crate::game::evaluation::{Evaluation}; use crate::wlist::word::{Word, WordData}; use crate::wlist::WordList; diff --git a/src/solve/naive/states.rs b/src/solve/naive/states.rs index 486d7e7..f7ee00d 100644 --- a/src/solve/naive/states.rs +++ b/src/solve/naive/states.rs @@ -2,7 +2,6 @@ use std::collections::{HashMap, HashSet}; use std::fmt::Debug; use std::ops::{Range, RangeBounds}; -use crate::error::WResult; use crate::wlist::word::Word; pub(crate) type CharMap = HashMap; diff --git a/src/wlist/mod.rs b/src/wlist/mod.rs index 57b2f8c..24f7324 100644 --- a/src/wlist/mod.rs +++ b/src/wlist/mod.rs @@ -94,7 +94,7 @@ pub trait WordList: Clone + std::fmt::Debug + Default + Sync + Display { buf } fn get_words_matching(&self, pattern: &str) -> WResult> { - let pattern = Regex::new(&pattern).map_err(WordlistError::from)?; + let pattern = Regex::new(pattern).map_err(WordlistError::from)?; let hay = self.raw_wordlist(); let keys = pattern.captures_iter(&hay); let mut buf = Vec::new(); diff --git a/tests/solver.rs b/tests/solver.rs index 3aa2634..5114211 100644 --- a/tests/solver.rs +++ b/tests/solver.rs @@ -85,49 +85,49 @@ 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")); game.guess( - next_guess.clone(), + &next_guess.clone(), Some(Evaluation::build(&next_guess, "xxffx")?), )?; next_guess = sl.guess_for(&game)?; assert_eq!(next_guess, Word::from("being")); game.guess( - next_guess.clone(), + &next_guess.clone(), Some(Evaluation::build(&next_guess, "xfffx")?), )?; next_guess = sl.guess_for(&game)?; assert_eq!(next_guess, Word::from("since")); game.guess( - next_guess.clone(), + &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(), + &next_guess.clone(), Some(Evaluation::build(&next_guess, "xcccc")?), )?; 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")); game.guess( - next_guess.clone(), + &next_guess.clone(), Some(Evaluation::build(&next_guess, "xcccc")?), )?; From 0a590598730610da9ab66199e8ade6dbf72a2da6 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Wed, 7 Aug 2024 14:59:43 +0200 Subject: [PATCH 2/2] test: remove predetermined tests, solver changes too often and there are better ways to test the solver --- tests/solver.rs | 118 ------------------------------------------------ 1 file changed, 118 deletions(-) diff --git a/tests/solver.rs b/tests/solver.rs index 5114211..86eaa43 100644 --- a/tests/solver.rs +++ b/tests/solver.rs @@ -19,121 +19,3 @@ fn test_build_builtin_solvers() { let _naive_solver = AnyBuiltinSolver::Naive(NaiveSolver::build(&wl).expect("could not build naive solver")); } - -#[test] -fn test_naive_play_predetermined_game() -> anyhow::Result<()> { - let wl = wordlist(); - let sl = - AnyBuiltinSolver::Naive(NaiveSolver::build(&wl).expect("could not build naive solver")); - let mut game = Game::build(5, false, 6, &wl, false)?; - game.set_solution(Some(("nines".into(), 0.002))); // The accuracy is made up but shouldn't - // matter - sl.make_a_move(&mut game)?; - assert_eq!( - game.responses().last().unwrap().guess(), - &Word::from("which") - ); - sl.make_a_move(&mut game)?; - assert_eq!( - game.responses().last().unwrap().guess(), - &Word::from("their") - ); - sl.make_a_move(&mut game)?; - assert_eq!( - game.responses().last().unwrap().guess(), - &Word::from("being") - ); - 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") - ); - sl.make_a_move(&mut game)?; - assert_eq!( - game.responses().last().unwrap().guess(), - &Word::from("mines") - ); - sl.make_a_move(&mut game)?; - assert_eq!( - game.responses().last().unwrap().guess(), - &Word::from("wines") - ); - - // naive is at the moment too bad to solve "nines" - assert!(game.finished()); - assert!(!game.won()); - - Ok(()) -} - -#[test] -fn test_naive_play_predetermined_game_manually() -> anyhow::Result<()> { - let wl = wordlist(); - let sl = - AnyBuiltinSolver::Naive(NaiveSolver::build(&wl).expect("could not build naive solver")); - // we don't insert the solution yet, - // pretend that a user inputs guesses manually - let mut game = Game::build(5, false, 6, &wl, false)?; - let _actual_solution: Option = Some(("nines".into(), 0.002)); - let mut next_guess: Word; - - next_guess = sl.guess_for(&game)?; - assert_eq!(next_guess, Word::from("which")); - game.guess( - &next_guess.clone(), - Some(Evaluation::build(&next_guess, "xxfxx")?), - )?; - - next_guess = sl.guess_for(&game)?; - assert_eq!(next_guess, Word::from("their")); - game.guess( - &next_guess.clone(), - Some(Evaluation::build(&next_guess, "xxffx")?), - )?; - - next_guess = sl.guess_for(&game)?; - assert_eq!(next_guess, Word::from("being")); - game.guess( - &next_guess.clone(), - Some(Evaluation::build(&next_guess, "xfffx")?), - )?; - - next_guess = sl.guess_for(&game)?; - assert_eq!(next_guess, Word::from("since")); - 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 = sl.guess_for(&game)?; - assert_eq!(next_guess, Word::from("mines")); - game.guess( - &next_guess.clone(), - Some(Evaluation::build(&next_guess, "xcccc")?), - )?; - - next_guess = sl.guess_for(&game)?; - assert_eq!(next_guess, Word::from("wines")); - game.guess( - &next_guess.clone(), - Some(Evaluation::build(&next_guess, "xcccc")?), - )?; - - // naive is at the moment too bad to solve "nines" - assert!(game.finished()); - assert!(!game.won()); - - Ok(()) -}