pub fn to_decimal(bcd: u8) -> u8 {
((bcd >> 4) * 10) + (bcd & 0x0F)
}
pub fn from_decimal(decimal: u8) -> u8 {
debug_assert!(decimal <= 99, "Decimal value must be <= 99 for BCD");
((decimal / 10) << 4) | (decimal % 10)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_to_decimal() {
assert_eq!(to_decimal(0x00), 0);
assert_eq!(to_decimal(0x01), 1);
assert_eq!(to_decimal(0x09), 9);
assert_eq!(to_decimal(0x10), 10);
assert_eq!(to_decimal(0x23), 23);
assert_eq!(to_decimal(0x45), 45);
assert_eq!(to_decimal(0x59), 59);
assert_eq!(to_decimal(0x99), 99);
}
#[test]
fn test_from_decimal() {
assert_eq!(from_decimal(0), 0x00);
assert_eq!(from_decimal(1), 0x01);
assert_eq!(from_decimal(9), 0x09);
assert_eq!(from_decimal(10), 0x10);
assert_eq!(from_decimal(23), 0x23);
assert_eq!(from_decimal(45), 0x45);
assert_eq!(from_decimal(59), 0x59);
assert_eq!(from_decimal(99), 0x99);
}
#[test]
fn test_bcd_round_trip() {
for i in 0..=99 {
let bcd = from_decimal(i);
let decimal = to_decimal(bcd);
assert_eq!(i, decimal);
}
}
#[test]
fn test_bcd_edge_cases() {
let test_cases = [0, 1, 9, 10, 23, 31, 59, 99];
for &value in &test_cases {
let bcd = from_decimal(value);
let back = to_decimal(bcd);
assert_eq!(value, back, "Failed for value: {value}");
}
}
}