Add some tests

This commit is contained in:
Timothy Warren 2020-02-14 12:11:57 -05:00
parent 2e47d97cc7
commit 16741c8c4d
3 changed files with 46 additions and 19 deletions

View File

@ -156,4 +156,10 @@ mod tests {
assert_eq!(u8::gcd(2, 2), 2); assert_eq!(u8::gcd(2, 2), 2);
assert_eq!(u16::gcd(36, 48), 12); assert_eq!(u16::gcd(36, 48), 12);
} }
#[test]
fn test_lcm() {
assert_eq!(usize::lcm(15, 30), 30);
assert_eq!(u128::lcm(1, 5), 5);
}
} }

View File

@ -53,7 +53,8 @@ impl<T: Unsigned> Frac<T> {
} }
macro_rules! impl_mul { macro_rules! impl_mul {
($Type: ty) => { ($( $Type: ty ),* ) => {
$(
impl Mul for Frac<$Type> { impl Mul for Frac<$Type> {
type Output = Self; type Output = Self;
@ -69,13 +70,10 @@ macro_rules! impl_mul {
} }
} }
} }
)*
}; };
} }
impl_mul!(u8); impl_mul!(u8, u16, u32, u64, usize, u128);
impl_mul!(u16);
impl_mul!(u32);
impl_mul!(u64);
impl_mul!(usize);
impl<T: Unsigned> Neg for Frac<T> { impl<T: Unsigned> Neg for Frac<T> {
type Output = Self; type Output = Self;
@ -89,4 +87,16 @@ impl<T: Unsigned> Neg for Frac<T> {
} }
#[cfg(test)] #[cfg(test)]
mod tests {} mod tests {
use super::*;
#[test]
fn mul_test() {
let frac1 = Frac::new(1u8, 3u8);
let frac2 = Frac::new(2u8, 3u8);
let expected = Frac::new(2u8, 9u8);
assert_eq!(frac1 * frac2, expected);
}
}

View File

@ -5,7 +5,7 @@
/// ///
/// Can calculate up to 186 using native unsigned 128 bit integers. /// Can calculate up to 186 using native unsigned 128 bit integers.
/// ///
/// Example /// Example:
/// ```rust /// ```rust
/// use rusty_numbers::seq::fibonacci; /// use rusty_numbers::seq::fibonacci;
/// ///
@ -47,6 +47,17 @@ fn _fibonacci(n: usize, table: &mut Vec<u128>) -> Option<u128> {
/// using a lookup table for better worst-case performance. /// using a lookup table for better worst-case performance.
/// ///
/// Can calculate up to 34! using native unsigned 128 bit integers. /// Can calculate up to 34! using native unsigned 128 bit integers.
///
/// Example:
/// ```rust
/// use rusty_numbers::seq::{factorial, fibonacci};
///
/// let valid = factorial(3); // Some(6)
/// # assert_eq!(6, valid.unwrap());
///
/// let invalid = factorial(35); // None
/// # assert!(invalid.is_none());
/// ```
pub fn factorial(n: usize) -> Option<u128> { pub fn factorial(n: usize) -> Option<u128> {
let mut table: Vec<u128> = vec![1, 1, 2]; let mut table: Vec<u128> = vec![1, 1, 2];