mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 03:57:44 +00:00
factor: Make random Factors instance generatable for tests
This commit is contained in:
parent
9afed1f25f
commit
998b3c11d3
1 changed files with 15 additions and 4 deletions
|
@ -239,9 +239,13 @@ mod tests {
|
|||
}
|
||||
|
||||
#[cfg(test)]
|
||||
impl quickcheck::Arbitrary for Factors {
|
||||
fn arbitrary<G: quickcheck::Gen>(gen: &mut G) -> Self {
|
||||
use rand::Rng;
|
||||
use rand::{
|
||||
distributions::{Distribution, Standard},
|
||||
Rng,
|
||||
};
|
||||
#[cfg(test)]
|
||||
impl Distribution<Factors> for Standard {
|
||||
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Factors {
|
||||
let mut f = Factors::one();
|
||||
let mut g = 1u64;
|
||||
let mut n = u64::MAX;
|
||||
|
@ -252,7 +256,7 @@ impl quickcheck::Arbitrary for Factors {
|
|||
// See Generating Random Factored Numbers, Easily, J. Cryptology (2003)
|
||||
'attempt: loop {
|
||||
while n > 1 {
|
||||
n = gen.gen_range(1, n);
|
||||
n = rng.gen_range(1, n);
|
||||
if miller_rabin::is_prime(n) {
|
||||
if let Some(h) = g.checked_mul(n) {
|
||||
f.push(n);
|
||||
|
@ -269,6 +273,13 @@ impl quickcheck::Arbitrary for Factors {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
impl quickcheck::Arbitrary for Factors {
|
||||
fn arbitrary<G: quickcheck::Gen>(g: &mut G) -> Self {
|
||||
g.gen()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
impl std::ops::BitXor<Exponent> for Factors {
|
||||
type Output = Self;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue