-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved tests, benches to separate folders
- Loading branch information
1 parent
985f389
commit 9388761
Showing
3 changed files
with
141 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#![feature(test)] | ||
|
||
extern crate test; | ||
extern crate primes; | ||
use primes::PrimeSet; | ||
|
||
use test::Bencher; | ||
|
||
#[bench] | ||
fn bench_primes(b : &mut Bencher){ | ||
b.iter(|| { | ||
let mut pset = PrimeSet::new(); | ||
let (_, _) = pset.find(1_000_000); | ||
//~ let (idx, n) = pset.find(1_000_000); | ||
//~ println!("Prime {}: {}", idx, n); | ||
}) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
extern crate primes; | ||
|
||
use primes::{PrimeSet,is_prime,factors,factors_uniq}; | ||
|
||
#[test] | ||
fn test_iter(){ | ||
let mut pset = PrimeSet::new(); | ||
let first_few = [2u64,3,5,7,11,13,17,19,23]; | ||
for (m, &n) in pset.iter().zip(first_few.iter()) { | ||
assert_eq!(m, n); | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_find(){ | ||
let mut pset = PrimeSet::new(); | ||
|
||
// pset is empty, so it needs to generate the primes | ||
assert_eq!(pset.find_vec(1000), None); | ||
|
||
let (ix_exp, n_exp) = (168, 1009); | ||
|
||
assert_eq!(pset.find(1000), (ix_exp, n_exp)); | ||
assert_eq!(pset.find(n_exp), (ix_exp, n_exp)); | ||
|
||
// We shouldn't have gone beyond 1009 | ||
{ | ||
let plst = pset.list(); | ||
let plen = plst.len(); | ||
assert_eq!(plen, ix_exp+1); | ||
|
||
assert_eq!(plst[plen-1], n_exp); | ||
} | ||
|
||
assert_eq!(pset.find_vec(n_exp), Some((ix_exp, n_exp))); | ||
} | ||
|
||
#[test] | ||
fn test_primes(){ | ||
let mut pset = PrimeSet::new(); | ||
|
||
// note: some are repeated, because the pset list grows as it goes | ||
|
||
assert!(!pset.is_prime(1)); | ||
assert!(!is_prime(1)); | ||
assert!(pset.is_prime(2)); | ||
assert!(is_prime(2)); | ||
assert!(pset.is_prime(13)); | ||
assert!(is_prime(13)); | ||
assert!(!pset.is_prime(45)); | ||
assert!(!is_prime(45)); | ||
assert!(!pset.is_prime(13*13)); | ||
assert!(!is_prime(13*13)); | ||
assert!(pset.is_prime(13)); | ||
assert!(pset.is_prime(7)); | ||
assert!(is_prime(7)); | ||
assert!(!pset.is_prime(9)); | ||
assert!(!is_prime(9)); | ||
assert!(pset.is_prime(5)); | ||
assert!(is_prime(5)); | ||
|
||
assert!(pset.is_prime(954377)); | ||
assert!(pset.is_prime(954379)); | ||
assert!(!pset.is_prime(954377*954379)); | ||
|
||
assert!(!is_prime(18409199*18409201)); | ||
assert!(pset.is_prime(18409199)); | ||
assert!(pset.is_prime(18409201)); | ||
|
||
assert!(!pset.is_prime(2147483643)); | ||
assert!(pset.is_prime(2147483647)); | ||
assert!(!pset.is_prime(2147483649)); | ||
assert!(!pset.is_prime(63061493)); | ||
assert!(!pset.is_prime(63061491)); | ||
assert!(pset.is_prime(63061489)); | ||
assert!(!pset.is_prime(63061487)); | ||
assert!(!pset.is_prime(63061485)); | ||
assert!(pset.is_prime(2147483647)); | ||
assert!(pset.is_prime(63061489)); | ||
assert!(!is_prime(63061489 * 2147483647)); | ||
assert!(!is_prime(63061489 * 63061489)); | ||
// assert!(!is_prime(2147483647 * 2147483647)); // Runs very long | ||
} | ||
|
||
#[test] | ||
fn test_factors(){ | ||
let mut pset = PrimeSet::new(); | ||
|
||
let ns = [ (1, vec!()), | ||
(2, vec!(2)), | ||
(3, vec!(3)), | ||
(4, vec!(2,2)), | ||
(5, vec!(5)), | ||
(6, vec!(2,3)), | ||
(9, vec!(3,3)), | ||
(12, vec!(2,2,3)), | ||
(121, vec!(11,11)), | ||
(144, vec!(2,2,2,2,3,3)), | ||
(10_000_000, vec!(2,2,2,2,2,2,2,5,5,5,5,5,5,5)), | ||
(100, vec!(2,2,5,5)), | ||
(121, vec!(11, 11)), | ||
]; | ||
|
||
// Test unique factors | ||
for &(n, ref v) in ns.iter(){ | ||
println!("{}: {:?}", n, v); | ||
assert_eq!(pset.prime_factors(n), *v); | ||
assert_eq!(factors(n), *v); | ||
|
||
let unique_factors = factors_uniq(n); | ||
|
||
// Get unique factors from the lists we made above | ||
let mut unique_factors_exp : Vec<u64> = v.iter().map(|&x| {x}).collect(); | ||
unique_factors_exp.dedup(); | ||
|
||
assert_eq!(unique_factors, unique_factors_exp); | ||
} | ||
|
||
pset = PrimeSet::new(); | ||
assert_eq!(pset.prime_factors(12), vec!(2,2,3)); | ||
|
||
} |