[go: up one dir, main page]

imbl/
arbitrary.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5use arbitrary::{size_hint, Arbitrary, Result, Unstructured};
6use std::hash::{BuildHasher, Hash};
7
8use crate::{
9    shared_ptr::SharedPointerKind, GenericHashMap, GenericHashSet, GenericOrdMap, GenericOrdSet,
10    GenericVector,
11};
12
13impl<'a, A: Arbitrary<'a> + Clone, P: SharedPointerKind + 'static> Arbitrary<'a>
14    for GenericVector<A, P>
15{
16    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
17        u.arbitrary_iter()?.collect()
18    }
19
20    fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> {
21        u.arbitrary_take_rest_iter()?.collect()
22    }
23
24    fn size_hint(depth: usize) -> (usize, Option<usize>) {
25        size_hint::recursion_guard(depth, |depth| {
26            size_hint::and(<usize as Arbitrary>::size_hint(depth), (0, None))
27        })
28    }
29}
30
31impl<
32        'a,
33        K: Arbitrary<'a> + Ord + Clone,
34        V: Arbitrary<'a> + Clone,
35        P: SharedPointerKind + 'static,
36    > Arbitrary<'a> for GenericOrdMap<K, V, P>
37{
38    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
39        u.arbitrary_iter()?.collect()
40    }
41
42    fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> {
43        u.arbitrary_take_rest_iter()?.collect()
44    }
45
46    fn size_hint(depth: usize) -> (usize, Option<usize>) {
47        size_hint::recursion_guard(depth, |depth| {
48            size_hint::and(<usize as Arbitrary>::size_hint(depth), (0, None))
49        })
50    }
51}
52
53impl<'a, A: Arbitrary<'a> + Ord + Clone, P: SharedPointerKind + 'static> Arbitrary<'a>
54    for GenericOrdSet<A, P>
55{
56    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
57        u.arbitrary_iter()?.collect()
58    }
59
60    fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> {
61        u.arbitrary_take_rest_iter()?.collect()
62    }
63
64    fn size_hint(depth: usize) -> (usize, Option<usize>) {
65        size_hint::recursion_guard(depth, |depth| {
66            size_hint::and(<usize as Arbitrary>::size_hint(depth), (0, None))
67        })
68    }
69}
70
71impl<'a, K, V, S, P> Arbitrary<'a> for GenericHashMap<K, V, S, P>
72where
73    K: Arbitrary<'a> + Hash + Eq + Clone,
74    V: Arbitrary<'a> + Clone,
75    S: BuildHasher + Clone + Default + 'static,
76    P: SharedPointerKind + 'static,
77{
78    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
79        u.arbitrary_iter()?.collect()
80    }
81
82    fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> {
83        u.arbitrary_take_rest_iter()?.collect()
84    }
85
86    fn size_hint(depth: usize) -> (usize, Option<usize>) {
87        size_hint::recursion_guard(depth, |depth| {
88            size_hint::and(<usize as Arbitrary>::size_hint(depth), (0, None))
89        })
90    }
91}
92
93impl<'a, A, S, P> Arbitrary<'a> for GenericHashSet<A, S, P>
94where
95    A: Arbitrary<'a> + Hash + Eq + Clone,
96    S: BuildHasher + Clone + Default + 'static,
97    P: SharedPointerKind + 'static,
98{
99    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
100        u.arbitrary_iter()?.collect()
101    }
102
103    fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> {
104        u.arbitrary_take_rest_iter()?.collect()
105    }
106
107    fn size_hint(depth: usize) -> (usize, Option<usize>) {
108        size_hint::recursion_guard(depth, |depth| {
109            size_hint::and(<usize as Arbitrary>::size_hint(depth), (0, None))
110        })
111    }
112}