factor::factor: Add integration tests
@@ -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<Factors> for Factors {
@@ -132,3 +139,22 @@ pub fn uumain(args: Vec<String>) -> 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));
+ }
+}
