21 Commits

Author SHA1 Message Date
herel ea74e1029b using Arc<Mutex<Vec<Human>>> 2022-05-04 20:37:40 +02:00
herel 6684ad2675 remove generate and add some testing 2022-05-04 14:57:55 +02:00
herel 3a41670fea check only normal people 2022-05-03 23:17:45 +02:00
herel fb6e9e82b3 remove unused code 2022-05-03 23:13:48 +02:00
herel bdb6e60e98 cargo fmt 2022-05-03 23:12:34 +02:00
herel a2bbd1d431 fix test propagate_infect_cure_all 2022-05-03 23:11:23 +02:00
herel b456a9fb63 human: remove new() trait 2022-05-03 23:10:54 +02:00
herel 89020eeb21 check surroundings instead 2022-05-03 23:01:48 +02:00
herel 5affe83fdc remove line 2022-05-03 22:29:51 +02:00
herel 7193fb2b16 use 1000 x 1000 2022-05-03 20:57:12 +02:00
herel 57db06ff9a move asserts to tests 2022-05-03 20:49:54 +02:00
herel 6aaede2208 fix population humans initialization 2022-05-03 20:46:55 +02:00
herel 1e26a78e3f main: display normals too 2022-05-03 20:45:40 +02:00
herel 87f0219b3b human: implement clone 2022-05-03 20:45:32 +02:00
herel dda944efff check angles 2022-05-03 18:37:53 +02:00
herel e6e67da8c1 remove casts and correct 0 limit situation on roll() 2022-05-03 16:52:12 +02:00
herel e4076cba12 take roll out of Population 2022-05-03 16:37:36 +02:00
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
herel 813c7668f6 verification 2022-05-03 16:12:39 +02:00
herel d0ae725d55 check infection rate first 2022-05-03 15:53:59 +02:00
4 changed files with 564 additions and 254 deletions
+5 -5
View File
@@ -5,14 +5,14 @@
// #[derive(Debug)] // #[derive(Debug)]
pub struct Disease { pub struct Disease {
pub infection_rate: u32, pub infection_rate: i32,
pub curing_rate: u32, pub curing_rate: i32,
pub death_rate: u32, pub death_rate: i32,
pub traits: Vec<u32>, pub traits: Vec<i32>,
pub name: String, pub name: String,
} }
impl Disease { impl Disease {
pub fn new(infection_r: u32, curing_r: u32, death_r: u32, the_name: String) -> Self { pub fn new(infection_r: i32, curing_r: i32, death_r: i32, the_name: String) -> Self {
Self { Self {
infection_rate: infection_r, infection_rate: infection_r,
curing_rate: curing_r, curing_rate: curing_r,
+19 -19
View File
@@ -1,33 +1,33 @@
// use crate::prelude::*; // use crate::prelude::*;
// #[derive(Copy, Clone, PartialEq)] // #[derive(Copy, Clone, PartialEq)]
#[derive(PartialEq)] #[derive(PartialEq, Debug, Clone)]
pub enum State { pub enum State {
Normal, Normal,
Infected, Infected,
Dead, Dead,
Immune, Immune,
} }
// #[derive(Clone)] #[derive(Clone)]
pub struct Human { pub struct Human {
pub present_state: State, pub present_state: State,
pub x: i32, pub x: i32,
pub y: i32, pub y: i32,
} }
impl Human { // impl Human {
// humans with state "Normal" // // humans with state "Normal"
pub fn new(pos_x: i32, pos_y: i32) -> Self { // pub fn new(pos_x: i32, pos_y: i32) -> Self {
Self { // Self {
present_state: State::Normal, // present_state: State::Normal,
x: pos_x, // x: pos_x,
y: pos_y, // y: pos_y,
} // }
} // }
// pub fn new_empty() -> Self{ // // pub fn new_empty() -> Self{
// Self{ // // Self{
// present_state:State::Normal, // // present_state:State::Normal,
// x : 0, // // x : 0,
// y : 0, // // y : 0,
// } // // }
// } // // }
} // }
+4 -9
View File
@@ -17,18 +17,13 @@ fn main() {
let term = Term::stdout(); let term = Term::stdout();
term.write_line("********** Rusty Propagation (Console) 2022 **********") term.write_line("********** Rusty Propagation (Console) 2022 **********")
.expect("Oops Looks like we have a problem here..."); .expect("Oops Looks like we have a problem here...");
term.write_line("Press any key to start the propagation")
.expect("Oops Looks like we have a problem here...");
let disease = Disease::new(20, 10, 5, String::from("Covid 44")); let disease = Disease::new(20, 10, 5, String::from("Covid 44"));
let mut population = Population::new(20, 10, 5, 300, 300, disease); let mut population = Population::new(20, 10, 5, 1000, 1000, disease);
//population.change_disease(disease); //population.change_disease(disease);
println!("Before Filling");
//population.display();
population.generate();
println!("After Filling"); println!("After Filling");
//population.display(); //population.display();
println!("After Propagation"); //population.display();
let mut stats: [i32; 4]; let mut stats: [i32; 4];
// = [0,0,0,0]; // = [0,0,0,0];
let mut counter: u32 = 0; let mut counter: u32 = 0;
@@ -37,8 +32,8 @@ fn main() {
stats = population.propagate(); stats = population.propagate();
//population.display(); //population.display();
println!( println!(
"Infecteds: {} Immunes: {} Deads: {}", "Normal: {} Infecteds: {} Immunes: {} Deads: {}",
stats[1], stats[2], stats[3] stats[0], stats[1], stats[2], stats[3]
); );
if stats[1] == 0 { if stats[1] == 0 {
break; break;
+513 -198
View File
@@ -1,80 +1,71 @@
use std::sync::Arc;
use std::sync::Mutex;
use crate::prelude::*; use crate::prelude::*;
use std::thread;
// #[derive(Debug)]
#[derive(Debug)]
pub struct Point { pub struct Point {
x: i32, x: i32,
y: i32, y: i32,
} }
#[derive(Debug)]
struct Stats {
normal: i32,
infected: i32,
immune: i32,
dead: i32,
}
impl Stats {
fn new() -> Stats {
Stats {
normal: 0,
infected: 0,
immune: 0,
dead: 0,
}
}
}
pub struct Population { pub struct Population {
pub start_infected_ratio: u32, pub start_infected_ratio: i32,
pub start_immune_ratio: u32, pub start_immune_ratio: i32,
pub start_dead_ratio: u32, pub start_dead_ratio: i32,
pub humans: Vec<Human>, pub humans: Arc<Mutex<Vec<Human>>>,
pub width: i32, pub width: i32,
pub height: i32, pub height: i32,
pub age: i32, pub age: i32,
pub plague: Disease, pub plague: Disease,
pub size: usize,
} }
pub fn human_idx(x: i32, y: i32, width: i32) -> usize { pub fn human_idx(x: i32, y: i32, width: i32) -> usize {
((y * width) + x) as usize ((y * width) + x) as usize
} }
fn humans_stats(humans: &Vec<Human>) -> Stats {
let mut stats: Stats = Stats::new();
for human in humans.iter() {
match human.present_state {
State::Normal => {
stats.normal += 1;
}
State::Infected => {
stats.infected += 1;
}
State::Immune => {
stats.immune += 1;
}
State::Dead => {
stats.dead += 1;
}
}
}
stats
}
impl Population { impl Population {
pub fn new( pub fn new(
start_infected_ratio: u32, start_infected_ratio: i32,
start_immune_ratio: u32, start_immune_ratio: i32,
start_dead_ratio: u32, start_dead_ratio: i32,
width: i32, width: i32,
height: i32, height: i32,
plague: Disease, plague: Disease,
) -> Self { ) -> Self {
let mut the_humans: Vec<Human> = Vec::with_capacity((width * height) as usize); let mut rng = rand::thread_rng();
let size: usize = (width * height) as usize;
let the_humans_arc = Arc::new(Mutex::new(vec![
Human {
x: 0,
y: 0,
present_state: State::Normal
};
size
]));
let the_humans = Arc::clone(&the_humans_arc);
for x in 0..width { for x in 0..width {
for y in 0..height { for y in 0..height {
the_humans.push(Human::new(x, y)); 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)
{
present_state = State::Infected;
} 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)
{
present_state = State::Dead;
}
the_humans.lock().unwrap()[idx] = Human{x: x, y: y, present_state: present_state};
} }
} }
Self { Self {
@@ -85,46 +76,56 @@ impl Population {
height: height, height: height,
plague: plague, plague: plague,
age: 0, age: 0,
humans: the_humans, humans: the_humans_arc,
size: size,
} }
} }
// pub fn change_disease(&mut self, plague:Disease){ // pub fn change_disease(&mut self, plague:Disease){
// self.plague = plague; // self.plague = plague;
// } // }
pub fn generate(&mut self) {
//The ratios will not be exact, for example someone who wants 100% infected 100% immune and 100% dead, he will have 100% dead because they are overwriting each others
//Maybe consider limiting the total to not exceed 100 in the view
//Other thing, there will always be more of the last one because someone who is already infected for example could be then put to immune or dead
//One solution to this issue would be to have if else else statements but in this case for example 20% would be lower with the last because its 20% on the remaining population and not of the all
//In other words I did it that way but it can be changed just its not the right method to have perfect ratios
let mut rng = rand::thread_rng();
for x in self.humans.iter_mut() { fn is_inside(&self, pos: &Point) -> bool {
if (self.start_infected_ratio) > 0 && (rng.gen_range(0..CORRECTED_PERCENTAGE) <= self.start_infected_ratio as i32) { if pos.x >= 0 && pos.x < self.width && pos.y >= 0 && pos.y < self.height {
x.present_state = State::Infected; true
} else if self.start_immune_ratio > 0 && rng.gen_range(0..CORRECTED_PERCENTAGE) <= self.start_immune_ratio as i32 { } else {
x.present_state = State::Immune; false
} else if self.start_dead_ratio > 0 && rng.gen_range(0..CORRECTED_PERCENTAGE) <= self.start_dead_ratio as i32 {
x.present_state = State::Dead;
} }
} }
fn is_inside_and_infected(&self, point: Point) -> bool {
let the_humans_arc = Arc::clone(&self.humans);
if self.is_inside(&point) {
let idx = human_idx(point.x, point.y, self.width);
let humans = the_humans_arc.lock().unwrap();
if humans[idx].present_state == State::Infected {
roll(self.plague.infection_rate)
} else {
false
}
} else {
false
}
} }
pub fn propagate(&mut self) -> [i32; 4] { pub fn propagate(&mut self) -> [i32; 4] {
let mut people_to_check: Vec<Point> = 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.size);
let mut people_to_infect: Vec<Point> = 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> = 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> = 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]; let mut stats: [i32; 4] = [0, 0, 0, 0];
// stats[0] Normal stats[1] Infected stats[2] Immune stats[3] Dead // stats[0] Normal stats[1] Infected stats[2] Immune stats[3] Dead
for h in self.humans.iter() { let humans = Arc::clone(&self.humans);
for h in humans.lock().unwrap().iter() {
match h.present_state { match h.present_state {
State::Normal => { State::Normal => {
possible_infected.push(Point{ x: h.x, y: h.y});
stats[0] += 1; stats[0] += 1;
} }
State::Infected => { State::Infected => {
@@ -145,152 +146,134 @@ impl Population {
//get all the other people next to me and check if i die cure or infect //get all the other people next to me and check if i die cure or infect
//now we can start to check if people would be infected or not //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); //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 roll(self.plague.curing_rate) {
if self.roll(self.plague.curing_rate) { //checks if the man recovers
//checks if the man dies
people_to_cure.push(Point { x: pos.x, y: pos.y }); people_to_cure.push(Point { x: pos.x, y: pos.y });
} else { } else {
if self.roll(self.plague.death_rate) { if roll(self.plague.death_rate) {
//cheks if the man dies //cheks if the man dies
people_to_kill.push(Point { x: pos.x, y: pos.y }); people_to_kill.push(Point { x: pos.x, y: pos.y });
} else { }
let mut possible_infections: Vec<Point> = Vec::with_capacity(8); }
// Vec::new(); }
//possible_infections.push(Point{x:pos.x,y:pos.y}); for pos in possible_infected.iter() {
let infected: bool = self.is_inside_and_infected(
possible_infections.push(Point { Point {
x: pos.x - 1, x: pos.x - 1,
y: pos.y - 1, y: pos.y - 1,
}); //Top Left },
possible_infections.push(Point { ) || //Top Left
self.is_inside_and_infected(
Point {
x: pos.x, x: pos.x,
y: pos.y - 1, y: pos.y - 1,
}); //Top },
possible_infections.push(Point { ) || //Top
self.is_inside_and_infected(
Point {
x: pos.x + 1, x: pos.x + 1,
y: pos.y - 1, y: pos.y - 1,
}); //Top Right },
) || //Top Right
possible_infections.push(Point { self.is_inside_and_infected(
Point {
x: pos.x - 1, x: pos.x - 1,
y: pos.y, y: pos.y,
}); //Left },
possible_infections.push(Point { ) || //Left
self.is_inside_and_infected(
Point {
x: pos.x + 1, x: pos.x + 1,
y: pos.y, y: pos.y,
}); //Right },
) || //Right
possible_infections.push(Point { self.is_inside_and_infected(
Point {
x: pos.x - 1, x: pos.x - 1,
y: pos.y + 1, y: pos.y + 1,
}); //Bottom Left },
possible_infections.push(Point { ) || //Bottom Left
self.is_inside_and_infected(
Point {
x: pos.x, x: pos.x,
y: pos.y + 1, y: pos.y + 1,
}); //Bottom },
possible_infections.push(Point { ) || //Bottom
self.is_inside_and_infected(
Point {
x: pos.x + 1, x: pos.x + 1,
y: pos.y + 1, y: pos.y + 1,
}); //Bottom Right },
); //Bottom Right
for poss_infected_pos in possible_infections.iter() { if infected {
//possible_infections.iter().map(|poss_infected_pos|{ people_to_infect.push(Point { x: pos.x, y: pos.y });
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) {
people_to_infect.push(Point {
x: poss_infected_pos.x,
y: poss_infected_pos.y,
});
} }
} }
// for infected_position in people_to_infect.iter() {
// // println!("To infect: {:?}", infected_position);
// //people_to_infect.iter().map(|infected_position|{
// let infected_index = human_idx(infected_position.x, infected_position.y, self.width);
// // let _ = infected_position.x;
// //DEBUG
// //println!("x: {} y: {} index: {}",infected_position.x,infected_position.y,infected_index);
// self.humans[infected_index].present_state = State::Infected;
// //DEBUG
// //println!("Infected someone");
// }
let mut threads = vec![];
{
let humans = Arc::clone(&self.humans);
let width = self.width;
threads.push(thread::spawn(move || {
for infected_position in people_to_infect.iter() {
let infected_index = human_idx(infected_position.x, infected_position.y, width);
humans.lock().unwrap()[infected_index].present_state = State::Infected;
} }
} }));
}
} else {
//TODO
//Check every special cases (corners sides etc..)
//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 });
// @##
// ###
// ###
// #@#
// ###
// ###
// ##@
// ###
// ###
// ###
// @##
// ###
// ###
// ##@
// ###
// ###
// ###
// @##
// ###
// ###
// #@#
// ###
// ###
// ##@
}
}
println!(
"{} to infect, {} to cure, {} to kill",
people_to_infect.len(),
people_to_cure.len(),
people_to_kill.len()
);
for infected_position in &people_to_infect {
// println!("To infect: {:?}", infected_position);
//people_to_infect.iter().map(|infected_position|{
let infected_index = human_idx(infected_position.x, infected_position.y, self.width);
// let _ = infected_position.x;
//DEBUG
//println!("x: {} y: {} index: {}",infected_position.x,infected_position.y,infected_index);
self.humans[infected_index].present_state = State::Infected;
//DEBUG
//println!("Infected someone");
} }
for cured_position in &people_to_cure { {
let humans = Arc::clone(&self.humans);
let width = self.width;
threads.push(thread::spawn(move || {
for cured_position in people_to_cure.iter() {
//people_to_cure.iter().map(|cured_position|{ //people_to_cure.iter().map(|cured_position|{
let cured_index = human_idx(cured_position.x, cured_position.y, self.width); let cured_index = human_idx(cured_position.x, cured_position.y, width);
self.humans[cured_index].present_state = State::Immune; if humans.lock().unwrap()[cured_index].present_state != State::Infected {
println!("not infected");
} else {
humans.lock().unwrap()[cured_index].present_state = State::Immune;
}
//DEBUG //DEBUG
//println!("Cured someone"); //println!("Cured someone");
} }
}));
}
for dead_position in &people_to_kill { for t in threads {
t.join().unwrap();
}
for dead_position in people_to_kill.iter() {
let humans = Arc::clone(&self.humans);
//people_to_kill.iter().map(|dead_position|{ //people_to_kill.iter().map(|dead_position|{
let dead_index = human_idx(dead_position.x, dead_position.y, self.width); let dead_index = human_idx(dead_position.x, dead_position.y, self.width);
if self.humans[dead_index].present_state == State::Dead { if humans.lock().unwrap()[dead_index].present_state == State::Dead {
println!("Already dead"); // println!("Already dead");
} else { } else {
self.humans[dead_index].present_state = State::Dead; humans.lock().unwrap()[dead_index].present_state = State::Dead;
} }
//DEBUG //DEBUG
} }
assert_eq!(
stats[0] + stats[1] + stats[2] + stats[3],
self.size as i32
);
stats stats
} }
pub fn roll(&self, probability: u32) -> bool {
let mut rng = rand::thread_rng();
rng.gen_range(0..CORRECTED_PERCENTAGE) <= probability as i32
}
// pub fn display(&mut self){ // pub fn display(&mut self){
// let sprite = "#"; // let sprite = "#";
// print!("\n"); // print!("\n");
@@ -310,11 +293,60 @@ impl Population {
// } // }
} }
pub fn roll(probability: i32) -> bool {
if probability > 0 {
let mut rng = rand::thread_rng();
rng.gen_range(0..CORRECTED_PERCENTAGE) <= probability
} else {
false
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use parameterized::parameterized; use parameterized::parameterized;
#[derive(Debug)]
struct Stats {
normal: i32,
infected: i32,
immune: i32,
dead: i32,
}
impl Stats {
fn new() -> Stats {
Stats {
normal: 0,
infected: 0,
immune: 0,
dead: 0,
}
}
}
fn humans_stats(humans: &Vec<Human>) -> Stats {
let mut stats: Stats = Stats::new();
for human in humans.iter() {
match human.present_state {
State::Normal => {
stats.normal += 1;
}
State::Infected => {
stats.infected += 1;
}
State::Immune => {
stats.immune += 1;
}
State::Dead => {
stats.dead += 1;
}
}
}
stats
}
#[parameterized(x = { #[parameterized(x = {
2, 3, 5 2, 3, 5
}, y = { }, y = {
@@ -334,7 +366,11 @@ mod tests {
let mut stats: Stats; let mut stats: Stats;
for _ in 0..10 { for _ in 0..10 {
humans.push(Human::new(0, 0)); humans.push(Human {
present_state: State::Normal,
x: 0,
y: 0,
});
} }
stats = humans_stats(&humans); stats = humans_stats(&humans);
assert_eq!(stats.normal, 10); assert_eq!(stats.normal, 10);
@@ -358,20 +394,24 @@ mod tests {
#[test] #[test]
fn population_new() { fn population_new() {
let disease = Disease::new(20, 10, 5, String::from("Covid 44")); let disease = Disease::new(20, 10, 5, String::from("Covid 44"));
let (width, height) = (5, 7);
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); let humans = Arc::clone(&population.humans);
for human in population.humans.iter() { assert_eq!(humans.lock().unwrap().len(), 5 * 7);
assert!(human.present_state == State::Normal, "all humans should be normal"); for h in humans.lock().unwrap().iter() {
let idx = human_idx(h.x, h.y, width);
assert_eq!(humans.lock().unwrap()[idx].x, h.x, "coordinates should match");
assert_eq!(humans.lock().unwrap()[idx].y, h.y, "coordinates should match");
} }
assert_eq!(humans.lock().unwrap().len(), (width * height) as usize);
} }
#[test] #[test]
fn population_gen() { fn population_gen() {
let disease = Disease::new(20, 10, 5, String::from("Covid 44")); let disease = Disease::new(20, 10, 5, String::from("Covid 44"));
let (width, height) = (5, 7); let (width, height) = (5, 7);
let mut population = Population::new(20, 10, 5, 5, 7, disease); let population = Population::new(20, 10, 5, 5, 7, disease);
population.generate();
let stats: Stats = humans_stats(&population.humans); let stats: Stats = humans_stats(&population.humans);
println!("Stats: {:?}", stats); println!("Stats: {:?}", stats);
@@ -390,48 +430,323 @@ mod tests {
disease = Disease::new(0, 0, 0, String::from("Test")); disease = Disease::new(0, 0, 0, String::from("Test"));
population = Population::new(0, 0, 0, width, height, disease); population = Population::new(0, 0, 0, width, height, disease);
population.generate();
stats = humans_stats(&population.humans); stats = humans_stats(&population.humans);
println!("should be normal: {:?}", stats); println!("should be normal: {:?}", stats);
assert_eq!(stats.normal, width * height); assert_eq!(stats.normal, width * height);
disease = Disease::new(0, 0, 0, String::from("Test")); disease = Disease::new(0, 0, 0, String::from("Test"));
population = Population::new(100, 0, 0, width, height, disease); population = Population::new(100, 0, 0, width, height, disease);
population.generate();
stats = humans_stats(&population.humans); stats = humans_stats(&population.humans);
println!("should be infected: {:?}", stats); println!("should be infected: {:?}", stats);
assert_eq!(stats.infected, width * height); assert_eq!(stats.infected, width * height);
disease = Disease::new(0, 0, 0, String::from("Test")); disease = Disease::new(0, 0, 0, String::from("Test"));
population = Population::new(0, 100, 0, width, height, disease); population = Population::new(0, 100, 0, width, height, disease);
population.generate();
stats = humans_stats(&population.humans); stats = humans_stats(&population.humans);
println!("should be immune: {:?}", stats); println!("should be immune: {:?}", stats);
assert_eq!(stats.immune, width * height); assert_eq!(stats.immune, width * height);
disease = Disease::new(0, 0, 0, String::from("Test")); disease = Disease::new(0, 0, 0, String::from("Test"));
population = Population::new(0, 0, 100, width, height, disease); population = Population::new(0, 0, 100, width, height, disease);
population.generate();
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(rate = {0, 100}, expected = {false, true})]
fn propage_test(infection_rate: u32, infected_expected: i32) { fn roll_test(rate: i32, expected: bool) {
let tries = 100000;
let mut result = 0;
println!("Testing roll, rate {}, expected {}", rate, expected);
for _x in 0..tries {
if roll(rate) == expected {
result += 1;
}
}
assert_eq!(result, tries);
}
#[test]
fn propagate_simple() {
let disease: Disease = Disease::new(0, 0, 100, String::from("Deadly"));
let mut population: Population = Population::new(100, 0, 0, 10, 10, disease);
let mut stats: Stats;
let mut propagate_stats: [i32; 4];
// infect every one
stats = humans_stats(&population.humans);
println!("stats after init: {:?}", stats);
assert_eq!(stats.infected, 100, "everybody should be infected");
// kill every one
propagate_stats = population.propagate();
stats = humans_stats(&population.humans);
println!("propate_stats: {:?}", propagate_stats);
assert_eq!(propagate_stats, [0, 100, 0, 0]);
assert_eq!(stats.normal, 0);
assert_eq!(stats.infected, 0);
assert_eq!(stats.immune, 0);
assert_eq!(stats.dead, 100);
for _x in 0..100 {
propagate_stats = population.propagate();
stats = humans_stats(&population.humans);
println!("propate_stats: {:?}", propagate_stats);
assert_eq!(propagate_stats, [0, 0, 0, 100]);
assert_eq!(stats.normal, 0);
assert_eq!(stats.infected, 0);
assert_eq!(stats.immune, 0);
assert_eq!(stats.dead, 100);
}
}
#[test]
fn propagate_infect_all() {
let disease: Disease = Disease::new(100, 0, 0, String::from("Deadly"));
let mut population: Population = Population::new(0, 0, 0, 3, 3, disease);
let mut stats: Stats;
let mut propagate_stats: [i32; 4];
// start with normal population
population.humans = vec![
Human {
present_state: State::Normal,
x: 0,
y: 0,
},
Human {
present_state: State::Normal,
x: 1,
y: 0,
},
Human {
present_state: State::Normal,
x: 2,
y: 0,
},
Human {
present_state: State::Normal,
x: 0,
y: 1,
},
Human {
present_state: State::Infected,
x: 1,
y: 1,
},
Human {
present_state: State::Normal,
x: 2,
y: 1,
},
Human {
present_state: State::Normal,
x: 0,
y: 2,
},
Human {
present_state: State::Normal,
x: 1,
y: 2,
},
Human {
present_state: State::Normal,
x: 2,
y: 2,
},
];
stats = humans_stats(&population.humans);
println!("stats after init: {:?}", stats);
assert_eq!(stats.normal, 8);
// kill every one
propagate_stats = population.propagate();
stats = humans_stats(&population.humans);
println!("propate_stats: {:?}", propagate_stats);
assert_eq!(propagate_stats, [8, 1, 0, 0]);
assert_eq!(stats.normal, 0);
assert_eq!(stats.infected, 9);
assert_eq!(stats.immune, 0);
assert_eq!(stats.dead, 0);
for _x in 0..100 {
propagate_stats = population.propagate();
stats = humans_stats(&population.humans);
println!("propate_stats: {:?}", propagate_stats);
assert_eq!(propagate_stats, [0, 9, 0, 0]);
assert_eq!(stats.normal, 0);
assert_eq!(stats.infected, 9);
assert_eq!(stats.immune, 0);
assert_eq!(stats.dead, 0);
}
}
#[test]
fn propagate_infect_cure_all() {
let disease: Disease = Disease::new(100, 100, 0, String::from("Deadly"));
let mut population: Population = Population::new(0, 0, 0, 3, 3, disease);
let mut stats: Stats;
let mut propagate_stats: [i32; 4];
// start with normal population
population.humans = vec![
Human {
present_state: State::Normal,
x: 0,
y: 0,
},
Human {
present_state: State::Normal,
x: 1,
y: 0,
},
Human {
present_state: State::Normal,
x: 2,
y: 0,
},
Human {
present_state: State::Normal,
x: 0,
y: 1,
},
Human {
present_state: State::Infected,
x: 1,
y: 1,
},
Human {
present_state: State::Normal,
x: 2,
y: 1,
},
Human {
present_state: State::Normal,
x: 0,
y: 2,
},
Human {
present_state: State::Normal,
x: 1,
y: 2,
},
Human {
present_state: State::Normal,
x: 2,
y: 2,
},
];
stats = humans_stats(&population.humans);
println!("stats after init: {:?}", stats);
assert_eq!(stats.normal, 8);
// infect every one
propagate_stats = population.propagate();
stats = humans_stats(&population.humans);
println!("propate_stats: {:?}", propagate_stats);
println!("population: {:?}", stats);
assert_eq!(propagate_stats, [8, 1, 0, 0]);
assert_eq!(stats.normal, 0);
assert_eq!(stats.infected, 8);
assert_eq!(stats.immune, 1);
assert_eq!(stats.dead, 0);
// cure every one
propagate_stats = population.propagate();
stats = humans_stats(&population.humans);
println!("propate_stats: {:?}", propagate_stats);
println!("population: {:?}", stats);
assert_eq!(propagate_stats, [0, 8, 1, 0]);
assert_eq!(stats.normal, 0);
assert_eq!(stats.infected, 0);
assert_eq!(stats.immune, 9);
assert_eq!(stats.dead, 0);
// then
for _x in 0..100 {
propagate_stats = population.propagate();
stats = humans_stats(&population.humans);
println!("propate_stats: {:?}", propagate_stats);
println!("population: {:?}", stats);
assert_eq!(propagate_stats, [0, 0, 9, 0]);
assert_eq!(stats.normal, 0);
assert_eq!(stats.infected, 0);
assert_eq!(stats.immune, 9);
assert_eq!(stats.dead, 0);
}
}
#[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; 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;
println!(
"infection rate: {}, death_rate: {}",
infection_rate, death_rate
);
disease = Disease::new(infection_rate, 0, death_rate, String::from("Test"));
population = Population::new(start_infected, 0, 0, width, height, disease);
disease = Disease::new(infection_rate, 0, 0, String::from("Test"));
population = Population::new(50, 0, 0, width, height, disease);
stats = humans_stats(&population.humans); stats = humans_stats(&population.humans);
println!("Population: {:?}", stats); println!("Population after init: {:?}", stats);
population.generate();
population.propagate(); // total * proba - 20% < infected < total * proba + 20%
let infected_at_start_proba = width * height * start_infected / 100;
let infected_tolerance = ((width * height) as f32 * 0.2) as i32;
assert!(stats.infected <= infected_at_start_proba + infected_tolerance);
assert!(stats.infected >= infected_at_start_proba - infected_tolerance);
assert_eq!(stats.dead, 0);
let infected_at_start = stats.infected;
let dead_at_start = stats.dead;
let propa_stats: [i32; 4] = population.propagate();
assert!(propa_stats[3] >= dead_at_start);
if death_rate == 0 {
assert_eq!(propa_stats[3], 0, "no human should have died");
}
stats = humans_stats(&population.humans); stats = humans_stats(&population.humans);
assert!(stats.normal <= width * height * infected_expected); println!("Population after propagate: {:?}", stats);
assert!(stats.infected <= infected_at_start + width * height * infected_expected);
let should_be_dead = infected_at_start * death_rate / 100;
let dead_tolerance = (should_be_dead as f32 * 0.20) as i32;
assert!(
stats.dead <= should_be_dead + dead_tolerance,
"death count should be less or equal than {}",
should_be_dead + dead_tolerance
);
assert!(stats.dead >= should_be_dead - dead_tolerance);
} }
} }