Skip to content

Commit

Permalink
Simplify and format
Browse files Browse the repository at this point in the history
  • Loading branch information
wackywendell committed Dec 26, 2019
1 parent 16b205f commit b30748f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 42 deletions.
3 changes: 1 addition & 2 deletions benches/benches.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#![feature(test)]

extern crate primes;
extern crate test;
use primes::PrimeSet;

use primes::PrimeSet;
use test::Bencher;

#[bench]
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ impl PrimeSet {
///
/// Note that this only requires primes up to `n.sqrt()` to be generated, and will generate
/// them as necessary on its own.
#[cfg_attr(feature = "cargo-clippy", allow(wrong_self_convention))]
#[cfg_attr(feature = "cargo-clippy", allow(clippy::wrong_self_convention))]
pub fn is_prime(&mut self, n: u64) -> bool {
if n <= 1 {
return false;
Expand Down
74 changes: 35 additions & 39 deletions tests/basictests.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
extern crate primes;

use primes::{PrimeSet,is_prime,factors,factors_uniq};
use primes::{factors, factors_uniq, is_prime, PrimeSet};

#[test]
fn test_iter(){
fn test_iter() {
let mut pset = PrimeSet::new();
let first_few = [2u64,3,5,7,11,13,17,19,23];
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(){
fn test_find() {
let mut pset = PrimeSet::new();

// pset is empty, so it needs to generate the primes
Expand All @@ -27,16 +25,16 @@ fn test_find(){
{
let plst = pset.list();
let plen = plst.len();
assert_eq!(plen, ix_exp+1);
assert_eq!(plen, ix_exp + 1);

assert_eq!(plst[plen-1], n_exp);
assert_eq!(plst[plen - 1], n_exp);
}

assert_eq!(pset.find_vec(n_exp), Some((ix_exp, n_exp)));
}

#[test]
fn test_primes(){
fn test_primes() {
let mut pset = PrimeSet::new();

// note: some are repeated, because the pset list grows as it goes
Expand All @@ -49,24 +47,22 @@ fn test_primes(){
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 * 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(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));
Expand All @@ -83,40 +79,40 @@ fn test_primes(){
}

#[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)),
];
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(){
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();
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));

}
pset = PrimeSet::new();
assert_eq!(pset.prime_factors(12), vec!(2, 2, 3));
}

0 comments on commit b30748f

Please sign in to comment.