[domistream] Tests: rustfmt

This commit is contained in:
2026-11-25 12:52:38 +01:00
parent 948f446a13
commit da1f3afd52
16 changed files with 1015 additions and 293 deletions
+59 -10
View File
@@ -33,15 +33,28 @@ fn erase(shards: &[Vec<u8>], lost: &[usize]) -> Vec<Option<Vec<u8>>> {
fn check_recovers(code: &ReedSolomon, full: &[Vec<u8>], lost: &[usize]) {
let mut s = erase(full, lost);
code.reconstruct(&mut s).unwrap_or_else(|e| panic!("k={} m={} lost {lost:?}: {e:?}", code.data_shards(), code.parity_shards()));
code.reconstruct(&mut s)
.unwrap_or_else(|e| panic!("k={} m={} lost {lost:?}: {e:?}", code.data_shards(), code.parity_shards()));
for (i, (got, want)) in s.iter().zip(full).enumerate() {
assert_eq!(got.as_deref(), Some(want.as_slice()), "k={} m={} lost {lost:?}: shard {i}", code.data_shards(), code.parity_shards());
assert_eq!(
got.as_deref(),
Some(want.as_slice()),
"k={} m={} lost {lost:?}: shard {i}",
code.data_shards(),
code.parity_shards()
);
}
}
fn check_refuses(code: &ReedSolomon, full: &[Vec<u8>], lost: &[usize]) {
let mut s = erase(full, lost);
assert_err!(code.reconstruct(&mut s), Error::Truncated { .. }, "k={} m={} lost {lost:?}", code.data_shards(), code.parity_shards());
assert_err!(
code.reconstruct(&mut s),
Error::Truncated { .. },
"k={} m={} lost {lost:?}",
code.data_shards(),
code.parity_shards()
);
}
fn subsets(n: usize, size: usize) -> Vec<Vec<usize>> {
@@ -52,7 +65,19 @@ fn subsets(n: usize, size: usize) -> Vec<Vec<usize>> {
#[test]
fn parity_counts() {
for (k, pct, m) in [(4usize, 50u8, 2usize), (1, 1, 1), (1, 100, 1), (10, 0, 0), (100, 100, 100), (3, 33, 1), (3, 34, 2), (7, 20, 2), (127, 100, 127), (255, 0, 0), (2, 255, 6)] {
for (k, pct, m) in [
(4usize, 50u8, 2usize),
(1, 1, 1),
(1, 100, 1),
(10, 0, 0),
(100, 100, 100),
(3, 33, 1),
(3, 34, 2),
(7, 20, 2),
(127, 100, 127),
(255, 0, 0),
(2, 255, 6),
] {
assert_eq!(parity_count(k, pct), Ok(m), "ceil({k} * {pct} / 100)");
}
assert_err!(parity_count(0, 50), Error::UnexpectedValue { .. });
@@ -81,7 +106,11 @@ fn explicit_matrix_is_used_exactly() {
assert_eq!(code.parity_matrix(), matrix.as_slice());
let data = data_shards(&mut rng, k, 33);
let full = encoded(&code, &data);
assert_eq!(&full[k..], gf::encode(&matrix, &data, m).as_slice(), "k={k} m={m}: parity = matrix x data over GF(2^8)/0x11D");
assert_eq!(
&full[k..],
gf::encode(&matrix, &data, m).as_slice(),
"k={k} m={m}: parity = matrix x data over GF(2^8)/0x11D"
);
check_recovers(&code, &full, &(0..m.min(k)).collect::<Vec<_>>());
}
// A second MDS matrix (rows 1,1,1,1 and 1,2,4,8): every square submatrix is invertible.
@@ -101,14 +130,34 @@ fn explicit_matrix_is_used_exactly() {
fn explicit_matrix_refusals() {
let good = gf::cauchy(2, 4);
assert_err!(ReedSolomon::with_parity_matrix(4, 2, &good[..7]), Error::Malformed { .. }, "short");
assert_err!(ReedSolomon::with_parity_matrix(4, 2, &[good.clone(), vec![1]].concat()), Error::Malformed { .. }, "long");
assert_err!(
ReedSolomon::with_parity_matrix(4, 2, &[good.clone(), vec![1]].concat()),
Error::Malformed { .. },
"long"
);
assert_err!(ReedSolomon::with_parity_matrix(4, 2, &[]), Error::Malformed { .. }, "empty");
let mut zero = good.clone();
zero[3] = 0;
assert_err!(ReedSolomon::with_parity_matrix(4, 2, &zero), Error::Malformed { .. }, "a zero entry: a 1x1 submatrix is singular");
assert_err!(ReedSolomon::with_parity_matrix(4, 2, &[1, 1, 1, 1, 1, 1, 1, 1]), Error::Malformed { .. }, "equal rows");
assert_err!(ReedSolomon::with_parity_matrix(4, 2, &[1, 2, 3, 4, 2, 4, 6, 8]), Error::Malformed { .. }, "row 2 = 2 x row 1");
assert_err!(ReedSolomon::with_parity_matrix(4, 2, &[1, 2, 3, 4, 1, 2, 5, 6]), Error::Malformed { .. }, "a singular 2x2 minor (cols 0,1)");
assert_err!(
ReedSolomon::with_parity_matrix(4, 2, &zero),
Error::Malformed { .. },
"a zero entry: a 1x1 submatrix is singular"
);
assert_err!(
ReedSolomon::with_parity_matrix(4, 2, &[1, 1, 1, 1, 1, 1, 1, 1]),
Error::Malformed { .. },
"equal rows"
);
assert_err!(
ReedSolomon::with_parity_matrix(4, 2, &[1, 2, 3, 4, 2, 4, 6, 8]),
Error::Malformed { .. },
"row 2 = 2 x row 1"
);
assert_err!(
ReedSolomon::with_parity_matrix(4, 2, &[1, 2, 3, 4, 1, 2, 5, 6]),
Error::Malformed { .. },
"a singular 2x2 minor (cols 0,1)"
);
assert_err!(ReedSolomon::with_parity_matrix(0, 2, &[]), Error::UnexpectedValue { .. });
assert_err!(ReedSolomon::with_parity_matrix(255, 1, &vec![1; 255]), Error::UnexpectedValue { .. });
}