1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-30 04:27:45 +00:00

factor::tests: Check that powers of known-factorization numbers are factored correctly (#1831)

* factor::tests::recombines_factors: Minor refactor (skip useless bool)

* factor::tests: Check factorizations of powers of factored numbers

* factor::Factors: Add debug assertions to (Factor ^ Exponent)

* factor::tests: Drop obsoleted tests

`factor_correctly_recombines_prior_test_failures` was replaced with
`factor_2044854919485649` as this was the only test not subsumed.

* factor::tests::2044854919485649: Check the expected factorisation
This commit is contained in:
nicoo 2021-03-17 13:58:53 +01:00 committed by GitHub
parent 74a5ed1544
commit 955fa74a42
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -225,20 +225,19 @@ pub fn factor(num: u64) -> Factors {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::{factor, Factors}; use super::{factor, Decomposition, Exponent, Factors};
use quickcheck::quickcheck; use quickcheck::quickcheck;
use smallvec::smallvec;
use std::cell::RefCell;
#[test] #[test]
fn factor_correctly_recombines_prior_test_failures() { fn factor_2044854919485649() {
let prior_failures = [ let f = Factors(RefCell::new(Decomposition(smallvec![
// * integers with duplicate factors (ie, N.pow(M)) (503, 1),
4566769_u64, // == 2137.pow(2) (2423, 1),
2044854919485649_u64, (40961, 2)
18446739546814299361_u64, ])));
18446738440860217487_u64, assert_eq!(factor(f.product()), f);
18446736729316206481_u64,
];
assert!(prior_failures.iter().all(|i| factor(*i).product() == *i));
} }
#[test] #[test]
@ -248,15 +247,6 @@ mod tests {
.all(|i| factor(i).product() == i)); .all(|i| factor(i).product() == i));
} }
#[test]
fn factor_recombines_small_squares() {
// factor(18446736729316206481) == 4294966441 ** 2 ; causes debug_assert fault for repeated decomposition factor in add()
// ToDO: explain/combine with factor_18446736729316206481 and factor_18446739546814299361 tests
assert!((1..10_000)
.map(|i| (2 * i + 1) * (2 * i + 1))
.all(|i| factor(i).product() == i));
}
#[test] #[test]
fn factor_recombines_overflowing() { fn factor_recombines_overflowing() {
assert!((0..250) assert!((0..250)
@ -282,9 +272,15 @@ mod tests {
i == 0 || factor(i).product() == i i == 0 || factor(i).product() == i
} }
fn recombines_factors(f: Factors) -> bool { fn recombines_factors(f: Factors) -> () {
assert_eq!(factor(f.product()), f); assert_eq!(factor(f.product()), f);
true }
fn exponentiate_factors(f: Factors, e: Exponent) -> () {
if e == 0 { return; }
if let Some(fe) = f.product().checked_pow(e.into()) {
assert_eq!(factor(fe), f ^ e);
}
} }
} }
} }
@ -319,3 +315,19 @@ impl quickcheck::Arbitrary for Factors {
} }
} }
} }
#[cfg(test)]
impl std::ops::BitXor<Exponent> for Factors {
type Output = Self;
fn bitxor(self, rhs: Exponent) -> Factors {
debug_assert_ne!(rhs, 0);
let mut r = Factors::one();
for (p, e) in self.0.borrow().0.iter() {
r.add(*p, rhs * e);
}
debug_assert_eq!(r.product(), self.product().pow(rhs.into()));
return r;
}
}