Skip to content

Commit

Permalink
test(time_int): Extract tests from documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
PTaylor-us committed Aug 6, 2020
1 parent 5b44cbe commit 7bf0387
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions src/time_int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,6 @@ pub trait TimeInt:
///
/// Returns truncated integer
///
/// # Examples
///
/// ```rust
/// # use embedded_time::{Fraction, prelude::*};
/// #
/// assert_eq!(8_u32.checked_mul_fraction(&Fraction::new(1,2)), Ok(4_u32));
///
/// // the result is not rounded, but truncated (8×(1/3)=2.66)
/// assert_eq!(8_u32.checked_mul_fraction(&Fraction::new(1,3)), Ok(2_u32));
/// ```
///
/// # Errors
///
/// [`ConversionError::Overflow`]
Expand All @@ -54,17 +43,6 @@ pub trait TimeInt:
///
/// Returns truncated integer
///
/// # Examples
///
/// ```rust
/// # use embedded_time::{Fraction, prelude::*};
/// #
/// assert_eq!(8_u32.checked_div_fraction(&Fraction::new(1,2)), Ok(16_u32));
///
/// // the result is not rounded, but truncated (8/3=2.66)
/// assert_eq!(8_u32.checked_div_fraction(&Fraction::new(3,1)), Ok(2_u32));
/// ```
///
/// # Errors
///
/// [`ConversionError::Overflow`]
Expand All @@ -83,3 +61,24 @@ pub trait TimeInt:

impl TimeInt for u32 {}
impl TimeInt for u64 {}

#[cfg(test)]
mod tests {
use crate::{Fraction, TimeInt};

#[test]
fn checked_integer_mul_fraction() {
assert_eq!(8_u32.checked_mul_fraction(&Fraction::new(1, 2)), Ok(4_u32));

// the result is not rounded, but truncated (8×(1/3)=2.66)
assert_eq!(8_u32.checked_mul_fraction(&Fraction::new(1, 3)), Ok(2_u32));
}

#[test]
fn checked_integer_div_fraction() {
assert_eq!(8_u32.checked_div_fraction(&Fraction::new(1, 2)), Ok(16_u32));

// the result is not rounded, but truncated (8/3=2.66)
assert_eq!(8_u32.checked_div_fraction(&Fraction::new(3, 1)), Ok(2_u32));
}
}

0 comments on commit 7bf0387

Please sign in to comment.