From 9940608781af90643238a6d96750852edc925ce9 Mon Sep 17 00:00:00 2001 From: Rene Luria Date: Tue, 3 May 2022 16:36:19 +0200 Subject: [PATCH] fixing the roll function --- src/population.rs | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/src/population.rs b/src/population.rs index c63ef11..dad61f7 100644 --- a/src/population.rs +++ b/src/population.rs @@ -151,12 +151,13 @@ impl Population { //now we can start to check if people would be infected or not //let idx = human_idx(pos.x as i32, pos.y as i32, self.width as i32); if pos.x > 0 && pos.x < self.width - 1 && pos.y > 0 && pos.y < self.height - 1 { - if self.roll(self.plague.curing_rate) { - //checks if the man dies + if Population::roll(self.plague.curing_rate) { + //checks if the man recovers people_to_cure.push(Point { x: pos.x, y: pos.y }); } else { - if self.roll(self.plague.death_rate) { + if Population::roll(self.plague.death_rate) { //cheks if the man dies + println!("someone should die"); people_to_kill.push(Point { x: pos.x, y: pos.y }); } else { let mut possible_infections: Vec = Vec::with_capacity(8); @@ -203,7 +204,7 @@ impl Population { let inf_idx = human_idx(poss_infected_pos.x, poss_infected_pos.y, self.width); if self.humans[inf_idx].present_state == State::Normal { - if self.roll(self.plague.infection_rate) { + if Population::roll(self.plague.infection_rate) { people_to_infect.push(Point { x: poss_infected_pos.x, y: poss_infected_pos.y, @@ -219,7 +220,7 @@ impl Population { //REMOVE WHEN IMPLEMENTED //It is here to prevent an infected in the borders to keep the programm running as he thinks there are still people to simulate - people_to_kill.push(Point { x: pos.x, y: pos.y }); + //people_to_kill.push(Point { x: pos.x, y: pos.y }); // @## // ### @@ -292,7 +293,7 @@ impl Population { } stats } - pub fn roll(&self, probability: u32) -> bool { + pub fn roll(probability: u32) -> bool { let mut rng = rand::thread_rng(); rng.gen_range(0..CORRECTED_PERCENTAGE) <= probability as i32 } @@ -425,6 +426,18 @@ mod tests { assert_eq!(stats.dead, width * height); } + #[parameterized(rate = {0, 100}, expected = {false, true})] + fn roll_test(rate: u32, expected: bool) { + let tries = 1000; + let mut result = 0; + for _x in 0..1000 { + if Population::roll(rate) == expected { + result += 1; + } + } + assert_eq!(result, tries); + } + #[parameterized(infection_rate = {0, 100, 0}, death_rate = {0, 0, 100}, infected_expected = {0, 1, 1})] fn propage_test( infection_rate: u32, @@ -455,7 +468,11 @@ mod tests { let infected_at_start = stats.infected; - population.propagate(); + let propa_stats: [i32; 4] = population.propagate(); + if death_rate == 0 { + assert_eq!(propa_stats[3], 0, "no human should have died"); + } + stats = humans_stats(&population.humans); println!("Population after propagate: {:?}", stats);