huff_tree_tap/
encoding_stats.rs

1/// Encoding stats for a given data size and encoded data size
2#[derive(Debug, PartialEq)]
3pub struct EncodingStats {
4    /// Size of the data
5    pub data_size: f32,
6    /// Size of the encoded data
7    pub encoded_size: f32,
8    /// Compression ratio
9    pub ratio: f32,
10}
11
12impl EncodingStats {
13    /// Returns the `EncodingStats` for a given set of data and its encoded version
14    ///
15    /// # Arguments
16    ///
17    /// * `data` - A reference to `Vec<u8>` containing the data
18    /// * `encoded_data` - A reference to `Vec<u8>` containing the data encoded
19    pub fn new(data: &[u8], encoded_data: &[u8]) -> EncodingStats {
20        let data_size = (data.len() * 8) as f32;
21        let encoded_size = (encoded_data.len() * 8) as f32;
22        let ratio = (1_f32 - (encoded_size / data_size)) * 100_f32;
23        EncodingStats {
24            data_size,
25            encoded_size,
26            ratio,
27        }
28    }
29}
30
31#[cfg(test)]
32mod tests {
33
34    use super::*;
35
36    #[test]
37    fn test_encoding_stats() {
38        let data = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
39        let encoded_data = vec![1, 2, 3, 4, 5];
40        let expected_data = EncodingStats {
41            data_size: 80_f32,
42            encoded_size: 40_f32,
43            ratio: 50_f32,
44        };
45
46        let test_ouput = EncodingStats::new(&data, &encoded_data);
47
48        assert_eq!(expected_data, test_ouput);
49    }
50}