summaryrefslogtreecommitdiffstats
path: root/vendor/im-rc/src/proptest.rs
blob: 9180ef2733b9f098c410b1ce4f719e96ab69823d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//! Proptest strategies.
//!
//! These are only available when using the `proptest` feature flag.

use crate::{HashMap, HashSet, OrdMap, OrdSet, Vector};
use ::proptest::collection::vec;
use ::proptest::strategy::{BoxedStrategy, Strategy, ValueTree};
use std::hash::Hash;
use std::iter::FromIterator;
use std::ops::Range;

/// A strategy for generating a [`Vector`][Vector] of a certain size.
///
/// # Examples
///
/// ```rust,no_run
/// # use ::proptest::proptest;
/// proptest! {
///     #[test]
///     fn proptest_a_vector(ref l in vector(".*", 10..100)) {
///         assert!(l.len() < 100);
///         assert!(l.len() >= 10);
///     }
/// }
/// ```
///
/// [Vector]: ../struct.Vector.html
pub fn vector<A: Strategy + 'static>(
    element: A,
    size: Range<usize>,
) -> BoxedStrategy<Vector<<A::Tree as ValueTree>::Value>>
where
    <A::Tree as ValueTree>::Value: Clone,
{
    vec(element, size).prop_map(Vector::from_iter).boxed()
}

/// A strategy for an [`OrdMap`][OrdMap] of a given size.
///
/// # Examples
///
/// ```rust,no_run
/// # use ::proptest::proptest;
/// proptest! {
///     #[test]
///     fn proptest_works(ref m in ord_map(0..9999, ".*", 10..100)) {
///         assert!(m.len() < 100);
///         assert!(m.len() >= 10);
///     }
/// }
/// ```
///
/// [OrdMap]: ../struct.OrdMap.html
pub fn ord_map<K: Strategy + 'static, V: Strategy + 'static>(
    key: K,
    value: V,
    size: Range<usize>,
) -> BoxedStrategy<OrdMap<<K::Tree as ValueTree>::Value, <V::Tree as ValueTree>::Value>>
where
    <K::Tree as ValueTree>::Value: Ord + Clone,
    <V::Tree as ValueTree>::Value: Clone,
{
    ::proptest::collection::vec((key, value), size.clone())
        .prop_map(OrdMap::from)
        .prop_filter("OrdMap minimum size".to_owned(), move |m| {
            m.len() >= size.start
        })
        .boxed()
}

/// A strategy for an [`OrdSet`][OrdSet] of a given size.
///
/// # Examples
///
/// ```rust,no_run
/// # use ::proptest::proptest;
/// proptest! {
///     #[test]
///     fn proptest_a_set(ref s in ord_set(".*", 10..100)) {
///         assert!(s.len() < 100);
///         assert!(s.len() >= 10);
///     }
/// }
/// ```
///
/// [OrdSet]: ../struct.OrdSet.html
pub fn ord_set<A: Strategy + 'static>(
    element: A,
    size: Range<usize>,
) -> BoxedStrategy<OrdSet<<A::Tree as ValueTree>::Value>>
where
    <A::Tree as ValueTree>::Value: Ord + Clone,
{
    ::proptest::collection::vec(element, size.clone())
        .prop_map(OrdSet::from)
        .prop_filter("OrdSet minimum size".to_owned(), move |s| {
            s.len() >= size.start
        })
        .boxed()
}

/// A strategy for a [`HashMap`][HashMap] of a given size.
///
/// # Examples
///
/// ```rust,no_run
/// # use ::proptest::proptest;
/// proptest! {
///     #[test]
///     fn proptest_works(ref m in hash_map(0..9999, ".*", 10..100)) {
///         assert!(m.len() < 100);
///         assert!(m.len() >= 10);
///     }
/// }
/// ```
///
/// [HashMap]: ../struct.HashMap.html
pub fn hash_map<K: Strategy + 'static, V: Strategy + 'static>(
    key: K,
    value: V,
    size: Range<usize>,
) -> BoxedStrategy<HashMap<<K::Tree as ValueTree>::Value, <V::Tree as ValueTree>::Value>>
where
    <K::Tree as ValueTree>::Value: Hash + Eq + Clone,
    <V::Tree as ValueTree>::Value: Clone,
{
    ::proptest::collection::vec((key, value), size.clone())
        .prop_map(HashMap::from)
        .prop_filter("Map minimum size".to_owned(), move |m| {
            m.len() >= size.start
        })
        .boxed()
}

/// A strategy for a [`HashSet`][HashSet] of a given size.
///
/// # Examples
///
/// ```rust,no_run
/// # use ::proptest::proptest;
/// proptest! {
///     #[test]
///     fn proptest_a_set(ref s in hash_set(".*", 10..100)) {
///         assert!(s.len() < 100);
///         assert!(s.len() >= 10);
///     }
/// }
/// ```
///
/// [HashSet]: ../struct.HashSet.html
pub fn hash_set<A: Strategy + 'static>(
    element: A,
    size: Range<usize>,
) -> BoxedStrategy<HashSet<<A::Tree as ValueTree>::Value>>
where
    <A::Tree as ValueTree>::Value: Hash + Eq + Clone,
{
    ::proptest::collection::vec(element, size.clone())
        .prop_map(HashSet::from)
        .prop_filter("HashSet minimum size".to_owned(), move |s| {
            s.len() >= size.start
        })
        .boxed()
}