summaryrefslogtreecommitdiffstats
path: root/vendor/indexmap/src/arbitrary.rs
blob: 7798438c1567722f9b5c5dc21602e748a1b20bd5 (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
#[cfg(feature = "arbitrary")]
#[cfg_attr(docsrs, doc(cfg(feature = "arbitrary")))]
mod impl_arbitrary {
    use crate::{IndexMap, IndexSet};
    use arbitrary::{Arbitrary, Result, Unstructured};
    use core::hash::{BuildHasher, Hash};

    impl<'a, K, V, S> Arbitrary<'a> for IndexMap<K, V, S>
    where
        K: Arbitrary<'a> + Hash + Eq,
        V: Arbitrary<'a>,
        S: BuildHasher + Default,
    {
        fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
            u.arbitrary_iter()?.collect()
        }

        fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> {
            u.arbitrary_take_rest_iter()?.collect()
        }
    }

    impl<'a, T, S> Arbitrary<'a> for IndexSet<T, S>
    where
        T: Arbitrary<'a> + Hash + Eq,
        S: BuildHasher + Default,
    {
        fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
            u.arbitrary_iter()?.collect()
        }

        fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> {
            u.arbitrary_take_rest_iter()?.collect()
        }
    }
}

#[cfg(feature = "quickcheck")]
#[cfg_attr(docsrs, doc(cfg(feature = "quickcheck")))]
mod impl_quickcheck {
    use crate::{IndexMap, IndexSet};
    use alloc::boxed::Box;
    use alloc::vec::Vec;
    use core::hash::{BuildHasher, Hash};
    use quickcheck::{Arbitrary, Gen};

    impl<K, V, S> Arbitrary for IndexMap<K, V, S>
    where
        K: Arbitrary + Hash + Eq,
        V: Arbitrary,
        S: BuildHasher + Default + Clone + 'static,
    {
        fn arbitrary(g: &mut Gen) -> Self {
            Self::from_iter(Vec::arbitrary(g))
        }

        fn shrink(&self) -> Box<dyn Iterator<Item = Self>> {
            let vec = Vec::from_iter(self.clone());
            Box::new(vec.shrink().map(Self::from_iter))
        }
    }

    impl<T, S> Arbitrary for IndexSet<T, S>
    where
        T: Arbitrary + Hash + Eq,
        S: BuildHasher + Default + Clone + 'static,
    {
        fn arbitrary(g: &mut Gen) -> Self {
            Self::from_iter(Vec::arbitrary(g))
        }

        fn shrink(&self) -> Box<dyn Iterator<Item = Self>> {
            let vec = Vec::from_iter(self.clone());
            Box::new(vec.shrink().map(Self::from_iter))
        }
    }
}