use super::*;
use crate::util::hex;
use crate::util::inflate::inflate;
fn noise(n: usize, seed: u64) -> Vec<u8> {
let mut x = seed | 1;
(0..n)
.map(|_| {
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
(x >> 24) as u8
})
.collect()
}
fn text(n: usize) -> Vec<u8> {
let words = [
"<div class=\"player\">",
"</div>",
"function",
"const ",
"return ",
"party.screen.set({ phase: ",
"\n",
" ",
"import { PartyClient } from \"/_tvpipe/lib/client.js\";",
"the ",
"drawing ",
"{",
"}",
];
let r = noise(n, 7);
let mut out = Vec::new();
let mut i = 0;
while out.len() < n {
out.extend_from_slice(words[usize::from(r[i % r.len()]) % words.len()].as_bytes());
i += 1;
}
out.truncate(n);
out
}
fn round_trip(data: &[u8]) -> Vec<u8> {
let z = deflate(data);
let back = inflate(&z, data.len()).unwrap_or_else(|e| panic!("{} bytes: {e}", data.len()));
assert_eq!(back, data);
z
}
#[test]
fn round_trips() {
for n in [0, 1, 2, 3, 4, 5, 100, 1000] {
round_trip(&noise(n, 1));
round_trip(&text(n));
round_trip(&vec![b'a'; n]);
}
let all: Vec<u8> = (0..=255u8).collect();
round_trip(&all);
round_trip(&vec![0u8; 100_000]);
let mut far = noise(40_000, 3);
far.extend_from_within(..2000);
round_trip(&far);
let mut mixed = text(200_000);
mixed.extend(noise(150_000, 9));
mixed.extend(text(50_000));
round_trip(&mixed);
}
#[test]
fn compresses() {
let t = text(100_000);
let z = round_trip(&t);
assert!(z.len() < t.len() / 4, "{} of {}", z.len(), t.len());
let r = noise(200_000, 5);
let z = round_trip(&r);
assert!(z.len() <= r.len() + r.len() / 1000 + 16, "{}", z.len());
let z = round_trip(&vec![7u8; 1 << 20]);
assert!(z.len() < 3000, "{}", z.len());
}
#[test]
fn deterministic_and_known_answers() {
let t = text(300_000);
assert_eq!(deflate(&t), deflate(&t));
assert_eq!(hex::encode(&deflate(b"")), "0300");
assert_eq!(hex::encode(&deflate(b"a")), "4b0400");
assert_eq!(
hex::encode(&deflate(b"hello hello hello hello")),
"cb48cdc9c957c02001"
);
}
#[test]
fn huffman_lengths_are_limited_and_complete() {
let mut f = vec![0u32; 40];
let (mut a, mut b) = (1u32, 1u32);
for x in f.iter_mut() {
*x = a;
(a, b) = (b, a.saturating_add(b));
}
for limit in [7u8, 15] {
let l = huffman_lengths(&f, limit);
assert!(l.iter().all(|&x| x >= 1 && x <= limit), "{l:?}");
let kraft: u64 = l.iter().map(|&x| 1u64 << (limit - x)).sum();
assert_eq!(kraft, 1 << limit, "{l:?}");
}
assert_eq!(huffman_lengths(&[0, 5, 0], 15), vec![1, 1, 0]);
assert_eq!(huffman_lengths(&[0, 0, 0], 15), vec![1, 1, 0]);
}