tinybz2 0.1.2

Minimal-memory bz2 compressor/decompressor for embedded targets
Documentation
//! Integration tests for tinybz2.
//!
//! Tests verify:
//! 1. Decompression of data produced by Python's bz2 module.
//! 2. Internal compress → decompress roundtrip.
//! 3. Various edge cases.

use tinybz2;

/// bz2-compressed "hello world" produced by Python: bz2.compress(b"hello world", compresslevel=1)
const BZ2_HELLO: [u8; 48] = [
    0x42, 0x5A, 0x68, 0x31, 0x31, 0x41, 0x59, 0x26,
    0x53, 0x59, 0x44, 0xF7, 0x13, 0x78, 0x00, 0x00,
    0x01, 0x91, 0x80, 0x40, 0x00, 0x06, 0x44, 0x90,
    0x80, 0x20, 0x00, 0x22, 0x03, 0x34, 0x84, 0x30,
    0x21, 0xB6, 0x81, 0x54, 0x27, 0x8B, 0xB9, 0x22,
    0x9C, 0x28, 0x48, 0x22, 0x7B, 0x89, 0xBC, 0x00,
];

#[test]
fn decompress_python_hello_world() {
    let mut output = [0u8; 256];
    let result = tinybz2::decompress(&BZ2_HELLO, &mut output);

    match result {
        Ok(n) => {
            assert_eq!(n, 11);
            assert_eq!(&output[..n], b"hello world");
        }
        Err(e) => {
            // If this fails, print detail for debugging
            panic!("Decompression failed: {}", e);
        }
    }
}

#[test]
fn internal_roundtrip_simple() {
    let input = b"Hello, World!";
    let mut compressed = [0u8; 512];
    let comp_len = tinybz2::compress(input, &mut compressed, 1).unwrap();

    assert!(comp_len > 0);
    // Verify bz2 header
    assert_eq!(&compressed[0..4], b"BZh1");

    let mut decompressed = [0u8; 256];
    let decomp_len = tinybz2::decompress(&compressed[..comp_len], &mut decompressed).unwrap();

    assert_eq!(decomp_len, input.len());
    assert_eq!(&decompressed[..decomp_len], &input[..]);
}

#[test]
fn internal_roundtrip_repeated() {
    let input = b"AAAAAAAAAAAAAAAA";
    let mut compressed = [0u8; 512];
    let comp_len = tinybz2::compress(input, &mut compressed, 1).unwrap();

    let mut decompressed = [0u8; 256];
    let decomp_len = tinybz2::decompress(&compressed[..comp_len], &mut decompressed).unwrap();

    assert_eq!(decomp_len, input.len());
    assert_eq!(&decompressed[..decomp_len], &input[..]);
}

#[test]
fn internal_roundtrip_binary() {
    // Binary data with all byte values
    let mut input = [0u8; 256];
    for i in 0..256 {
        input[i] = i as u8;
    }

    let mut compressed = [0u8; 1024];
    let comp_len = tinybz2::compress(&input, &mut compressed, 1).unwrap();

    let mut decompressed = [0u8; 512];
    let decomp_len = tinybz2::decompress(&compressed[..comp_len], &mut decompressed).unwrap();

    assert_eq!(decomp_len, 256);
    assert_eq!(&decompressed[..256], &input[..]);
}

#[test]
fn bad_block_size_rejected() {
    let input = b"test";
    let mut output = [0u8; 256];
    assert_eq!(
        tinybz2::compress(input, &mut output, 0),
        Err(tinybz2::Error::BadBlockSize)
    );
    assert_eq!(
        tinybz2::compress(input, &mut output, 10),
        Err(tinybz2::Error::BadBlockSize)
    );
}

#[test]
fn bad_magic_rejected() {
    let bad_data = b"not bz2 data at all";
    let mut output = [0u8; 256];
    assert_eq!(
        tinybz2::decompress(bad_data, &mut output),
        Err(tinybz2::Error::BadMagic)
    );
}