use super::{adler32, inflate, zlib_compress};
fn roundtrip(raw: &[u8]) -> usize {
let compressed = zlib_compress(raw);
assert_eq!(inflate(&compressed), raw, "roundtrip mismatch");
compressed.len()
}
#[test]
fn empty_and_tiny_inputs() {
roundtrip(b"");
roundtrip(b"a");
roundtrip(b"abc");
roundtrip(b"abcd");
roundtrip(&[0, 0, 0, 0]);
roundtrip(&[144, 200, 255, 128]); }
#[test]
fn text_and_repetition() {
roundtrip(b"the quick brown fox jumps over the lazy dog");
roundtrip(b"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
roundtrip("mixed ασκII ▲▼ and unicode ␀".as_bytes());
}
#[test]
fn a_flat_raster_collapses() {
let raw = vec![0u8; 4 * 1024 * 1024];
let size = roundtrip(&raw);
assert!(size < raw.len() / 100, "flat data must crush: {size} bytes");
}
#[test]
fn structured_raster_roundtrips() {
let mut raw = Vec::with_capacity(400_000);
for row in 0..250u32 {
for column in 0..400u32 {
let on = (column / 7 + row / 11) % 5 == 0;
let pixel = if on { [220, 66, 52, 255] } else { [0, 0, 0, 0] };
raw.extend_from_slice(&pixel);
}
}
let size = roundtrip(&raw);
assert!(size < raw.len() / 20, "structured data compresses: {size}");
}
#[test]
fn incompressible_noise_survives() {
let mut state = 0x9E37_79B9_7F4A_7C15u64;
let raw: Vec<u8> = (0..100_000)
.map(|_| {
state ^= state << 13;
state ^= state >> 7;
state ^= state << 17;
(state >> 33) as u8
})
.collect();
let size = roundtrip(&raw);
assert!(size < raw.len() + raw.len() / 6 + 64);
}
#[test]
fn all_match_lengths_and_far_distances() {
for length in [
3usize, 4, 10, 11, 18, 19, 34, 66, 130, 131, 257, 258, 259, 300,
] {
let mut raw = vec![7u8];
raw.extend(std::iter::repeat_n(7u8, length));
raw.push(9);
roundtrip(&raw);
}
let mut state = 1u64;
let mut raw: Vec<u8> = (0..30_000)
.map(|_| {
state = state
.wrapping_mul(6364136223846793005)
.wrapping_add(1442695040888963407);
(state >> 33) as u8
})
.collect();
let block: Vec<u8> = (0..200).map(|i| (i * 7 % 251) as u8).collect();
raw.splice(0..0, block.iter().copied());
raw.extend_from_slice(&block);
roundtrip(&raw);
}
#[test]
fn block_boundary_edges() {
roundtrip(&vec![7u8; 16 * 1024]);
roundtrip(&vec![7u8; 16 * 1024 + 1]);
roundtrip(&vec![7u8; 16 * 1024 + 100]);
let mut state = 3u64;
let raw: Vec<u8> = (0..40_000)
.map(|_| {
state ^= state << 13;
state ^= state >> 7;
state ^= state << 17;
(state >> 33) as u8
})
.collect();
roundtrip(&raw);
}
#[test]
fn adler_reference_values() {
assert_eq!(adler32(b""), 1);
assert_eq!(adler32(b"Wikipedia"), 0x11E6_0398);
}