Increase test coverage of non-biginit stuff
All checks were successful
timw4mail/rusty-numbers/pipeline/head This commit looks good

This commit is contained in:
Timothy Warren 2020-03-13 17:08:43 -04:00
parent e9b125aa7a
commit a2efcb0198
2 changed files with 46 additions and 10 deletions

View File

@ -284,6 +284,18 @@ impl_empty!(Signed, (i8, i16, i32, i64, i128, isize));
mod tests { mod tests {
use super::*; use super::*;
#[test]
fn test_sign() {
let s = Sign::default();
assert_eq!(s, Sign::Positive);
let ms = -s;
assert_eq!(ms, Sign::Negative);
let ns = !s;
assert_eq!(ns, Sign::Negative);
}
#[test] #[test]
fn test_los() { fn test_los() {
assert_eq!(4u8.left_overflowing_sub(2).0, 2u8); assert_eq!(4u8.left_overflowing_sub(2).0, 2u8);
@ -292,6 +304,8 @@ mod tests {
#[test] #[test]
fn test_gcd() { fn test_gcd() {
assert_eq!(u8::gcd(5, 0), 5);
assert_eq!(u8::gcd(0, 5), 5);
assert_eq!(u8::gcd(2, 3), 1); assert_eq!(u8::gcd(2, 3), 1);
assert_eq!(u8::gcd(2, 2), 2); assert_eq!(u8::gcd(2, 2), 2);
assert_eq!(u8::gcd(2, 8), 2); assert_eq!(u8::gcd(2, 8), 2);

View File

@ -117,23 +117,23 @@ impl<T: Unsigned> Frac<T> {
/// Determine the output sign given the two input signs /// Determine the output sign given the two input signs
fn get_sign(a: Self, b: Self, op: FracOp) -> Sign { fn get_sign(a: Self, b: Self, op: FracOp) -> Sign {
let mut output = Sign::default(); // -a + -b = -c
if op == FracOp::Addition && a.sign == Negative && b.sign == Negative {
return Negative;
}
if op == FracOp::Addition && !(a.sign == Positive && b.sign == Positive) { // a - -b = c
output = Negative; if op == FracOp::Subtraction && a.sign == Positive && b.sign == Negative {
return Positive;
} }
if a.sign != b.sign { if a.sign != b.sign {
output = if op == FracOp::Subtraction && b.sign == Negative {
Positive
} else {
Negative Negative
} else {
Positive
} }
} }
output
}
/// Convert the fraction to its simplest form /// Convert the fraction to its simplest form
pub fn reduce(mut self) -> Self { pub fn reduce(mut self) -> Self {
let gcd = T::gcd(self.numer, self.denom); let gcd = T::gcd(self.numer, self.denom);
@ -325,6 +325,28 @@ mod tests {
Frac::raw(1u8, 0u8, Sign::default()); Frac::raw(1u8, 0u8, Sign::default());
} }
#[test]
#[should_panic(expected = "Fraction can not have a zero denominator")]
fn zero_denom_new() {
frac!(1/0);
}
#[test]
fn test_get_sign() {
assert_eq!(Sign::Positive, Frac::get_sign(frac!(1), frac!(-1), FracOp::Subtraction));
assert_eq!(Sign::Negative, Frac::get_sign(frac!(-1), frac!(-1), FracOp::Addition));
assert_eq!(Sign::Negative, Frac::get_sign(frac!(-1), frac!(1), FracOp::Addition));
assert_eq!(Sign::Negative, Frac::get_sign(frac!(-1), frac!(1), FracOp::Subtraction));
assert_eq!(Sign::Negative, Frac::get_sign(frac!(-1), frac!(1), FracOp::Other));
}
#[test]
fn test_cmp() {
assert_eq!(Ordering::Greater, frac!(3/4).cmp(&frac!(1/4)));
assert_eq!(Ordering::Less, frac!(1/4).cmp(&frac!(3/4)));
assert_eq!(Ordering::Equal, frac!(1/2).cmp(&frac!(4/8)));
}
#[test] #[test]
fn macro_test() { fn macro_test() {
let frac1 = frac!(1 / 3); let frac1 = frac!(1 / 3);