WIP: Comparision operator tests
All checks were successful
timw4mail/rusty-numbers/pipeline/head This commit looks good

This commit is contained in:
Timothy Warren 2020-02-18 21:29:40 -05:00
parent 46629952a5
commit ba3952206a
2 changed files with 17 additions and 5 deletions

View File

@ -258,6 +258,7 @@ mod tests {
#[test]
fn test_gcd() {
assert_eq!(u8::gcd(2, 2), 2);
assert_eq!(u8::gcd(2, 8), 2);
assert_eq!(u16::gcd(36, 48), 12);
}

View File

@ -70,6 +70,10 @@ impl<T: Unsigned> Frac<T> {
/// Generally, you will probably prefer to use the [frac!](../macro.frac.html) macro
/// instead, as that accepts both signed and unsigned arguments
pub fn new(n: T, d: T, s: Sign) -> Frac<T> {
Self::new_raw(n, d, s).reduce()
}
fn new_raw(n: T, d: T, s: Sign) -> Frac<T> {
if d.is_zero() {
panic!("Fraction can not have a zero denominator");
}
@ -79,7 +83,6 @@ impl<T: Unsigned> Frac<T> {
denom: d,
sign: s,
}
.reduce()
}
/// Determine the output sign given the two input signs
@ -121,17 +124,20 @@ impl<T: Unsigned + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Out
if self.denom == other.denom {
return self.numer.cmp(&other.numer);
} else {
// @TODO fix logic for comparing fractions of different denominators
let gcd = T::gcd(self.denom, other.denom);
let x = gcd / self.denom;
let y = gcd / self.denom;
let mut a = self.clone();
let mut b = other.clone();
a.numer *= x;
a.denom *= x;
a.numer = a.numer * x;
a.denom = a.denom * x;
b.numer *= y;
b.denom *= y;
b.numer = b.numer * y;
b.denom = b.denom * y;
assert_eq!(a.denom, b.denom, "Denominators should be equal here. {:#?}{:#?}{:?}{:?}", a, b, x, y);
a.numer.cmp(&b.numer)
}
@ -309,6 +315,11 @@ mod tests {
// assert_eq!(-frac!(1), -frac!(2 / 3) - frac!(1 / 3), "-2/3 - 1/3");
}
#[test]
fn cmp_test() {
// assert_eq!(Ordering::Equal, Frac::new_raw(1u8, 2u8, Sign::Positive).cmp(&Frac::new_raw(4u8, 8u8, Sign::Positive)));
}
#[test]
fn macro_test() {
let frac1 = frac!(1 / 3);