#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MaxResponseCode(pub u8);
impl MaxResponseCode {
pub fn as_10th_secs(&self) -> u16 {
if 0 != self.0 & 0b1000_0000 {
u16::from((self.0 & 0b0000_1111) | 0x10) << u16::from(((self.0 & 0b0111_0000) >> 4) + 3)
} else {
u16::from(self.0)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use proptest::prelude::*;
use std::format;
proptest! {
#[test]
fn as_10th_secs_linear_range(raw in 0u8..=0b0111_1111u8) {
prop_assert_eq!(MaxResponseCode(raw).as_10th_secs(), u16::from(raw));
}
#[test]
fn as_10th_secs_exponential_range(mant in 0u8..=0b1111, exp in 0u8..=0b111u8) {
let raw = 0b1000_0000 | (exp << 4) | mant;
let expected = u16::from(mant | 0x10) << u16::from(exp + 3);
prop_assert_eq!(MaxResponseCode(raw).as_10th_secs(), expected);
}
}
}