crates/kleene-plan/tests/id_ranges.rs
//! `IdRanges`: the ids of an id space's blocks in some intervals, from the numbers alone,
//! against a plain filter over every id.
use kleene_plan::{External, IdRanges, Value};
/// Three types: ids 0..3, 3..3 (none), 3..7; then 7..10.
const BLOCKS: [(Value, Value); 4] = [(0, 3), (3, 0), (3, 4), (7, 3)];
fn plain(blocks: &[(Value, Value)], wanted: &[(Value, Value)]) -> Vec<Value> {
(0..20)
.filter(|&id| blocks.iter().any(|b| id >= b.0 && id < b.0 + b.1))
.filter(|&id| wanted.iter().any(|w| (w.0..=w.1).contains(&id)))
.collect()
}
fn listed(r: &IdRanges) -> Vec<Value> {
let mut got = Vec::new();
r.lookup(0, &[&[]], 1, &mut |_, row| got.push(row[0]))
.unwrap();
got
}
#[test]
fn the_ids_in_the_intervals_from_the_numbers() {
let some: [&[(Value, Value)]; 7] = [
&[(4, 4)], // an equality
&[(2, 8)], // a range across three blocks
&[(1, 1), (8, 8), (5, 5)], // an OR of ids
&[(10, 10)], // beyond the last block
&[(9, Value::MAX)], // open above
&[(5, 2)], // empty
&[(0, 4), (3, 6), (6, 6)], // overlapping: each id once
];
for wanted in some {
let r = IdRanges::new(BLOCKS, wanted.iter().copied());
assert_eq!(listed(&r), plain(&BLOCKS, wanted), "{wanted:?}");
assert_eq!(
r.ids().collect::<Vec<_>>(),
plain(&BLOCKS, wanted),
"{wanted:?}"
);
}
// Bound: the keys among them, in their places.
let r = IdRanges::new([(0, 3), (7, 3)], [(2, 8)]);
let mut got = Vec::new();
r.lookup(1, &[&[9, 2, 5, 7, 11]], 5, &mut |k, row| {
got.push((k, row[0]))
})
.unwrap();
assert_eq!(got, vec![(1, 2), (3, 7)]);
// One key: every id for it.
let mut got = Vec::new();
r.lookup(0, &[&[]], 2, &mut |k, row| got.push((k, row[0])))
.unwrap();
assert_eq!(got, vec![(0, 2), (0, 7), (0, 8), (1, 2), (1, 7), (1, 8)]);
}