forked from Maxluli/RustyPropagation
Compare commits
4 Commits
with_fixed
...
theads-no-
| Author | SHA1 | Date | |
|---|---|---|---|
| a6d56af613 | |||
| 1abf452ce5 | |||
| ea74e1029b | |||
|
6684ad2675
|
144
Cargo.lock
generated
144
Cargo.lock
generated
@@ -2,18 +2,74 @@
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "atty"
|
||||
version = "0.2.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
|
||||
dependencies = [
|
||||
"hermit-abi",
|
||||
"libc",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "1.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "clap"
|
||||
version = "3.1.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "85a35a599b11c089a7f49105658d089b8f2cf0882993c17daf6de15285c2c35d"
|
||||
dependencies = [
|
||||
"atty",
|
||||
"bitflags",
|
||||
"clap_derive",
|
||||
"clap_lex",
|
||||
"indexmap",
|
||||
"lazy_static",
|
||||
"strsim",
|
||||
"termcolor",
|
||||
"textwrap",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_derive"
|
||||
version = "3.1.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a3aab4734e083b809aaf5794e14e756d1c798d2c69c7f7de7a09a2f5214993c1"
|
||||
dependencies = [
|
||||
"heck",
|
||||
"proc-macro-error",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_lex"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a37c35f1112dad5e6e0b1adaff798507497a18fceeb30cceb3bae7d1427b9213"
|
||||
dependencies = [
|
||||
"os_str_bytes",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "console"
|
||||
version = "0.15.0"
|
||||
@@ -52,6 +108,21 @@ version = "0.9.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d7afe4a420e3fe79967a00898cc1f4db7c8a49a9333a29f8a4bd76a253d5cd04"
|
||||
|
||||
[[package]]
|
||||
name = "heck"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9"
|
||||
|
||||
[[package]]
|
||||
name = "hermit-abi"
|
||||
version = "0.1.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "indexmap"
|
||||
version = "1.6.2"
|
||||
@@ -62,6 +133,12 @@ dependencies = [
|
||||
"hashbrown",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.124"
|
||||
@@ -74,6 +151,12 @@ version = "1.10.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "87f3e037eac156d1775da914196f0f37741a274155e34a0b7e427c35d2a2ecb9"
|
||||
|
||||
[[package]]
|
||||
name = "os_str_bytes"
|
||||
version = "6.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8e22443d1643a904602595ba1cd8f7d896afe56d26712531c5ff73a15b2fbf64"
|
||||
|
||||
[[package]]
|
||||
name = "parameterized"
|
||||
version = "1.0.0"
|
||||
@@ -101,6 +184,30 @@ version = "0.2.16"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro-error"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
|
||||
dependencies = [
|
||||
"proc-macro-error-attr",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"version_check",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro-error-attr"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"version_check",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.37"
|
||||
@@ -168,11 +275,18 @@ checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"
|
||||
name = "rusty_propagation"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"clap",
|
||||
"console",
|
||||
"parameterized",
|
||||
"rand",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "strsim"
|
||||
version = "0.10.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.92"
|
||||
@@ -184,6 +298,15 @@ dependencies = [
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "termcolor"
|
||||
version = "1.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755"
|
||||
dependencies = [
|
||||
"winapi-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "terminal_size"
|
||||
version = "0.1.17"
|
||||
@@ -194,6 +317,12 @@ dependencies = [
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "textwrap"
|
||||
version = "0.15.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-width"
|
||||
version = "0.1.9"
|
||||
@@ -206,6 +335,12 @@ version = "0.2.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "957e51f3646910546462e67d5f7599b9e4fb8acdd304b087a6494730f9eebf04"
|
||||
|
||||
[[package]]
|
||||
name = "version_check"
|
||||
version = "0.9.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.10.2+wasi-snapshot-preview1"
|
||||
@@ -228,6 +363,15 @@ version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||
|
||||
[[package]]
|
||||
name = "winapi-util"
|
||||
version = "0.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
|
||||
dependencies = [
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi-x86_64-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
|
||||
@@ -7,3 +7,4 @@ edition = "2021"
|
||||
console = "0.15.0"
|
||||
rand = "0.8.5"
|
||||
parameterized = "1.0.0"
|
||||
clap = { version = "3.0", features = ["derive"] }
|
||||
|
||||
16
src/main.rs
16
src/main.rs
@@ -1,3 +1,5 @@
|
||||
use clap::Parser;
|
||||
|
||||
mod disease;
|
||||
mod human;
|
||||
mod population;
|
||||
@@ -13,7 +15,14 @@ mod prelude {
|
||||
|
||||
use prelude::*;
|
||||
|
||||
#[derive(Parser)]
|
||||
struct Cli {
|
||||
/// Number of threads to use
|
||||
threads: usize,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args = Cli::parse();
|
||||
let term = Term::stdout();
|
||||
term.write_line("********** Rusty Propagation (Console) 2022 **********")
|
||||
.expect("Oops Looks like we have a problem here...");
|
||||
@@ -21,18 +30,15 @@ fn main() {
|
||||
let disease = Disease::new(20, 10, 5, String::from("Covid 44"));
|
||||
let mut population = Population::new(20, 10, 5, 1000, 1000, disease);
|
||||
//population.change_disease(disease);
|
||||
println!("Before Filling");
|
||||
//population.display();
|
||||
population.generate();
|
||||
println!("After Filling");
|
||||
//population.display();
|
||||
println!("After Propagation");
|
||||
//population.display();
|
||||
let mut stats: [i32; 4];
|
||||
// = [0,0,0,0];
|
||||
let mut counter: u32 = 0;
|
||||
loop {
|
||||
counter += 1;
|
||||
stats = population.propagate();
|
||||
stats = population.propagate(args.threads);
|
||||
//population.display();
|
||||
println!(
|
||||
"Normal: {} Infecteds: {} Immunes: {} Deads: {}",
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
448
src/tests.rs
Normal file
448
src/tests.rs
Normal file
@@ -0,0 +1,448 @@
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
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 = {
|
||||
2, 3, 5
|
||||
}, y = {
|
||||
1, 4, 0
|
||||
}, width = {
|
||||
3, 5, 7
|
||||
}, res = {
|
||||
5, 23, 5
|
||||
})]
|
||||
fn test_human_idx(x: i32, y: i32, width: i32, res: usize) {
|
||||
assert_eq!(human_idx(x, y, width), res);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_human_stats() {
|
||||
let mut humans: Vec<Human> = Vec::with_capacity(10);
|
||||
let mut stats: Stats;
|
||||
|
||||
for _ in 0..10 {
|
||||
humans.push(Human {
|
||||
present_state: State::Normal,
|
||||
x: 0,
|
||||
y: 0,
|
||||
});
|
||||
}
|
||||
stats = humans_stats(&humans);
|
||||
assert_eq!(stats.normal, 10);
|
||||
|
||||
for x in 0..2 {
|
||||
humans[x].present_state = State::Infected;
|
||||
}
|
||||
for x in 2..5 {
|
||||
humans[x].present_state = State::Immune;
|
||||
}
|
||||
for x in 5..9 {
|
||||
humans[x].present_state = State::Dead;
|
||||
}
|
||||
stats = humans_stats(&humans);
|
||||
assert_eq!(stats.normal, 1);
|
||||
assert_eq!(stats.infected, 2);
|
||||
assert_eq!(stats.immune, 3);
|
||||
assert_eq!(stats.dead, 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn population_new() {
|
||||
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 humans = Arc::clone(&population.humans);
|
||||
assert_eq!(humans.lock().unwrap().len(), 5 * 7);
|
||||
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]
|
||||
fn population_gen() {
|
||||
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 stats: Stats = humans_stats(&population.humans);
|
||||
println!("Stats: {:?}", stats);
|
||||
|
||||
assert_eq!(
|
||||
stats.normal + stats.infected + stats.immune + stats.dead,
|
||||
width * height
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn plague_init_stats() {
|
||||
let mut disease: Disease;
|
||||
let mut population: Population;
|
||||
let mut stats: Stats;
|
||||
let (width, height) = (5, 7);
|
||||
|
||||
disease = Disease::new(0, 0, 0, String::from("Test"));
|
||||
population = Population::new(0, 0, 0, width, height, disease);
|
||||
stats = humans_stats(&population.humans);
|
||||
println!("should be normal: {:?}", stats);
|
||||
assert_eq!(stats.normal, width * height);
|
||||
|
||||
disease = Disease::new(0, 0, 0, String::from("Test"));
|
||||
population = Population::new(100, 0, 0, width, height, disease);
|
||||
stats = humans_stats(&population.humans);
|
||||
println!("should be infected: {:?}", stats);
|
||||
assert_eq!(stats.infected, width * height);
|
||||
|
||||
disease = Disease::new(0, 0, 0, String::from("Test"));
|
||||
population = Population::new(0, 100, 0, width, height, disease);
|
||||
stats = humans_stats(&population.humans);
|
||||
println!("should be immune: {:?}", stats);
|
||||
assert_eq!(stats.immune, width * height);
|
||||
|
||||
disease = Disease::new(0, 0, 0, String::from("Test"));
|
||||
population = Population::new(0, 0, 100, width, height, disease);
|
||||
stats = humans_stats(&population.humans);
|
||||
println!("should be dead: {:?}", stats);
|
||||
assert_eq!(stats.dead, width * height);
|
||||
}
|
||||
|
||||
#[parameterized(rate = {0, 100}, expected = {false, true})]
|
||||
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 mut population: Population;
|
||||
let mut stats: Stats;
|
||||
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);
|
||||
|
||||
stats = humans_stats(&population.humans);
|
||||
println!("Population after init: {:?}", stats);
|
||||
|
||||
// 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);
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user