Skip to content

Commit

Permalink
Merge pull request #99 from schneiderfelipe/support-quickcheck
Browse files Browse the repository at this point in the history
Support quickcheck
  • Loading branch information
jeffparsons authored Dec 2, 2024
2 parents f53ee9a + d3ea7c7 commit 383c9c0
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ categories = ["data-structures"]
rust-version = "1.66.0"

[dependencies]
quickcheck = { version = "1.0.3", optional = true }
serde = { version = "1", optional = true }

[dev-dependencies]
Expand All @@ -42,6 +43,7 @@ nightly = []
# Requires a nightly compiler because `const_btree_new` is an unstable feature,
# but is soon to be stabilized: <https://github.com/rust-lang/rust/issues/71835>
const_fn = []
quickcheck = ["dep:quickcheck"]

[[bench]]
name = "benches"
Expand Down
22 changes: 22 additions & 0 deletions src/inclusive_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,21 @@ where
}
}

#[cfg(feature = "quickcheck")]
impl<K, V> quickcheck::Arbitrary for RangeInclusiveMap<K, V>
where
K: quickcheck::Arbitrary + Ord + StepLite,
V: quickcheck::Arbitrary + Eq,
{
fn arbitrary(g: &mut quickcheck::Gen) -> Self {
// REVISIT: allocation could be avoided if Gen::gen_size were public (https://github.com/BurntSushi/quickcheck/issues/326#issue-2653601170)
<alloc::vec::Vec<(RangeInclusive<_>, _)>>::arbitrary(g)
.into_iter()
.filter(|(range, _)| !range.is_empty())
.collect()
}
}

impl<K, V, StepFnsT> RangeInclusiveMap<K, V, StepFnsT> {
/// Gets an iterator over all pairs of key range and value,
/// ordered by key range.
Expand Down Expand Up @@ -1920,4 +1935,11 @@ mod tests {
const _MAP: RangeInclusiveMap<u32, bool> = RangeInclusiveMap::new();
#[cfg(feature = "const_fn")]
const _MAP2: RangeInclusiveMap<u32, bool> = RangeInclusiveMap::new_with_step_fns();

#[cfg(feature = "quickcheck")]
quickcheck::quickcheck! {
fn prop(xs: RangeInclusiveMap<usize, usize>) -> bool {
xs == xs
}
}
}
19 changes: 19 additions & 0 deletions src/inclusive_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ impl<T, StepFnsT> Default for RangeInclusiveSet<T, StepFnsT> {
}
}

#[cfg(feature = "quickcheck")]
impl<K> quickcheck::Arbitrary for RangeInclusiveSet<K>
where
K: quickcheck::Arbitrary + Ord + StepLite,
{
fn arbitrary(g: &mut quickcheck::Gen) -> Self {
Self {
rm: RangeInclusiveMap::arbitrary(g),
}
}
}

impl<T> RangeInclusiveSet<T, T>
where
T: Ord + Clone + StepLite,
Expand Down Expand Up @@ -827,4 +839,11 @@ mod tests {
const _SET: RangeInclusiveSet<u32> = RangeInclusiveSet::new();
#[cfg(feature = "const_fn")]
const _SET2: RangeInclusiveSet<u32> = RangeInclusiveSet::new_with_step_fns();

#[cfg(feature = "quickcheck")]
quickcheck::quickcheck! {
fn prop(xs: RangeInclusiveSet<usize, usize>) -> bool {
xs == xs
}
}
}
22 changes: 22 additions & 0 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,21 @@ where
}
}

#[cfg(feature = "quickcheck")]
impl<K, V> quickcheck::Arbitrary for RangeMap<K, V>
where
K: quickcheck::Arbitrary + Ord,
V: quickcheck::Arbitrary + Eq,
{
fn arbitrary(g: &mut quickcheck::Gen) -> Self {
// REVISIT: allocation could be avoided if Gen::gen_size were public (https://github.com/BurntSushi/quickcheck/issues/326#issue-2653601170)
<alloc::vec::Vec<(Range<_>, _)>>::arbitrary(g)
.into_iter()
.filter(|(range, _)| !range.is_empty())
.collect()
}
}

impl<K, V> RangeMap<K, V> {
/// Makes a new empty `RangeMap`.
#[cfg(feature = "const_fn")]
Expand Down Expand Up @@ -1818,4 +1833,11 @@ mod tests {

#[cfg(feature = "const_fn")]
const _MAP: RangeMap<u32, bool> = RangeMap::new();

#[cfg(feature = "quickcheck")]
quickcheck::quickcheck! {
fn prop(xs: RangeMap<usize, usize>) -> bool {
xs == xs
}
}
}
19 changes: 19 additions & 0 deletions src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ impl<T> Default for RangeSet<T> {
}
}

#[cfg(feature = "quickcheck")]
impl<K> quickcheck::Arbitrary for RangeSet<K>
where
K: quickcheck::Arbitrary + Ord,
{
fn arbitrary(g: &mut quickcheck::Gen) -> Self {
Self {
rm: RangeMap::arbitrary(g),
}
}
}

impl<T> RangeSet<T>
where
T: Ord + Clone,
Expand Down Expand Up @@ -793,4 +805,11 @@ mod tests {

#[cfg(feature = "const_fn")]
const _SET: RangeSet<u32> = RangeSet::new();

#[cfg(feature = "quickcheck")]
quickcheck::quickcheck! {
fn prop(xs: RangeSet<usize>) -> bool {
xs == xs
}
}
}

0 comments on commit 383c9c0

Please sign in to comment.