generated from ProcyOS/rust-template
add block frequency test to the combined test
This commit is contained in:
parent
ace180dc6f
commit
a33907d2c1
1 changed files with 24 additions and 17 deletions
41
src/tests.rs
41
src/tests.rs
|
@ -3,37 +3,44 @@
|
||||||
mod block_frequency;
|
mod block_frequency;
|
||||||
mod monobit;
|
mod monobit;
|
||||||
|
|
||||||
|
use core::f64;
|
||||||
|
|
||||||
use bitvec::prelude::{BitOrder, BitSlice, BitStore};
|
use bitvec::prelude::{BitOrder, BitSlice, BitStore};
|
||||||
|
pub use block_frequency::*;
|
||||||
pub use monobit::*;
|
pub use monobit::*;
|
||||||
|
|
||||||
use crate::RandomTest;
|
use crate::RandomTest;
|
||||||
|
|
||||||
/// This combines all supported tests into one
|
/// This combines all supported tests into one
|
||||||
///
|
|
||||||
/// Specifically it returns the lowest metric and stops if it goes below the p-value threshold.
|
|
||||||
#[derive(Copy, Clone, Debug)]
|
#[derive(Copy, Clone, Debug)]
|
||||||
pub struct CombinedRandomTest(f64);
|
pub struct CombinedRandomTest;
|
||||||
|
|
||||||
impl CombinedRandomTest {
|
const fn max_of<const N: usize>(v: [usize; N]) -> usize {
|
||||||
/// Creates a new combined random test
|
let mut m = v[0];
|
||||||
pub fn new(p_threshold: f64) -> Self {
|
let mut i = 1;
|
||||||
Self(p_threshold)
|
while i < v.len() {
|
||||||
|
if m < v[i] {
|
||||||
|
m = v[i];
|
||||||
}
|
}
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
m
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RandomTest for CombinedRandomTest {
|
impl RandomTest for CombinedRandomTest {
|
||||||
const RECOMMENDED_BIT_SIZE: usize = Monobit::RECOMMENDED_BIT_SIZE;
|
const RECOMMENDED_BIT_SIZE: usize = max_of([
|
||||||
|
Monobit::RECOMMENDED_BIT_SIZE,
|
||||||
|
BlockFrequency::RECOMMENDED_BIT_SIZE,
|
||||||
|
]);
|
||||||
|
|
||||||
const MINIMUM_BIT_SIZE: usize = Monobit::MINIMUM_BIT_SIZE;
|
const MINIMUM_BIT_SIZE: usize =
|
||||||
|
max_of([Monobit::MINIMUM_BIT_SIZE, BlockFrequency::MINIMUM_BIT_SIZE]);
|
||||||
|
|
||||||
fn evaluate<T: BitStore, O: BitOrder>(&self, bs: &BitSlice<T, O>) -> Option<f64> {
|
fn evaluate<T: BitStore, O: BitOrder>(&self, bs: &BitSlice<T, O>) -> Option<f64> {
|
||||||
Monobit.evaluate(bs)
|
let mut worst_test_score = f64::MAX;
|
||||||
}
|
worst_test_score = worst_test_score.min(Monobit.evaluate(bs)?);
|
||||||
}
|
worst_test_score = worst_test_score.min(BlockFrequency.evaluate(bs)?);
|
||||||
|
Some(worst_test_score)
|
||||||
impl Default for CombinedRandomTest {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self(0.01)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,7 +54,7 @@ mod tests {
|
||||||
use super::CombinedRandomTest;
|
use super::CombinedRandomTest;
|
||||||
let mut rng = rand::thread_rng();
|
let mut rng = rand::thread_rng();
|
||||||
assert!(
|
assert!(
|
||||||
CombinedRandomTest::default()
|
CombinedRandomTest
|
||||||
.evaluate_rng(&mut rng)
|
.evaluate_rng(&mut rng)
|
||||||
.unwrap_or_default()
|
.unwrap_or_default()
|
||||||
> 0.01
|
> 0.01
|
||||||
|
|
Loading…
Reference in a new issue