More tests

This commit is contained in:
Dennis Schwerdel 2017-06-27 15:25:20 +02:00
parent 8197c70697
commit 18891198f1
2 changed files with 169 additions and 1 deletions

View File

@ -7,7 +7,7 @@ pub fn to_file_size(size: u64) -> String {
if size >= 512.0 {
size /= 1024.0;
} else {
return format!("{:.0} Bytes", size);
return format!("{:.0} Byte", size);
}
if size >= 512.0 {
size /= 1024.0;
@ -80,3 +80,52 @@ impl<T: Iterator> Iterator for ProgressIter<T> {
}
}
}
mod tests {
#[allow(unused_imports)]
use super::*;
#[test]
fn test_to_file_size() {
assert_eq!("0 Byte", to_file_size(0));
assert_eq!("1 Byte", to_file_size(1));
assert_eq!("15 Byte", to_file_size(15));
assert_eq!("456 Byte", to_file_size(456));
assert_eq!("0.7 KiB", to_file_size(670));
assert_eq!("237.0 KiB", to_file_size(242670));
assert_eq!("442.5 KiB", to_file_size(453170));
assert_eq!("0.7 MiB", to_file_size(753170));
assert_eq!("12.2 MiB", to_file_size(12753170));
assert_eq!("222.0 MiB", to_file_size(232753170));
assert_eq!("5.1 GiB", to_file_size(5435353170));
assert_eq!("291.1 GiB", to_file_size(312534553170));
assert_eq!("3.9 TiB", to_file_size(4312534553170));
}
#[test]
fn test_to_speed() {
assert_eq!("0 Byte/s", to_speed(0, 1.0));
assert_eq!("100 Byte/s", to_speed(100, 1.0));
assert_eq!("1.0 KiB/s", to_speed(100, 0.1));
assert_eq!("10 Byte/s", to_speed(100, 10.0));
assert_eq!("237.0 KiB/s", to_speed(242670, 1.0));
assert_eq!("0.7 MiB/s", to_speed(753170, 1.0));
assert_eq!("222.0 MiB/s", to_speed(232753170, 1.0));
assert_eq!("291.1 GiB/s", to_speed(312534553170, 1.0));
assert_eq!("3.9 TiB/s", to_speed(4312534553170, 1.0));
}
#[test]
fn test_to_duration() {
assert_eq!("0:00:00.0", to_duration(0.0));
assert_eq!("0:00:00.1", to_duration(0.1));
assert_eq!("0:00:01.0", to_duration(1.0));
assert_eq!("0:01:00.0", to_duration(60.0));
assert_eq!("1:00:00.0", to_duration(3600.0));
assert_eq!("2:02:02.2", to_duration(7322.2));
}
}

View File

@ -274,3 +274,122 @@ impl Drop for CompressionStream {
unsafe { squash_object_unref(self.stream as *mut libc::c_void); }
}
}
mod tests {
#[allow(unused_imports)]
use super::*;
#[test]
fn test_parse() {
let method = Compression::from_string("deflate/1").unwrap();
assert_eq!(("deflate", 1), (method.name(), method.level()));
let method = Compression::from_string("zlib/2").unwrap();
assert_eq!(("deflate", 2), (method.name(), method.level()));
let method = Compression::from_string("gzip/3").unwrap();
assert_eq!(("deflate", 3), (method.name(), method.level()));
let method = Compression::from_string("brotli/1").unwrap();
assert_eq!(("brotli", 1), (method.name(), method.level()));
let method = Compression::from_string("lzma/1").unwrap();
assert_eq!(("lzma", 1), (method.name(), method.level()));
let method = Compression::from_string("lzma2/2").unwrap();
assert_eq!(("lzma", 2), (method.name(), method.level()));
let method = Compression::from_string("xz/3").unwrap();
assert_eq!(("lzma", 3), (method.name(), method.level()));
let method = Compression::from_string("lz4/1").unwrap();
assert_eq!(("lz4", 1), (method.name(), method.level()));
}
#[test]
fn test_to_string() {
assert_eq!("brotli/1", Compression::from_string("brotli/1").unwrap().to_string());
assert_eq!("deflate/1", Compression::from_string("gzip/1").unwrap().to_string());
}
#[allow(dead_code, needless_range_loop)]
fn test_data(n: usize) -> Vec<u8> {
let mut input = vec![0; n];
for i in 0..input.len() {
input[i] = (i * i * i) as u8;
}
input
}
#[allow(dead_code)]
fn test_compression(method: &str, min_lvl: u8, max_lvl: u8) {
let input = test_data(16*1024);
for i in min_lvl..max_lvl+1 {
let method = Compression::from_string(&format!("{}/{}", method, i)).unwrap();
println!("{}", method.to_string());
let compressed = method.compress(&input).unwrap();
let decompressed = method.decompress(&compressed).unwrap();
assert_eq!(input.len(), decompressed.len());
for i in 0..input.len() {
assert_eq!(input[i], decompressed[i]);
}
}
}
#[test]
fn test_compression_deflate() {
test_compression("deflate", 1, 9)
}
#[test]
fn test_compression_brotli() {
test_compression("brotli", 1, 11)
}
#[test]
fn test_compression_lzma() {
test_compression("lzma", 1, 9)
}
#[test]
fn test_compression_lz4() {
test_compression("lz4", 1, 11)
}
#[allow(dead_code)]
fn test_stream_compression(method: &str, min_lvl: u8, max_lvl: u8) {
let input = test_data(512*1024);
for i in min_lvl..max_lvl+1 {
let method = Compression::from_string(&format!("{}/{}", method, i)).unwrap();
println!("{}", method.to_string());
let mut compressor = method.compress_stream().unwrap();
let mut compressed = Vec::with_capacity(input.len());
compressor.process(&input, &mut compressed).unwrap();
compressor.finish(&mut compressed).unwrap();
let mut decompressor = method.decompress_stream().unwrap();
let mut decompressed = Vec::with_capacity(input.len());
decompressor.process(&compressed, &mut decompressed).unwrap();
decompressor.finish(&mut decompressed).unwrap();
assert_eq!(input.len(), decompressed.len());
for i in 0..input.len() {
assert_eq!(input[i], decompressed[i]);
}
}
}
#[test]
fn test_stream_compression_deflate() {
test_stream_compression("deflate", 1, 9)
}
#[test]
fn test_stream_compression_brotli() {
test_stream_compression("brotli", 1, 11)
}
#[test]
fn test_stream_compression_lzma() {
test_stream_compression("lzma", 1, 9)
}
#[test]
fn test_stream_compression_lz4() {
test_stream_compression("lz4", 1, 11)
}
}