Skip to main content

ftracker_identifiers/country/
arbitrary.rs

1use arbitrary::{Arbitrary, Unstructured};
2
3use super::CountryCode;
4use super::table::ASSIGNED_CODES;
5
6impl<'a> Arbitrary<'a> for CountryCode {
7    fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
8        // Pick straight from the assigned set so every generated value is valid by construction.
9        let code = *u.choose(ASSIGNED_CODES)?;
10        CountryCode::from_bytes(code).map_err(|_| arbitrary::Error::IncorrectFormat)
11    }
12}
13
14#[cfg(test)]
15mod tests {
16    use super::*;
17
18    #[test]
19    fn always_produces_assigned_codes() {
20        for seed in 0u32..256 {
21            let data = seed.to_le_bytes().repeat(8);
22            let mut u = Unstructured::new(&data);
23            let code = CountryCode::arbitrary(&mut u).expect("arbitrary should always succeed");
24            // Re-validating via parse() proves the value round trips through the exact same checks
25            // a hand typed input would.
26            assert!(CountryCode::parse(code.as_str()).is_ok());
27        }
28    }
29}