-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a test framework for PrimeFactorization (#18)
- Loading branch information
1 parent
3bbb35d
commit bce31de
Showing
10 changed files
with
151 additions
and
55 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
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
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
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 @@ | ||
pub mod prime_factorization; |
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,4 @@ | ||
mod builder; | ||
mod check_test; | ||
|
||
pub use self::builder::CheckTestBuilder; |
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,43 @@ | ||
use super::check_test::CheckTest; | ||
use crate::traits::PrimeFactorization; | ||
use bnum::types::U512; | ||
|
||
type Factors = Vec<U512>; | ||
type TestCase = (U512, Factors); | ||
pub struct CheckTestBuilder { | ||
cases: Vec<TestCase>, | ||
} | ||
|
||
impl CheckTestBuilder { | ||
pub fn new() -> Self { | ||
Self { cases: Vec::new() } | ||
} | ||
|
||
pub fn case(mut self, n: u128, factors: &[u128]) -> Self { | ||
let n_u512 = U512::from(n); | ||
let factors_u512 = factors.iter().map(|&f| U512::from(f)).collect(); | ||
self.cases.push((n_u512, factors_u512)); | ||
self | ||
} | ||
|
||
pub fn build<F: PrimeFactorization>(self) -> CheckTest<F> { | ||
CheckTest::<F>::new(self.cases) | ||
} | ||
} | ||
|
||
impl Default for CheckTestBuilder { | ||
fn default() -> Self { | ||
Self::new() | ||
.case(2, &[2]) | ||
.case(3, &[3]) | ||
.case(60, &[2, 2, 3, 5]) | ||
.case(101, &[101]) | ||
.case(121, &[11, 11]) | ||
.case(143, &[11, 13]) | ||
.case(221, &[13, 17]) | ||
.case(396, &[2, 2, 3, 3, 11]) | ||
.case(729, &[3; 6]) | ||
.case(510, &[2, 3, 5, 17]) | ||
.case(897, &[3, 13, 23]) | ||
} | ||
} |
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,31 @@ | ||
use crate::traits::PrimeFactorization; | ||
use bnum::types::U512; | ||
use std::marker::PhantomData; | ||
|
||
type TestCase = (U512, Vec<U512>); | ||
|
||
pub struct CheckTest<F: PrimeFactorization> { | ||
cases: Vec<TestCase>, | ||
_maker: PhantomData<F>, | ||
} | ||
|
||
impl<F: PrimeFactorization> CheckTest<F> { | ||
pub fn new(cases: Vec<TestCase>) -> Self { | ||
Self { | ||
cases, | ||
_maker: PhantomData, | ||
} | ||
} | ||
|
||
pub fn check_cases(self) { | ||
for (n, factors) in &self.cases { | ||
Self::check(n, &factors); | ||
} | ||
} | ||
|
||
pub fn check(n: &U512, expected: &[U512]) { | ||
let mut actual = F::prime_factorization(&n); | ||
actual.sort_unstable(); | ||
assert_eq!(actual, expected, "Test failed for n = {}", 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