|
| 1 | +// Byte-exact golden tests for the shared Frequency Table wire format. |
| 2 | +// |
| 3 | +// These tests pin the exact bytes emitted by write_frequencies so that |
| 4 | +// refactors that silently change endianness, field order, or count encoding |
| 5 | +// will fail immediately rather than silently producing incompatible output. |
| 6 | +// |
| 7 | +// Wire format (from contract-inventory.md § Binary format and Frequency Table facts): |
| 8 | +// count : uint32 little-endian -- number of frequency entries |
| 9 | +// freq[0] : uint32 little-endian |
| 10 | +// ... |
| 11 | +// freq[n-1] : uint32 little-endian |
| 12 | +// |
| 13 | +// The exact 257-symbol layout used by Huffman, Arithmetic, and Range algorithms |
| 14 | +// is not tested here because it depends on input data; instead we test the |
| 15 | +// wire-format primitives with small, hand-computed vectors. |
| 16 | + |
| 17 | +use compresskit_codec::codec::frequency::{read_frequencies_exact, write_frequencies}; |
| 18 | + |
| 19 | +// --------------------------------------------------------------------------- |
| 20 | +// Golden byte vectors (hand-computed, do not edit without updating comments) |
| 21 | +// --------------------------------------------------------------------------- |
| 22 | + |
| 23 | +/// write_frequencies([]) must produce count=0 encoded as 4 little-endian zero bytes. |
| 24 | +/// Wire: 00 00 00 00 |
| 25 | +#[test] |
| 26 | +fn golden_write_empty_table() { |
| 27 | + let freq: Vec<u32> = vec![]; |
| 28 | + let mut out = Vec::new(); |
| 29 | + write_frequencies(&mut out, &freq); |
| 30 | + assert_eq!( |
| 31 | + out, |
| 32 | + vec![0x00, 0x00, 0x00, 0x00], |
| 33 | + "empty frequency table must encode as 4-byte LE zero count" |
| 34 | + ); |
| 35 | +} |
| 36 | + |
| 37 | +/// write_frequencies([1]) must produce count=1 then value=1, each as 4 LE bytes. |
| 38 | +/// Wire: 01 00 00 00 01 00 00 00 |
| 39 | +#[test] |
| 40 | +fn golden_write_single_entry() { |
| 41 | + let freq: Vec<u32> = vec![1]; |
| 42 | + let mut out = Vec::new(); |
| 43 | + write_frequencies(&mut out, &freq); |
| 44 | + assert_eq!( |
| 45 | + out, |
| 46 | + vec![ |
| 47 | + 0x01, 0x00, 0x00, 0x00, // count = 1 |
| 48 | + 0x01, 0x00, 0x00, 0x00, // freq[0] = 1 |
| 49 | + ], |
| 50 | + "single-entry table wire bytes mismatch" |
| 51 | + ); |
| 52 | +} |
| 53 | + |
| 54 | +/// write_frequencies([1, 2, 3]) must produce count=3 then each value as 4 LE bytes. |
| 55 | +/// Wire: 03 00 00 00 01 00 00 00 02 00 00 00 03 00 00 00 |
| 56 | +#[test] |
| 57 | +fn golden_write_three_entries() { |
| 58 | + let freq: Vec<u32> = vec![1, 2, 3]; |
| 59 | + let mut out = Vec::new(); |
| 60 | + write_frequencies(&mut out, &freq); |
| 61 | + assert_eq!( |
| 62 | + out, |
| 63 | + vec![ |
| 64 | + 0x03, 0x00, 0x00, 0x00, // count = 3 |
| 65 | + 0x01, 0x00, 0x00, 0x00, // freq[0] = 1 |
| 66 | + 0x02, 0x00, 0x00, 0x00, // freq[1] = 2 |
| 67 | + 0x03, 0x00, 0x00, 0x00, // freq[2] = 3 |
| 68 | + ], |
| 69 | + "three-entry table wire bytes mismatch" |
| 70 | + ); |
| 71 | +} |
| 72 | + |
| 73 | +/// Max u32 value must be preserved exactly in the wire format. |
| 74 | +/// Wire: 01 00 00 00 FF FF FF FF |
| 75 | +#[test] |
| 76 | +fn golden_write_max_u32_entry() { |
| 77 | + let freq: Vec<u32> = vec![u32::MAX]; |
| 78 | + let mut out = Vec::new(); |
| 79 | + write_frequencies(&mut out, &freq); |
| 80 | + assert_eq!( |
| 81 | + out, |
| 82 | + vec![ |
| 83 | + 0x01, 0x00, 0x00, 0x00, // count = 1 |
| 84 | + 0xFF, 0xFF, 0xFF, 0xFF, // freq[0] = u32::MAX |
| 85 | + ], |
| 86 | + "max u32 entry wire bytes mismatch" |
| 87 | + ); |
| 88 | +} |
| 89 | + |
| 90 | +/// Multi-byte count value: 256 entries. |
| 91 | +/// Wire: 00 01 00 00 (count=256 LE) followed by 256 * 4 bytes. |
| 92 | +#[test] |
| 93 | +fn golden_write_256_entries_count_encoding() { |
| 94 | + let freq: Vec<u32> = vec![0u32; 256]; |
| 95 | + let mut out = Vec::new(); |
| 96 | + write_frequencies(&mut out, &freq); |
| 97 | + // Only check the first 4 bytes (count field). |
| 98 | + assert_eq!( |
| 99 | + &out[..4], |
| 100 | + &[0x00, 0x01, 0x00, 0x00], |
| 101 | + "count=256 must encode as 00 01 00 00 in little-endian" |
| 102 | + ); |
| 103 | + assert_eq!(out.len(), 4 + 256 * 4, "total length must be 4 + 256*4"); |
| 104 | +} |
| 105 | + |
| 106 | +// --------------------------------------------------------------------------- |
| 107 | +// Round-trip tests: write then read_frequencies_exact must recover original data |
| 108 | +// --------------------------------------------------------------------------- |
| 109 | + |
| 110 | +#[test] |
| 111 | +fn roundtrip_three_entries_via_exact_reader() { |
| 112 | + let original: Vec<u32> = vec![7, 11, 13]; |
| 113 | + let mut out = Vec::new(); |
| 114 | + write_frequencies(&mut out, &original); |
| 115 | + |
| 116 | + let mut pos = 0; |
| 117 | + let recovered = read_frequencies_exact( |
| 118 | + &out, |
| 119 | + &mut pos, |
| 120 | + 3, |
| 121 | + "truncated count", |
| 122 | + "truncated entries", |
| 123 | + "invalid count", |
| 124 | + ) |
| 125 | + .expect("read_frequencies_exact must succeed on valid data"); |
| 126 | + |
| 127 | + assert_eq!(recovered, original, "round-trip must recover original frequencies"); |
| 128 | + assert_eq!(pos, out.len(), "reader must consume all bytes"); |
| 129 | +} |
| 130 | + |
| 131 | +#[test] |
| 132 | +fn roundtrip_exact_reader_rejects_count_mismatch() { |
| 133 | + let freq: Vec<u32> = vec![1, 2, 3]; |
| 134 | + let mut out = Vec::new(); |
| 135 | + write_frequencies(&mut out, &freq); |
| 136 | + |
| 137 | + let mut pos = 0; |
| 138 | + let result = read_frequencies_exact( |
| 139 | + &out, |
| 140 | + &mut pos, |
| 141 | + 4, // wrong expected count |
| 142 | + "truncated count", |
| 143 | + "truncated entries", |
| 144 | + "invalid count", |
| 145 | + ); |
| 146 | + |
| 147 | + assert!( |
| 148 | + result.is_err(), |
| 149 | + "read_frequencies_exact must reject count mismatch" |
| 150 | + ); |
| 151 | +} |
0 commit comments