1 use crate::{std::convert::TryInto, Builder, Uuid}; 2 3 use arbitrary::{Arbitrary, Unstructured}; 4 5 impl Arbitrary<'_> for Uuid { arbitrary(u: &mut Unstructured<'_>) -> arbitrary::Result<Self>6 fn arbitrary(u: &mut Unstructured<'_>) -> arbitrary::Result<Self> { 7 let b = u 8 .bytes(16)? 9 .try_into() 10 .map_err(|_| arbitrary::Error::NotEnoughData)?; 11 12 Ok(Builder::from_random_bytes(b).into_uuid()) 13 } 14 size_hint(_: usize) -> (usize, Option<usize>)15 fn size_hint(_: usize) -> (usize, Option<usize>) { 16 (16, Some(16)) 17 } 18 } 19 20 #[cfg(test)] 21 mod tests { 22 use super::*; 23 24 use crate::{Variant, Version}; 25 26 #[test] test_arbitrary()27 fn test_arbitrary() { 28 let mut bytes = Unstructured::new(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]); 29 30 let uuid = Uuid::arbitrary(&mut bytes).unwrap(); 31 32 assert_eq!(Some(Version::Random), uuid.get_version()); 33 assert_eq!(Variant::RFC4122, uuid.get_variant()); 34 } 35 36 #[test] test_arbitrary_empty()37 fn test_arbitrary_empty() { 38 let mut bytes = Unstructured::new(&[]); 39 40 // Ensure we don't panic when building an arbitrary `Uuid` 41 let uuid = Uuid::arbitrary(&mut bytes); 42 43 assert!(uuid.is_err()); 44 } 45 } 46