use crate::range_decoder::RangeDecoder;
pub(crate) const LCG_SEED_ICDF: &[u8] = &[192, 128, 64, 0];
pub fn decode_lcg_seed(rd: &mut RangeDecoder<'_>) -> u8 {
rd.dec_icdf(LCG_SEED_ICDF, 8) as u8
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn table43_pdf_sums_to_256() {
let pdf = [64u32, 64, 64, 64];
assert_eq!(pdf.iter().sum::<u32>(), 256);
assert_eq!(LCG_SEED_ICDF, &[192u8, 128, 64, 0]);
for w in LCG_SEED_ICDF.windows(2) {
assert!(w[0] > w[1]);
}
assert_eq!(*LCG_SEED_ICDF.last().unwrap(), 0);
}
#[test]
fn decode_lcg_seed_returns_0_through_3() {
let buffers: [&[u8]; 4] = [
&[0x00u8, 0x00, 0x00, 0x00],
&[0xFFu8, 0xFF, 0xFF, 0xFF],
&[0x55u8, 0xAA, 0x55, 0xAA],
&[0xC3u8, 0x3C, 0x96, 0x69],
];
for buf in buffers {
let mut rd = RangeDecoder::new(buf);
let seed = decode_lcg_seed(&mut rd);
assert!(seed <= 3, "seed {seed} out of range");
}
}
#[test]
fn decode_lcg_seed_distribution_partition() {
let mut seen = [false; 4];
for byte in 0..=255u8 {
let buf = [byte, byte ^ 0x5A, byte.wrapping_add(0x33), 0xA5];
let mut rd = RangeDecoder::new(&buf);
let seed = decode_lcg_seed(&mut rd);
seen[seed as usize] = true;
}
assert!(
seen.iter().all(|&s| s),
"uniform PDF should produce every symbol across 256 buffers: seen={seen:?}"
);
}
#[test]
fn decode_lcg_seed_consumes_one_symbol() {
let buf = [0x57u8, 0xC4, 0x9E, 0x1B, 0x86];
let mut rd = RangeDecoder::new(&buf);
let tell0 = rd.tell();
let s0 = decode_lcg_seed(&mut rd);
let tell1 = rd.tell();
let s1 = decode_lcg_seed(&mut rd);
let tell2 = rd.tell();
assert!(s0 <= 3 && s1 <= 3);
assert!(tell1 > tell0);
assert!(tell2 > tell1);
assert_eq!(tell1 - tell0, 2);
assert_eq!(tell2 - tell1, 2);
}
}