use super::entropy::{build_lz_pixel_code_tails, lz77_literals};
use super::write::{write_lz_section, write_lz_section_tail};
use super::{ConstantTail, LzToken, lz77_compress_runs_channels};
use crate::bit_writer::BitWriter;
use crate::coder_scratch::CoderScratch;
use crate::entropy::Token;
fn stream(seed: u32, len: usize, contexts: u32) -> Vec<Token> {
let mut state = seed;
(0..len)
.map(|_| {
state ^= state << 13;
state ^= state >> 17;
state ^= state << 5;
Token::new(state % contexts, (state >> 8) % 40)
})
.collect()
}
const CODE_KINDS: [(bool, bool, bool); 5] = [
(true, true, true),
(true, true, false),
(false, true, true),
(false, true, false),
(false, false, false),
];
#[test]
fn tail_context_is_pinned_to_a_single_symbol_cluster() {
for (thorough, refined, ans_cluster) in CODE_KINDS {
let tokens = stream(7, 4096, 3);
let tail = ConstantTail {
context: 3,
count: 100_000,
};
let mut scratch = CoderScratch::default();
let code = build_lz_pixel_code_tails(
thorough,
&[tokens.as_slice()],
&[tail],
4,
64,
refined,
ans_cluster,
None,
&mut scratch.lz_entropy,
&mut scratch.huffman_pool,
);
let cluster = code.context_map[3] as usize;
assert!(
code.context_map
.iter()
.enumerate()
.all(|(context, &c)| context == 3 || c as usize != cluster),
"the tail shares no cluster ({refined}, {ans_cluster})"
);
let (symbol, nbits, _) =
crate::entropy::uint_encode_with_config(0, code.hybrid_uint_configs[cluster]);
assert_eq!(nbits, 0);
if code.use_prefix_code {
assert_eq!(code.prefix_codes[cluster].depths[symbol as usize], 0);
} else {
assert_eq!(
code.ans_symbols[cluster][symbol as usize].freq as u32,
crate::entropy::ANS_TAB_SIZE
);
}
}
}
#[test]
fn tail_writes_the_same_bits_as_materialized_zeros() {
for (thorough, refined, ans_cluster) in CODE_KINDS {
for with_runs in [false, true] {
let literals = stream(11, 3000, 3);
let tail = ConstantTail {
context: 3,
count: 5000,
};
let tokens: Vec<LzToken> = if with_runs {
lz77_compress_runs_channels(vec![literals.clone()])
} else {
lz77_literals(&literals)
};
let mut scratch = CoderScratch::default();
let code = build_lz_pixel_code_tails(
thorough,
&[tokens.as_slice()],
&[tail],
4,
64,
refined,
ans_cluster,
None,
&mut scratch.lz_entropy,
&mut scratch.huffman_pool,
);
let mut with_tail = BitWriter::new();
write_lz_section_tail(&tokens, tail, 4, &code, 64, &mut with_tail);
let mut materialized = tokens.clone();
materialized.extend((0..tail.count).map(|_| LzToken::pixel(3, 0)));
let mut plain = BitWriter::new();
write_lz_section(&materialized, 4, &code, 64, &mut plain);
assert_eq!(with_tail.bits_written(), plain.bits_written());
with_tail.zero_pad_to_byte();
plain.zero_pad_to_byte();
assert_eq!(with_tail.into_bytes(), plain.into_bytes());
}
}
}
#[test]
fn no_tail_keeps_the_code_and_section_unchanged() {
let literals = stream(3, 2000, 3);
let mut scratch = CoderScratch::default();
let code = build_lz_pixel_code_tails(
false,
&[literals.as_slice()],
&[ConstantTail::NONE],
3,
64,
true,
true,
None,
&mut scratch.lz_entropy,
&mut scratch.huffman_pool,
);
let mut a = BitWriter::new();
write_lz_section_tail(&literals, ConstantTail::NONE, 3, &code, 64, &mut a);
let mut b = BitWriter::new();
write_lz_section(&literals, 3, &code, 64, &mut b);
assert_eq!(a.bits_written(), b.bits_written());
a.zero_pad_to_byte();
b.zero_pad_to_byte();
assert_eq!(a.into_bytes(), b.into_bytes());
}