Skip to content

Commit

Permalink
test: mirror testing boiler where possible for multinomial testing
Browse files Browse the repository at this point in the history
  • Loading branch information
YeungOnion committed Sep 24, 2024
1 parent d32bd1a commit 7b8e8cb
Show file tree
Hide file tree
Showing 2 changed files with 285 additions and 97 deletions.
28 changes: 18 additions & 10 deletions src/distribution/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub mod test {
#[macro_export]
macro_rules! testing_boiler {
($($arg_name:ident: $arg_ty:ty),+; $dist:ty; $dist_err:ty) => {
fn make_param_text($($arg_name: $arg_ty),+) -> String {
fn make_param_text($($arg_name: &$arg_ty),+) -> String {
// ""
let mut param_text = String::new();

Expand All @@ -75,12 +75,13 @@ pub mod test {
/// Creates and returns a distribution with the given parameters,
/// panicking if `::new` fails.
fn create_ok($($arg_name: $arg_ty),+) -> $dist {
let param_text = make_param_text($(&$arg_name),+);
match <$dist>::new($($arg_name),+) {
Ok(d) => d,
Err(e) => panic!(
"{}::new was expected to succeed, but failed for {} with error: '{}'",
stringify!($dist),
make_param_text($($arg_name),+),
param_text,
e
)
}
Expand All @@ -90,12 +91,13 @@ pub mod test {
/// panicking if `::new` succeeds.
#[allow(dead_code)]
fn create_err($($arg_name: $arg_ty),+) -> $dist_err {
let param_text = make_param_text($(&$arg_name),+);
match <$dist>::new($($arg_name),+) {
Err(e) => e,
Ok(d) => panic!(
"{}::new was expected to fail, but succeeded for {} with result: {:?}",
stringify!($dist),
make_param_text($($arg_name),+),
param_text,
d
)
}
Expand Down Expand Up @@ -124,13 +126,14 @@ pub mod test {
F: Fn($dist) -> T,
T: ::core::cmp::PartialEq + ::core::fmt::Debug
{
let param_text = make_param_text($(&$arg_name),+);
let x = create_and_get($($arg_name),+, get_fn);
if x != expected {
panic!(
"Expected {:?}, got {:?} for {}",
expected,
x,
make_param_text($($arg_name),+)
param_text
);
}
}
Expand All @@ -142,10 +145,12 @@ pub mod test {
///
/// Panics if `::new` fails.
#[allow(dead_code)]
fn test_relative<F>($($arg_name: $arg_ty),+, expected: f64, get_fn: F)
fn test_relative<F, T>($($arg_name: $arg_ty),+, expected: T, get_fn: F)
where
F: Fn($dist) -> f64,
F: Fn($dist) -> T,
T: ::std::fmt::Debug + ::approx::RelativeEq<Epsilon = f64>
{
let param_text = make_param_text($(&$arg_name),+);
let x = create_and_get($($arg_name),+, get_fn);
let max_relative = $crate::consts::ACC;

Expand All @@ -155,7 +160,7 @@ pub mod test {
x,
expected,
max_relative,
make_param_text($($arg_name),+)
param_text
);
}
}
Expand All @@ -171,6 +176,7 @@ pub mod test {
where
F: Fn($dist) -> f64,
{
let param_text = make_param_text($(&$arg_name),+);
let x = create_and_get($($arg_name),+, get_fn);

// abs_diff_eq! cannot handle infinities, so we manually accept them here
Expand All @@ -184,7 +190,7 @@ pub mod test {
x,
expected,
acc,
make_param_text($($arg_name),+)
param_text
);
}
}
Expand All @@ -196,14 +202,15 @@ pub mod test {
#[allow(dead_code)]
fn test_create_err($($arg_name: $arg_ty),+, expected: $dist_err)
{
let param_text = make_param_text($(&$arg_name),+);
let err = create_err($($arg_name),+);
if err != expected {
panic!(
"{}::new was expected to fail with error {:?}, but failed with error {:?} for {}",
stringify!($dist),
expected,
err,
make_param_text($($arg_name),+)
param_text
)
}
}
Expand Down Expand Up @@ -231,13 +238,14 @@ pub mod test {
F: Fn($dist) -> Option<T>,
T: ::core::fmt::Debug,
{
let param_text = make_param_text($(&$arg_name),+);
let x = create_and_get($($arg_name),+, get_fn);

if let Some(inner) = x {
panic!(
"Expected None, got {:?} for {}",
inner,
make_param_text($($arg_name),+)
param_text
)
}
}
Expand Down
Loading

0 comments on commit 7b8e8cb

Please sign in to comment.