diff --git a/crates/domistream/tests/video_packetizer.rs b/crates/domistream/tests/video_packetizer.rs index 2ef0608..6512e89 100644 --- a/crates/domistream/tests/video_packetizer.rs +++ b/crates/domistream/tests/video_packetizer.rs @@ -438,16 +438,18 @@ fn fec_plan_properties() { assert!((1..=MAX_FEC_BLOCKS).contains(&plan.len()), "{ctx}: {} blocks", plan.len()); assert_eq!(plan.iter().map(|b| usize::from(b.data_shards)).sum::(), n, "{ctx}"); let first_k = plan[0].data_shards; + // Even split (RFC 5052 9.1, as a BSD-2 host does it, PROTOCOL.md 5.3): sizes differ by at most one, + // the larger blocks first. + let q = n / plan.len(); + let r = n % plan.len(); for (i, b) in plan.iter().enumerate() { let (k, m, p) = (usize::from(b.data_shards), usize::from(b.parity_shards), usize::from(b.fec_percentage)); assert!(k >= 1 && k + m <= 255, "{ctx}: k {k} m {m}"); assert_eq!(m, parity_count(k, b.fec_percentage).unwrap(), "{ctx}: m follows from the percentage"); - if i + 1 < plan.len() { - assert_eq!(b.data_shards, first_k, "{ctx}: every block but the last carries the same k"); - } else { - assert!(b.data_shards <= first_k, "{ctx}"); - } + let want = if i < r { q + 1 } else { q }; + assert_eq!(usize::from(b.data_shards), want, "{ctx}: block {i} of an even split"); + assert!(b.data_shards <= first_k, "{ctx}"); let t = target(k, pct, min); assert_eq!( b.reduced, @@ -480,6 +482,15 @@ fn fec_plan_properties() { ); } +#[test] +fn blocks_split_evenly_larger_first() { + // 217 shards at 255 %: at most 72 data shards fit a block (72 + 184 = 256 would not), so 4 blocks, split + // 55, 54, 54, 54 (RFC 5052 9.1), not 55, 55, 55, 52. + let plan = plan_fec_blocks(217, 255, 0).unwrap(); + let ks: Vec = plan.iter().map(|b| b.data_shards).collect(); + assert_eq!(ks, vec![55, 54, 54, 54]); +} + #[test] fn minimum_parity_raises_the_written_percentage() { // One data shard at 0 % with at least 2 parity packets: 101 % is the smallest percentage giving