proptest/arbitrary/_std/
ffi.rs1use crate::std_facade::{Box, String, Vec};
13use std::ffi::*;
14use std::ops::RangeInclusive;
15
16use crate::arbitrary::*;
17use crate::collection::*;
18use crate::strategy::statics::static_map;
19use crate::strategy::*;
20
21use super::string::not_utf8_bytes;
22
23arbitrary!(CString,
24 SFnPtrMap<VecStrategy<RangeInclusive<u8>>, Self>, SizeRange;
25 args => static_map(vec(1..=::std::u8::MAX, args + 1), |mut vec| {
26 vec.pop().unwrap();
27 Self::new(vec).unwrap()
29 })
30);
31
32arbitrary!(OsString, MapInto<StrategyFor<String>, Self>,
33 <String as Arbitrary>::Parameters;
34 a => any_with::<String>(a).prop_map_into()
35);
36
37macro_rules! dst_wrapped {
38 ($($w: ident),*) => {
39 $(arbitrary!($w<CStr>, MapInto<StrategyFor<CString>, Self>, SizeRange;
40 a => any_with::<CString>(a).prop_map_into()
41 );)*
42 $(arbitrary!($w<OsStr>, MapInto<StrategyFor<OsString>, Self>,
43 <String as Arbitrary>::Parameters;
44 a => any_with::<OsString>(a).prop_map_into()
45 );)*
46 };
47}
48
49dst_wrapped!(Box);
50
51#[cfg(feature = "unstable")]
52use std::rc::Rc;
53#[cfg(feature = "unstable")]
54use std::sync::Arc;
55#[cfg(feature = "unstable")]
56dst_wrapped!(Rc, Arc);
57
58arbitrary!(FromBytesWithNulError, SMapped<Option<u16>, Self>; {
59 static_map(any::<Option<u16>>(), |opt_pos| {
60 if let Some(pos) = opt_pos {
64 let pos = pos as usize;
65 let mut v = Vec::<u8>::with_capacity(pos + 2);
67 v.extend(::std::iter::repeat(1).take(pos));
68 v.push(0);
69 v.push(1);
70 CStr::from_bytes_with_nul(v.as_slice()).unwrap_err()
71 } else {
72 CStr::from_bytes_with_nul(b"").unwrap_err()
73 }
74 })
75});
76
77arbitrary!(IntoStringError, SFnPtrMap<BoxedStrategy<Vec<u8>>, Self>;
78 static_map(not_utf8_bytes(false).boxed(), |bytes|
79 CString::new(bytes).unwrap().into_string().unwrap_err()
80 )
81);
82
83#[cfg(test)]
84mod test {
85 no_panic_test!(
86 c_string => CString,
87 os_string => OsString,
88 box_c_str => Box<CStr>,
89 box_os_str => Box<OsStr>,
90 into_string_error => IntoStringError,
91 from_bytes_with_nul => FromBytesWithNulError
92 );
93 #[cfg(feature = "unstable")]
94 no_panic_test!(
95 rc_c_str => Rc<CStr>,
96 rc_os_str => Rc<OsStr>,
97 arc_c_str => Arc<CStr>,
98 arc_os_str => Arc<OsStr>
99 );
100}