dess_examples/tests/
tests_core.rs

1///compares two sets of values to see if they remain within epsilon of each other
2///using relative error calculation of val1-val2 divided by the average of val1 and val2 (to avoid dividing by 0
3///while maintaining similar range of values to regular relative error)
4pub fn within_epsilon(comparison_vec: Vec<(&f64, &f64)>, epsilon: f64) -> bool {
5    let mut close = vec![];
6    for item in comparison_vec {
7        if (2. * (item.0 - item.1) / (item.0 + item.1)).abs() < epsilon
8            || (item.0 - item.1).abs() < epsilon
9        {
10            close.push(true);
11        } else {
12            close.push(false);
13            break;
14        }
15    }
16    if close.is_empty() {
17        unreachable!("comparison_vec needs to be nonempty")
18    } else {
19        let length = close.len();
20        close[length - 1]
21    }
22}
23///function that compares two sets of values and returns true if the absolute error is within
24///specified epsilon
25pub fn within_epsilon_absolute_error_only(comparison_vec: Vec<(&f64, &f64)>, epsilon: f64) -> bool {
26    let mut close = vec![];
27    for item in comparison_vec {
28        if (item.0 - item.1).abs() < epsilon {
29            close.push(true);
30        } else {
31            close.push(false);
32            break;
33        }
34    }
35    if close.is_empty() {
36        unreachable!("comparison_vec needs to be nonempty")
37    } else {
38        let length = close.len();
39        close[length - 1]
40    }
41}
42///compares two sets of values and returns the average distance between the two (in absolute value)
43pub fn average_distance(comparison_vec: Vec<(&f64, &f64)>) -> f64 {
44    let mut sum = 0.;
45    let mut index = 0.;
46    //not sure why this clone is needed, but without it, comparison_vec.is_empty() doesn't compile
47    let comparison_vec_1 = comparison_vec.clone();
48    for item in comparison_vec {
49        sum += (item.0 - item.1).abs();
50        index += 1.;
51    }
52    if comparison_vec_1.is_empty() {
53        unreachable!("comparison_vec needs to be nonempty")
54    } else {
55        sum / index
56    }
57}