S10: aac huffman test helper; every SBR and PS codeword decodes to its own symbol through Tree (checked before the books are routed through it)
Browse files at this commit9794cb44b234571f6f4462062e69ed009aef0223Parent: 4fd4ed41 changed file +33 -0
- crates/ec-aac/src/huffman.rs +33 -0
crates/ec-aac/src/huffman.rsmodified+33 -0
| @@ -83,3 +83,36 @@ impl Default for Books { | |||
| 83 | Books::new() | 83 | Books::new() |
| 84 | } | 84 | } |
| 85 | } | 85 | } |
| 86 | + | ||
| 87 | +#[cfg(test)] | ||
| 88 | +pub(crate) fn assert_each_codeword_decodes_to_itself(codes: &[(u8, u32)]) { | ||
| 89 | + let tree = Tree::new(codes); | ||
| 90 | + for (symbol, &(len, code)) in codes.iter().enumerate() { | ||
| 91 | + let mut bytes = [0u8; 8]; | ||
| 92 | + for (i, depth) in (0..len).rev().enumerate() { | ||
| 93 | + if (code >> depth) & 1 == 1 { | ||
| 94 | + bytes[i / 8] |= 0x80 >> (i % 8); | ||
| 95 | + } | ||
| 96 | + } | ||
| 97 | + let mut r = BitReader::new(&bytes); | ||
| 98 | + assert_eq!(tree.decode(&mut r).unwrap(), symbol); | ||
| 99 | + } | ||
| 100 | +} | ||
| 101 | + | ||
| 102 | +#[cfg(test)] | ||
| 103 | +mod tests { | ||
| 104 | + use super::*; | ||
| 105 | + use crate::sbr_tables::*; | ||
| 106 | + | ||
| 107 | + #[test] | ||
| 108 | + fn every_sbr_codeword_decodes_to_its_own_symbol() { | ||
| 109 | + let books: [&[(u8, u32, i32)]; 12] = [ | ||
| 110 | + &ENV15_F, &ENV15_T, &ENV30_F, &ENV30_T, &NOISE_F, &NOISE_T, &ENVB15_F, &ENVB15_T, | ||
| 111 | + &ENVB30_F, &ENVB30_T, &NOISEB_F, &NOISEB_T, | ||
| 112 | + ]; | ||
| 113 | + for book in books { | ||
| 114 | + let codes: Vec<(u8, u32)> = book.iter().map(|&(len, code, _)| (len, code)).collect(); | ||
| 115 | + assert_each_codeword_decodes_to_itself(&codes); | ||
| 116 | + } | ||
| 117 | + } | ||
| 118 | +} | ||