1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#[cfg(test)]
#[macro_use]
extern crate proptest;
#[cfg(test)]
extern crate hex;

mod encode;

pub use encode::{hex_string, hex_to};

#[cfg(test)]
mod tests {
    use super::*;
    use encode::hex_string;
    use std::str;

    fn _test_hex(s: &String) {
        let mut buffer = vec![0; s.as_bytes().len() * 2];
        hex_to(s.as_bytes(), &mut buffer, false).unwrap();
        let encode = unsafe { str::from_utf8_unchecked(&buffer[..s.as_bytes().len() * 2]) };

        let hex_string = hex_string(s.as_bytes(), false).unwrap();

        assert_eq!(encode, hex::encode(s));
        assert_eq!(hex_string, hex::encode(s));
    }

    proptest! {
        #[test]
        fn test_hex(ref s in ".*") {
            _test_hex(s);
        }
    }

    #[test]
    fn test_trim_left_zero() {
        let s = [1];
        let string = hex_string(&s, false).unwrap();
        let trimed = hex_string(&s, true).unwrap();

        assert_eq!(string, "01".to_string());
        assert_eq!(trimed, "1".to_string());
    }
}