proptest/arbitrary/_alloc/
ops.rs

1//-
2// Copyright 2017, 2018 The proptest developers
3//
4// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7// option. This file may not be copied, modified, or distributed
8// except according to those terms.
9
10//! Arbitrary implementations for `std::ops`.
11
12use crate::std_facade::Arc;
13use core::ops::*;
14
15use crate::arbitrary::*;
16use crate::strategy::statics::static_map;
17use crate::strategy::*;
18
19arbitrary!(RangeFull; ..);
20wrap_ctor!(RangeFrom, |a| a..);
21wrap_ctor!(RangeTo, |a| ..a);
22
23wrap_ctor!(RangeToInclusive, |a| ..=a);
24
25arbitrary!(
26    [A: PartialOrd + Arbitrary] RangeInclusive<A>,
27    SMapped<(A, A), Self>, product_type![A::Parameters, A::Parameters];
28    args => static_map(any_with::<(A, A)>(args),
29        |(a, b)| if b < a { b..=a } else { a..=b })
30);
31
32lift1!([PartialOrd] RangeInclusive<A>; base => {
33    let base = Arc::new(base);
34    (base.clone(), base).prop_map(|(a, b)| if b < a { b..=a } else { a..=b })
35});
36
37arbitrary!(
38    [A: PartialOrd + Arbitrary] Range<A>,
39    SMapped<(A, A), Self>, product_type![A::Parameters, A::Parameters];
40    args => static_map(any_with::<(A, A)>(args),
41        |(a, b)| if b < a { b..a } else { a..b })
42);
43
44lift1!([PartialOrd] Range<A>; base => {
45    let base = Arc::new(base);
46    (base.clone(), base).prop_map(|(a, b)| if b < a { b..a } else { a..b })
47});
48
49#[cfg(feature = "unstable")]
50arbitrary!(
51    [Y: Arbitrary, R: Arbitrary] CoroutineState<Y, R>,
52    TupleUnion<(WA<SMapped<Y, Self>>, WA<SMapped<R, Self>>)>,
53    product_type![Y::Parameters, R::Parameters];
54    args => {
55        let product_unpack![y, r] = args;
56        prop_oneof![
57            static_map(any_with::<Y>(y), CoroutineState::Yielded),
58            static_map(any_with::<R>(r), CoroutineState::Complete)
59        ]
60    }
61);
62
63#[cfg(feature = "unstable")]
64use core::fmt;
65
66#[cfg(feature = "unstable")]
67impl<A: fmt::Debug + 'static, B: fmt::Debug + 'static>
68    functor::ArbitraryF2<A, B> for CoroutineState<A, B>
69{
70    type Parameters = ();
71
72    fn lift2_with<AS, BS>(
73        fst: AS,
74        snd: BS,
75        _args: Self::Parameters,
76    ) -> BoxedStrategy<Self>
77    where
78        AS: Strategy<Value = A> + 'static,
79        BS: Strategy<Value = B> + 'static,
80    {
81        prop_oneof![
82            fst.prop_map(CoroutineState::Yielded),
83            snd.prop_map(CoroutineState::Complete)
84        ]
85        .boxed()
86    }
87}
88
89#[cfg(test)]
90mod test {
91    no_panic_test!(
92        range_full => RangeFull,
93        range_from => RangeFrom<usize>,
94        range_to   => RangeTo<usize>,
95        range      => Range<usize>,
96        range_inclusive => RangeInclusive<usize>,
97        range_to_inclusive => RangeToInclusive<usize>
98    );
99
100    #[cfg(feature = "unstable")]
101    no_panic_test!(
102        generator_state => CoroutineState<u32, u64>
103    );
104}