use super::{Case, FOLD_CAP, fold, lookup};
#[test]
fn fold_lowercases_ascii_and_leaves_the_rest_alone() {
let mut buf = [0u8; FOLD_CAP];
assert_eq!(fold("BT709", &mut buf), Some(&b"bt709"[..]));
let mut buf = [0u8; FOLD_CAP];
assert_eq!(
fold("Chroma-Derived-NC", &mut buf),
Some(&b"chroma-derived-nc"[..])
);
let mut buf = [0u8; FOLD_CAP];
assert_eq!(fold("yuv420p", &mut buf), Some(&b"yuv420p"[..]));
let mut buf = [0u8; FOLD_CAP];
assert_eq!(fold("İ", &mut buf), Some("İ".as_bytes()));
}
#[test]
fn fold_returns_the_folded_bytes_up_to_capacity() {
let brim = core::str::from_utf8(&[b'X'; FOLD_CAP]).unwrap();
let mut buf = [0u8; FOLD_CAP];
let folded = fold(brim, &mut buf).expect("an input that exactly fills the buffer folds");
assert_eq!(folded.len(), FOLD_CAP);
assert!(folded.iter().all(|b| *b == b'x'));
let mut buf = [0u8; FOLD_CAP];
assert_eq!(fold("", &mut buf), Some(&b""[..]));
}
#[test]
fn an_input_longer_than_any_slug_is_a_miss_not_a_panic() {
let mut buf = [0u8; FOLD_CAP];
let long = core::str::from_utf8(&[b'x'; FOLD_CAP + 1]).unwrap();
assert_eq!(fold(long, &mut buf), None);
}
#[test]
fn lookup_insensitive_matches_fold_unwrap_or() {
for input in ["BT709", "Bt709", "bt709", "Chroma-Derived-NC", ""] {
let mut via_fold = [0u8; FOLD_CAP];
let mut via_lookup = [0u8; FOLD_CAP];
assert_eq!(
lookup(Case::Insensitive, input, &mut via_lookup),
fold(input, &mut via_fold).unwrap_or(input.as_bytes()),
"lookup(Insensitive, {input:?}, _) diverged from fold(...).unwrap_or(...)"
);
}
let long = core::str::from_utf8(&[b'X'; FOLD_CAP + 1]).unwrap();
let mut buf = [0u8; FOLD_CAP];
assert_eq!(lookup(Case::Insensitive, long, &mut buf), long.as_bytes());
}
#[test]
fn lookup_sensitive_takes_the_bytes_verbatim() {
let mut a = [0u8; FOLD_CAP];
let mut b = [0u8; FOLD_CAP];
assert_eq!(lookup(Case::Sensitive, "AVC1", &mut a), b"AVC1");
assert_eq!(lookup(Case::Sensitive, "avc1", &mut b), b"avc1");
let mut a = [0u8; FOLD_CAP];
let mut b = [0u8; FOLD_CAP];
assert_ne!(
lookup(Case::Sensitive, "AVC1", &mut a),
lookup(Case::Sensitive, "avc1", &mut b)
);
}
#[test]
fn lookup_sensitive_has_no_fold_cap_limit() {
let long_bytes = [b'x'; FOLD_CAP * 4];
let long = core::str::from_utf8(&long_bytes).unwrap();
let mut buf = [0u8; FOLD_CAP];
assert_eq!(lookup(Case::Sensitive, long, &mut buf), long.as_bytes());
}
#[cfg(any(feature = "std", feature = "alloc"))]
#[derive(Debug, Clone, PartialEq, Eq)]
enum TestFourCc {
Avc1,
H264,
Other(smol_str::SmolStr),
}
#[cfg(any(feature = "std", feature = "alloc"))]
impl TestFourCc {
fn as_str(&self) -> &str {
match self {
Self::Avc1 => "AVC1",
Self::H264 => "H264",
Self::Other(s) => s.as_str(),
}
}
fn other(slug: impl AsRef<str>) -> Self {
<Self as core::str::FromStr>::from_str(slug.as_ref()).unwrap()
}
}
#[cfg(any(feature = "std", feature = "alloc"))]
impl core::str::FromStr for TestFourCc {
type Err = core::convert::Infallible;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut buf = [0u8; FOLD_CAP];
let key = lookup(Case::Sensitive, s, &mut buf);
Ok(match key {
b"AVC1" => Self::Avc1,
b"H264" => Self::H264,
_ => Self::Other(smol_str::SmolStr::new(s)),
})
}
}
#[cfg(any(feature = "std", feature = "alloc"))]
#[test]
fn sensitive_exact_case_hits_the_named_variant() {
assert_eq!("AVC1".parse::<TestFourCc>().unwrap(), TestFourCc::Avc1);
assert_eq!("H264".parse::<TestFourCc>().unwrap(), TestFourCc::H264);
}
#[cfg(any(feature = "std", feature = "alloc"))]
#[test]
fn sensitive_case_variant_of_a_roster_name_is_a_table_miss_not_a_healed_hit() {
for spelling in ["avc1", "Avc1", "aVC1", "AVc1", "h264"] {
let v = spelling.parse::<TestFourCc>().unwrap();
assert_eq!(
v,
TestFourCc::Other(smol_str::SmolStr::new(spelling)),
"{spelling:?} healed to a named variant under Case::Sensitive"
);
assert_eq!(v.as_str(), spelling);
}
}
#[cfg(any(feature = "std", feature = "alloc"))]
#[test]
fn sensitive_stranger_round_trips_through_other_verbatim() {
let v = "VeNdOr_Tag".parse::<TestFourCc>().unwrap();
assert_eq!(v, TestFourCc::Other(smol_str::SmolStr::new("VeNdOr_Tag")));
assert_eq!(v.as_str(), "VeNdOr_Tag");
assert_eq!(v.as_str().parse::<TestFourCc>().unwrap(), v);
}
#[cfg(any(feature = "std", feature = "alloc"))]
#[test]
fn sensitive_other_inherits_the_declared_case_with_no_extra_wiring() {
assert_eq!(TestFourCc::other("AVC1"), TestFourCc::Avc1);
assert_eq!(
TestFourCc::other("avc1"),
TestFourCc::Other(smol_str::SmolStr::new("avc1"))
);
}