3 Commits

Author SHA1 Message Date
herel d528ae76a0 remove output 2022-05-04 14:56:40 +02:00
herel 600976cc6a add test for harmless disease 2022-05-04 00:42:45 +02:00
herel 0f2702a5c0 less frequest cast with size 2022-05-04 00:21:10 +02:00
2 changed files with 38 additions and 14 deletions
-1
View File
@@ -24,7 +24,6 @@ fn main() {
println!("After Filling");
//population.display();
//population.display();
println!("After Propagation");
let mut stats: [i32; 4];
// = [0,0,0,0];
let mut counter: u32 = 0;
+38 -13
View File
@@ -15,6 +15,7 @@ pub struct Population {
pub height: i32,
pub age: i32,
pub plague: Disease,
pub size: usize,
}
pub fn human_idx(x: i32, y: i32, width: i32) -> usize {
@@ -32,28 +33,30 @@ impl Population {
) -> Self {
let mut rng = rand::thread_rng();
let size: usize = (width * height) as usize;
let mut the_humans: Vec<Human> = vec![
Human {
x: 0,
y: 0,
present_state: State::Normal
};
(width * height) as usize
size
];
for x in 0..width {
for y in 0..height {
let idx = human_idx(x, y, width);
let mut present_state = State::Normal;
if (start_infected_ratio) > 0
&& (rng.gen_range(0..CORRECTED_PERCENTAGE) <= start_infected_ratio as i32)
if (start_infected_ratio > 0)
&& (rng.gen_range(0..CORRECTED_PERCENTAGE) <= start_infected_ratio)
{
present_state = State::Infected;
} else if start_immune_ratio > 0
&& rng.gen_range(0..CORRECTED_PERCENTAGE) <= start_immune_ratio as i32
} else if (start_immune_ratio > 0)
&& (rng.gen_range(0..CORRECTED_PERCENTAGE) <= start_immune_ratio)
{
present_state = State::Immune;
} else if start_dead_ratio > 0
&& rng.gen_range(0..CORRECTED_PERCENTAGE) <= start_dead_ratio as i32
} else if (start_dead_ratio > 0)
&& (rng.gen_range(0..CORRECTED_PERCENTAGE) <= start_dead_ratio)
{
present_state = State::Dead;
}
@@ -69,6 +72,7 @@ impl Population {
plague: plague,
age: 0,
humans: the_humans,
size: size,
}
}
// pub fn change_disease(&mut self, plague:Disease){
@@ -98,15 +102,15 @@ impl Population {
pub fn propagate(&mut self) -> [i32; 4] {
let mut people_to_check: Vec<Point> =
Vec::with_capacity((self.width * self.height) as usize);
Vec::with_capacity(self.size);
let mut possible_infected: Vec<Point> =
Vec::with_capacity((self.width * self.height) as usize);
Vec::with_capacity(self.size);
let mut people_to_infect: Vec<Point> =
Vec::with_capacity((self.width * self.height) as usize);
Vec::with_capacity(self.size);
let mut people_to_cure: Vec<Point> =
Vec::with_capacity((self.width * self.height) as usize);
Vec::with_capacity(self.size);
let mut people_to_kill: Vec<Point> =
Vec::with_capacity((self.width * self.height) as usize);
Vec::with_capacity(self.size);
let mut stats: [i32; 4] = [0, 0, 0, 0];
// stats[0] Normal stats[1] Infected stats[2] Immune stats[3] Dead
@@ -233,7 +237,7 @@ impl Population {
}
assert_eq!(
stats[0] + stats[1] + stats[2] + stats[3],
self.humans.len() as i32
self.size as i32
);
stats
}
@@ -639,6 +643,27 @@ mod tests {
}
}
#[parameterized(infection_start = {0, 50, 100})]
fn propagate_harmless(infection_start: i32) {
let disease: Disease = Disease::new(0, 0, 0, String::from("Harmless"));
let (width, height) = (100, 100);
let mut population: Population = Population::new(infection_start, 0, 0, width, height, disease);
let stats_before: Stats;
let stats_after: Stats;
stats_before = humans_stats(&population.humans);
let should_be_infected = population.size as i32 * infection_start / 100;
let tolerance = (should_be_infected as f32 * 0.20) as i32;
println!("{:?}", stats_before);
assert!(stats_before.infected <= should_be_infected + tolerance, "{} infected, should be less than {}", stats_before.infected, should_be_infected + tolerance);
assert!(stats_before.infected >= should_be_infected - tolerance, "{} infected, should be more than {}", stats_before.infected, should_be_infected - tolerance);
population.propagate();
stats_after = humans_stats(&population.humans);
assert_eq!(stats_before.infected, stats_after.infected, "no one should have been infected");
}
#[parameterized(infection_rate = {0, 100, 0}, death_rate = {0, 0, 100}, infected_expected = {0, 1, 0})]
fn propagate_test(infection_rate: i32, death_rate: i32, infected_expected: i32) {
let disease: Disease;