Skip to content

Commit

Permalink
Merge pull request #92 from xfbs/tests
Browse files Browse the repository at this point in the history
Expands test coverage
  • Loading branch information
jeffparsons authored Feb 8, 2024
2 parents 9a67f56 + ae6b9c0 commit 1f6f22b
Show file tree
Hide file tree
Showing 4 changed files with 232 additions and 46 deletions.
56 changes: 55 additions & 1 deletion src/inclusive_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,16 @@ mod tests {
}
}

#[proptest]
fn test_len(mut map: RangeInclusiveMap<u64, String>) {
assert_eq!(map.len(), map.iter().count());
assert_eq!(map.is_empty(), map.len() == 0);
map.clear();
assert_eq!(map.len(), 0);
assert!(map.is_empty());
assert_eq!(map.iter().count(), 0);
}

#[proptest]
fn test_first(set: RangeInclusiveMap<u64, String>) {
assert_eq!(
Expand Down Expand Up @@ -1026,6 +1036,47 @@ mod tests {
}
}

#[proptest]
#[allow(deprecated)]
fn test_hash(left: RangeInclusiveMap<u64, u64>, right: RangeInclusiveMap<u64, u64>) {
use core::hash::{Hash, Hasher, SipHasher};

let hash = |set: &RangeInclusiveMap<_, _>| {
let mut hasher = SipHasher::new();
set.hash(&mut hasher);
hasher.finish()
};

if left == right {
assert!(
hash(&left) == hash(&right),
"if two values are equal, their hash must be equal"
);
}

// if the hashes are equal the values might not be the same (collision)
if hash(&left) != hash(&right) {
assert!(
left != right,
"if two value's hashes are not equal, they must not be equal"
);
}
}

#[proptest]
fn test_ord(left: RangeInclusiveMap<u64, u64>, right: RangeInclusiveMap<u64, u64>) {
assert_eq!(
left == right,
left.cmp(&right).is_eq(),
"ordering and equality must match"
);
assert_eq!(
left.cmp(&right),
left.partial_cmp(&right).unwrap(),
"ordering is total for ordered parameters"
);
}

#[test]
fn test_from_array() {
let mut map = RangeInclusiveMap::new();
Expand All @@ -1039,7 +1090,10 @@ mod tests {

#[test]
fn test_macro() {
assert_eq!(range_inclusive_map![], RangeInclusiveMap::<i64, i64>::new());
assert_eq!(
range_inclusive_map![],
RangeInclusiveMap::<i64, i64>::default()
);
assert_eq!(
range_inclusive_map!(0..=100 => "abc", 100..=200 => "def", 200..=300 => "ghi"),
[(0..=100, "abc"), (100..=200, "def"), (200..=300, "ghi")]
Expand Down
85 changes: 60 additions & 25 deletions src/inclusive_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,16 @@ mod tests {
assert_eq!(set.first(), set.iter().min_by_key(|range| range.start()));
}

#[proptest]
fn test_len(mut map: RangeInclusiveSet<u64>) {
assert_eq!(map.len(), map.iter().count());
assert_eq!(map.is_empty(), map.len() == 0);
map.clear();
assert_eq!(map.len(), 0);
assert!(map.is_empty());
assert_eq!(map.iter().count(), 0);
}

#[proptest]
fn test_last(set: RangeInclusiveSet<u64>) {
assert_eq!(set.last(), set.iter().max_by_key(|range| range.end()));
Expand Down Expand Up @@ -551,6 +561,47 @@ mod tests {
}
}

#[proptest]
#[allow(deprecated)]
fn test_hash(left: RangeInclusiveSet<u64>, right: RangeInclusiveSet<u64>) {
use core::hash::{Hash, Hasher, SipHasher};

let hash = |set: &RangeInclusiveSet<_>| {
let mut hasher = SipHasher::new();
set.hash(&mut hasher);
hasher.finish()
};

if left == right {
assert!(
hash(&left) == hash(&right),
"if two values are equal, their hash must be equal"
);
}

// if the hashes are equal the values might not be the same (collision)
if hash(&left) != hash(&right) {
assert!(
left != right,
"if two value's hashes are not equal, they must not be equal"
);
}
}

#[proptest]
fn test_ord(left: RangeInclusiveSet<u64>, right: RangeInclusiveSet<u64>) {
assert_eq!(
left == right,
left.cmp(&right).is_eq(),
"ordering and equality must match"
);
assert_eq!(
left.cmp(&right),
left.partial_cmp(&right).unwrap(),
"ordering is total for ordered parameters"
);
}

#[test]
fn test_from_array() {
let mut set = RangeInclusiveSet::new();
Expand All @@ -561,18 +612,15 @@ mod tests {

#[test]
fn test_macro() {
assert_eq!(range_inclusive_set![], RangeInclusiveSet::<i64>::new());
assert_eq!(range_inclusive_set![], RangeInclusiveSet::<i64>::default());
assert_eq!(
range_inclusive_set![0..=100, 200..=300, 400..=500],
[0..=100, 200..=300, 400..=500].iter().cloned().collect(),
);
}

#[proptest]
fn test_union_overlaps_u8(left: Vec<RangeInclusive<u8>>, right: Vec<RangeInclusive<u8>>) {
let left: RangeInclusiveSet<_> = filter_ranges(left).into_iter().collect();
let right: RangeInclusiveSet<_> = filter_ranges(right).into_iter().collect();

fn test_union_overlaps_u8(left: RangeInclusiveSet<u8>, right: RangeInclusiveSet<u8>) {
let mut union = RangeInclusiveSet::new();
for range in left.union(&right) {
// there should not be any overlaps in the ranges returned by the union
Expand All @@ -582,10 +630,8 @@ mod tests {
}

#[proptest]
fn test_union_contains_u8(left: Vec<RangeInclusive<u8>>, right: Vec<RangeInclusive<u8>>) {
let left: RangeInclusiveSet<_> = filter_ranges(left).into_iter().collect();
let right: RangeInclusiveSet<_> = filter_ranges(right).into_iter().collect();
let union: RangeInclusiveSet<_> = left.union(&right).collect();
fn test_union_contains_u8(left: RangeInclusiveSet<u8>, right: RangeInclusiveSet<u8>) {
let union = (&left) | (&right);

// value should be in the union if and only if it is in either set
for value in 0..u8::MAX {
Expand All @@ -597,31 +643,20 @@ mod tests {
}

#[proptest]
fn test_intersection_contains_u8(
left: Vec<RangeInclusive<u8>>,
right: Vec<RangeInclusive<u8>>,
) {
let left: RangeInclusiveSet<_> = filter_ranges(left).into_iter().collect();
let right: RangeInclusiveSet<_> = filter_ranges(right).into_iter().collect();
let union: RangeInclusiveSet<_> = left.intersection(&right).collect();
fn test_intersection_contains_u8(left: RangeInclusiveSet<u8>, right: RangeInclusiveSet<u8>) {
let intersection = (&left) & (&right);

// value should be in the union if and only if it is in either set
// value should be in the intersection if and only if it is in both sets
for value in 0..u8::MAX {
assert_eq!(
union.contains(&value),
intersection.contains(&value),
left.contains(&value) && right.contains(&value)
);
}
}

#[proptest]
fn test_intersection_overlaps_u8(
left: Vec<RangeInclusive<u8>>,
right: Vec<RangeInclusive<u8>>,
) {
let left: RangeInclusiveSet<_> = filter_ranges(left).into_iter().collect();
let right: RangeInclusiveSet<_> = filter_ranges(right).into_iter().collect();

fn test_intersection_overlaps_u8(left: RangeInclusiveSet<u8>, right: RangeInclusiveSet<u8>) {
let mut union = RangeInclusiveSet::new();
for range in left.intersection(&right) {
// there should not be any overlaps in the ranges returned by the
Expand Down
53 changes: 52 additions & 1 deletion src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,16 @@ mod tests {
);
}

#[proptest]
fn test_len(mut map: RangeMap<u64, String>) {
assert_eq!(map.len(), map.iter().count());
assert_eq!(map.is_empty(), map.len() == 0);
map.clear();
assert_eq!(map.len(), 0);
assert!(map.is_empty());
assert_eq!(map.iter().count(), 0);
}

#[proptest]
fn test_last(set: RangeMap<u64, String>) {
assert_eq!(
Expand Down Expand Up @@ -911,6 +921,47 @@ mod tests {
}
}

#[proptest]
#[allow(deprecated)]
fn test_hash(left: RangeMap<u64, u64>, right: RangeMap<u64, u64>) {
use core::hash::{Hash, Hasher, SipHasher};

let hash = |set: &RangeMap<_, _>| {
let mut hasher = SipHasher::new();
set.hash(&mut hasher);
hasher.finish()
};

if left == right {
assert!(
hash(&left) == hash(&right),
"if two values are equal, their hash must be equal"
);
}

// if the hashes are equal the values might not be the same (collision)
if hash(&left) != hash(&right) {
assert!(
left != right,
"if two value's hashes are not equal, they must not be equal"
);
}
}

#[proptest]
fn test_ord(left: RangeMap<u64, u64>, right: RangeMap<u64, u64>) {
assert_eq!(
left == right,
left.cmp(&right).is_eq(),
"ordering and equality must match"
);
assert_eq!(
left.cmp(&right),
left.partial_cmp(&right).unwrap(),
"ordering is total for ordered parameters"
);
}

#[test]
fn test_from_array() {
let mut map = RangeMap::new();
Expand All @@ -924,7 +975,7 @@ mod tests {

#[test]
fn test_macro() {
assert_eq!(range_map![], RangeMap::<i64, i64>::new());
assert_eq!(range_map![], RangeMap::<i64, i64>::default());
assert_eq!(
range_map!(0..100 => "abc", 100..200 => "def", 200..300 => "ghi"),
[(0..100, "abc"), (100..200, "def"), (200..300, "ghi")]
Expand Down
Loading

0 comments on commit 1f6f22b

Please sign in to comment.