Skip to content

Commit

Permalink
refactor: move functions to test_utils
Browse files Browse the repository at this point in the history
  • Loading branch information
aner-starkware committed Mar 31, 2024
1 parent 5e380e7 commit ac08002
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 26 deletions.
31 changes: 5 additions & 26 deletions crates/committer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,26 +29,6 @@ pub mod test {
assert_eq!(dummy(), 7);
}

fn mean(data: &[Duration]) -> Duration {
data.iter()
.sum::<Duration>()
.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::<f32>();
variance /= data.len() as f32;
Duration::from_secs_f32(variance.sqrt())
}

//run with `cargo test --release -- --nocapture bench`
#[test]
fn bench() {
Expand Down Expand Up @@ -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<Box<SNTreeNode>> {
let node = match height {
TREE_HEIGHT => SNTreeNode {
Expand Down Expand Up @@ -433,3 +409,6 @@ pub mod test {
);
}
}

#[cfg(any(feature = "testing", test))]
pub mod test_utils;
30 changes: 30 additions & 0 deletions crates/committer/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use pathfinder_crypto::Felt;
use rand::Rng;
use std::time::Duration;

pub fn mean(data: &[Duration]) -> Duration {
data.iter()
.sum::<Duration>()
.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::<f32>();
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 ;(")
}

0 comments on commit ac08002

Please sign in to comment.