]> git.lizzy.rs Git - rust.git/blob - src/libserialize/hex/tests.rs
Rollup merge of #68440 - matthiaskrgr:xpyclippy, r=Mark-Simulacrum
[rust.git] / src / libserialize / hex / tests.rs
1 extern crate test;
2 use crate::hex::{FromHex, ToHex};
3 use test::Bencher;
4
5 #[test]
6 pub fn test_to_hex() {
7     assert_eq!("foobar".as_bytes().to_hex(), "666f6f626172");
8 }
9
10 #[test]
11 pub fn test_from_hex_okay() {
12     assert_eq!("666f6f626172".from_hex().unwrap(), b"foobar");
13     assert_eq!("666F6F626172".from_hex().unwrap(), b"foobar");
14 }
15
16 #[test]
17 pub fn test_from_hex_odd_len() {
18     assert!("666".from_hex().is_err());
19     assert!("66 6".from_hex().is_err());
20 }
21
22 #[test]
23 pub fn test_from_hex_invalid_char() {
24     assert!("66y6".from_hex().is_err());
25 }
26
27 #[test]
28 pub fn test_from_hex_ignores_whitespace() {
29     assert_eq!("666f 6f6\r\n26172 ".from_hex().unwrap(), b"foobar");
30 }
31
32 #[test]
33 pub fn test_to_hex_all_bytes() {
34     for i in 0..256 {
35         assert_eq!([i as u8].to_hex(), format!("{:02x}", i as usize));
36     }
37 }
38
39 #[test]
40 pub fn test_from_hex_all_bytes() {
41     for i in 0..256 {
42         let ii: &[u8] = &[i as u8];
43         assert_eq!(format!("{:02x}", i as usize).from_hex().unwrap(), ii);
44         assert_eq!(format!("{:02X}", i as usize).from_hex().unwrap(), ii);
45     }
46 }
47
48 #[bench]
49 pub fn bench_to_hex(b: &mut Bencher) {
50     let s = "イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム \
51              ウヰノオクヤマ ケフコエテ アサキユメミシ ヱヒモセスン";
52     b.iter(|| {
53         s.as_bytes().to_hex();
54     });
55     b.bytes = s.len() as u64;
56 }
57
58 #[bench]
59 pub fn bench_from_hex(b: &mut Bencher) {
60     let s = "イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム \
61              ウヰノオクヤマ ケフコエテ アサキユメミシ ヱヒモセスン";
62     let sb = s.as_bytes().to_hex();
63     b.iter(|| {
64         sb.from_hex().unwrap();
65     });
66     b.bytes = sb.len() as u64;
67 }