1 #![no_std]
2
3 #[cfg(feature = "alloc")]
4 extern crate alloc;
5 extern crate pretty_hex;
6
7 #[cfg(feature = "alloc")]
8 use alloc::{format, string::String, vec, vec::Vec};
9 use pretty_hex::*;
10
11 #[cfg(feature = "alloc")]
12 #[test]
test_simple()13 fn test_simple() {
14 let bytes: Vec<u8> = (0..16).collect();
15 let expected = "00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f";
16 assert_eq!(expected, simple_hex(&bytes));
17 assert_eq!(expected, format!("{}", bytes.hex_dump()));
18 assert_eq!(simple_hex(&bytes), config_hex(&bytes, HexConfig::simple()));
19
20 let mut have = String::new();
21 simple_hex_write(&mut have, &bytes).unwrap();
22 assert_eq!(expected, have);
23
24 let str = "string";
25 let string: String = String::from("string");
26 let slice: &[u8] = &[0x73, 0x74, 0x72, 0x69, 0x6e, 0x67];
27 assert_eq!(simple_hex(&str), "73 74 72 69 6e 67");
28 assert_eq!(simple_hex(&str), simple_hex(&string));
29 assert_eq!(simple_hex(&str), simple_hex(&slice));
30
31 assert!(simple_hex(&vec![]).is_empty());
32 }
33
34 #[cfg(feature = "alloc")]
35 #[test]
test_pretty()36 fn test_pretty() {
37 let bytes: Vec<u8> = (0..256).map(|x| x as u8).collect();
38 let want = include_str!("256.txt");
39
40 let mut hex = String::new();
41 pretty_hex_write(&mut hex, &bytes).unwrap();
42 assert_eq!(want, hex);
43 assert_eq!(want, format!("{:?}", bytes.hex_dump()));
44 assert_eq!(want, pretty_hex(&bytes));
45 assert_eq!(want, config_hex(&bytes, HexConfig::default()));
46
47 assert_eq!("Length: 0 (0x0) bytes\n", pretty_hex(&vec![]));
48 }
49
50 #[cfg(feature = "alloc")]
51 #[test]
test_config()52 fn test_config() {
53 let cfg = HexConfig {
54 title: false,
55 ascii: false,
56 width: 0,
57 group: 0,
58 chunk: 0,
59 };
60 assert!(config_hex(&vec![], cfg).is_empty());
61 assert_eq!("2425262728", config_hex(&"$%&'(", cfg));
62
63 let v = include_bytes!("data");
64 let cfg = HexConfig {
65 title: false,
66 group: 8,
67 ..HexConfig::default()
68 };
69 let hex =
70 "0000: 6b 4e 1a c3 af 03 d2 1e 7e 73 ba c8 bd 84 0f 83 kN......~s......\n\
71 0010: 89 d5 cf 90 23 67 4b 48 db b1 bc 35 bf ee ....#gKH...5..";
72 assert_eq!(hex, config_hex(&v, cfg));
73 assert_eq!(hex, format!("{:?}", v.hex_conf(cfg)));
74 let mut str = String::new();
75 hex_write(&mut str, v, cfg).unwrap();
76 assert_eq!(hex, str);
77
78 assert_eq!(
79 config_hex(&v, HexConfig{ascii: false, ..cfg}),
80 "0000: 6b 4e 1a c3 af 03 d2 1e 7e 73 ba c8 bd 84 0f 83\n\
81 0010: 89 d5 cf 90 23 67 4b 48 db b1 bc 35 bf ee"
82 );
83
84 assert_eq!(
85 config_hex(&v,
86 HexConfig {
87 ascii: false,
88 group: 4,
89 chunk: 2,
90 ..cfg
91 }
92 ),
93 "0000: 6b4e 1ac3 af03 d21e 7e73 bac8 bd84 0f83\n\
94 0010: 89d5 cf90 2367 4b48 dbb1 bc35 bfee"
95 );
96
97 let v: Vec<u8> = (0..21).collect();
98 let want = r##"Length: 21 (0x15) bytes
99 0000: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f ................
100 0010: 10 11 12 13 14 ....."##;
101 assert_eq!(want, pretty_hex(&v));
102
103 let v: Vec<u8> = (0..13).collect();
104 assert_eq!(
105 config_hex(&v,
106 HexConfig {
107 title: false,
108 ascii: true,
109 width: 11,
110 group: 2,
111 chunk: 3
112 }),
113 "0000: 000102 030405 060708 090a ...........\n\
114 000b: 0b0c .."
115 );
116
117 let v: Vec<u8> = (0..19).collect();
118 assert_eq!(
119 config_hex(&v,
120 HexConfig {
121 title: false,
122 ascii: true,
123 width: 16,
124 group: 3,
125 chunk: 3
126 }
127 ),
128 "0000: 000102 030405 060708 090a0b 0c0d0e 0f ................\n\
129 0010: 101112 ..."
130 );
131
132 let cfg = HexConfig {
133 title: false,
134 group: 0,
135 ..HexConfig::default()
136 };
137 assert_eq!(
138 format!("{:?}", v.hex_conf(cfg)),
139 "0000: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f ................\n\
140 0010: 10 11 12 ..."
141 );
142 assert_eq!(
143 format!("{}", v.hex_conf(cfg)),
144 "00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12"
145 );
146 }
147
148 // This test case checks that hex_write works even without the alloc crate.
149 // Decorators to this function like simple_hex_write or PrettyHex::hex_dump()
150 // will be tested when the alloc feature is selected because it feels quite
151 // cumbersome to set up these tests without the comodity from `alloc`.
152 #[test]
test_hex_write_with_simple_config()153 fn test_hex_write_with_simple_config() {
154 let config = HexConfig::simple();
155 let bytes = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
156 let expected = core::str::from_utf8(b"00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f").unwrap();
157 let mut buffer = heapless::Vec::<u8, heapless::consts::U50>::new();
158
159 hex_write(&mut buffer, &bytes, config).unwrap();
160
161 let have = core::str::from_utf8(&buffer).unwrap();
162 assert_eq!(expected, have);
163 }
164