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