From ac08002dacc0733bae8f518ab7eaa5f6ac7a6f2c Mon Sep 17 00:00:00 2001 From: Aner Ben Efraim Date: Sun, 31 Mar 2024 14:09:41 +0300 Subject: [PATCH] refactor: move functions to test_utils --- crates/committer/src/lib.rs | 31 +++++------------------------- crates/committer/src/test_utils.rs | 30 +++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 26 deletions(-) create mode 100644 crates/committer/src/test_utils.rs diff --git a/crates/committer/src/lib.rs b/crates/committer/src/lib.rs index 04dc8ba4..cd891ade 100644 --- a/crates/committer/src/lib.rs +++ b/crates/committer/src/lib.rs @@ -9,6 +9,8 @@ pub mod test { use std::time::Duration; use std::time::Instant; + use crate::test_utils::{mean, random_felt, std_deviation}; + use super::dummy; use pathfinder_crypto::Felt; use pathfinder_crypto::MontFelt; @@ -27,26 +29,6 @@ pub mod test { assert_eq!(dummy(), 7); } - fn mean(data: &[Duration]) -> Duration { - data.iter() - .sum::() - .checked_div(data.len().try_into().unwrap()) - .unwrap() - } - #[allow(clippy::as_conversions)] - fn std_deviation(data: &[Duration]) -> Duration { - let mean = mean(data).as_secs_f32(); - let mut variance = data - .iter() - .map(|x| { - let diff = (*x).as_secs_f32() - mean; - diff * diff - }) - .sum::(); - variance /= data.len() as f32; - Duration::from_secs_f32(variance.sqrt()) - } - //run with `cargo test --release -- --nocapture bench` #[test] fn bench() { @@ -184,12 +166,6 @@ pub mod test { // _leaf_data: Option<(ClassHash, ClassTreeNode)>, } - pub fn random_felt() -> Felt { - let mut buf: [u8; 32] = rand::thread_rng().gen(); - buf[0] &= 0x07; // clear the 5 most significant bits - Felt::from_be_bytes(buf).expect("Overflow ;(") - } - pub fn create_dummy_tree(height: u8) -> Option> { let node = match height { TREE_HEIGHT => SNTreeNode { @@ -433,3 +409,6 @@ pub mod test { ); } } + +#[cfg(any(feature = "testing", test))] +pub mod test_utils; diff --git a/crates/committer/src/test_utils.rs b/crates/committer/src/test_utils.rs new file mode 100644 index 00000000..b811f0f3 --- /dev/null +++ b/crates/committer/src/test_utils.rs @@ -0,0 +1,30 @@ +use pathfinder_crypto::Felt; +use rand::Rng; +use std::time::Duration; + +pub fn mean(data: &[Duration]) -> Duration { + data.iter() + .sum::() + .checked_div(data.len().try_into().unwrap()) + .unwrap() +} + +#[allow(clippy::as_conversions)] +pub fn std_deviation(data: &[Duration]) -> Duration { + let mean = mean(data).as_secs_f32(); + let mut variance = data + .iter() + .map(|x| { + let diff = (*x).as_secs_f32() - mean; + diff * diff + }) + .sum::(); + variance /= data.len() as f32; + Duration::from_secs_f32(variance.sqrt()) +} + +pub fn random_felt() -> Felt { + let mut buf: [u8; 32] = rand::thread_rng().gen(); + buf[0] &= 0x07; // clear the 5 most significant bits + Felt::from_be_bytes(buf).expect("Overflow ;(") +}