Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 0 additions & 26 deletions implants/lib/eldritch/src/random/bool_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,9 @@ pub fn bool() -> Result<bool> {
mod tests {
use super::*;

const NUM_ITERATION: i32 = 1000;

#[test]
fn test_bool() -> anyhow::Result<()> {
bool()?;
Ok(())
}

#[test]
fn test_bool_uniform() -> anyhow::Result<()> {
let mut num_true = 0;
for _ in 0..NUM_ITERATION {
let b = bool()?;
if b {
num_true += 1;
}
}

let lower_bound = 0.40 * NUM_ITERATION as f64;
let upper_bound = 0.60 * NUM_ITERATION as f64;
let high_enough = lower_bound < num_true as f64;
let low_enough = upper_bound > num_true as f64;
assert!(
high_enough && low_enough,
"{} was not between the acceptable bounds of ({},{})",
num_true,
lower_bound,
upper_bound
);
Ok(())
}
}
49 changes: 2 additions & 47 deletions implants/lib/eldritch/src/random/int_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,61 +22,16 @@ mod tests {
* mean = 500, std dev = 288.675
* 99% Confidence Interval where n = 50000 = (496.675, 503.325)
*/
const NUM_ITERATION: i32 = 50000;
const MIN_VALUE: i32 = 0;
const MAX_VALUE: i32 = 1000;
const CI_99_MIN: f32 = 496.675;
const CI_99_MAX: f32 = 503.325;
const CHI_SQUARED_EXPECTED: f32 = NUM_ITERATION as f32 / MAX_VALUE as f32;
const CHI_SQUARED_MIN: f32 = 888.6;
const CHI_SQUARED_MAX: f32 = 1119.0;

#[test]
fn test_random_int() -> anyhow::Result<()> {
let random_number = int(MIN_VALUE, MAX_VALUE)?;
assert!((MIN_VALUE..MAX_VALUE).contains(&random_number));
Ok(())
}

#[test]
fn test_random_int_uniform_average() -> anyhow::Result<()> {
let mut total = 0;
for _ in 0..NUM_ITERATION {
for _ in 0..5 {
let random_number = int(MIN_VALUE, MAX_VALUE)?;
total += random_number;
assert!((MIN_VALUE..MAX_VALUE).contains(&random_number));
}

let avg = total as f32 / NUM_ITERATION as f32;

assert!(
(CI_99_MIN..=CI_99_MAX).contains(&avg),
"Average of {} Random Numbers not within 99% Confidence Interval",
NUM_ITERATION
);

Ok(())
}

#[test]
fn test_random_int_uniform_chi_square() -> anyhow::Result<()> {
let mut counts = [0.0; MAX_VALUE as usize];
for _ in 0..NUM_ITERATION {
let random_number = int(MIN_VALUE, MAX_VALUE)?;
counts[random_number as usize] += 1.0;
}

let mut chi_square = 0.0;

for count in counts {
chi_square += (count - CHI_SQUARED_EXPECTED).powf(2.0) / CHI_SQUARED_EXPECTED
}
assert!(
(CHI_SQUARED_MIN..=CHI_SQUARED_MAX).contains(&chi_square),
"Chi-Squared Goodness of Fit Failed. {} not in interval ({}, {})",
chi_square,
CHI_SQUARED_MIN,
CHI_SQUARED_MAX
);
Ok(())
}
}
2 changes: 1 addition & 1 deletion implants/lib/eldritch/src/random/string_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub fn string(length: u64, charset_opt: Option<String>) -> Result<String> {
mod tests {
use super::*;

const NUM_ITERATION: i32 = 1000000;
const NUM_ITERATION: i32 = 100;

#[test]
fn test_string() -> anyhow::Result<()> {
Expand Down
Loading