1 | #[cfg (feature = "arbitrary" )] |
2 | mod impl_arbitrary { |
3 | use crate::{IndexMap, IndexSet}; |
4 | use arbitrary::{Arbitrary, Result, Unstructured}; |
5 | use core::hash::{BuildHasher, Hash}; |
6 | |
7 | impl<'a, K, V, S> Arbitrary<'a> for IndexMap<K, V, S> |
8 | where |
9 | K: Arbitrary<'a> + Hash + Eq, |
10 | V: Arbitrary<'a>, |
11 | S: BuildHasher + Default, |
12 | { |
13 | fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
14 | u.arbitrary_iter()?.collect() |
15 | } |
16 | |
17 | fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> { |
18 | u.arbitrary_take_rest_iter()?.collect() |
19 | } |
20 | } |
21 | |
22 | impl<'a, T, S> Arbitrary<'a> for IndexSet<T, S> |
23 | where |
24 | T: Arbitrary<'a> + Hash + Eq, |
25 | S: BuildHasher + Default, |
26 | { |
27 | fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
28 | u.arbitrary_iter()?.collect() |
29 | } |
30 | |
31 | fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> { |
32 | u.arbitrary_take_rest_iter()?.collect() |
33 | } |
34 | } |
35 | } |
36 | |
37 | #[cfg (feature = "quickcheck" )] |
38 | mod impl_quickcheck { |
39 | use crate::{IndexMap, IndexSet}; |
40 | use alloc::boxed::Box; |
41 | use alloc::vec::Vec; |
42 | use core::hash::{BuildHasher, Hash}; |
43 | use quickcheck::{Arbitrary, Gen}; |
44 | |
45 | impl<K, V, S> Arbitrary for IndexMap<K, V, S> |
46 | where |
47 | K: Arbitrary + Hash + Eq, |
48 | V: Arbitrary, |
49 | S: BuildHasher + Default + Clone + 'static, |
50 | { |
51 | fn arbitrary(g: &mut Gen) -> Self { |
52 | Self::from_iter(Vec::arbitrary(g)) |
53 | } |
54 | |
55 | fn shrink(&self) -> Box<dyn Iterator<Item = Self>> { |
56 | let vec = Vec::from_iter(self.clone()); |
57 | Box::new(vec.shrink().map(Self::from_iter)) |
58 | } |
59 | } |
60 | |
61 | impl<T, S> Arbitrary for IndexSet<T, S> |
62 | where |
63 | T: Arbitrary + Hash + Eq, |
64 | S: BuildHasher + Default + Clone + 'static, |
65 | { |
66 | fn arbitrary(g: &mut Gen) -> Self { |
67 | Self::from_iter(Vec::arbitrary(g)) |
68 | } |
69 | |
70 | fn shrink(&self) -> Box<dyn Iterator<Item = Self>> { |
71 | let vec = Vec::from_iter(self.clone()); |
72 | Box::new(vec.shrink().map(Self::from_iter)) |
73 | } |
74 | } |
75 | } |
76 | |