#[doc(hidden)]
#[macro_export]
macro_rules! count_args {
() => (0usize);
($x:tt $($xs:tt)*) => (1usize + count_args!($($xs)*));
}
#[macro_export]
macro_rules! try_bytes {
($x:literal) => {{
use core::str::FromStr;
use $crate::ByteArray;
ByteArray::from_str($x)
}};
}
#[macro_export]
macro_rules! try_hex {
($x:literal) => {{
use $crate::ByteArray;
ByteArray::from_hex($x)
}};
}
#[macro_export]
macro_rules! try_bin {
($x:literal) => {{
use $crate::ByteArray;
ByteArray::from_bin($x)
}};
}
#[macro_export]
macro_rules! bytes {
[$pat:expr;$len:expr] => {{
use $crate::ByteArray;
ByteArray::init_value($pat,$len)
}};
}
#[cfg(test)]
mod tests {
use crate::ByteArray;
use crate::errors::ByteArrayError;
use alloc::vec;
#[test]
fn test_malformed_input() {
let res = try_bytes!("0xZZFEFE").err().unwrap();
assert_eq!(res, ByteArrayError::InvalidHexChar('Z'));
}
#[test]
fn test_empty_bytes() {
let res = try_bytes!("").err().unwrap();
assert_eq!(res, ByteArrayError::EmptyInput);
}
#[test]
fn test_standard_utf() {
let res = try_bytes!("hello").unwrap();
assert_eq!(res.as_bytes(), b"hello");
}
#[test]
fn test_hex_with_prefix() {
let res = try_bytes!("0xDEADBEEF").unwrap();
assert_eq!(res.as_bytes(), &[0xDE, 0xAD, 0xBE, 0xEF]);
}
#[test]
fn test_hex_without_prefix() {
let res = try_bytes!("DEADBEEF").unwrap();
assert_eq!(res.as_bytes(), b"DEADBEEF");
}
#[test]
fn test_lowercase_hex() {
let res = try_bytes!("0xdeadbeef").unwrap();
assert_eq!(res.as_bytes(), &[0xDE, 0xAD, 0xBE, 0xEF]);
}
#[test]
fn test_mixed_case_hex() {
let res = try_bytes!("0xDeAdBeEf").unwrap();
assert_eq!(res.as_bytes(), &[0xDE, 0xAD, 0xBE, 0xEF]);
}
#[test]
fn test_single_byte_hex() {
let res = try_bytes!("0xFF").unwrap();
assert_eq!(res.as_bytes(), &[0xFF]);
}
#[test]
fn test_two_byte_hex() {
let res = try_bytes!("0xCAFE").unwrap();
assert_eq!(res.as_bytes(), &[0xCA, 0xFE]);
}
#[test]
fn test_odd_length_hex() {
let res = try_bytes!("0xFFF");
assert!(res.is_ok() || res.is_err());
}
#[test]
fn test_special_characters_utf8() {
let res = try_bytes!("hello world!").unwrap();
assert_eq!(res.as_bytes(), b"hello world!");
}
#[test]
fn test_unicode_utf8() {
let res = try_bytes!("\u{1F980}").unwrap();
assert_eq!(res.as_bytes(), &[0xF0, 0x9F, 0xa6, 0x80]);
}
#[test]
fn test_from_array() {
let res = ByteArray::from([0x01, 0x02, 0x03, 0xFF]);
assert_eq!(res.as_bytes(), &[0x01, 0x02, 0x03, 0xFF]);
}
#[test]
fn test_from_vec() {
let res = ByteArray::from(vec![0xDE, 0xAD, 0xBE, 0xEF]);
assert_eq!(res.as_bytes(), &[0xDE, 0xAD, 0xBE, 0xEF]);
}
#[test]
fn test_from_slice() {
let slice: &[u8] = &[0xCA, 0xFE];
let res = ByteArray::from(slice);
assert_eq!(res.as_bytes(), &[0xCA, 0xFE]);
}
#[test]
fn test_try_hex_macro() {
let res = try_hex!("deadbeef").unwrap();
assert_eq!(res.as_bytes(), &[0xde, 0xad, 0xbe, 0xef]);
}
#[test]
fn test_try_bin_macro() {
let res = try_bin!("11110000").unwrap();
assert_eq!(res.as_bytes(), &[0xf0]);
}
#[test]
fn test_bytes_macro_zeros() {
let bytes = bytes![0;0];
assert_eq!(bytes, ByteArray::default());
}
#[test]
fn test_bytes_macro_pat_zero() {
let bytes = bytes![8u8;0];
assert_eq!(bytes, ByteArray::default());
}
#[test]
fn test_bytes_macro_general() {
let bytes = bytes![0xff;1065];
assert_eq!(bytes.as_bytes(), vec![0xff; 1065]);
}
}