use super::OwnReadError;
use crate::{
Scored,
retrieve::{EqSeen, HashSeen, Ranked, ranked_keys, ranked_keys_excluding},
};
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct SegmentKey {
media: &'static str,
segment: i64,
}
const fn key(media: &'static str, segment: i64) -> SegmentKey {
SegmentKey { media, segment }
}
fn hit(media: &'static str, segment: i64, score: f32) -> Scored<SegmentKey, f32> {
Scored::new(key(media, segment), score)
}
fn stream() -> Vec<Scored<SegmentKey, f32>> {
vec![
hit("m1", 1, 1.00),
hit("m1", 2, 0.90),
hit("m2", 3, 0.80),
hit("m2", 4, 0.70),
]
}
fn placed(ranked: &[Ranked<SegmentKey, f32>]) -> Vec<(i64, u64)> {
ranked
.iter()
.map(|item| (item.node_ref().segment, item.rank()))
.collect()
}
#[test]
fn the_example_leaves_its_ordinal_behind() {
let plain = ranked_keys::<_, _, HashSeen<SegmentKey>>(stream(), &0.0, 10);
assert_eq!(placed(&plain), vec![(1, 1), (2, 2), (3, 3), (4, 4)]);
let without =
ranked_keys_excluding::<_, _, HashSeen<SegmentKey>>(stream(), &0.0, 10, &key("m1", 1));
assert_eq!(placed(&without), vec![(2, 2), (3, 3), (4, 4)]);
}
#[test]
fn the_budget_counts_neighbours_rather_than_counting_the_example() {
let plain = ranked_keys::<_, _, HashSeen<SegmentKey>>(stream(), &0.0, 2);
assert_eq!(placed(&plain), vec![(1, 1), (2, 2)]);
let without =
ranked_keys_excluding::<_, _, HashSeen<SegmentKey>>(stream(), &0.0, 2, &key("m1", 1));
assert_eq!(placed(&without), vec![(2, 2), (3, 3)]);
}
#[test]
fn either_dedup_word_carries_the_exclusion() {
let hashed =
ranked_keys_excluding::<_, _, HashSeen<SegmentKey>>(stream(), &0.0, 10, &key("m2", 3));
let compared =
ranked_keys_excluding::<_, _, EqSeen<SegmentKey>>(stream(), &0.0, 10, &key("m2", 3));
assert_eq!(placed(&hashed), vec![(1, 1), (2, 2), (4, 4)]);
assert_eq!(placed(&hashed), placed(&compared));
}
#[test]
fn an_example_outside_the_stream_drops_nothing() {
let without =
ranked_keys_excluding::<_, _, HashSeen<SegmentKey>>(stream(), &0.0, 10, &key("m2", 1));
assert_eq!(placed(&without), vec![(1, 1), (2, 2), (3, 3), (4, 4)]);
}
#[test]
fn the_floor_still_terminates_and_the_hole_survives_it() {
let without =
ranked_keys_excluding::<_, _, HashSeen<SegmentKey>>(stream(), &0.75, 10, &key("m1", 1));
assert_eq!(placed(&without), vec![(2, 2), (3, 3)]);
}
#[test]
fn a_zero_budget_answers_nothing() {
let without =
ranked_keys_excluding::<_, _, HashSeen<SegmentKey>>(stream(), &0.0, 0, &key("m1", 1));
assert!(without.is_empty());
}
#[test]
fn every_refusal_names_what_the_read_found() {
let unindexed = OwnReadError::<core::convert::Infallible>::Unindexed.to_string();
let several = OwnReadError::<core::convert::Infallible>::Several { held: 3 }.to_string();
let width = OwnReadError::<core::convert::Infallible>::Width {
declared: 768,
stored: 384,
}
.to_string();
assert_eq!(unindexed, "the key holds no vector in this column");
assert_eq!(
several,
"the key holds 3 vectors in this column where the election declared one"
);
assert_eq!(
width,
"the stored vector has 384 coordinates where the election declared 768"
);
let mut distinct = vec![unindexed, several, width];
distinct.sort();
distinct.dedup();
assert_eq!(
distinct.len(),
3,
"two arms say the same thing: {distinct:?}"
);
}
#[test]
fn only_the_engine_arm_has_a_source() {
#[derive(Debug)]
struct Refusal;
impl core::fmt::Display for Refusal {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str("the toy engine refused")
}
}
impl core::error::Error for Refusal {}
let engine = OwnReadError::Engine(Refusal);
assert_eq!(engine.to_string(), "the toy engine refused");
assert!(core::error::Error::source(&engine).is_some());
let unindexed = OwnReadError::<Refusal>::Unindexed;
assert!(core::error::Error::source(&unindexed).is_none());
assert!(core::error::Error::source(&OwnReadError::<Refusal>::Several { held: 2 }).is_none());
assert!(
core::error::Error::source(&OwnReadError::<Refusal>::Width {
declared: 4,
stored: 5
})
.is_none()
);
}