factor::factor: Add integration tests

This commit is contained in:
nicoo 2020-05-25 16:50:17 +02:00
parent bada7530fb
commit e91155519a

View file

@ -47,6 +47,13 @@ impl Factors {
fn push(&mut self, prime: u64) { fn push(&mut self, prime: u64) {
self.add(prime, 1) 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<Factors> for Factors { impl ops::MulAssign<Factors> for Factors {
@ -132,3 +139,22 @@ pub fn uumain(args: Vec<String>) -> i32 {
} }
0 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));
}
}