#![cfg(feature = "snappy")]
use compcol::snappy::{Decoder, Encoder, Snappy};
use compcol::{Algorithm, Decoder as _, Encoder as _, Status};
fn encode_chunked(input: &[u8], in_chunk: usize, out_chunk: usize) -> Vec<u8> {
let mut enc = Encoder::new();
let mut encoded = Vec::new();
let mut buf = vec![0u8; out_chunk.max(1)];
let mut i = 0;
while i < input.len() {
let end = (i + in_chunk.max(1)).min(input.len());
let chunk = &input[i..end];
let mut consumed = 0;
while consumed < chunk.len() {
let (p, status) = enc.encode(&chunk[consumed..], &mut buf).unwrap();
encoded.extend_from_slice(&buf[..p.written]);
consumed += p.consumed;
match status {
Status::InputEmpty | Status::StreamEnd => break,
Status::OutputFull => continue,
}
}
i = end;
}
loop {
let (p, status) = enc.finish(&mut buf).unwrap();
encoded.extend_from_slice(&buf[..p.written]);
match status {
Status::StreamEnd => break,
Status::OutputFull | Status::InputEmpty => {
if p.written == 0 {
panic!("encoder finish stalled");
}
}
}
}
encoded
}
fn decode_chunked(encoded: &[u8], in_chunk: usize, out_chunk: usize) -> Vec<u8> {
let mut dec = Decoder::new();
let mut decoded = Vec::new();
let mut buf = vec![0u8; out_chunk.max(1)];
let mut i = 0;
while i < encoded.len() {
let end = (i + in_chunk.max(1)).min(encoded.len());
let chunk = &encoded[i..end];
let mut consumed = 0;
while consumed < chunk.len() {
let (p, status) = dec.decode(&chunk[consumed..], &mut buf).unwrap();
decoded.extend_from_slice(&buf[..p.written]);
consumed += p.consumed;
match status {
Status::InputEmpty | Status::StreamEnd => break,
Status::OutputFull => continue,
}
}
i = end;
}
loop {
let (p, status) = dec.finish(&mut buf).unwrap();
decoded.extend_from_slice(&buf[..p.written]);
match status {
Status::StreamEnd => break,
Status::OutputFull | Status::InputEmpty => {
if p.written == 0 {
panic!("decoder finish stalled");
}
}
}
}
decoded
}
fn round_trip(input: &[u8]) {
let chunk_in = (input.len() / 4).max(1);
let chunk_out = (input.len() / 4).max(16);
let encoded = encode_chunked(input, chunk_in, chunk_out);
let decoded = decode_chunked(&encoded, chunk_in, chunk_out);
assert_eq!(decoded.len(), input.len(), "round-trip length mismatch");
assert!(decoded == input, "round-trip byte mismatch");
}
#[test]
fn name_is_snappy() {
assert_eq!(<Snappy as Algorithm>::NAME, "snappy");
}
#[test]
fn empty_round_trip() {
round_trip(&[]);
}
#[test]
fn single_byte() {
round_trip(&[0xA5]);
}
#[test]
fn hello_world() {
round_trip(b"hello world");
}
#[test]
fn long_run_of_one_byte() {
let input = vec![0x7Fu8; 10 * 1024];
round_trip(&input);
}
#[test]
fn ascii_text_over_64_kib() {
let phrase = b"The quick brown fox jumps over the lazy dog. ";
let mut input = Vec::with_capacity(70 * 1024);
while input.len() < 65 * 1024 {
input.extend_from_slice(phrase);
}
round_trip(&input);
let encoded = encode_chunked(&input, input.len(), input.len() * 2 + 8);
assert!(
encoded.len() < input.len() / 2,
"expected at least 2x compression on repetitive ASCII, got {} -> {}",
input.len(),
encoded.len()
);
}
#[test]
fn mixed_corpus_over_64_kib() {
let mut input = Vec::with_capacity(80 * 1024);
let phrase = b"The quick brown fox jumps over the lazy dog. ";
while input.len() < 32 * 1024 {
input.extend_from_slice(phrase);
}
let mut state: u32 = 0xFEEDFACEu32;
for _ in 0..16 * 1024 {
state = state.wrapping_mul(1_664_525).wrapping_add(1_013_904_223);
input.push((state >> 16) as u8);
}
while input.len() < 80 * 1024 {
input.extend_from_slice(phrase);
}
assert!(input.len() >= 64 * 1024);
round_trip(&input);
}
#[test]
fn pseudo_random_data() {
let mut state: u32 = 0xDECAFBADu32;
let mut input = Vec::with_capacity(8192);
for _ in 0..8192 {
state = state.wrapping_mul(1_664_525).wrapping_add(1_013_904_223);
input.push((state >> 16) as u8);
}
round_trip(&input);
}
#[test]
fn chunked_one_byte_at_a_time() {
let mut input = Vec::with_capacity(200);
for i in 0..200u32 {
input.push((i * 31) as u8);
}
let tail = input.clone();
input.extend_from_slice(&tail[..50]);
let encoded = encode_chunked(&input, 1, 1);
let decoded = decode_chunked(&encoded, 1, 1);
assert_eq!(decoded, input);
}
#[test]
fn encode_reports_input_empty() {
let mut enc = Encoder::new();
let mut out = [0u8; 32];
let (p, status) = enc.encode(b"hello", &mut out).unwrap();
assert_eq!(p.consumed, 5);
assert_eq!(p.written, 0);
assert!(matches!(status, Status::InputEmpty));
}
#[test]
fn finish_streams_end_marker() {
let mut enc = Encoder::new();
let mut out = [0u8; 64];
let _ = enc.encode(b"hello world", &mut out).unwrap();
let (_p, status) = enc.finish(&mut out).unwrap();
assert!(matches!(status, Status::StreamEnd));
let (p2, status2) = enc.finish(&mut out).unwrap();
assert_eq!(p2.written, 0);
assert!(matches!(status2, Status::StreamEnd));
}
#[test]
fn finish_drains_across_calls() {
let phrase = b"hello hello hello hello hello hello hello hello";
let mut enc = Encoder::new();
let mut tiny = [0u8; 1];
let (p, status) = enc.encode(phrase, &mut tiny).unwrap();
assert_eq!(p.consumed, phrase.len());
assert!(matches!(status, Status::InputEmpty));
let mut produced = Vec::new();
loop {
let (p, status) = enc.finish(&mut tiny).unwrap();
produced.extend_from_slice(&tiny[..p.written]);
match status {
Status::StreamEnd => break,
Status::OutputFull | Status::InputEmpty => {
if p.written == 0 {
panic!("encoder finish stalled");
}
}
}
}
let decoded = decode_chunked(&produced, 1, 1);
assert_eq!(decoded, phrase);
}
#[test]
fn reset_clears_state() {
let mut enc = Encoder::new();
let mut out = [0u8; 64];
let _ = enc.encode(b"first input data", &mut out).unwrap();
enc.reset();
let _ = enc.encode(b"hello", &mut out).unwrap();
let mut produced = Vec::new();
loop {
let (p, status) = enc.finish(&mut out).unwrap();
produced.extend_from_slice(&out[..p.written]);
match status {
Status::StreamEnd => break,
Status::OutputFull | Status::InputEmpty => {
if p.written == 0 {
panic!("encoder finish stalled");
}
}
}
}
let mut dec = Decoder::new();
let _ = dec.decode(&produced, &mut out).unwrap();
let mut decoded = Vec::new();
loop {
let (p, status) = dec.finish(&mut out).unwrap();
decoded.extend_from_slice(&out[..p.written]);
match status {
Status::StreamEnd => break,
Status::OutputFull | Status::InputEmpty => {
if p.written == 0 {
panic!("decoder finish stalled");
}
}
}
}
assert_eq!(&decoded, b"hello");
}
#[test]
fn reset_clears_decoder_state() {
let encoded_hello = encode_chunked(b"hello", 32, 32);
let encoded_world = encode_chunked(b"world", 32, 32);
let mut dec = Decoder::new();
let decoded = decode_full(&mut dec, &encoded_hello);
assert_eq!(&decoded, b"hello");
dec.reset();
let decoded = decode_full(&mut dec, &encoded_world);
assert_eq!(&decoded, b"world");
}
fn decode_full(dec: &mut Decoder, encoded: &[u8]) -> Vec<u8> {
let mut out = [0u8; 64];
let mut consumed = 0;
while consumed < encoded.len() {
let (p, _status) = dec.decode(&encoded[consumed..], &mut out).unwrap();
consumed += p.consumed;
if p.consumed == 0 && p.written == 0 {
break;
}
}
let mut decoded = Vec::new();
loop {
let (p, status) = dec.finish(&mut out).unwrap();
decoded.extend_from_slice(&out[..p.written]);
match status {
Status::StreamEnd => break,
Status::OutputFull | Status::InputEmpty => {
if p.written == 0 {
panic!("decoder finish stalled");
}
}
}
}
decoded
}
#[test]
fn decoder_rejects_truncated_block() {
let mut dec = Decoder::new();
let mut out = [0u8; 16];
let full = encode_chunked(b"hi", 32, 32);
assert!(full.len() >= 3);
let truncated = &full[..full.len() - 1];
let _ = dec.decode(truncated, &mut out).unwrap();
let err = dec.finish(&mut out).unwrap_err();
let _ = err; }
#[test]
fn decoder_rejects_completely_empty_input() {
let mut dec = Decoder::new();
let mut out = [0u8; 16];
let err = dec.finish(&mut out).unwrap_err();
let _ = err;
}
#[test]
fn decoder_rejects_corrupt_offset() {
let block = [0x04u8, 0b00_000_001u8, 0x0A];
let mut dec = Decoder::new();
let mut out = [0u8; 16];
let _ = dec.decode(&block, &mut out).unwrap();
let err = dec.finish(&mut out).unwrap_err();
let _ = err;
}
#[cfg(feature = "factory")]
mod factory {
use compcol::Status;
use compcol::factory;
#[test]
fn lookup_known() {
assert!(factory::encoder_by_name("snappy").is_some());
assert!(factory::decoder_by_name("snappy").is_some());
}
#[test]
fn lookup_unknown() {
assert!(factory::encoder_by_name("does-not-exist").is_none());
assert!(factory::decoder_by_name("does-not-exist").is_none());
}
#[test]
fn names_contains_snappy() {
assert!(factory::names().contains(&"snappy"));
}
#[test]
fn boxed_round_trip() {
let mut enc = factory::encoder_by_name("snappy").unwrap();
let mut dec = factory::decoder_by_name("snappy").unwrap();
let input = b"hello hello hello";
let mut scratch = vec![0u8; 64];
let (_p, status) = enc.encode(input, &mut scratch).unwrap();
assert!(matches!(status, Status::InputEmpty));
let mut encoded = Vec::new();
loop {
let (pf, status) = enc.finish(&mut scratch).unwrap();
encoded.extend_from_slice(&scratch[..pf.written]);
match status {
Status::StreamEnd => break,
Status::OutputFull | Status::InputEmpty => {
if pf.written == 0 {
panic!("encoder finish stalled");
}
}
}
}
let (_pd, status) = dec.decode(&encoded, &mut scratch).unwrap();
assert!(matches!(status, Status::InputEmpty));
let mut decoded = Vec::new();
loop {
let (pf, status) = dec.finish(&mut scratch).unwrap();
decoded.extend_from_slice(&scratch[..pf.written]);
match status {
Status::StreamEnd => break,
Status::OutputFull | Status::InputEmpty => {
if pf.written == 0 {
panic!("decoder finish stalled");
}
}
}
}
assert_eq!(&decoded, input);
}
}