From 32903ac065356d1395133754ab5a52cff5bd11eb Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Tue, 10 Jan 2023 01:46:34 +0100 Subject: [PATCH] Test f32_hz_then_khz_with_note_name roundtrip This was failing in clap-validator. --- src/formatters.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/formatters.rs b/src/formatters.rs index 2035707c..100f5dc8 100644 --- a/src/formatters.rs +++ b/src/formatters.rs @@ -308,3 +308,28 @@ pub fn s2v_bool_bypass() -> Arc Option + Send + Sync> { } }) } + +#[cfg(test)] +mod tests { + use super::*; + + // More of these validators could use tests, but this one in particular is tricky and I noticed + // an issue where it didn't roundtrip correctly + #[test] + fn f32_hz_then_khz_with_note_name_roundtrip() { + let v2s = v2s_f32_hz_then_khz_with_note_name(1, true); + let s2v = s2v_f32_hz_then_khz(); + + for freq in [0.0, 69.420, 18181.8, 133333.7] { + let string = v2s(freq); + // We can't compare `freq` and `roundtrip_freq` because the string is rounded on both + // cents and frequency and is thus lossy + let roundtrip_freq = s2v(&string).unwrap(); + let roundtrip_string = v2s(roundtrip_freq); + assert_eq!( + string, roundtrip_string, + "Unexpected: {string} -> {roundtrip_freq} -> {roundtrip_string}" + ); + } + } +}