proptest_arbitrary/
primitives.rs

1//! Arbitrary implementations for primitive types.
2
3//==============================================================================
4// Primitive types:
5//==============================================================================
6
7use proptest::{bool, char};
8use proptest::num::{isize, usize, f32, f64, i16, i32, i64, i8, u16, u32, u64, u8};
9
10arbitrary!(
11    bool, f32, f64,
12    i8, i16, i32, i64, isize,
13    u8, u16, u32, u64, usize
14);
15
16//==============================================================================
17// Primitive types, char:
18//==============================================================================
19
20use std::borrow::Cow;
21
22/// An inclusive char range from fst to snd.
23/// TODO: replace with `std::ops::RangeInclusive<char>` once stabilized.
24type CharRange = (char, char);
25type CowSlices<'a, T> = Cow<'a, [T]>;
26
27const WHOLE_RANGE: &[CharRange] = &[('\x00', ::std::char::MAX)];
28
29/// Equivalent to `proptest::char::ANY`.
30impl<'a> Default for CharParam<'a> {
31    fn default() -> Self {
32        Self {
33            special: Cow::Borrowed(char::DEFAULT_SPECIAL_CHARS),
34            preferred: Cow::Borrowed(char::DEFAULT_PREFERRED_RANGES),
35            ranges: Cow::Borrowed(WHOLE_RANGE),
36        }
37    }
38}
39
40/// Parameters to pass to `proptest::char::CharStrategy::new(..)`.
41#[derive(Clone, PartialEq, Eq, Hash, Debug, From, Into)]
42#[cfg_attr(feature = "frunk", derive(Generic))]
43pub struct CharParam<'a> {
44    special: CowSlices<'a, char>,
45    preferred: CowSlices<'a, CharRange>,
46    ranges: CowSlices<'a, CharRange>,
47}
48
49arbitrary!(char, char::CharStrategy<'a>, CharParam<'a>; args => {
50    char::CharStrategy::new(args.special, args.preferred, args.ranges)
51});
52
53#[cfg(test)]
54mod test {
55    no_panic_test!(
56        bool => bool,
57        char => char,
58        f32 => f32, f64 => f64,
59        isize => isize, usize => usize,
60        i8 => i8, i16 => i16, i32 => i32, i64 => i64,
61        u8 => u8, u16 => u16, u32 => u32, u64 => u64
62    );
63}