diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /third_party/rust/deflate/tests/test.rs | |
parent | Initial commit. (diff) | |
download | firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/deflate/tests/test.rs')
-rw-r--r-- | third_party/rust/deflate/tests/test.rs | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/third_party/rust/deflate/tests/test.rs b/third_party/rust/deflate/tests/test.rs new file mode 100644 index 0000000000..7ab6fe9b99 --- /dev/null +++ b/third_party/rust/deflate/tests/test.rs @@ -0,0 +1,135 @@ +extern crate deflate; +extern crate flate2; + +use std::io::{Write, Read}; +use deflate::CompressionOptions; + +fn get_test_file_data(name: &str) -> Vec<u8> { + use std::fs::File; + let mut input = Vec::new(); + let mut f = File::open(name).unwrap(); + + f.read_to_end(&mut input).unwrap(); + input +} + +fn get_test_data() -> Vec<u8> { + use std::env; + let path = env::var("TEST_FILE").unwrap_or_else(|_| "tests/pg11.txt".to_string()); + get_test_file_data(&path) +} + +fn roundtrip(data: &[u8]) { + roundtrip_conf(data, CompressionOptions::default()) +} + +fn roundtrip_conf(data: &[u8], level: CompressionOptions) { + let compressed = deflate::deflate_bytes_zlib_conf(data, level); + println!("Compressed len: {}, level: {:?}", compressed.len(), level); + let decompressed = { + let mut d = flate2::read::ZlibDecoder::new(compressed.as_slice()); + let mut out = Vec::new(); + d.read_to_end(&mut out).unwrap(); + out + }; + assert!(decompressed.as_slice() == data); +} + +// A test comparing the compression ratio of the library with flate2 +#[test] +fn file_zlib_compare_output() { + use flate2::Compression; + let test_data = get_test_data(); + let flate2_compressed = { + let mut e = flate2::write::ZlibEncoder::new(Vec::new(), Compression::best()); + e.write_all(&test_data).unwrap(); + e.finish().unwrap() + }; + + // { + // use std::fs::File; + // use std::io::Write; + // { + // let mut f = File::create("out.deflate").unwrap(); + // f.write_all(&deflate_compressed).unwrap(); + // } + // { + // let mut f = File::create("out.flate2").unwrap(); + // f.write_all(&flate2_compressed).unwrap(); + // } + // } + + println!("flate2 len: {}", flate2_compressed.len(),); + + roundtrip_conf(&test_data, CompressionOptions::high()); +} + +#[test] +fn block_type() { + let test_file = "tests/short.bin"; + let test_data = get_test_file_data(test_file); + let compressed = deflate::deflate_bytes_zlib(&test_data); + assert_eq!(compressed.len(), 30); + + roundtrip(b"test"); +} + +#[test] +fn issue_17() { + // This is window size + 1 bytes long which made the hash table + // slide when there was only the two end-bytes that don't need to be hashed left + // and triggered an assertion. + let data = vec![0; 65537]; + + roundtrip(&data); +} + +#[test] +fn fast() { + let test_data = get_test_data(); + roundtrip_conf(&test_data, CompressionOptions::fast()); +} + +#[test] +fn rle() { + use deflate::{deflate_bytes_conf, CompressionOptions}; + let test_data = get_test_data(); + let compressed = deflate_bytes_conf(&test_data, CompressionOptions::rle()); + let decompressed = { + let mut d = flate2::read::DeflateDecoder::new(compressed.as_slice()); + let mut out = Vec::new(); + d.read_to_end(&mut out).unwrap(); + out + }; + + println!("Input size: {}", test_data.len()); + println!("Rle compressed len: {}", compressed.len()); + + assert!(test_data == decompressed); +} + +#[test] +fn issue_26() { + use deflate::write::ZlibEncoder; + let fp = Vec::new(); + let mut fp = ZlibEncoder::new( fp, CompressionOptions::default() ); + + fp.write( &[0] ).unwrap(); + fp.flush().unwrap(); + fp.write( &[0] ).unwrap(); + fp.write( &[0, 0] ).unwrap(); +} + +#[cfg(feature = "gzip")] +#[test] +fn issue_26_gzip() { + use deflate::write::DeflateEncoder; + let fp = Vec::new(); + let mut fp = DeflateEncoder::new( fp, CompressionOptions::default() ); + + fp.write( &[0] ).unwrap(); + fp.flush().unwrap(); + fp.write( &[0] ).unwrap(); + fp.write( &[0, 0] ).unwrap(); + +} |