1pub(crate) fn round_volume_level(volume: f32) -> f32 {
11 #[allow(clippy::manual_clamp)]
12 ((volume * 100.0).round() / 100.0).max(0.0).min(1.0)
13}
14
15#[cfg(test)]
16mod tests {
17 use super::round_volume_level;
18
19 #[fuchsia::test]
21 #[allow(clippy::float_cmp)]
23 fn test_round_volume() {
24 assert_eq!(round_volume_level(1.0), 1.0);
25 assert_eq!(round_volume_level(0.0), 0.0);
26 assert_eq!(round_volume_level(0.222222), 0.22);
27 assert_eq!(round_volume_level(0.349), 0.35);
28 assert_eq!(round_volume_level(0.995), 1.0);
29 assert_eq!(round_volume_level(0.994), 0.99);
30 }
31
32 #[fuchsia::test]
34 #[allow(clippy::float_cmp)]
36 fn test_round_volume_below_range() {
37 assert_eq!(round_volume_level(-1.0), 0.0);
38 assert_eq!(round_volume_level(-0.1), 0.0);
39 assert_eq!(round_volume_level(-0.0), 0.0);
40 assert_eq!(round_volume_level(std::f32::MIN), 0.0);
41 }
42
43 #[fuchsia::test]
45 #[allow(clippy::float_cmp)]
47 fn test_round_volume_above_range() {
48 assert_eq!(round_volume_level(2.0), 1.0);
49 assert_eq!(round_volume_level(1.1), 1.0);
50 assert_eq!(round_volume_level(std::f32::MAX), 1.0);
51 }
52}