Increase test coverage of non-biginit stuff
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
e9b125aa7a
commit
a2efcb0198
14
src/num.rs
14
src/num.rs
@ -284,6 +284,18 @@ impl_empty!(Signed, (i8, i16, i32, i64, i128, isize));
|
||||
mod tests {
|
||||
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]
|
||||
fn test_los() {
|
||||
assert_eq!(4u8.left_overflowing_sub(2).0, 2u8);
|
||||
@ -292,6 +304,8 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
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, 2), 2);
|
||||
assert_eq!(u8::gcd(2, 8), 2);
|
||||
|
@ -117,23 +117,23 @@ impl<T: Unsigned> Frac<T> {
|
||||
|
||||
/// Determine the output sign given the two input signs
|
||||
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) {
|
||||
output = Negative;
|
||||
// a - -b = c
|
||||
if op == FracOp::Subtraction && a.sign == Positive && b.sign == Negative {
|
||||
return Positive;
|
||||
}
|
||||
|
||||
if a.sign != b.sign {
|
||||
output = if op == FracOp::Subtraction && b.sign == Negative {
|
||||
Positive
|
||||
} else {
|
||||
Negative
|
||||
} else {
|
||||
Positive
|
||||
}
|
||||
}
|
||||
|
||||
output
|
||||
}
|
||||
|
||||
/// Convert the fraction to its simplest form
|
||||
pub fn reduce(mut self) -> Self {
|
||||
let gcd = T::gcd(self.numer, self.denom);
|
||||
@ -325,6 +325,28 @@ mod tests {
|
||||
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]
|
||||
fn macro_test() {
|
||||
let frac1 = frac!(1 / 3);
|
||||
|
Loading…
Reference in New Issue
Block a user