byte_size/
tests.rs

1#[cfg(test)]
2mod tests {
3    use crate::builder::Builder;
4    use crate::engine::Engine;
5    use crate::iterator::CodeIterator;
6
7    fn full_ser_deser_builder(string: &str, engine: & Engine, compressed_size: usize) {
8        use smaz::compress;
9
10
11
12
13        let bytes = engine.compress(string);
14
15        let x = engine.decompress(bytes.as_slice()).unwrap();
16
17        let smaz_len = compress(string.as_bytes()).len();
18        let code_len = bytes.len();
19
20        println!("String: '{}' ({:?})", string, string);
21        println!("    Original Size:         {}", string.len());
22        println!("    Compression:           {:?}", bytes);
23        println!("    Code Points:           {:?}", CodeIterator::new(string, &engine).collect::<Vec<_>>());
24        println!("    Compression size:      {} ({}% compression ratio)", code_len, 100f32 - code_len as f32 / string.as_bytes().len() as f32 * 100f32);
25        println!("    Smaz Compression size: {} ({}% compression ratio)", smaz_len, 100f32 - smaz_len as f32 / string.as_bytes().len() as f32 * 100f32);
26
27        //First make sure that the decompression worked correctly
28        assert_eq!(string, x.as_str());
29
30        //Next we make sure the size hasn't changed. This means if we change the algorithm, we can catch any changes
31        assert_eq!(code_len, compressed_size);
32
33        //Finally we see if the compressed size is smaller than smaz
34        assert!(code_len <= smaz_len);
35
36    }
37
38    #[test]
39    fn time() {
40        for _ in 0..1000 {
41            Builder::empty().engine().decompress(Builder::empty().engine().compress("The quick brown fox jumped over the lazy dog").as_slice()).unwrap();
42        }
43    }
44
45    fn full_ser_deser(string: & str, compressed_size: usize) {
46        full_ser_deser_builder(string, &Builder::default().engine(), compressed_size)
47    }
48
49    #[test]
50    fn number_test() {
51        full_ser_deser("6709376338672", 8);
52    }
53
54    #[test]
55    fn number_test1() {
56        full_ser_deser("6709323423423763138672", 13);
57    }
58
59    #[test]
60    fn number_test2() {
61        full_ser_deser("999", 3);
62    }
63
64    #[test]
65    fn number_test3() {
66        full_ser_deser("1000", 3);
67    }
68
69    #[test]
70    fn number_test4() {
71        full_ser_deser("1000 ", 4);
72    }
73
74    #[test]
75    fn http1() {
76        full_ser_deser("http://google.com", 6);
77    }
78
79    #[test]
80    fn http2() {
81        full_ser_deser("http://programming.reddit.com", 11);
82    }
83
84    #[test]
85    fn http3() {
86        full_ser_deser_builder("http://github.com/antirez/smaz/tree/master", &Builder::default().push_custom("http://github.com/").engine() , 16);
87    }
88
89    #[test]
90    fn patch() {
91        full_ser_deser("patch", 3);
92    }
93
94    #[test]
95    fn ascii_control_test() {
96        full_ser_deser("\x01", 2);
97    }
98
99    #[test]
100    fn repetition_test() {
101        full_ser_deser("hehehehe", 3);
102    }
103
104    #[test]
105    fn repetition_test1() {
106        full_ser_deser("he he he he ", 3);
107    }
108
109    #[test]
110    fn repetition_test2() {
111        full_ser_deser("oohe he he he he he ", 7);
112    }
113
114    #[test]
115    fn repetition_test3() {
116        full_ser_deser("hhhhhhhhhhhhhhohhhhhhhhhhh", 8);
117    }
118
119    #[test]
120    fn ascii_lf() {
121        full_ser_deser("\n", 1);
122    }
123
124    #[test]
125    fn ascii_crlf() {
126        full_ser_deser("\r\n", 1);
127    }
128
129    #[test]
130    fn message_test() {
131        full_ser_deser("yeh thats fine mate 🙂", 17);
132    }
133
134    #[test]
135    fn unicode_test() {
136        full_ser_deser("✔️ ❤️ ☆", 22);
137    }
138
139    #[test]
140    fn fox() {
141        full_ser_deser("The quick brown fox jumped over the lazy dog", 20);
142    }
143
144    #[test]
145    fn therefore() {
146        full_ser_deser("therefore", 2);
147    }
148
149    #[test]
150    fn download() {
151        full_ser_deser("download", 2);
152    }
153
154    #[test]
155    fn smaz1() {
156        full_ser_deser("This is a small string", 9);
157    }
158
159    #[test]
160    fn smaz2() {
161        full_ser_deser("foobar", 4);
162    }
163
164    #[test]
165    fn smaz3() {
166        full_ser_deser("the end", 3);
167    }
168
169    #[test]
170    fn smaz4() {
171        full_ser_deser("not-a-g00d-Exampl333", 18);
172    }
173
174    #[test]
175    fn smaz5() {
176        full_ser_deser("Smaz is a simple compression library", 13);
177    }
178
179    #[test]
180    fn smaz6() {
181        full_ser_deser("Nothing is more difficult, and therefore more precious, than to be able to decide", 32);
182    }
183
184    #[test]
185    fn smaz7() {
186        full_ser_deser("this is an example of what works very well with smaz", 23);
187    }
188
189    #[test]
190    fn smaz8() {
191        full_ser_deser("1000 numbers 2000 will 10 20 30 compress very little", 28);
192    }
193
194    #[test]
195    fn smaz9() {
196        full_ser_deser("Smaz is a simple compression library suitable for compressing very short
197strings. General purpose compression libraries will build the state needed
198for compressing data dynamically, in order to be able to compress every kind
199of data. This is a very good idea, but not for a specific problem: compressing
200small strings will not work.", 127);
201    }
202
203    #[test]
204    fn smaz10() {
205        full_ser_deser("small string shrinker", 8);
206    }
207
208    #[test]
209    fn test_custom_space() {
210        full_ser_deser_builder(" customstringspacetest", &Builder::default().set_custom_spaces(true).push_custom("customstringspacetest").engine(), 2)
211    }
212
213    #[test]
214    fn test_custom_space2() {
215        full_ser_deser_builder(" customstringspacetest", &Builder::default().set_custom_spaces(false).push_custom("customstringspacetest").engine(), 9)
216    }
217
218    #[test]
219    fn test_custom_space3() {
220        full_ser_deser_builder("customstringspacetest", &Builder::default().set_custom_spaces(false).push_custom("customstringspacetest").engine(), 2)
221    }
222
223    #[test]
224    #[should_panic]
225    fn test_bad_double() {
226        crate::engine::decompress([255].as_slice()).unwrap();
227    }
228
229    #[test]
230    #[should_panic]
231    fn test_bad_unicode() {
232        crate::engine::decompress([240, 0x80, 0x81].as_slice()).unwrap();
233    }
234
235    #[test]
236    fn test_lengths() {
237        let mut count = 0;
238
239        let str = std::fs::read_to_string(".\\.3m.txt").unwrap();
240
241        for line in str.lines().take(10000) {
242            let line = line.split_whitespace().nth(0).unwrap();
243
244            let code_len = crate::engine::compress(line).len();
245
246            if code_len > 3 {
247                println!("{}: {:?}", line, CodeIterator::new(line, &Builder::default().engine()).collect::<Vec<_>>());
248                count += 1;
249            }
250
251
252        }
253
254        assert_eq!(count, 0)
255    }
256
257    #[test]
258    fn test_list() {
259        use smaz::compress;
260
261        let str = std::fs::read_to_string(".\\.3m.txt").unwrap();
262
263        let max = 10000;
264        let mut count = 0;
265        let mut total = 0;
266
267
268        for line in str.lines() {
269            let line = line.split_whitespace().nth(0).unwrap();
270
271            let code_len = crate::engine::compress(line).len();
272
273            let smaz_len = compress(line.as_bytes()).len();
274
275            if code_len > smaz_len {
276                count += 1;
277            }
278
279            total += 1;
280            if total >= max {
281                println!("Scanned the first {} words out of .3m.txt, with {} times smaz was better ({}%)", total, count, count as f32 / total as f32 * 100f32);
282                assert!(count as f32 / total as f32 * 100f32 < 1f32);
283                break;
284            }
285        }
286
287    }
288
289
290}