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