WIP: Comparision operator tests
All checks were successful
timw4mail/rusty-numbers/pipeline/head This commit looks good
All checks were successful
timw4mail/rusty-numbers/pipeline/head This commit looks good
This commit is contained in:
parent
46629952a5
commit
ba3952206a
@ -258,6 +258,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_gcd() {
|
fn test_gcd() {
|
||||||
assert_eq!(u8::gcd(2, 2), 2);
|
assert_eq!(u8::gcd(2, 2), 2);
|
||||||
|
assert_eq!(u8::gcd(2, 8), 2);
|
||||||
assert_eq!(u16::gcd(36, 48), 12);
|
assert_eq!(u16::gcd(36, 48), 12);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,6 +70,10 @@ impl<T: Unsigned> Frac<T> {
|
|||||||
/// Generally, you will probably prefer to use the [frac!](../macro.frac.html) macro
|
/// Generally, you will probably prefer to use the [frac!](../macro.frac.html) macro
|
||||||
/// instead, as that accepts both signed and unsigned arguments
|
/// instead, as that accepts both signed and unsigned arguments
|
||||||
pub fn new(n: T, d: T, s: Sign) -> Frac<T> {
|
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() {
|
if d.is_zero() {
|
||||||
panic!("Fraction can not have a zero denominator");
|
panic!("Fraction can not have a zero denominator");
|
||||||
}
|
}
|
||||||
@ -79,7 +83,6 @@ impl<T: Unsigned> Frac<T> {
|
|||||||
denom: d,
|
denom: d,
|
||||||
sign: s,
|
sign: s,
|
||||||
}
|
}
|
||||||
.reduce()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Determine the output sign given the two input signs
|
/// 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 {
|
if self.denom == other.denom {
|
||||||
return self.numer.cmp(&other.numer);
|
return self.numer.cmp(&other.numer);
|
||||||
} else {
|
} else {
|
||||||
|
// @TODO fix logic for comparing fractions of different denominators
|
||||||
let gcd = T::gcd(self.denom, other.denom);
|
let gcd = T::gcd(self.denom, other.denom);
|
||||||
let x = gcd / self.denom;
|
let x = gcd / self.denom;
|
||||||
let y = gcd / self.denom;
|
let y = gcd / self.denom;
|
||||||
let mut a = self.clone();
|
let mut a = self.clone();
|
||||||
let mut b = other.clone();
|
let mut b = other.clone();
|
||||||
|
|
||||||
a.numer *= x;
|
a.numer = a.numer * x;
|
||||||
a.denom *= x;
|
a.denom = a.denom * x;
|
||||||
|
|
||||||
b.numer *= y;
|
b.numer = b.numer * y;
|
||||||
b.denom *= 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)
|
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");
|
// 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]
|
#[test]
|
||||||
fn macro_test() {
|
fn macro_test() {
|
||||||
let frac1 = frac!(1 / 3);
|
let frac1 = frac!(1 / 3);
|
||||||
|
Loading…
Reference in New Issue
Block a user