diff --git a/src/population.rs b/src/population.rs index 1ae1bba..6a97d9b 100644 --- a/src/population.rs +++ b/src/population.rs @@ -61,7 +61,6 @@ fn humans_stats(humans: &Vec) -> Stats { stats } - impl Population { pub fn new( start_infected_ratio: u32, @@ -100,11 +99,17 @@ impl Population { let mut rng = rand::thread_rng(); for x in self.humans.iter_mut() { - if (self.start_infected_ratio) > 0 && (rng.gen_range(0..CORRECTED_PERCENTAGE) <= self.start_infected_ratio as i32) { + if (self.start_infected_ratio) > 0 + && (rng.gen_range(0..CORRECTED_PERCENTAGE) <= self.start_infected_ratio as i32) + { x.present_state = State::Infected; - } else if self.start_immune_ratio > 0 && rng.gen_range(0..CORRECTED_PERCENTAGE) <= self.start_immune_ratio as i32 { + } else if self.start_immune_ratio > 0 + && rng.gen_range(0..CORRECTED_PERCENTAGE) <= self.start_immune_ratio as i32 + { x.present_state = State::Immune; - } else if self.start_dead_ratio > 0 && rng.gen_range(0..CORRECTED_PERCENTAGE) <= self.start_dead_ratio as i32 { + } else if self.start_dead_ratio > 0 + && rng.gen_range(0..CORRECTED_PERCENTAGE) <= self.start_dead_ratio as i32 + { x.present_state = State::Dead; } } @@ -361,7 +366,10 @@ mod tests { let population = Population::new(20, 10, 5, 5, 7, disease); assert_eq!(population.humans.len(), 5 * 7); for human in population.humans.iter() { - assert!(human.present_state == State::Normal, "all humans should be normal"); + assert!( + human.present_state == State::Normal, + "all humans should be normal" + ); } } @@ -415,31 +423,46 @@ mod tests { stats = humans_stats(&population.humans); println!("should be dead: {:?}", stats); assert_eq!(stats.dead, width * height); - } - #[parameterized(infection_rate = {0, 100}, infected_expected = {0, 1})] - fn propage_test(infection_rate: u32, infected_expected: i32) { + #[parameterized(infection_rate = {0, 100, 0}, death_rate = {0, 0, 100}, infected_expected = {0, 1, 1})] + fn propage_test( + infection_rate: u32, + death_rate: u32, + infected_expected: i32, + ) { let disease: Disease; let mut population: Population; let mut stats: Stats; let (width, height) = (100, 100); let start_infected = 50; - disease = Disease::new(infection_rate, 0, 0, String::from("Test")); - population = Population::new(start_infected, 0, 0, width, height, disease); - stats = humans_stats(&population.humans); - println!("Population: {:?}", stats); + println!("infection rate: {}, death_rate: {}", infection_rate, death_rate); - // total * proba - 20% < infected < total * proba + 20% - let expected_infected = width * height * start_infected as i32 / 100; - let tolerance = ((width * height) as f32 * 0.2) as i32; - assert!(stats.infected < expected_infected + tolerance); - assert!(stats.infected > expected_infected - tolerance); + disease = Disease::new(infection_rate, 0, death_rate, String::from("Test")); + population = Population::new(start_infected, 0, 0, width, height, disease); population.generate(); + stats = humans_stats(&population.humans); + println!("Population after generate: {:?}", stats); + + // total * proba - 20% < infected < total * proba + 20% + let infected_at_start = width * height * start_infected as i32 / 100; + let infected_tolerance = ((width * height) as f32 * 0.2) as i32; + assert!(stats.infected < infected_at_start + infected_tolerance); + assert!(stats.infected > infected_at_start - infected_tolerance); + assert_eq!(stats.dead, 0); + population.propagate(); stats = humans_stats(&population.humans); - assert!(stats.normal <= width * height * infected_expected); + println!("Population after propagate: {:?}", stats); + + assert!(stats.normal <= infected_at_start + width * height * infected_expected); + + let should_be_dead = infected_at_start * death_rate as i32 / 100; + let dead_tolerance = (should_be_dead as f32 * 0.20) as i32; + + assert!(stats.dead < should_be_dead + dead_tolerance); + assert!(stats.dead > should_be_dead - dead_tolerance); } }