1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-05-17 07:52:58 +00:00
hls_m3u8/src/types/signed_decimal_floating_point.rs

109 lines
2.9 KiB
Rust
Raw Normal View History

2019-09-22 18:33:40 +00:00
use core::ops::Deref;
use derive_more::{Display, FromStr};
2019-09-06 11:20:40 +00:00
/// Signed decimal floating-point number.
///
/// See: [4.2. Attribute Lists]
///
/// [4.2. Attribute Lists]: https://tools.ietf.org/html/rfc8216#section-4.2
2019-09-22 18:33:40 +00:00
#[derive(Default, Debug, Clone, Copy, PartialEq, PartialOrd, Display, FromStr)]
pub(crate) struct SignedDecimalFloatingPoint(f64);
2019-09-06 11:20:40 +00:00
impl SignedDecimalFloatingPoint {
2019-09-22 18:33:40 +00:00
/// Makes a new [SignedDecimalFloatingPoint] instance.
2019-09-06 11:20:40 +00:00
///
2019-09-22 18:33:40 +00:00
/// # Panics
/// The given value must be finite, otherwise this function will panic!
pub fn new(value: f64) -> Self {
if value.is_infinite() {
panic!("Floating point value must be finite!");
2019-09-13 14:06:52 +00:00
}
2019-09-22 18:33:40 +00:00
Self(value)
2019-09-06 11:20:40 +00:00
}
2019-09-22 18:33:40 +00:00
pub(crate) const fn from_f64_unchecked(value: f64) -> Self {
Self(value)
}
/// Converts [DecimalFloatingPoint] to [f64].
2019-09-08 10:23:33 +00:00
pub const fn as_f64(self) -> f64 {
2019-09-06 11:20:40 +00:00
self.0
}
}
2019-09-22 18:33:40 +00:00
impl Deref for SignedDecimalFloatingPoint {
type Target = f64;
fn deref(&self) -> &Self::Target {
&self.0
2019-09-06 11:20:40 +00:00
}
}
impl Eq for SignedDecimalFloatingPoint {}
2019-09-22 18:33:40 +00:00
#[cfg(test)]
mod tests {
use super::*;
macro_rules! test_from {
( $( $input:expr => $output:expr ),* ) => {
use ::core::convert::From;
#[test]
fn test_from() {
$(
assert_eq!(
$input,
$output,
);
)*
}
}
2019-09-06 11:20:40 +00:00
}
2019-09-22 18:33:40 +00:00
test_from![
SignedDecimalFloatingPoint::from(1u8) => SignedDecimalFloatingPoint::new(1.0),
SignedDecimalFloatingPoint::from(1i8) => SignedDecimalFloatingPoint::new(1.0),
SignedDecimalFloatingPoint::from(1u16) => SignedDecimalFloatingPoint::new(1.0),
SignedDecimalFloatingPoint::from(1i16) => SignedDecimalFloatingPoint::new(1.0),
SignedDecimalFloatingPoint::from(1u32) => SignedDecimalFloatingPoint::new(1.0),
SignedDecimalFloatingPoint::from(1i32) => SignedDecimalFloatingPoint::new(1.0),
SignedDecimalFloatingPoint::from(1.0f32) => SignedDecimalFloatingPoint::new(1.0),
SignedDecimalFloatingPoint::from(1.0f64) => SignedDecimalFloatingPoint::new(1.0)
];
#[test]
fn test_display() {
assert_eq!(
SignedDecimalFloatingPoint::new(1.0).to_string(),
1.0f64.to_string()
);
}
#[test]
#[should_panic]
fn test_new_panic() {
SignedDecimalFloatingPoint::new(::std::f64::INFINITY);
}
#[test]
fn test_parser() {
assert_eq!(
SignedDecimalFloatingPoint::new(1.0),
"1.0".parse::<SignedDecimalFloatingPoint>().unwrap()
);
assert!("garbage".parse::<SignedDecimalFloatingPoint>().is_err());
}
#[test]
fn test_as_f64() {
assert_eq!(SignedDecimalFloatingPoint::new(1.0).as_f64(), 1.0);
}
2019-09-13 14:06:52 +00:00
2019-09-22 18:33:40 +00:00
#[test]
fn test_deref() {
assert_eq!(SignedDecimalFloatingPoint::from(0.1).floor(), 0.0);
2019-09-06 11:20:40 +00:00
}
}