byte_array_ops/byte_array/
macros.rs

1#[macro_export]
2macro_rules! count_args {
3    () => (0usize);
4    ($x:tt $($xs:tt)*) => (1usize + count_args!($($xs)*));
5}
6
7#[macro_export]
8macro_rules! try_bytes {
9    ($x:literal) => {{
10        use core::str::FromStr;
11        use $crate::ByteArray;
12
13        ByteArray::from_str($x)
14    }};
15}
16
17#[macro_export]
18macro_rules! try_hex {
19    ($x:literal) => {{
20        use $crate::ByteArray;
21
22        ByteArray::from_hex($x)
23    }};
24}
25
26#[macro_export]
27macro_rules! try_bin {
28    ($x:literal) => {{
29        use $crate::ByteArray;
30
31        ByteArray::from_bin($x)
32    }};
33}
34
35#[cfg(test)]
36mod tests {
37    use crate::ByteArray;
38    use crate::errors::ByteArrayError;
39    use alloc::vec;
40
41    #[test]
42    fn test_malformed_input() {
43        let res = try_bytes!("0xZZFEFE").err().unwrap();
44        assert_eq!(res, ByteArrayError::InvalidHexChar('Z'));
45    }
46
47    #[test]
48    fn test_empty_bytes() {
49        let res = try_bytes!("").err().unwrap();
50        assert_eq!(res, ByteArrayError::EmptyInput);
51    }
52
53    #[test]
54    fn test_standard_utf() {
55        let res = try_bytes!("hello").unwrap();
56        assert_eq!(res.as_bytes(), b"hello");
57    }
58
59    #[test]
60    fn test_hex_with_prefix() {
61        let res = try_bytes!("0xDEADBEEF").unwrap();
62        assert_eq!(res.as_bytes(), &[0xDE, 0xAD, 0xBE, 0xEF]);
63    }
64
65    #[test]
66    fn test_hex_without_prefix() {
67        // treated as utf8
68        let res = try_bytes!("DEADBEEF").unwrap();
69        assert_eq!(res.as_bytes(), b"DEADBEEF");
70    }
71
72    #[test]
73    fn test_lowercase_hex() {
74        let res = try_bytes!("0xdeadbeef").unwrap();
75        assert_eq!(res.as_bytes(), &[0xDE, 0xAD, 0xBE, 0xEF]);
76    }
77
78    #[test]
79    fn test_mixed_case_hex() {
80        let res = try_bytes!("0xDeAdBeEf").unwrap();
81        assert_eq!(res.as_bytes(), &[0xDE, 0xAD, 0xBE, 0xEF]);
82    }
83
84    #[test]
85    fn test_single_byte_hex() {
86        let res = try_bytes!("0xFF").unwrap();
87        assert_eq!(res.as_bytes(), &[0xFF]);
88    }
89
90    #[test]
91    fn test_two_byte_hex() {
92        let res = try_bytes!("0xCAFE").unwrap();
93        assert_eq!(res.as_bytes(), &[0xCA, 0xFE]);
94    }
95
96    #[test]
97    fn test_odd_length_hex() {
98        // Test your implementation's behavior with odd-length hex strings
99        let res = try_bytes!("0xFFF");
100        // Adjust assertion based on whether you pad or error
101        assert!(res.is_ok() || res.is_err());
102    }
103
104    #[test]
105    fn test_special_characters_utf8() {
106        let res = try_bytes!("hello world!").unwrap();
107        assert_eq!(res.as_bytes(), b"hello world!");
108    }
109
110    #[test]
111    fn test_unicode_utf8() {
112        let res = try_bytes!("\u{1F980}").unwrap();
113        assert_eq!(res.as_bytes(), &[0xF0, 0x9F, 0xa6, 0x80]);
114    }
115
116    // Test that array construction still works via From/Into
117    #[test]
118    fn test_from_array() {
119        let res = ByteArray::from([0x01, 0x02, 0x03, 0xFF]);
120        assert_eq!(res.as_bytes(), &[0x01, 0x02, 0x03, 0xFF]);
121    }
122
123    #[test]
124    fn test_from_vec() {
125        let res = ByteArray::from(vec![0xDE, 0xAD, 0xBE, 0xEF]);
126        assert_eq!(res.as_bytes(), &[0xDE, 0xAD, 0xBE, 0xEF]);
127    }
128
129    #[test]
130    fn test_from_slice() {
131        let slice: &[u8] = &[0xCA, 0xFE];
132        let res = ByteArray::from(slice);
133        assert_eq!(res.as_bytes(), &[0xCA, 0xFE]);
134    }
135
136    #[test]
137    fn test_try_hex_macro() {
138        let res = try_hex!("deadbeef").unwrap();
139        assert_eq!(res.as_bytes(), &[0xde, 0xad, 0xbe, 0xef]);
140    }
141
142    #[test]
143    fn test_try_bin_macro() {
144        let res = try_bin!("11110000").unwrap();
145        assert_eq!(res.as_bytes(), &[0xf0]);
146    }
147}