117 lines
2.2 KiB
Rust
117 lines
2.2 KiB
Rust
use prime_factors::*;
|
|
|
|
#[test]
|
|
fn zero() {
|
|
let factors = factors(0);
|
|
let expected = [];
|
|
assert_eq!(factors, expected);
|
|
}
|
|
|
|
#[test]
|
|
fn no_factors() {
|
|
let factors = factors(1);
|
|
let expected = [];
|
|
assert_eq!(factors, expected);
|
|
}
|
|
|
|
#[test]
|
|
fn prime_number() {
|
|
let factors = factors(2);
|
|
let expected = [2];
|
|
assert_eq!(factors, expected);
|
|
}
|
|
|
|
#[test]
|
|
fn another_prime_number() {
|
|
let factors = factors(3);
|
|
let expected = [3];
|
|
assert_eq!(factors, expected);
|
|
}
|
|
|
|
#[test]
|
|
fn square_of_a_prime() {
|
|
let factors = factors(9);
|
|
let expected = [3, 3];
|
|
assert_eq!(factors, expected);
|
|
}
|
|
|
|
#[test]
|
|
fn product_of_first_prime() {
|
|
let factors = factors(4);
|
|
let expected = [2, 2];
|
|
assert_eq!(factors, expected);
|
|
}
|
|
|
|
#[test]
|
|
fn cube_of_a_prime() {
|
|
let factors = factors(8);
|
|
let expected = [2, 2, 2];
|
|
assert_eq!(factors, expected);
|
|
}
|
|
|
|
#[test]
|
|
fn product_of_second_prime() {
|
|
let factors = factors(27);
|
|
let expected = [3, 3, 3];
|
|
assert_eq!(factors, expected);
|
|
}
|
|
|
|
#[test]
|
|
fn product_of_third_prime() {
|
|
let factors = factors(625);
|
|
let expected = [5, 5, 5, 5];
|
|
assert_eq!(factors, expected);
|
|
}
|
|
|
|
#[test]
|
|
fn product_of_first_and_second_prime() {
|
|
let factors = factors(6);
|
|
let expected = [2, 3];
|
|
assert_eq!(factors, expected);
|
|
}
|
|
|
|
#[test]
|
|
fn product_of_primes_and_non_primes() {
|
|
let factors = factors(12);
|
|
let expected = [2, 2, 3];
|
|
assert_eq!(factors, expected);
|
|
}
|
|
|
|
#[test]
|
|
fn product_of_primes() {
|
|
let factors = factors(901_255);
|
|
let expected = [5, 17, 23, 461];
|
|
assert_eq!(factors, expected);
|
|
}
|
|
|
|
#[test]
|
|
fn factors_include_a_large_prime() {
|
|
let factors = factors(93_819_012_551);
|
|
let expected = [11, 9_539, 894_119];
|
|
assert_eq!(factors, expected);
|
|
}
|
|
|
|
#[test]
|
|
fn large_pow2() {
|
|
let factors = factors(1 << 32);
|
|
let expected = [2; 32];
|
|
assert_eq!(factors, expected);
|
|
}
|
|
|
|
#[test]
|
|
fn fuzz() {
|
|
for i in 1..(1 << 16) {
|
|
let factors = factors(i);
|
|
assert!(factors.iter().all(|x| is_prime(*x)), "{factors:?} of {i} are not all prime");
|
|
let product: u64 = factors.iter().product();
|
|
assert_eq!(product, i);
|
|
}
|
|
}
|
|
|
|
fn is_prime(n: u64) -> bool {
|
|
(2..=isqrt(n)).all(|x| n % x != 0)
|
|
}
|
|
|
|
fn isqrt(n: u64) -> u64 {
|
|
(n as f64).sqrt() as _
|
|
}
|