diff --git a/src/types/decimal_floating_point.rs b/src/types/decimal_floating_point.rs index 8cff64a..9fafba3 100644 --- a/src/types/decimal_floating_point.rs +++ b/src/types/decimal_floating_point.rs @@ -32,21 +32,17 @@ impl DecimalFloatingPoint { } pub(crate) fn to_duration(self) -> Duration { - let secs = self.0 as u64; - let nanos = (self.0.fract() * 1_000_000_000.0) as u32; - Duration::new(secs, nanos) + Duration::from_secs_f64(self.0) } pub(crate) fn from_duration(duration: Duration) -> Self { - let n = - (duration.as_secs() as f64) + (f64::from(duration.subsec_nanos()) / 1_000_000_000.0); - DecimalFloatingPoint(n) + Self(duration.as_secs_f64()) } } impl From for DecimalFloatingPoint { fn from(f: u32) -> Self { - DecimalFloatingPoint(f64::from(f)) + Self(f64::from(f)) } } @@ -108,5 +104,44 @@ mod tests { decimal_floating_point, "4.1".parse::().unwrap() ); + + assert!("1#".parse::().is_err()); + } + + #[test] + fn test_new() { + assert!(DecimalFloatingPoint::new(std::f64::INFINITY).is_err()); + assert!(DecimalFloatingPoint::new(-1.0).is_err()); + } + + #[test] + fn test_as_f64() { + assert_eq!(DecimalFloatingPoint::new(1.0).unwrap().as_f64(), 1.0); + } + + #[test] + fn test_from_duration() { + assert_eq!( + DecimalFloatingPoint::from_duration(Duration::from_nanos(11_234_500_112_345)), + DecimalFloatingPoint::new(11234.500112345).unwrap() + ); + } + + #[test] + fn test_from() { + assert_eq!( + DecimalFloatingPoint::from(1u32), + DecimalFloatingPoint::new(1.0).unwrap() + ); + + assert_eq!( + DecimalFloatingPoint::from(1 as f64), + DecimalFloatingPoint::new(1.0).unwrap() + ); + + assert_eq!( + DecimalFloatingPoint::from(1 as f32), + DecimalFloatingPoint::new(1.0).unwrap() + ); } } diff --git a/src/types/decryption_key.rs b/src/types/decryption_key.rs index 3e4166c..1159461 100644 --- a/src/types/decryption_key.rs +++ b/src/types/decryption_key.rs @@ -494,6 +494,29 @@ mod test { DecryptionKey::new(EncryptionMethod::Aes128, "https://www.example.com/") .required_version(), ProtocolVersion::V1 - ) + ); + + assert_eq!( + DecryptionKey::builder() + .method(EncryptionMethod::Aes128) + .uri("https://www.example.com/") + .key_format(KeyFormat::Identity) + .key_format_versions(vec![1, 2, 3]) + .build() + .unwrap() + .required_version(), + ProtocolVersion::V5 + ); + + assert_eq!( + DecryptionKey::builder() + .method(EncryptionMethod::Aes128) + .uri("https://www.example.com/") + .iv([1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7]) + .build() + .unwrap() + .required_version(), + ProtocolVersion::V2 + ); } } diff --git a/src/types/encryption_method.rs b/src/types/encryption_method.rs index 066dcc0..ac51a4f 100644 --- a/src/types/encryption_method.rs +++ b/src/types/encryption_method.rs @@ -104,5 +104,7 @@ mod tests { EncryptionMethod::None, "NONE".parse::().unwrap() ); + + assert!("unknown".parse::().is_err()); } } diff --git a/src/types/hdcp_level.rs b/src/types/hdcp_level.rs index 0b3bbe9..28de702 100644 --- a/src/types/hdcp_level.rs +++ b/src/types/hdcp_level.rs @@ -56,5 +56,7 @@ mod tests { let level = HdcpLevel::None; assert_eq!(level, "NONE".parse::().unwrap()); + + assert!("unk".parse::().is_err()); } }