Skip to content

Commit

Permalink
Moved tests, benches to separate folders
Browse files Browse the repository at this point in the history
  • Loading branch information
wackywendell committed Apr 27, 2015
1 parent 985f389 commit 9388761
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 136 deletions.
18 changes: 18 additions & 0 deletions benches/benches.rs
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);
})
}

137 changes: 1 addition & 136 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ This also provides a few functions unconnected to `PrimeSet`, which will be fast
case, but slower in the long term as they do not use any caching of primes.
*/
#![feature(step_by,test)]
#![feature(step_by)]
#![doc(html_root_url = "https://wackywendell.github.io/primes/")]

#[warn(non_camel_case_types)]
Expand All @@ -69,15 +69,10 @@ case, but slower in the long term as they do not use any caching of primes.
#[warn(non_upper_case_globals)]
#[warn(missing_docs)]

extern crate test;

use std::ops::Index;
use std::slice;
use std::cmp::Ordering::{Equal,Less,Greater};

#[cfg(test)]
use test::Bencher;

/** A prime generator, using the Sieve of Eratosthenes.
Create with `let mut pset = PrimeSet::new()`, and then use `pset.iter()` to iterate over all primes.
Expand Down Expand Up @@ -298,133 +293,3 @@ pub fn is_prime(n : u64) -> bool {
if n <= 1 {return false;}
firstfac(n) == n
}

#[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));

}


#[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);
})
}
122 changes: 122 additions & 0 deletions tests/basictests.rs
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));

}

0 comments on commit 9388761

Please sign in to comment.