From 1e65c7842b1fa9c27bbd10445fb6af0bdc68c1af Mon Sep 17 00:00:00 2001 From: Matthew Heffernan Date: Thu, 27 Aug 2026 21:37:47 -0700 Subject: [PATCH 1/7] Convert one test to a PBT with Hegel --- Cargo.toml | 1 + src/maximin_utils.rs | 14 ++++++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) 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/src/maximin_utils.rs b/src/maximin_utils.rs index 1f6f3f4..4fcd446 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,12 +71,13 @@ 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() { +fn test_maximin_criterion(tc: TestCase) { let n_iterations: u64 = 1000; - let n_samples: u64 = 100; - let n_dim: u64 = 5; + let n_samples: u64 = tc.draw(gs::integers::().min_value(2).max_value(1000)); + let n_dim: u64 = tc.draw(gs::integers::().min_value(1).max_value(100)); let seed: u64 = 12345; let mut rng: StdRng = SeedableRng::seed_from_u64(seed); for _i in 0..n_iterations { From f4cac8863140bf2aeaf029106c2d2aeff3bcbf9b Mon Sep 17 00:00:00 2001 From: Matthew Heffernan Date: Thu, 27 Aug 2026 21:44:22 -0700 Subject: [PATCH 2/7] Convert more tests to PBT --- src/maximin_utils.rs | 17 ++++++++--------- src/maxpro_utils.rs | 25 ++++++++++++++----------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/maximin_utils.rs b/src/maximin_utils.rs index 4fcd446..7db57c3 100644 --- a/src/maximin_utils.rs +++ b/src/maximin_utils.rs @@ -75,17 +75,16 @@ pub fn maximin_criterion(design: &Vec>) -> f64 { #[hegel::test(test_cases = 1000)] /// Test that the maximin criterion obeys simple properties across many iterations fn test_maximin_criterion(tc: TestCase) { - let n_iterations: u64 = 1000; - let n_samples: u64 = tc.draw(gs::integers::().min_value(2).max_value(1000)); - let n_dim: u64 = tc.draw(gs::integers::().min_value(1).max_value(100)); + 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) } #[test] diff --git a/src/maxpro_utils.rs b/src/maxpro_utils.rs index 19a6810..11a37c5 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,19 @@ 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) } #[test] From d47f73af9076de03192e7a50be893b7b454bfb00 Mon Sep 17 00:00:00 2001 From: Matthew Heffernan Date: Thu, 27 Aug 2026 21:49:39 -0700 Subject: [PATCH 3/7] Add order.rs test to hegel suite --- src/order.rs | 47 ++++++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 23 deletions(-) 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] From 4aaddbffe52b96b1f523167fea6832638e34d48e Mon Sep 17 00:00:00 2001 From: Matthew Heffernan Date: Thu, 27 Aug 2026 21:55:25 -0700 Subject: [PATCH 4/7] Use hegel to generate truly random (non-LHD) designs --- src/maximin_utils.rs | 18 ++++++++++++++++++ src/maxpro_utils.rs | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/maximin_utils.rs b/src/maximin_utils.rs index 7db57c3..3e3fc16 100644 --- a/src/maximin_utils.rs +++ b/src/maximin_utils.rs @@ -87,6 +87,24 @@ fn test_maximin_criterion(tc: TestCase) { 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); +} + #[test] /// Test that the value matches expectations to high tolerance fn test_maximin_criterion_value_1() { diff --git a/src/maxpro_utils.rs b/src/maxpro_utils.rs index 11a37c5..50e11d5 100644 --- a/src/maxpro_utils.rs +++ b/src/maxpro_utils.rs @@ -117,6 +117,24 @@ fn test_maxpro_criterion(tc: TestCase) { 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] /// Test that the value matches expectations to high tolerance fn test_maxpro_criterion_value_1() { From 3f41d1a8a69961ab2b7c9428b915647f96ce1b73 Mon Sep 17 00:00:00 2001 From: Matthew Heffernan Date: Thu, 27 Aug 2026 21:59:23 -0700 Subject: [PATCH 5/7] Use hegel for testing LHD generation --- src/lhd.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) 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); From 941e26920f247cbbbffba4a39f321598f09e9aec Mon Sep 17 00:00:00 2001 From: Matthew Heffernan Date: Thu, 27 Aug 2026 22:17:17 -0700 Subject: [PATCH 6/7] Add test for l2 distance calculation symmetry --- src/maximin_utils.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/maximin_utils.rs b/src/maximin_utils.rs index 3e3fc16..6fadc22 100644 --- a/src/maximin_utils.rs +++ b/src/maximin_utils.rs @@ -105,6 +105,33 @@ fn test_maximin_criterion_square_uniform(tc: TestCase) { 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] /// Test that the value matches expectations to high tolerance fn test_maximin_criterion_value_1() { From fce201c52746fd9c9a056a098e68936bed1a6733 Mon Sep 17 00:00:00 2001 From: Matthew Heffernan Date: Thu, 27 Aug 2026 22:23:21 -0700 Subject: [PATCH 7/7] Update AI policy --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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.