Add some tests
This commit is contained in:
parent
2e47d97cc7
commit
16741c8c4d
@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -53,29 +53,27 @@ impl<T: Unsigned> Frac<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! impl_mul {
|
macro_rules! impl_mul {
|
||||||
($Type: ty) => {
|
($( $Type: ty ),* ) => {
|
||||||
impl Mul for Frac<$Type> {
|
$(
|
||||||
type Output = Self;
|
impl Mul for Frac<$Type> {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
fn mul(self, rhs: Self) -> Self {
|
fn mul(self, rhs: Self) -> Self {
|
||||||
let numer = self.numer * rhs.numer;
|
let numer = self.numer * rhs.numer;
|
||||||
let denom = self.denom * rhs.denom;
|
let denom = self.denom * rhs.denom;
|
||||||
|
|
||||||
// Figure out the sign
|
// Figure out the sign
|
||||||
if self.sign != rhs.sign {
|
if self.sign != rhs.sign {
|
||||||
Self::new_neg(numer, denom)
|
Self::new_neg(numer, denom)
|
||||||
} else {
|
} else {
|
||||||
Self::new(numer, denom)
|
Self::new(numer, denom)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
)*
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
13
src/seq.rs
13
src/seq.rs
@ -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];
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user