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 monobit;
|
||||
|
||||
use core::f64;
|
||||
|
||||
use bitvec::prelude::{BitOrder, BitSlice, BitStore};
|
||||
pub use block_frequency::*;
|
||||
pub use monobit::*;
|
||||
|
||||
use crate::RandomTest;
|
||||
|
||||
/// 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)]
|
||||
pub struct CombinedRandomTest(f64);
|
||||
pub struct CombinedRandomTest;
|
||||
|
||||
impl CombinedRandomTest {
|
||||
/// Creates a new combined random test
|
||||
pub fn new(p_threshold: f64) -> Self {
|
||||
Self(p_threshold)
|
||||
const fn max_of<const N: usize>(v: [usize; N]) -> usize {
|
||||
let mut m = v[0];
|
||||
let mut i = 1;
|
||||
while i < v.len() {
|
||||
if m < v[i] {
|
||||
m = v[i];
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
m
|
||||
}
|
||||
|
||||
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> {
|
||||
Monobit.evaluate(bs)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for CombinedRandomTest {
|
||||
fn default() -> Self {
|
||||
Self(0.01)
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -47,7 +54,7 @@ mod tests {
|
|||
use super::CombinedRandomTest;
|
||||
let mut rng = rand::thread_rng();
|
||||
assert!(
|
||||
CombinedRandomTest::default()
|
||||
CombinedRandomTest
|
||||
.evaluate_rng(&mut rng)
|
||||
.unwrap_or_default()
|
||||
> 0.01
|
||||
|
|
Loading…
Reference in a new issue