-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement `bitmap.iter_range()` and `bitmap.into_iter_range()`, based on `advance_to()` and `advance_back_to()`. Open questions: - The equivalent to `.iter_range` for BTreeSet is called `.range`. Is that a better name: `.range`/`.into_range`? - `.range` panics on start > end. (or start == end when both are excluded). Should we? Co-authored-by: Matthew Herzl <[email protected]>
- Loading branch information
Showing
2 changed files
with
162 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use proptest::collection::btree_set; | ||
use proptest::prelude::*; | ||
use roaring::RoaringBitmap; | ||
|
||
#[test] | ||
fn range_array() { | ||
let mut rb = RoaringBitmap::new(); | ||
rb.insert(0); | ||
rb.insert(1); | ||
rb.insert(10); | ||
rb.insert(100_000); | ||
rb.insert(999_999); | ||
rb.insert(1_000_000); | ||
|
||
let expected = vec![1, 10, 100_000, 999_999]; | ||
let actual: Vec<u32> = rb.iter_range(1..=999_999).collect(); | ||
assert_eq!(expected, actual); | ||
} | ||
|
||
#[test] | ||
fn range_bitmap() { | ||
let rb = RoaringBitmap::from_sorted_iter(10..5000).unwrap(); | ||
|
||
let expected = vec![10, 11, 12]; | ||
let actual: Vec<u32> = rb.iter_range(0..13).collect(); | ||
assert_eq!(expected, actual); | ||
} | ||
|
||
#[test] | ||
fn range_none() { | ||
let rb = RoaringBitmap::from_sorted_iter(10..5000).unwrap(); | ||
|
||
let expected: Vec<u32> = vec![]; | ||
#[allow(clippy::reversed_empty_ranges)] | ||
let actual: Vec<u32> = rb.iter_range(13..0).collect(); | ||
assert_eq!(expected, actual); | ||
} | ||
|
||
proptest! { | ||
#[test] | ||
fn proptest_range( | ||
values in btree_set(..=262_143_u32, ..=1000), | ||
range_a in 0u32..262_143, | ||
range_b in 0u32..262_143, | ||
){ | ||
let range = range_a.min(range_b)..=range_a.max(range_b); | ||
|
||
let bitmap = RoaringBitmap::from_sorted_iter(values.iter().cloned()).unwrap(); | ||
let expected: Vec<u32> = values.range(range.clone()).copied().collect(); | ||
let actual: Vec<u32> = bitmap.iter_range(range.clone()).collect(); | ||
|
||
assert_eq!(expected, actual); | ||
} | ||
} |