1#[doc(hidden)]
2#[macro_export]
3macro_rules! count_args {
4 () => (0usize);
5 ($x:tt $($xs:tt)*) => (1usize + count_args!($($xs)*));
6}
7
8#[macro_export]
38macro_rules! try_bytes {
39 ($x:literal) => {{
40 use core::str::FromStr;
41 use $crate::ByteArray;
42
43 ByteArray::from_str($x)
44 }};
45}
46
47#[macro_export]
61macro_rules! try_hex {
62 ($x:literal) => {{
63 use $crate::ByteArray;
64
65 ByteArray::from_hex($x)
66 }};
67}
68
69#[macro_export]
83macro_rules! try_bin {
84 ($x:literal) => {{
85 use $crate::ByteArray;
86
87 ByteArray::from_bin($x)
88 }};
89}
90
91#[macro_export]
102macro_rules! bytes {
103 [$pat:expr;$len:expr] => {{
104 use $crate::ByteArray;
105
106 ByteArray::init_value($pat,$len)
107 }};
108}
109
110#[cfg(test)]
111mod tests {
112 use crate::ByteArray;
113 use crate::errors::ByteArrayError;
114 use alloc::vec;
115
116 #[test]
117 fn test_malformed_input() {
118 let res = try_bytes!("0xZZFEFE").err().unwrap();
119 assert_eq!(res, ByteArrayError::InvalidHexChar('Z'));
120 }
121
122 #[test]
123 fn test_empty_bytes() {
124 let res = try_bytes!("").err().unwrap();
125 assert_eq!(res, ByteArrayError::EmptyInput);
126 }
127
128 #[test]
129 fn test_standard_utf() {
130 let res = try_bytes!("hello").unwrap();
131 assert_eq!(res.as_bytes(), b"hello");
132 }
133
134 #[test]
135 fn test_hex_with_prefix() {
136 let res = try_bytes!("0xDEADBEEF").unwrap();
137 assert_eq!(res.as_bytes(), &[0xDE, 0xAD, 0xBE, 0xEF]);
138 }
139
140 #[test]
141 fn test_hex_without_prefix() {
142 let res = try_bytes!("DEADBEEF").unwrap();
144 assert_eq!(res.as_bytes(), b"DEADBEEF");
145 }
146
147 #[test]
148 fn test_lowercase_hex() {
149 let res = try_bytes!("0xdeadbeef").unwrap();
150 assert_eq!(res.as_bytes(), &[0xDE, 0xAD, 0xBE, 0xEF]);
151 }
152
153 #[test]
154 fn test_mixed_case_hex() {
155 let res = try_bytes!("0xDeAdBeEf").unwrap();
156 assert_eq!(res.as_bytes(), &[0xDE, 0xAD, 0xBE, 0xEF]);
157 }
158
159 #[test]
160 fn test_single_byte_hex() {
161 let res = try_bytes!("0xFF").unwrap();
162 assert_eq!(res.as_bytes(), &[0xFF]);
163 }
164
165 #[test]
166 fn test_two_byte_hex() {
167 let res = try_bytes!("0xCAFE").unwrap();
168 assert_eq!(res.as_bytes(), &[0xCA, 0xFE]);
169 }
170
171 #[test]
172 fn test_odd_length_hex() {
173 let res = try_bytes!("0xFFF");
175 assert!(res.is_ok() || res.is_err());
177 }
178
179 #[test]
180 fn test_special_characters_utf8() {
181 let res = try_bytes!("hello world!").unwrap();
182 assert_eq!(res.as_bytes(), b"hello world!");
183 }
184
185 #[test]
186 fn test_unicode_utf8() {
187 let res = try_bytes!("\u{1F980}").unwrap();
188 assert_eq!(res.as_bytes(), &[0xF0, 0x9F, 0xa6, 0x80]);
189 }
190
191 #[test]
193 fn test_from_array() {
194 let res = ByteArray::from([0x01, 0x02, 0x03, 0xFF]);
195 assert_eq!(res.as_bytes(), &[0x01, 0x02, 0x03, 0xFF]);
196 }
197
198 #[test]
199 fn test_from_vec() {
200 let res = ByteArray::from(vec![0xDE, 0xAD, 0xBE, 0xEF]);
201 assert_eq!(res.as_bytes(), &[0xDE, 0xAD, 0xBE, 0xEF]);
202 }
203
204 #[test]
205 fn test_from_slice() {
206 let slice: &[u8] = &[0xCA, 0xFE];
207 let res = ByteArray::from(slice);
208 assert_eq!(res.as_bytes(), &[0xCA, 0xFE]);
209 }
210
211 #[test]
212 fn test_try_hex_macro() {
213 let res = try_hex!("deadbeef").unwrap();
214 assert_eq!(res.as_bytes(), &[0xde, 0xad, 0xbe, 0xef]);
215 }
216
217 #[test]
218 fn test_try_bin_macro() {
219 let res = try_bin!("11110000").unwrap();
220 assert_eq!(res.as_bytes(), &[0xf0]);
221 }
222
223 #[test]
229 fn test_bytes_macro_zeros() {
230 let bytes = bytes![0;0];
231
232 assert_eq!(bytes, ByteArray::default());
233 }
234
235 #[test]
236 fn test_bytes_macro_pat_zero() {
237 let bytes = bytes![8u8;0];
238
239 assert_eq!(bytes, ByteArray::default());
240 }
241
242 #[test]
243 fn test_bytes_macro_general() {
244 let bytes = bytes![0xff;1065];
245
246 assert_eq!(bytes.as_bytes(), vec![0xff; 1065]);
247 }
248}