|
| 1 | +use crate::Flag; |
| 2 | + |
| 3 | +/// Compares a single value to a higher resolution sequence, where the single value should never |
| 4 | +/// be greater than any value in the sequence (including an adjustment) |
| 5 | +/// |
| 6 | +/// Returns: |
| 7 | +/// For the single value: |
| 8 | +/// - [`Flag::DataMissing`] if the single value is missing, |
| 9 | +/// - [`Flag::Fail`] if this invariant is broken (i.e the single value is greater than the any element |
| 10 | +/// of the sequence plus the adjustment), |
| 11 | +/// - [`Flag::DataMissing`] if any of the elements is missing, as we cannot be sure a missing data |
| 12 | +/// point did not violate the invariant. |
| 13 | +/// - [`Flag::Pass`] otherwise. |
| 14 | +/// |
| 15 | +/// For each element in the sequence: |
| 16 | +/// - [`Flag::DataMissing`] if the single value or the element is missing, |
| 17 | +/// - [`Flag::Fail`] if this invariant is broken (i.e the single value is greater than the element |
| 18 | +/// plus the adjustment), |
| 19 | +/// - [`Flag::Pass`] otherwise. |
| 20 | +pub fn single_greater_than_any( |
| 21 | + single: Option<f32>, |
| 22 | + sequence: &[Option<f32>], |
| 23 | + adjustment: f32, |
| 24 | +) -> (Flag, Vec<Flag>) { |
| 25 | + let single = match single { |
| 26 | + Some(value) => value, |
| 27 | + None => { |
| 28 | + // If the single is missing, we can't do a check at all |
| 29 | + return (Flag::DataMissing, vec![Flag::DataMissing; sequence.len()]); |
| 30 | + } |
| 31 | + }; |
| 32 | + |
| 33 | + let sequence_flags: Vec<Flag> = sequence |
| 34 | + .iter() |
| 35 | + // for each element of the sequence |
| 36 | + .map(|elem| match elem { |
| 37 | + Some(value) => { |
| 38 | + if single > value + adjustment { |
| 39 | + // if the value violates the invariant, flag it as Fail |
| 40 | + Flag::Fail |
| 41 | + } else { |
| 42 | + Flag::Pass |
| 43 | + } |
| 44 | + } |
| 45 | + // if the value is missing, flag as DataMissing |
| 46 | + None => Flag::DataMissing, |
| 47 | + }) |
| 48 | + .collect(); |
| 49 | + |
| 50 | + let single_flag = if sequence_flags.iter().any(|f| *f == Flag::Fail) { |
| 51 | + // if the invariant was violated for any element of the sequence, it was for the single |
| 52 | + // value too |
| 53 | + Flag::Fail |
| 54 | + } else if sequence_flags.iter().any(|f| *f == Flag::DataMissing) { |
| 55 | + // else if no violation was detected, but there was missing data in the sequence, we cannot |
| 56 | + // say for sure that the invariant wasn't invalidated |
| 57 | + Flag::DataMissing |
| 58 | + } else { |
| 59 | + Flag::Pass |
| 60 | + }; |
| 61 | + |
| 62 | + (single_flag, sequence_flags) |
| 63 | +} |
| 64 | + |
| 65 | +#[cfg(test)] |
| 66 | +mod tests { |
| 67 | + use super::*; |
| 68 | + |
| 69 | + #[test] |
| 70 | + fn test_single_less_than_max() { |
| 71 | + assert_eq!( |
| 72 | + single_greater_than_any(Some(1.), &[Some(1.), Some(2.), Some(2.)], 0.2), |
| 73 | + (Flag::Pass, vec![Flag::Pass, Flag::Pass, Flag::Pass]) |
| 74 | + ); |
| 75 | + assert_eq!( |
| 76 | + single_greater_than_any(Some(1.), &[Some(1.), Some(2.), Some(2.)], -0.2), |
| 77 | + (Flag::Fail, vec![Flag::Fail, Flag::Pass, Flag::Pass]) |
| 78 | + ); |
| 79 | + assert_eq!( |
| 80 | + single_greater_than_any(Some(1.), &[Some(1.), None, Some(2.)], -0.2), |
| 81 | + (Flag::Fail, vec![Flag::Fail, Flag::DataMissing, Flag::Pass]) |
| 82 | + ); |
| 83 | + assert_eq!( |
| 84 | + single_greater_than_any(Some(1.), &[Some(1.), None, Some(2.)], 0.2), |
| 85 | + ( |
| 86 | + Flag::DataMissing, |
| 87 | + vec![Flag::Pass, Flag::DataMissing, Flag::Pass] |
| 88 | + ) |
| 89 | + ); |
| 90 | + } |
| 91 | +} |
0 commit comments