use ::arbitrary::{Arbitrary, Unstructured};
fn drive<F: FnMut(&mut Unstructured<'_>)>(seed: u64, rounds: usize, mut body: F) {
let mut bytes = ::std::vec![0u8; 4096];
for (i, b) in bytes.iter_mut().enumerate() {
*b = ((seed.wrapping_mul(0x9E37_79B9_7F4A_7C15) ^ i as u64) & 0xff) as u8;
}
let mut u = Unstructured::new(&bytes);
for _ in 0..rounds {
body(&mut u);
}
}
#[test]
fn geo_location_invariant_lat_lon_in_range() {
drive(0xA11CE, 256, |u| {
let g = crate::capture::GeoLocation::arbitrary(u).unwrap();
assert!(
(-90.0..=90.0).contains(&g.lat()),
"lat out of range: {}",
g.lat()
);
assert!(
(-180.0..=180.0).contains(&g.lon()),
"lon out of range: {}",
g.lon()
);
if let Some(alt) = g.altitude() {
assert!(
alt.is_finite(),
"altitude must be finite when Some, got {alt}"
);
}
});
}
#[test]
fn fingerprint_invariant_algorithm_non_empty() {
drive(0xB0B, 256, |u| {
let fp = crate::audio::Fingerprint::arbitrary(u).unwrap();
assert!(!fp.algorithm().is_empty(), "algorithm must be non-empty");
});
}
#[test]
fn cover_art_invariant_mime_and_data_non_empty() {
drive(0xC0FFEE, 256, |u| {
let c = crate::audio::CoverArt::arbitrary(u).unwrap();
assert!(!c.mime().is_empty(), "mime must be non-empty");
assert!(!c.data().is_empty(), "data must be non-empty");
});
}
#[test]
fn smoke_yields_values_for_representative_types() {
drive(0xD1CE, 64, |u| {
let _ = crate::codec::VideoCodec::arbitrary(u).unwrap();
let _ = crate::color::Info::arbitrary(u).unwrap();
let _ = crate::frame::FrameRate::arbitrary(u).unwrap();
let _ = crate::lang::Language::arbitrary(u).unwrap();
let _ = crate::disposition::TrackDisposition::arbitrary(u).unwrap();
});
}
fn drive_per_round<F: FnMut(&mut Unstructured<'_>)>(seed: u64, rounds: usize, mut body: F) {
let mut state = seed
.wrapping_mul(0x9E37_79B9_7F4A_7C15)
.wrapping_add(0xDEAD_BEEF_CAFE_F00D);
for _ in 0..rounds {
let mut bytes = ::std::vec![0u8; 64];
for b in bytes.iter_mut() {
state = state
.wrapping_add(0x9E37_79B9_7F4A_7C15)
.wrapping_mul(0xBF58_476D_1CE4_E5B9);
let mixed = state ^ (state >> 27);
*b = (mixed.wrapping_mul(0x94D0_49BB_1331_11EB) >> 56) as u8;
}
let mut u = Unstructured::new(&bytes);
body(&mut u);
}
}
#[test]
fn reachability_small_closed_coded_enums_hit_all_named() {
use ::std::collections::BTreeSet;
let mut br: BTreeSet<u32> = BTreeSet::new();
let mut co: BTreeSet<u32> = BTreeSet::new();
drive_per_round(0x12C0DE5_u64, 2048, |u| {
br.insert(crate::audio::BitRateMode::arbitrary(u).unwrap().to_u32());
co.insert(crate::audio::ChannelOrder::arbitrary(u).unwrap().to_u32());
});
assert_eq!(br.len(), 3, "BitRateMode coverage: {br:?}");
assert_eq!(co.len(), 4, "ChannelOrder coverage: {co:?}");
}
#[test]
fn reachability_channel_records_reach_their_incoherent_combinations() {
use crate::audio::{ChannelLayoutDescription, ChannelOrder, ChannelSpec};
let mut indices = 0u32;
let mut raw_ids = 0u32;
let mut labelled = false;
let mut custom_without_channels = false;
let mut native_without_mask = false;
let mut populated = false;
let mut described = false;
drive_per_round(0xC4A11E1_u64, 2048, |u| {
let spec = ChannelSpec::arbitrary(u).unwrap();
indices |= spec.index();
raw_ids |= spec.raw_id();
labelled |= !spec.label().is_empty();
let d = ChannelLayoutDescription::arbitrary(u).unwrap();
custom_without_channels |= d.order() == ChannelOrder::Custom && d.custom_channels().is_empty();
native_without_mask |= d.order() == ChannelOrder::Native && d.native_mask().is_none();
populated |= !d.custom_channels().is_empty();
described |= !d.text().is_empty();
});
assert_ne!(indices, 0, "spec index never left zero");
assert_ne!(raw_ids, 0, "spec raw_id never left zero");
assert!(labelled, "spec label was never populated");
assert!(
custom_without_channels,
"a Custom order with no channel list is unreachable"
);
assert!(
native_without_mask,
"a Native order with no mask is unreachable"
);
assert!(populated, "custom_channels was never non-empty");
assert!(described, "text was never populated");
}
#[test]
fn reachability_track_origin_hits_all_named_and_escape() {
use crate::subtitle::TrackOrigin;
use ::std::collections::BTreeSet;
let mut named: BTreeSet<u32> = BTreeSet::new();
let mut saw_other = false;
drive_per_round(0x7401C1_u64, 2048, |u| {
match TrackOrigin::arbitrary(u).unwrap().to_u32() {
Some(code) => {
named.insert(code);
}
None => saw_other = true,
}
});
assert_eq!(named.len(), 4, "TrackOrigin named coverage: {named:?}");
assert!(saw_other, "TrackOrigin `Other` arm never generated");
}
#[test]
fn reachability_weighted_coded_enum_hits_all_named_and_unknown() {
use crate::frame::Rotation;
let mut saw_d0 = false;
let mut saw_d90 = false;
let mut saw_d180 = false;
let mut saw_d270 = false;
let mut saw_other = false;
drive_per_round(0x20C0DE5_u64, 2048, |u| {
match Rotation::arbitrary(u).unwrap() {
Rotation::D0 => saw_d0 = true,
Rotation::D90 => saw_d90 = true,
Rotation::D180 => saw_d180 = true,
Rotation::D270 => saw_d270 = true,
Rotation::Other(_) => saw_other = true,
}
});
assert!(
saw_d0 && saw_d90 && saw_d180 && saw_d270 && saw_other,
"Rotation coverage: D0={saw_d0} D90={saw_d90} D180={saw_d180} D270={saw_d270} Other={saw_other}"
);
}
#[test]
fn reachability_sample_format_all_named_plus_arms() {
use crate::audio::SampleFormat;
use ::std::collections::BTreeSet;
let mut named: BTreeSet<::std::string::String> = BTreeSet::new();
let mut saw_other = false;
drive_per_round(0x3F0_FEED_u64, 4096, |u| {
match SampleFormat::arbitrary(u).unwrap() {
SampleFormat::Other(_) => saw_other = true,
other => {
named.insert(::std::string::String::from(other.as_str()));
}
}
});
assert_eq!(
named.len(),
12,
"missing named SampleFormat variants; observed: {named:?}"
);
assert!(saw_other, "SampleFormat: never observed `Other(_)`");
}
#[test]
fn reachability_range_weighted_enums_hit_named_codes() {
use ::std::collections::BTreeSet;
let mut matrix: BTreeSet<u32> = BTreeSet::new();
let mut primaries: BTreeSet<u32> = BTreeSet::new();
let mut transfer: BTreeSet<u32> = BTreeSet::new();
let mut pixel: BTreeSet<u32> = BTreeSet::new();
drive_per_round(0x4A_C0DE5_u64, 8192, |u| {
matrix.extend(crate::color::Matrix::arbitrary(u).unwrap().to_u32());
primaries.extend(crate::color::Primaries::arbitrary(u).unwrap().to_u32());
transfer.extend(crate::color::Transfer::arbitrary(u).unwrap().to_u32());
pixel.extend(
crate::pixel_format::PixelFormat::arbitrary(u)
.unwrap()
.to_u32(),
);
});
let in_range = |s: &BTreeSet<u32>, max: u32| s.iter().filter(|&&c| c <= max).count();
assert!(
in_range(&matrix, 17) >= 3,
"Matrix named-range coverage too low: {matrix:?}"
);
assert!(
matrix.contains(&crate::color::DOMAIN_EXT_BASE),
"Matrix::Bt601 (DOMAIN_EXT_BASE) never generated"
);
assert!(
in_range(&primaries, 22) >= 3,
"Primaries named-range coverage too low: {primaries:?}"
);
assert!(
in_range(&transfer, 18) >= 3,
"Transfer named-range coverage too low: {transfer:?}"
);
assert!(
in_range(&pixel, 947) >= 3,
"PixelFormat named-range coverage too low: {} distinct",
in_range(&pixel, 947)
);
}
#[test]
fn generated_vocabulary_values_round_trip_through_their_name() {
drive(0xE11E, 128, |u| {
macro_rules! rt {
($ty:path) => {{
let v = <$ty>::arbitrary(u).unwrap();
assert_eq!(v.as_str().parse::<$ty>(), Ok(v.clone()), "{v:?}");
}};
}
rt!(crate::color::Matrix);
rt!(crate::pixel_format::PixelFormat);
rt!(crate::frame::Rotation);
let d = crate::disposition::TrackDisposition::arbitrary(u).unwrap();
assert_eq!(
crate::disposition::TrackDisposition::from_u32(d.to_u32()),
d
);
});
}
#[cfg(feature = "serde")]
#[test]
fn arbitrary_values_survive_serde_round_trip() {
drive_per_round(0x5E2DE_u64, 4096, |u| {
let sf = crate::audio::SampleFormat::arbitrary(u).unwrap();
let json = serde_json::to_string(&sf).unwrap();
let back: crate::audio::SampleFormat = serde_json::from_str(&json).unwrap();
assert_eq!(back, sf, "SampleFormat lost identity via serde: {json}");
let vc = crate::codec::VideoCodec::arbitrary(u).unwrap();
let json = serde_json::to_string(&vc).unwrap();
let back: crate::codec::VideoCodec = serde_json::from_str(&json).unwrap();
assert_eq!(back, vc, "VideoCodec lost identity via serde: {json}");
let ld = crate::audio::Loudness::arbitrary(u).unwrap();
let json = serde_json::to_string(&ld).unwrap();
let back: crate::audio::Loudness = serde_json::from_str(&json).unwrap();
assert_eq!(back, ld, "Loudness lost identity via serde: {json}");
});
}
#[test]
fn reachability_tags_language_hits_none_and_some() {
let mut saw_none = false;
let mut saw_some = false;
drive_per_round(
0x007A_651A_u64,
1024,
|u| match crate::audio::Tags::arbitrary(u).unwrap().language() {
None => saw_none = true,
Some(_) => saw_some = true,
},
);
assert!(saw_none, "Tags.language never generated `None`");
assert!(saw_some, "Tags.language never generated `Some(_)`");
}