forked from Maxluli/RustyPropagation
verification
This commit is contained in:
+41
-18
@@ -61,7 +61,6 @@ fn humans_stats(humans: &Vec<Human>) -> Stats {
|
|||||||
stats
|
stats
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
impl Population {
|
impl Population {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
start_infected_ratio: u32,
|
start_infected_ratio: u32,
|
||||||
@@ -100,11 +99,17 @@ impl Population {
|
|||||||
let mut rng = rand::thread_rng();
|
let mut rng = rand::thread_rng();
|
||||||
|
|
||||||
for x in self.humans.iter_mut() {
|
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;
|
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;
|
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;
|
x.present_state = State::Dead;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -361,7 +366,10 @@ mod tests {
|
|||||||
let population = Population::new(20, 10, 5, 5, 7, disease);
|
let population = Population::new(20, 10, 5, 5, 7, disease);
|
||||||
assert_eq!(population.humans.len(), 5 * 7);
|
assert_eq!(population.humans.len(), 5 * 7);
|
||||||
for human in population.humans.iter() {
|
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);
|
stats = humans_stats(&population.humans);
|
||||||
println!("should be dead: {:?}", stats);
|
println!("should be dead: {:?}", stats);
|
||||||
assert_eq!(stats.dead, width * height);
|
assert_eq!(stats.dead, width * height);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[parameterized(infection_rate = {0, 100}, infected_expected = {0, 1})]
|
#[parameterized(infection_rate = {0, 100, 0}, death_rate = {0, 0, 100}, infected_expected = {0, 1, 1})]
|
||||||
fn propage_test(infection_rate: u32, infected_expected: i32) {
|
fn propage_test(
|
||||||
|
infection_rate: u32,
|
||||||
|
death_rate: u32,
|
||||||
|
infected_expected: i32,
|
||||||
|
) {
|
||||||
let disease: Disease;
|
let disease: Disease;
|
||||||
let mut population: Population;
|
let mut population: Population;
|
||||||
let mut stats: Stats;
|
let mut stats: Stats;
|
||||||
let (width, height) = (100, 100);
|
let (width, height) = (100, 100);
|
||||||
let start_infected = 50;
|
let start_infected = 50;
|
||||||
|
|
||||||
disease = Disease::new(infection_rate, 0, 0, String::from("Test"));
|
println!("infection rate: {}, death_rate: {}", infection_rate, death_rate);
|
||||||
population = Population::new(start_infected, 0, 0, width, height, disease);
|
|
||||||
stats = humans_stats(&population.humans);
|
|
||||||
println!("Population: {:?}", stats);
|
|
||||||
|
|
||||||
// total * proba - 20% < infected < total * proba + 20%
|
disease = Disease::new(infection_rate, 0, death_rate, String::from("Test"));
|
||||||
let expected_infected = width * height * start_infected as i32 / 100;
|
population = Population::new(start_infected, 0, 0, width, height, disease);
|
||||||
let tolerance = ((width * height) as f32 * 0.2) as i32;
|
|
||||||
assert!(stats.infected < expected_infected + tolerance);
|
|
||||||
assert!(stats.infected > expected_infected - tolerance);
|
|
||||||
|
|
||||||
population.generate();
|
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();
|
population.propagate();
|
||||||
stats = humans_stats(&population.humans);
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user