test #14740: assert wav header sizes after finalize

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
hauke
2026-11-05 14:21:09 +01:00
co-authored by Claude Opus 5.5
parent e3e3391ca8
commit 7beae18a85
+45
View File
@@ -169,6 +169,51 @@ mod tests {
let data_size = std::fs::metadata(&path).unwrap().len() - HEADER_LEN; let data_size = std::fs::metadata(&path).unwrap().len() - HEADER_LEN;
assert_eq!(data_size, 32_000 * 2); assert_eq!(data_size, 32_000 * 2);
// The header itself (not just the file length) must carry the final
// sizes: `finalize` must have rewritten them, not just left whatever
// the last periodic flush wrote.
let bytes = std::fs::read(&path).unwrap();
assert_eq!(&bytes[40..44], &64_000u32.to_le_bytes(), "data chunk size");
assert_eq!(
&bytes[4..8],
&(36 + 64_000u32).to_le_bytes(),
"RIFF chunk size"
);
}
#[test]
fn finalize_rewrites_header_for_a_non_flush_aligned_length() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("track.wav");
let mut writer = TrackWriter::create(&path).unwrap();
// Two separate `write` calls: the first exactly hits the periodic
// flush threshold (16 000 samples), the second (100 samples) does
// not reach another threshold on its own. The trailing 100 samples
// are only ever reflected in the on-disk header if `finalize`
// rewrites it again afterwards.
writer.write(&vec![0.0f32; 16_000]).unwrap();
writer.write(&vec![0.0f32; 100]).unwrap();
let duration = writer.finalize().unwrap();
assert!((duration.as_secs_f64() - 16_100.0 / SAMPLE_RATE as f64).abs() < 1e-9);
let bytes = std::fs::read(&path).unwrap();
let expected_data_size = 16_100u32 * 2;
assert_eq!(
&bytes[40..44],
&expected_data_size.to_le_bytes(),
"data chunk size"
);
assert_eq!(
&bytes[4..8],
&(36 + expected_data_size).to_le_bytes(),
"RIFF chunk size"
);
let reader = hound::WavReader::open(&path).unwrap();
assert_eq!(reader.into_samples::<i16>().count(), 16_100);
} }
#[test] #[test]