Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support quickcheck #99

Merged
merged 4 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}
}
Loading