2 use crate::hex::{FromHex, ToHex};
7 assert_eq!("foobar".as_bytes().to_hex(), "666f6f626172");
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");
17 pub fn test_from_hex_odd_len() {
18 assert!("666".from_hex().is_err());
19 assert!("66 6".from_hex().is_err());
23 pub fn test_from_hex_invalid_char() {
24 assert!("66y6".from_hex().is_err());
28 pub fn test_from_hex_ignores_whitespace() {
29 assert_eq!("666f 6f6\r\n26172 ".from_hex().unwrap(), b"foobar");
33 pub fn test_to_hex_all_bytes() {
35 assert_eq!([i as u8].to_hex(), format!("{:02x}", i as usize));
40 pub fn test_from_hex_all_bytes() {
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);
49 pub fn bench_to_hex(b: &mut Bencher) {
50 let s = "イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム \
51 ウヰノオクヤマ ケフコエテ アサキユメミシ ヱヒモセスン";
53 s.as_bytes().to_hex();
55 b.bytes = s.len() as u64;
59 pub fn bench_from_hex(b: &mut Bencher) {
60 let s = "イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム \
61 ウヰノオクヤマ ケフコエテ アサキユメミシ ヱヒモセスン";
62 let sb = s.as_bytes().to_hex();
64 sb.from_hex().unwrap();
66 b.bytes = sb.len() as u64;