From a5c27e42b438aa3c6727929fbb948e413cdbae36 Mon Sep 17 00:00:00 2001 From: "Dr. Matthias Ratajczak" Date: Tue, 27 Sep 2022 18:33:46 +0200 Subject: [PATCH] add tests for 84 --- src/euler84/Cargo.toml | 3 +++ src/euler84/src/lib.rs | 56 +++++++++++++++++++++++++++++++++++++++--- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/src/euler84/Cargo.toml b/src/euler84/Cargo.toml index b532ff6..41b030e 100644 --- a/src/euler84/Cargo.toml +++ b/src/euler84/Cargo.toml @@ -9,3 +9,6 @@ edition = "2021" rand = "0.8.5" strum = "0.24.1" strum_macros = "0.24.1" + +[profile.release] +debug = true diff --git a/src/euler84/src/lib.rs b/src/euler84/src/lib.rs index db9335a..786152b 100644 --- a/src/euler84/src/lib.rs +++ b/src/euler84/src/lib.rs @@ -122,7 +122,6 @@ impl Monopoly { let t1 = iter.next().unwrap(); let t2 = iter.next().unwrap(); let is_double = t1 == t2; - // dbg!(t1, t2); (t1 + t2, is_double) } @@ -179,7 +178,6 @@ impl Monopoly { fn handle_chance(&mut self, square: Squares) -> Squares { let card = self.draw_from_chance_pile_and_put_at_bottom(); - // dbg!(card); match card { Chance::AdvanceToGo => self.advance_to_go(), Chance::GoToJail => self.go_to_jail(), @@ -240,7 +238,7 @@ impl Monopoly { } fn go_back_3_squares(&mut self) -> Squares { - self.board.nth(NUMBER_SQUARES - 3).unwrap() + self.board.nth(NUMBER_SQUARES - 4).unwrap() } fn check_too_many_doubles(&mut self, is_double: bool) -> bool { @@ -252,6 +250,11 @@ impl Monopoly { false } } + + #[cfg(test)] + fn advance_by_number(&mut self, number: usize) -> Squares { + self.board.nth(number).unwrap() + } } pub struct Statistics { @@ -288,3 +291,50 @@ impl Statistics { } } } + +#[cfg(test)] +pub mod test { + use super::{Monopoly, Squares}; + + #[test] + fn jail() { + let mut monopoly = Monopoly::new(); + assert_eq!(monopoly.go_to_jail(), Squares::Jail); + assert_eq!(monopoly.go_to_jail(), Squares::Jail); + } + + #[test] + fn utility() { + let mut monopoly = Monopoly::new(); + assert_eq!(monopoly.go_to_utility(), Squares::U1); + assert_eq!(monopoly.go_to_utility(), Squares::U2); + assert_eq!(monopoly.go_to_utility(), Squares::U1); + } + + #[test] + fn railway() { + let mut monopoly = Monopoly::new(); + assert_eq!(monopoly.go_to_railway(), Squares::R1); + assert_eq!(monopoly.go_to_railway(), Squares::R2); + assert_eq!(monopoly.go_to_railway(), Squares::R3); + assert_eq!(monopoly.go_to_railway(), Squares::R4); + assert_eq!(monopoly.go_to_railway(), Squares::R1); + } + + #[test] + fn go() { + let mut monopoly = Monopoly::new(); + assert_eq!(monopoly.advance_to_go(), Squares::Go); + assert_eq!(monopoly.advance_to_go(), Squares::Go); + } + + #[test] + fn back_3() { + let mut monopoly = Monopoly::new(); + assert_eq!(monopoly.advance_by_number(3) as isize, 3); + assert_eq!(monopoly.go_back_3_squares(), Squares::Go); + + assert_eq!(monopoly.advance_by_number(27) as isize, 27); + assert_eq!(monopoly.go_back_3_squares() as isize, 24); + } +}