-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
impl Arbitrary for IndexMap and IndexSet
This implements both `arbitrary::Arbitrary` and `quickcheck::Arbitrary` behind optional dependencies on their respective crates.
- Loading branch information
Showing
4 changed files
with
84 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#[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")] | ||
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)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters