1 extern crate deflate;
2 extern crate flate2;
3
4 use std::io::{Write, Read};
5 use deflate::CompressionOptions;
6
get_test_file_data(name: &str) -> Vec<u8>7 fn get_test_file_data(name: &str) -> Vec<u8> {
8 use std::fs::File;
9 let mut input = Vec::new();
10 let mut f = File::open(name).unwrap();
11
12 f.read_to_end(&mut input).unwrap();
13 input
14 }
15
get_test_data() -> Vec<u8>16 fn get_test_data() -> Vec<u8> {
17 use std::env;
18 let path = env::var("TEST_FILE").unwrap_or_else(|_| "tests/pg11.txt".to_string());
19 get_test_file_data(&path)
20 }
21
roundtrip(data: &[u8])22 fn roundtrip(data: &[u8]) {
23 roundtrip_conf(data, CompressionOptions::default())
24 }
25
roundtrip_conf(data: &[u8], level: CompressionOptions)26 fn roundtrip_conf(data: &[u8], level: CompressionOptions) {
27 let compressed = deflate::deflate_bytes_zlib_conf(data, level);
28 println!("Compressed len: {}, level: {:?}", compressed.len(), level);
29 let decompressed = {
30 let mut d = flate2::read::ZlibDecoder::new(compressed.as_slice());
31 let mut out = Vec::new();
32 d.read_to_end(&mut out).unwrap();
33 out
34 };
35 assert!(decompressed.as_slice() == data);
36 }
37
38 // A test comparing the compression ratio of the library with flate2
39 #[test]
file_zlib_compare_output()40 fn file_zlib_compare_output() {
41 use flate2::Compression;
42 let test_data = get_test_data();
43 let flate2_compressed = {
44 let mut e = flate2::write::ZlibEncoder::new(Vec::new(), Compression::best());
45 e.write_all(&test_data).unwrap();
46 e.finish().unwrap()
47 };
48
49 // {
50 // use std::fs::File;
51 // use std::io::Write;
52 // {
53 // let mut f = File::create("out.deflate").unwrap();
54 // f.write_all(&deflate_compressed).unwrap();
55 // }
56 // {
57 // let mut f = File::create("out.flate2").unwrap();
58 // f.write_all(&flate2_compressed).unwrap();
59 // }
60 // }
61
62 println!("flate2 len: {}", flate2_compressed.len(),);
63
64 roundtrip_conf(&test_data, CompressionOptions::high());
65 }
66
67 #[test]
block_type()68 fn block_type() {
69 let test_file = "tests/short.bin";
70 let test_data = get_test_file_data(test_file);
71 let compressed = deflate::deflate_bytes_zlib(&test_data);
72 assert_eq!(compressed.len(), 30);
73
74 roundtrip(b"test");
75 }
76
77 #[test]
issue_17()78 fn issue_17() {
79 // This is window size + 1 bytes long which made the hash table
80 // slide when there was only the two end-bytes that don't need to be hashed left
81 // and triggered an assertion.
82 let data = vec![0; 65537];
83
84 roundtrip(&data);
85 }
86
87 #[test]
fast()88 fn fast() {
89 let test_data = get_test_data();
90 roundtrip_conf(&test_data, CompressionOptions::fast());
91 }
92
93 #[test]
rle()94 fn rle() {
95 use deflate::{deflate_bytes_conf, CompressionOptions};
96 let test_data = get_test_data();
97 let compressed = deflate_bytes_conf(&test_data, CompressionOptions::rle());
98 let decompressed = {
99 let mut d = flate2::read::DeflateDecoder::new(compressed.as_slice());
100 let mut out = Vec::new();
101 d.read_to_end(&mut out).unwrap();
102 out
103 };
104
105 println!("Input size: {}", test_data.len());
106 println!("Rle compressed len: {}", compressed.len());
107
108 assert!(test_data == decompressed);
109 }
110
111 #[test]
issue_26()112 fn issue_26() {
113 use deflate::write::ZlibEncoder;
114 let fp = Vec::new();
115 let mut fp = ZlibEncoder::new( fp, CompressionOptions::default() );
116
117 fp.write( &[0] ).unwrap();
118 fp.flush().unwrap();
119 fp.write( &[0] ).unwrap();
120 fp.write( &[0, 0] ).unwrap();
121 }
122
123 #[cfg(feature = "gzip")]
124 #[test]
issue_26_gzip()125 fn issue_26_gzip() {
126 use deflate::write::DeflateEncoder;
127 let fp = Vec::new();
128 let mut fp = DeflateEncoder::new( fp, CompressionOptions::default() );
129
130 fp.write( &[0] ).unwrap();
131 fp.flush().unwrap();
132 fp.write( &[0] ).unwrap();
133 fp.write( &[0, 0] ).unwrap();
134
135 }
136