1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-08-04 23:17:46 +00:00

factor::numeric: Generalise tests for Arithmetic trait

This commit is contained in:
nicoo 2020-06-24 17:50:38 +02:00
parent 4d28f48ad9
commit 774feb0a40

View file

@ -328,11 +328,10 @@ mod tests {
test_inverter::<u64>() test_inverter::<u64>()
} }
#[test] fn test_add<A: Arithmetic>() {
fn test_montgomery_add() {
for n in 0..100 { for n in 0..100 {
let n = 2 * n + 1; let n = 2 * n + 1;
let m = Montgomery::new(n); let m = A::new(n);
for x in 0..n { for x in 0..n {
let m_x = m.from_u64(x); let m_x = m.from_u64(x);
for y in 0..=x { for y in 0..=x {
@ -345,10 +344,19 @@ mod tests {
} }
#[test] #[test]
fn test_montgomery_mult() { fn test_add_m32() {
test_add::<Montgomery<u32>>()
}
#[test]
fn test_add_m64() {
test_add::<Montgomery<u64>>()
}
fn test_mult<A: Arithmetic>() {
for n in 0..100 { for n in 0..100 {
let n = 2 * n + 1; let n = 2 * n + 1;
let m = Montgomery::new(n); let m = A::new(n);
for x in 0..n { for x in 0..n {
let m_x = m.from_u64(x); let m_x = m.from_u64(x);
for y in 0..=x { for y in 0..=x {
@ -360,14 +368,33 @@ mod tests {
} }
#[test] #[test]
fn test_montgomery_roundtrip() { fn test_mult_m32() {
test_mult::<Montgomery<u32>>()
}
#[test]
fn test_mult_m64() {
test_mult::<Montgomery<u64>>()
}
fn test_roundtrip<A: Arithmetic>() {
for n in 0..100 { for n in 0..100 {
let n = 2 * n + 1; let n = 2 * n + 1;
let m = Montgomery::new(n); let m = A::new(n);
for x in 0..n { for x in 0..n {
let x_ = m.from_u64(x); let x_ = m.from_u64(x);
assert_eq!(x, m.to_u64(x_)); assert_eq!(x, m.to_u64(x_));
} }
} }
} }
#[test]
fn test_roundtrip_m32() {
test_roundtrip::<Montgomery<u32>>()
}
#[test]
fn test_roundtrip_m64() {
test_roundtrip::<Montgomery<u64>>()
}
} }