1use std::error::Error;
6use std::io::prelude::*;
7pub mod huffman;
8pub mod lzw;
9
10pub trait Compressor {
12 fn encode<R: Read + Seek, W: Write + Seek>(input: &mut R, output: &mut W) -> Result<(), Box<dyn Error>>;
13 fn decode<R: Read + Seek, W: Write + Seek>(input: &mut R, output: &mut W) -> Result<(), Box<dyn Error>>;
14}
15
16#[cfg(test)]
17mod tests {
18 use std::fs::File;
19 use std::io::{prelude::*};
20 use tempfile::*;
21 use super::huffman::Huffman;
22 use super::Compressor;
23 use super::lzw::LZW;
24
25 #[test]
26 fn log_huffman_performance() {
27 let mut log = File::create("../documentation/huffman_stats.txt").unwrap();
28 let mut big1 = File::open("../testfiles/big1.txt").unwrap();
29 let mut big2 = File::open("../testfiles/big2.txt").unwrap();
30 let mut big3 = File::open("../testfiles/big3.txt").unwrap();
31 let mut temp1 = tempfile().unwrap();
32 let mut temp2 = tempfile().unwrap();
33 let mut temp3 = tempfile().unwrap();
34 Huffman::encode(&mut big1, &mut temp1).unwrap();
35 Huffman::encode(&mut big2, &mut temp2).unwrap();
36 Huffman::encode(&mut big3, &mut temp3).unwrap();
37 let orig1 = big1.metadata().unwrap().len();
38 let orig2 = big2.metadata().unwrap().len();
39 let orig3 = big3.metadata().unwrap().len();
40 let comp1 = temp1.metadata().unwrap().len();
41 let comp2 = temp2.metadata().unwrap().len();
42 let comp3 = temp3.metadata().unwrap().len();
43 let perc1: f64 = (comp1 as f64/orig1 as f64) * 100 as f64;
44 let perc2: f64 = (comp2 as f64/orig2 as f64) * 100 as f64;
45 let perc3: f64 = (comp3 as f64/orig3 as f64) * 100 as f64;
46 let res1 = format!("\nbig1.txt: {} bytes\ncomp1: {} bytes\n{:.1}% of original\n", orig1, comp1, perc1);
47 let res2 = format!("\nbig2.txt: {} bytes\ncomp2: {} bytes\n{:.1}% of original\n", orig2, comp2, perc2);
48 let res3 = format!("\nbig3.txt: {} bytes\ncomp3: {} bytes\n{:.1}% of original\n", orig3, comp3, perc3);
49 log.write(res1.as_bytes()).unwrap();
50 log.write(res2.as_bytes()).unwrap();
51 log.write(res3.as_bytes()).unwrap();
52 }
53
54 #[test]
55 fn log_lzw_performance() {
56 let mut log = File::create("../documentation/lzw_stats.txt").unwrap();
57 let mut big1 = File::open("../testfiles/big1.txt").unwrap();
58 let mut big2 = File::open("../testfiles/big2.txt").unwrap();
59 let mut big3 = File::open("../testfiles/big3.txt").unwrap();
60 let mut temp1 = tempfile().unwrap();
61 let mut temp2 = tempfile().unwrap();
62 let mut temp3 = tempfile().unwrap();
63 LZW::encode(&mut big1, &mut temp1).unwrap();
64 LZW::encode(&mut big2, &mut temp2).unwrap();
65 LZW::encode(&mut big3, &mut temp3).unwrap();
66 let orig1 = big1.metadata().unwrap().len();
67 let orig2 = big2.metadata().unwrap().len();
68 let orig3 = big3.metadata().unwrap().len();
69 let comp1 = temp1.metadata().unwrap().len();
70 let comp2 = temp2.metadata().unwrap().len();
71 let comp3 = temp3.metadata().unwrap().len();
72 let perc1: f64 = (comp1 as f64/orig1 as f64) * 100 as f64;
73 let perc2: f64 = (comp2 as f64/orig2 as f64) * 100 as f64;
74 let perc3: f64 = (comp3 as f64/orig3 as f64) * 100 as f64;
75 let res1 = format!("\norig1: {} bytes\ncomp1: {} bytes\n{:.1}% of original\n", orig1, comp1, perc1);
76 let res2 = format!("\norig2: {} bytes\ncomp2: {} bytes\n{:.1}% of original\n", orig2, comp2, perc2);
77 let res3 = format!("\norig3: {} bytes\ncomp3: {} bytes\n{:.1}% of original\n", orig3, comp3, perc3);
78 log.write(res1.as_bytes()).unwrap();
79 log.write(res2.as_bytes()).unwrap();
80 log.write(res3.as_bytes()).unwrap();
81 }
82}