diff --git a/Cargo.lock b/Cargo.lock index b059821bd..991ac3a07 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -649,6 +649,23 @@ dependencies = [ "pkg-config 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "paste" +version = "0.1.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "paste-impl 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro-hack 0.5.16 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "paste-impl" +version = "0.1.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro-hack 0.5.16 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "pkg-config" version = "0.3.17" @@ -668,6 +685,11 @@ name = "ppv-lite86" version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "proc-macro-hack" +version = "0.5.16" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "proc-macro2" version = "1.0.18" @@ -1336,6 +1358,7 @@ name = "uu_factor" version = "0.0.1" dependencies = [ "num-traits 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)", + "paste 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)", "quickcheck 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", "uucore 0.0.4 (git+https://github.com/uutils/uucore.git?branch=canary)", @@ -2254,9 +2277,12 @@ dependencies = [ "checksum numtoa 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b8f8bdf33df195859076e54ab11ee78a1b208382d3a26ec40d142ffc1ecc49ef" "checksum onig 4.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8518fcb2b1b8c2f45f0ad499df4fda6087fc3475ca69a185c173b8315d2fb383" "checksum onig_sys 69.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388410bf5fa341f10e58e6db3975f4bea1ac30247dd79d37a9e5ced3cb4cc3b0" +"checksum paste 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)" = "45ca20c77d80be666aef2b45486da86238fabe33e38306bd3118fe4af33fa880" +"checksum paste-impl 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)" = "d95a7db200b97ef370c8e6de0088252f7e0dfff7d047a28528e47456c0fc98b6" "checksum pkg-config 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)" = "05da548ad6865900e60eaba7f589cc0783590a92e940c26953ff81ddbab2d677" "checksum platform-info 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f2fd076acdc7a98374de6e300bf3af675997225bef21aecac2219553f04dd7e8" "checksum ppv-lite86 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "237a5ed80e274dbc66f86bd59c1e25edc039660be53194b5fe0a482e0f2612ea" +"checksum proc-macro-hack 0.5.16 (registry+https://github.com/rust-lang/crates.io-index)" = "7e0456befd48169b9f13ef0f0ad46d492cf9d2dbb918bcf38e01eed4ce3ec5e4" "checksum proc-macro2 1.0.18 (registry+https://github.com/rust-lang/crates.io-index)" = "beae6331a816b1f65d04c45b078fd8e6c93e8071771f41b8163255bbd8d7c8fa" "checksum quick-error 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" "checksum quickcheck 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a44883e74aa97ad63db83c4bf8ca490f02b2fc02f92575e720c8551e843c945f" diff --git a/src/uu/factor/Cargo.toml b/src/uu/factor/Cargo.toml index 8954ac2f4..47104d5dd 100644 --- a/src/uu/factor/Cargo.toml +++ b/src/uu/factor/Cargo.toml @@ -24,6 +24,7 @@ uucore = { version="0.0.4", package="uucore", git="https://github.com/uutils/uuc uucore_procs = { version="0.0.4", package="uucore_procs", git="https://github.com/uutils/uucore.git", branch="canary" } [dev-dependencies] +paste = "0.1.18" quickcheck = "0.9.2" [[bin]] diff --git a/src/uu/factor/src/numeric.rs b/src/uu/factor/src/numeric.rs index b1f929d4b..7ecc8cf3a 100644 --- a/src/uu/factor/src/numeric.rs +++ b/src/uu/factor/src/numeric.rs @@ -308,6 +308,21 @@ pub(crate) fn modular_inverse(a: T) -> T { mod tests { use super::*; + macro_rules! parametrized_check { + ( $f:ident ) => { + paste::item! { + #[test] + fn [< $f _ u32 >]() { + $f::() + } + #[test] + fn [< $f _ u64 >]() { + $f::() + } + } + }; + } + fn test_inverter() { // All odd integers from 1 to 20 000 let one = T::from(1).unwrap(); @@ -318,21 +333,12 @@ mod tests { assert!(test_values.all(|x| x.wrapping_mul(&modular_inverse(x)) == one)); } + parametrized_check!(test_inverter); - #[test] - fn test_inverter_u32() { - test_inverter::() - } - - #[test] - fn test_inverter_u64() { - test_inverter::() - } - - fn test_add() { + fn test_add() { for n in 0..100 { let n = 2 * n + 1; - let m = A::new(n); + let m = Montgomery::::new(n); for x in 0..n { let m_x = m.from_u64(x); for y in 0..=x { @@ -343,21 +349,12 @@ mod tests { } } } + parametrized_check!(test_add); - #[test] - fn test_add_m32() { - test_add::>() - } - - #[test] - fn test_add_m64() { - test_add::>() - } - - fn test_mult() { + fn test_mult() { for n in 0..100 { let n = 2 * n + 1; - let m = A::new(n); + let m = Montgomery::::new(n); for x in 0..n { let m_x = m.from_u64(x); for y in 0..=x { @@ -367,35 +364,17 @@ mod tests { } } } + parametrized_check!(test_mult); - #[test] - fn test_mult_m32() { - test_mult::>() - } - - #[test] - fn test_mult_m64() { - test_mult::>() - } - - fn test_roundtrip() { + fn test_roundtrip() { for n in 0..100 { let n = 2 * n + 1; - let m = A::new(n); + let m = Montgomery::::new(n); for x in 0..n { let x_ = m.from_u64(x); assert_eq!(x, m.to_u64(x_)); } } } - - #[test] - fn test_roundtrip_m32() { - test_roundtrip::>() - } - - #[test] - fn test_roundtrip_m64() { - test_roundtrip::>() - } + parametrized_check!(test_roundtrip); }