2 Commits

Author SHA1 Message Date
herel 9940608781 fixing the roll function 2022-05-03 16:36:19 +02:00
herel 6bd0f13526 Fix test 2022-05-03 16:14:56 +02:00
+29 -10
View File
@@ -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<Point> = 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,
@@ -447,13 +460,19 @@ mod tests {
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_at_start_proba = 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!(stats.infected < infected_at_start_proba + infected_tolerance);
assert!(stats.infected > infected_at_start_proba - infected_tolerance);
assert_eq!(stats.dead, 0);
population.propagate();
let infected_at_start = stats.infected;
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);