augurs_testing/
lib.rs

1//! Testing utilities and data for the augurs time series framework.
2//!
3//! Eventually I'd like this to be a fully fledged testing harness to automatically
4//! compare results between the augurs, Python and R implementations, but for now
5//! it's just a place to put some data.
6#![warn(
7    missing_docs,
8    missing_debug_implementations,
9    rust_2018_idioms,
10    unreachable_pub
11)]
12
13pub mod data;
14
15pub use assert_approx_eq::assert_approx_eq;
16
17/// Assert that two slices are approximately equal.
18#[track_caller]
19pub fn assert_all_close(actual: &[f64], expected: &[f64]) {
20    assert_eq!(
21        actual.len(),
22        expected.len(),
23        "slices have different lengths"
24    );
25    for (actual, expected) in actual.iter().zip(expected) {
26        if actual.is_nan() {
27            assert!(expected.is_nan());
28        } else {
29            assert_approx_eq!(actual, expected, 1e-1);
30        }
31    }
32}
33
34/// Assert that a is within (tol * 100)% of b.
35#[macro_export]
36macro_rules! assert_within_pct {
37    ($a:expr, $b:expr, $tol:expr) => {
38        if $a == 0.0 {
39            assert!(
40                ($b as f64).abs() < $tol,
41                "{} is not within {}% of 0",
42                $b,
43                $tol * 100.0
44            );
45        } else {
46            assert!(
47                (($a - $b) / $a).abs() < $tol,
48                "{} is not within {}% of {}",
49                $a,
50                $tol * 100.0,
51                $b
52            );
53        }
54    };
55}