Move conversion function up for reuse

pull/4/head
Diego Barrios Romero 2018-11-03 07:54:47 +01:00
parent 2304e08d1f
commit 4675e902ad
2 changed files with 41 additions and 40 deletions

View File

@ -264,43 +264,3 @@ fn is_am(hours_data: u8) -> bool {
hours_data & BitFlags::AM_PM == 0 hours_data & BitFlags::AM_PM == 0
} }
// Transforms a decimal number to packed BCD format
fn decimal_to_packed_bcd(dec: u8) -> u8 {
((dec / 10) << 4) | (dec % 10)
}
// Transforms a number in packed BCD format to decimal
fn packed_bcd_to_decimal(bcd: u8) -> u8 {
(bcd >> 4) * 10 + (bcd & 0xF)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn can_convert_packed_bcd_to_decimal() {
assert_eq!(0, packed_bcd_to_decimal(0b0000_0000));
assert_eq!(1, packed_bcd_to_decimal(0b0000_0001));
assert_eq!(9, packed_bcd_to_decimal(0b0000_1001));
assert_eq!(10, packed_bcd_to_decimal(0b0001_0000));
assert_eq!(11, packed_bcd_to_decimal(0b0001_0001));
assert_eq!(19, packed_bcd_to_decimal(0b0001_1001));
assert_eq!(20, packed_bcd_to_decimal(0b0010_0000));
assert_eq!(21, packed_bcd_to_decimal(0b0010_0001));
assert_eq!(59, packed_bcd_to_decimal(0b0101_1001));
}
#[test]
fn can_convert_decimal_to_packed_bcd() {
assert_eq!(0b0000_0000, decimal_to_packed_bcd( 0));
assert_eq!(0b0000_0001, decimal_to_packed_bcd( 1));
assert_eq!(0b0000_1001, decimal_to_packed_bcd( 9));
assert_eq!(0b0001_0000, decimal_to_packed_bcd(10));
assert_eq!(0b0001_0001, decimal_to_packed_bcd(11));
assert_eq!(0b0001_1001, decimal_to_packed_bcd(19));
assert_eq!(0b0010_0000, decimal_to_packed_bcd(20));
assert_eq!(0b0010_0001, decimal_to_packed_bcd(21));
assert_eq!(0b0101_1001, decimal_to_packed_bcd(59));
}
}

View File

@ -2,3 +2,44 @@ mod configuration;
mod status; mod status;
mod datetime; mod datetime;
pub use self::datetime::{ Hours, DateTime }; pub use self::datetime::{ Hours, DateTime };
// Transforms a decimal number to packed BCD format
fn decimal_to_packed_bcd(dec: u8) -> u8 {
((dec / 10) << 4) | (dec % 10)
}
// Transforms a number in packed BCD format to decimal
fn packed_bcd_to_decimal(bcd: u8) -> u8 {
(bcd >> 4) * 10 + (bcd & 0xF)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn can_convert_packed_bcd_to_decimal() {
assert_eq!(0, packed_bcd_to_decimal(0b0000_0000));
assert_eq!(1, packed_bcd_to_decimal(0b0000_0001));
assert_eq!(9, packed_bcd_to_decimal(0b0000_1001));
assert_eq!(10, packed_bcd_to_decimal(0b0001_0000));
assert_eq!(11, packed_bcd_to_decimal(0b0001_0001));
assert_eq!(19, packed_bcd_to_decimal(0b0001_1001));
assert_eq!(20, packed_bcd_to_decimal(0b0010_0000));
assert_eq!(21, packed_bcd_to_decimal(0b0010_0001));
assert_eq!(59, packed_bcd_to_decimal(0b0101_1001));
}
#[test]
fn can_convert_decimal_to_packed_bcd() {
assert_eq!(0b0000_0000, decimal_to_packed_bcd( 0));
assert_eq!(0b0000_0001, decimal_to_packed_bcd( 1));
assert_eq!(0b0000_1001, decimal_to_packed_bcd( 9));
assert_eq!(0b0001_0000, decimal_to_packed_bcd(10));
assert_eq!(0b0001_0001, decimal_to_packed_bcd(11));
assert_eq!(0b0001_1001, decimal_to_packed_bcd(19));
assert_eq!(0b0010_0000, decimal_to_packed_bcd(20));
assert_eq!(0b0010_0001, decimal_to_packed_bcd(21));
assert_eq!(0b0101_1001, decimal_to_packed_bcd(59));
}
}