1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-09-15 03:26:18 +00:00

use num_prime for factorisation to fix gnu test

This commit is contained in:
Ulrich Hornung 2024-04-20 23:29:26 +02:00
parent 64027e5a57
commit d202baba97
22 changed files with 298 additions and 1690 deletions

View file

@ -17,10 +17,10 @@ array-init = "2.0.0"
criterion = "0.3"
rand = "0.8"
rand_chacha = "0.3.1"
num-bigint = "0.4.4"
num-prime = "0.4.3"
num-traits = "0.2.18"
[[bench]]
name = "gcd"
harness = false
[[bench]]
name = "table"

View file

@ -1,33 +0,0 @@
// This file is part of the uutils coreutils package.
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use uu_factor::numeric;
fn gcd(c: &mut Criterion) {
let inputs = {
// Deterministic RNG; use an explicitly-named RNG to guarantee stability
use rand::{RngCore, SeedableRng};
use rand_chacha::ChaCha8Rng;
const SEED: u64 = 0xab4d_1dea_dead_cafe;
let mut rng = ChaCha8Rng::seed_from_u64(SEED);
std::iter::repeat_with(move || (rng.next_u64(), rng.next_u64()))
};
let mut group = c.benchmark_group("gcd");
for (n, m) in inputs.take(10) {
group.bench_with_input(
BenchmarkId::from_parameter(format!("{}_{}", n, m)),
&(n, m),
|b, &(n, m)| {
b.iter(|| numeric::gcd(n, m));
},
);
}
group.finish();
}
criterion_group!(benches, gcd);
criterion_main!(benches);

View file

@ -2,9 +2,11 @@
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
// spell-checker:ignore funcs
use array_init::array_init;
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
use uu_factor::{table::*, Factors};
fn table(c: &mut Criterion) {
#[cfg(target_os = "linux")]
@ -26,21 +28,10 @@ fn table(c: &mut Criterion) {
group.throughput(Throughput::Elements(INPUT_SIZE as _));
for a in inputs.take(10) {
let a_str = format!("{:?}", a);
group.bench_with_input(BenchmarkId::new("factor_chunk", &a_str), &a, |b, &a| {
b.iter(|| {
let mut n_s = a;
let mut f_s: [_; INPUT_SIZE] = array_init(|_| Factors::one());
for (n_s, f_s) in n_s.chunks_mut(CHUNK_SIZE).zip(f_s.chunks_mut(CHUNK_SIZE)) {
factor_chunk(n_s.try_into().unwrap(), f_s.try_into().unwrap());
}
});
});
group.bench_with_input(BenchmarkId::new("factor", &a_str), &a, |b, &a| {
b.iter(|| {
let mut n_s = a;
let mut f_s: [_; INPUT_SIZE] = array_init(|_| Factors::one());
for (n, f) in n_s.iter_mut().zip(f_s.iter_mut()) {
factor(n, f);
for n in a {
let _r = num_prime::nt_funcs::factors(n, None);
}
});
});

View file

@ -3,16 +3,12 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
// spell-checker:ignore (methods) hexdigest
// spell-checker:ignore (methods) hexdigest funcs nprimes
use crate::common::util::TestScenario;
use std::time::{Duration, SystemTime};
#[path = "../../src/uu/factor/sieve.rs"]
mod sieve;
use self::sieve::Sieve;
use rand::distributions::{Distribution, Uniform};
use rand::{rngs::SmallRng, Rng, SeedableRng};
@ -155,7 +151,7 @@ fn test_cli_args() {
#[test]
fn test_random() {
let log_num_primes = f64::from(u32::try_from(NUM_PRIMES).unwrap()).log2().ceil();
let primes = Sieve::primes().take(NUM_PRIMES).collect::<Vec<u64>>();
let primes = num_prime::nt_funcs::nprimes(NUM_PRIMES);
let rng_seed = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
@ -1269,3 +1265,53 @@ const PRIMES50: &[u64] = &[
fn fails_on_directory() {
new_ucmd!().pipe_in(".").fails();
}
#[test]
fn succeeds_with_numbers_larger_than_u64() {
new_ucmd!()
.arg("158909489063877810457")
.succeeds()
.stdout_is("158909489063877810457: 3401347 3861211 12099721\n");
new_ucmd!()
.arg("222087527029934481871")
.succeeds()
.stdout_is("222087527029934481871: 15601 26449 111427 4830277\n");
new_ucmd!()
.arg("12847291069740315094892340035")
.succeeds()
.stdout_is(
"12847291069740315094892340035: \
5 4073 18899 522591721 63874247821\n",
);
}
#[test]
fn succeeds_with_numbers_larger_than_u128() {
new_ucmd!()
.arg("-h")
.arg("340282366920938463463374607431768211456")
.succeeds()
.stdout_is("340282366920938463463374607431768211456: 2^128\n");
new_ucmd!()
.arg("+170141183460469231731687303715884105729")
.succeeds()
.stdout_is(
"170141183460469231731687303715884105729: \
3 56713727820156410577229101238628035243\n",
);
}
#[test]
fn succeeds_with_numbers_larger_than_u256() {
new_ucmd!()
.arg("-h")
.arg(
"115792089237316195423570985008687907853\
269984665640564039457584007913129639936",
)
.succeeds()
.stdout_is(
"115792089237316195423570985008687907853\
269984665640564039457584007913129639936: 2^256\n",
);
}