diff --git a/Cargo.toml b/Cargo.toml index 1123bfb..842c172 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,6 +24,7 @@ crate-type = ["cdylib", "rlib"] [dependencies] clap = {version = "4.5.49", features = ["derive"]} +hegeltest = "0.32.4" rand = "0.9.2" rayon = "1.11.0" diff --git a/README.md b/README.md index e26687e..f13dca1 100644 --- a/README.md +++ b/README.md @@ -49,9 +49,9 @@ Versions 0.1.* are reserved for bug fixes and performance improvements to existi - Categorical design dimensions ## AI Policy -This project's AI policy is that no AI-written code is included in the core Rust module or in the python bindings. AI-written code may be present in the `python/` directory but is restricted to analysis. AI code is not used for benchmarking either correctness or speed. +This project's AI policy is that no AI-written code is included in the core Rust module or in the python bindings. AI-written code may be present in the `python/` directory but is restricted to analysis. AI code is not used for benchmarking either correctness or speed. AI may be used to assist writing tests or documentation, but writing and test quality are the responsibility of the author. -Gemini code review is used in development and any code suggestions must be human tested and are not auto-accepted. No AI-generated contributions will be accepted, neither will code contributions where the author cannot explain design choices and tradeoffs. +AI code review is used in development and any code suggestions must be human tested and are not auto-accepted. No AI-generated contributions will be accepted, neither will code contributions where the author cannot explain design choices and tradeoffs. ## Style Rust formatting by `cargo fmt`. Python formatting by `ruff`. `flake8` used for PEP, line length not enforced in docstrings within reason. diff --git a/src/lhd.rs b/src/lhd.rs index f4bc427..7f1ec72 100644 --- a/src/lhd.rs +++ b/src/lhd.rs @@ -1,3 +1,7 @@ +#[cfg(test)] +use hegel::TestCase; +#[cfg(test)] +use hegel::generators as gs; #[cfg(feature = "debug")] use plotters::prelude::*; #[cfg(feature = "pyo3-bindings")] @@ -131,12 +135,13 @@ pub fn generate_lhd(n_samples: u64, n_dim: u64, rng: &mut StdRng) -> Vec().min_value(2).max_value(100)); + let n_dim: u64 = tc.draw(gs::integers::().min_value(1).max_value(10)); let seed: u64 = 12345; let mut rng: StdRng = SeedableRng::seed_from_u64(seed); let design = generate_lhd(n_samples, n_dim, &mut rng); diff --git a/src/maximin_utils.rs b/src/maximin_utils.rs index 1f6f3f4..6fadc22 100644 --- a/src/maximin_utils.rs +++ b/src/maximin_utils.rs @@ -1,5 +1,9 @@ #[cfg(test)] use crate::lhd::generate_lhd; +#[cfg(test)] +use hegel::TestCase; +#[cfg(test)] +use hegel::generators as gs; #[cfg(feature = "pyo3-bindings")] use pyo3::PyResult; #[cfg(feature = "pyo3-bindings")] @@ -10,6 +14,7 @@ use pyo3::prelude::*; use rand::SeedableRng; #[cfg(test)] use rand::rngs::StdRng; + /// Calculate the L2 distance between two points, also known as the /// Euclidean distance. /// @@ -66,20 +71,65 @@ pub fn maximin_criterion(design: &Vec>) -> f64 { min_distance } -#[test] +#[cfg(test)] +#[hegel::test(test_cases = 1000)] /// Test that the maximin criterion obeys simple properties across many iterations -fn test_maximin_criterion() { - let n_iterations: u64 = 1000; - let n_samples: u64 = 100; - let n_dim: u64 = 5; +fn test_maximin_criterion(tc: TestCase) { + let n_samples: u64 = tc.draw(gs::integers::().min_value(2).max_value(100)); + let n_dim: u64 = tc.draw(gs::integers::().min_value(1).max_value(10)); let seed: u64 = 12345; + let mut rng: StdRng = SeedableRng::seed_from_u64(seed); - for _i in 0..n_iterations { - let lhd = generate_lhd(n_samples, n_dim, &mut rng); - let maximin_metric: f64 = maximin_criterion(&lhd); - assert!(maximin_metric >= 0.0); - assert!(maximin_metric < f64::INFINITY) - } + + let lhd = generate_lhd(n_samples, n_dim, &mut rng); + let maximin_metric: f64 = maximin_criterion(&lhd); + assert!(maximin_metric >= 0.0); + assert!(maximin_metric < f64::INFINITY) +} + +#[cfg(test)] +#[hegel::test(test_cases = 100)] +/// Test that the maximin criterion is well-defined for a generated n×n design +/// whose entries are all bounded in [0, 1] +fn test_maximin_criterion_square_uniform(tc: TestCase) { + let n: u64 = tc.draw(gs::integers::().min_value(2).max_value(50)); + let n_index: usize = n as usize; + + let row = gs::vecs(gs::floats::().min_value(0.0).max_value(1.0)) + .min_size(n_index) + .max_size(n_index); + let design: Vec> = tc.draw(gs::vecs(row).min_size(n_index).max_size(n_index)); + + let maximin_metric: f64 = maximin_criterion(&design); + assert!(maximin_metric >= 0.0); + assert!(maximin_metric < f64::INFINITY); +} + +#[cfg(test)] +#[hegel::test(test_cases = 100)] +/// Test that the L2 distance is symmetric and zero for identical points +fn test_calculate_l2_distance_symmetry(tc: TestCase) { + let n_dim: u64 = tc.draw(gs::integers::().min_value(1).max_value(10)); + let n_index: usize = n_dim as usize; + let tolerance: f64 = 10.0_f64.powf(-9.0); + + let point: Vec = tc.draw( + gs::vecs(gs::floats::().min_value(0.0).max_value(1.0)) + .min_size(n_index) + .max_size(n_index), + ); + let other: Vec = tc.draw( + gs::vecs(gs::floats::().min_value(0.0).max_value(1.0)) + .min_size(n_index) + .max_size(n_index), + ); + + let distance_ab: f64 = calculate_l2_distance(&point, &other); + let distance_ba: f64 = calculate_l2_distance(&other, &point); + assert!((distance_ab - distance_ba).abs() < tolerance); + + let distance_aa: f64 = calculate_l2_distance(&point, &point); + assert_eq!(distance_aa, 0.0); } #[test] diff --git a/src/maxpro_utils.rs b/src/maxpro_utils.rs index 19a6810..50e11d5 100644 --- a/src/maxpro_utils.rs +++ b/src/maxpro_utils.rs @@ -1,5 +1,9 @@ #[cfg(test)] use crate::lhd::generate_lhd; +#[cfg(test)] +use hegel::TestCase; +#[cfg(test)] +use hegel::generators as gs; #[cfg(feature = "pyo3-bindings")] use pyo3::PyResult; #[cfg(feature = "pyo3-bindings")] @@ -98,20 +102,37 @@ pub fn py_maxpro_criterion(design: Vec>) -> PyResult { Ok(maxpro_criterion(&design)) } -#[test] +#[cfg(test)] +#[hegel::test(test_cases = 1000)] /// Ensures maxpro criterion is well-conditioned over randomized inputs -fn test_maxpro_criterion() { - let n_iterations: u64 = 1000; - let n_samples: u64 = 100; - let n_dim: u64 = 5; +fn test_maxpro_criterion(tc: TestCase) { + let n_samples: u64 = tc.draw(gs::integers::().min_value(2).max_value(100)); + let n_dim: u64 = tc.draw(gs::integers::().min_value(1).max_value(10)); let seed: u64 = 12345; let mut rng: StdRng = SeedableRng::seed_from_u64(seed); - for _i in 0..n_iterations { - let lhd = generate_lhd(n_samples, n_dim, &mut rng); - let maxpro_metric: f64 = maxpro_criterion(&lhd); - assert!(maxpro_metric >= 0.0); - assert!(maxpro_metric < f64::INFINITY) - } + + let lhd = generate_lhd(n_samples, n_dim, &mut rng); + let maxpro_metric: f64 = maxpro_criterion(&lhd); + assert!(maxpro_metric >= 0.0); + assert!(maxpro_metric < f64::INFINITY) +} + +#[cfg(test)] +#[hegel::test(test_cases = 100)] +/// Test that the maxpro criterion is well-defined for a generated n×n design +/// whose entries are all bounded in [0, 1] +fn test_maxpro_criterion_square_uniform(tc: TestCase) { + let n: u64 = tc.draw(gs::integers::().min_value(2).max_value(50)); + let n_index: usize = n as usize; + + let row = gs::vecs(gs::floats::().min_value(0.0).max_value(1.0)) + .min_size(n_index) + .max_size(n_index); + let design: Vec> = tc.draw(gs::vecs(row).min_size(n_index).max_size(n_index)); + + let maxpro_metric: f64 = maxpro_criterion(&design); + assert!(maxpro_metric >= 0.0); + assert!(maxpro_metric < f64::INFINITY); } #[test] diff --git a/src/order.rs b/src/order.rs index 91071d1..8e9d26f 100644 --- a/src/order.rs +++ b/src/order.rs @@ -6,13 +6,16 @@ use crate::maximin_utils::maximin_criterion; #[cfg(any(test, feature = "pyo3-bindings"))] use crate::maxpro_utils::maxpro_criterion; use core::f64; +#[cfg(test)] +use hegel::TestCase; +#[cfg(test)] +use hegel::generators as gs; #[cfg(feature = "pyo3-bindings")] use pyo3::PyResult; #[cfg(feature = "pyo3-bindings")] use pyo3::exceptions::PyValueError; #[cfg(feature = "pyo3-bindings")] use pyo3::prelude::*; - #[cfg(test)] use rand::SeedableRng; #[cfg(test)] @@ -104,37 +107,35 @@ where ordered_design } -#[test] +#[cfg(test)] +#[hegel::test(test_cases = 1000)] /// Test that the LHD has the same metric values before and after ordering -fn test_ordered_criteria_parity() { - let n_iterations: u64 = 1000; - let n_samples: u64 = 50; - let n_dim: u64 = 5; +fn test_ordered_criteria_parity(tc: TestCase) { + let n_samples: u64 = tc.draw(gs::integers::().min_value(2).max_value(100)); + let n_dim: u64 = tc.draw(gs::integers::().min_value(1).max_value(10)); let seed: u64 = 12345; let atol: f64 = 1e-10; let mut rng: StdRng = SeedableRng::seed_from_u64(seed); - for _i in 0..n_iterations { - // Generate LHD - let lhd = generate_lhd(n_samples, n_dim, &mut rng); - let maxpro_metric_before: f64 = maxpro_criterion(&lhd); - let maximin_metric_before: f64 = maximin_criterion(&lhd); + // Generate LHD + let lhd = generate_lhd(n_samples, n_dim, &mut rng); + let maxpro_metric_before: f64 = maxpro_criterion(&lhd); + let maximin_metric_before: f64 = maximin_criterion(&lhd); - // Order Design - let ordered_design = order_design(lhd, maximin_criterion, false); + // Order Design + let ordered_design = order_design(lhd, maximin_criterion, false); - // Ensure parity - let maxpro_metric_after: f64 = maxpro_criterion(&ordered_design); - let maximin_metric_after: f64 = maximin_criterion(&ordered_design); + // Ensure parity + let maxpro_metric_after: f64 = maxpro_criterion(&ordered_design); + let maximin_metric_after: f64 = maximin_criterion(&ordered_design); - assert!(maxpro_metric_after >= 0.0); - assert!(maxpro_metric_after < f64::INFINITY); - assert!(maximin_metric_after >= 0.0); - assert!(maximin_metric_after < f64::INFINITY); + assert!(maxpro_metric_after >= 0.0); + assert!(maxpro_metric_after < f64::INFINITY); + assert!(maximin_metric_after >= 0.0); + assert!(maximin_metric_after < f64::INFINITY); - assert!((maximin_metric_before - maximin_metric_after).abs() < atol); - assert!((maxpro_metric_before - maxpro_metric_after).abs() < atol); - } + assert!((maximin_metric_before - maximin_metric_after).abs() < atol); + assert!((maxpro_metric_before - maxpro_metric_after).abs() < atol); } #[test]