1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-30 20:47:46 +00:00

factor::miller_rabin: Deduplicate parametrized_check macro (#1575)

* factor::miller_rabin::tests::first_composite: Drop useless generic

* factor::miller_rabin::tests: Reuse parametrized_check macro
This commit is contained in:
nicoo 2020-08-03 13:58:09 +02:00 committed by GitHub
parent 1eabda91cf
commit 70828329ba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 24 deletions

View file

@ -110,7 +110,8 @@ pub(crate) fn is_prime(n: u64) -> bool {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::numeric::{Arithmetic, Montgomery}; use crate::numeric::{traits::DoubleInt, Arithmetic, Montgomery};
use crate::parametrized_check;
use quickcheck::quickcheck; use quickcheck::quickcheck;
use std::iter; use std::iter;
const LARGEST_U64_PRIME: u64 = 0xFFFFFFFFFFFFFFC5; const LARGEST_U64_PRIME: u64 = 0xFFFFFFFFFFFFFFC5;
@ -144,25 +145,16 @@ mod tests {
assert!(is_prime(2)); assert!(is_prime(2));
} }
// TODO: Deduplicate with macro in numeric.rs fn first_primes<A: DoubleInt>()
macro_rules! parametrized_check { where
( $f:ident ) => { Montgomery<A>: Basis,
paste::item! { {
#[test]
fn [< $f _ u32 >]() {
$f::<Montgomery<u32>>()
}
#[test]
fn [< $f _ u64 >]() {
$f::<Montgomery<u64>>()
}
}
};
}
fn first_primes<A: Arithmetic + Basis>() {
for p in odd_primes() { for p in odd_primes() {
assert!(test(A::new(p)).is_prime(), "{} reported composite", p); assert!(
test(Montgomery::<A>::new(p)).is_prime(),
"{} reported composite",
p
);
} }
} }
parametrized_check!(first_primes); parametrized_check!(first_primes);
@ -176,14 +168,14 @@ mod tests {
assert!(!is_prime(0)); assert!(!is_prime(0));
} }
fn first_composites<A: Arithmetic + Basis>() { #[test]
fn first_composites() {
for (p, q) in primes().zip(odd_primes()) { for (p, q) in primes().zip(odd_primes()) {
for i in p + 1..q { for i in p + 1..q {
assert!(!is_prime(i), "{} reported prime", i); assert!(!is_prime(i), "{} reported prime", i);
} }
} }
} }
parametrized_check!(first_composites);
#[test] #[test]
fn issue_1556() { fn issue_1556() {
@ -191,11 +183,14 @@ mod tests {
assert!(!is_prime(10_425_511)); assert!(!is_prime(10_425_511));
} }
fn small_semiprimes<A: Arithmetic + Basis>() { fn small_semiprimes<A: DoubleInt>()
where
Montgomery<A>: Basis,
{
for p in odd_primes() { for p in odd_primes() {
for q in odd_primes().take_while(|q| *q <= p) { for q in odd_primes().take_while(|q| *q <= p) {
let n = p * q; let n = p * q;
let m = A::new(n); let m = Montgomery::<A>::new(n);
assert!(!test(m).is_prime(), "{} = {} × {} reported prime", n, p, q); assert!(!test(m).is_prime(), "{} = {} × {} reported prime", n, p, q);
} }
} }

View file

@ -8,7 +8,7 @@
mod gcd; mod gcd;
pub use gcd::gcd; pub use gcd::gcd;
mod traits; pub(crate) mod traits;
mod modular_inverse; mod modular_inverse;
pub(crate) use modular_inverse::modular_inverse; pub(crate) use modular_inverse::modular_inverse;