From e91155519a0b3c0b7e0ba5952a5c2d8091a7ea26 Mon Sep 17 00:00:00 2001 From: nicoo Date: Mon, 25 May 2020 16:50:17 +0200 Subject: [PATCH] factor::factor: Add integration tests --- src/uu/factor/src/factor.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/uu/factor/src/factor.rs b/src/uu/factor/src/factor.rs index 2fcf66ad4..bd6c26e4a 100644 --- a/src/uu/factor/src/factor.rs +++ b/src/uu/factor/src/factor.rs @@ -47,6 +47,13 @@ impl Factors { fn push(&mut self, prime: u64) { self.add(prime, 1) } + + #[cfg(test)] + fn product(&self) -> u64 { + self.f + .iter() + .fold(1, |acc, (p, exp)| acc * p.pow(*exp as u32)) + } } impl ops::MulAssign for Factors { @@ -132,3 +139,22 @@ pub fn uumain(args: Vec) -> i32 { } 0 } + +#[cfg(test)] +mod tests { + use super::factor; + + #[test] + fn factor_recombines_small() { + assert!((1..10_000) + .map(|i| 2 * i + 1) + .all(|i| factor(i).product() == i)); + } + + #[test] + fn factor_recombines_overflowing() { + assert!((0..250) + .map(|i| 2 * i + 2u64.pow(32) + 1) + .all(|i| factor(i).product() == i)); + } +}