use std::rc::Rc;
use super::super::{EqJoin, Fused, Fusion, HashJoin, Rooted, fuse};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Saw {
Transcript,
Voiceprint,
Caption,
}
#[derive(Debug, Clone, PartialEq)]
struct Coordinate(f64);
const EPSILON: f64 = 1e-15;
fn list(hits: &[(&'static str, u64, Saw)]) -> Vec<Rooted<&'static str, Saw>> {
hits
.iter()
.map(|(root, rank, saw)| Rooted::new(*root, *rank, *saw))
.collect()
}
fn saw<R>(row: &Fused<R, Saw>) -> Vec<Saw> {
row.evidence().iter().map(|held| *held.seen()).collect()
}
fn order<V>(fused: &[Fused<&'static str, V>]) -> Vec<&'static str> {
fused.iter().map(|row| row.root()).collect()
}
fn score<V>(fused: &[Fused<&'static str, V>], root: &str) -> f64 {
fused
.iter()
.find(|row| *row.root_ref() == root)
.unwrap_or_else(|| panic!("{root} is in the answer"))
.score()
.get()
}
#[track_caller]
fn close(got: f64, hand_computed: f64) {
assert!(
(got - hand_computed).abs() < EPSILON,
"{got} is not {hand_computed}"
);
}
#[test]
fn two_overlapping_lists_fuse_to_the_hand_computed_scores() {
let fused = fuse::<_, _, HashJoin<_>>(
vec![
list(&[
("m1", 1, Saw::Transcript),
("m2", 2, Saw::Transcript),
("m3", 3, Saw::Transcript),
]),
list(&[
("m2", 1, Saw::Voiceprint),
("m3", 2, Saw::Voiceprint),
("m4", 3, Saw::Voiceprint),
]),
],
Fusion::rrf(),
);
assert_eq!(order(&fused), vec!["m2", "m3", "m1", "m4"]);
close(score(&fused, "m1"), 0.016_393_442_622_950_82);
close(score(&fused, "m2"), 0.032_522_474_881_015_34);
close(score(&fused, "m3"), 0.032_002_048_131_080_39);
close(score(&fused, "m4"), 0.015_873_015_873_015_872);
}
#[test]
fn the_evidence_leads_with_the_member_that_moved_the_root_most() {
let fused = fuse::<_, _, HashJoin<_>>(
vec![
list(&[("m2", 2, Saw::Transcript)]),
list(&[("m2", 1, Saw::Voiceprint)]),
],
Fusion::rrf(),
);
assert_eq!(
saw(&fused[0]),
[Saw::Voiceprint, Saw::Transcript],
"the biggest contribution is the UI's primary jump point"
);
let terms: Vec<f64> = fused[0]
.evidence()
.iter()
.map(super::super::Sighting::contribution)
.collect();
assert!(terms[0] > terms[1], "{terms:?}");
assert!(
(terms.iter().sum::<f64>() - fused[0].score().get()).abs() < 1e-12,
"the entries do not add up to the score they made: {terms:?}",
);
}
#[test]
fn evidence_names_only_the_members_that_ranked_it() {
let fused = fuse::<_, _, HashJoin<_>>(
vec![
list(&[("m1", 1, Saw::Transcript)]),
list(&[("m2", 1, Saw::Voiceprint)]),
list(&[("m1", 1, Saw::Caption)]),
],
Fusion::rrf(),
);
assert_eq!(saw(&fused[0]), [Saw::Transcript, Saw::Caption]);
assert_eq!(saw(&fused[1]), [Saw::Voiceprint]);
}
#[test]
fn disjoint_lists_interleave_by_score_and_ties_keep_first_sighting_order() {
let fused = fuse::<_, _, HashJoin<_>>(
vec![
list(&[("a", 1, Saw::Transcript), ("b", 2, Saw::Transcript)]),
list(&[("c", 1, Saw::Voiceprint), ("d", 2, Saw::Voiceprint)]),
],
Fusion::rrf(),
);
assert_eq!(order(&fused), vec!["a", "c", "b", "d"]);
close(score(&fused, "a"), 0.016_393_442_622_950_82);
close(score(&fused, "c"), 0.016_393_442_622_950_82);
close(score(&fused, "b"), 0.016_129_032_258_064_516);
close(score(&fused, "d"), 0.016_129_032_258_064_516);
}
#[test]
fn the_empty_cases_answer_empty() {
let no_members = fuse::<&str, Saw, HashJoin<_>>(vec![], Fusion::rrf());
assert!(no_members.is_empty(), "no members at all");
let empty_members = fuse::<&str, Saw, HashJoin<_>>(vec![vec![], vec![]], Fusion::rrf());
assert!(empty_members.is_empty(), "members that returned nothing");
let one_answered = fuse::<_, _, HashJoin<_>>(
vec![vec![], list(&[("m1", 1, Saw::Transcript)]), vec![]],
Fusion::rrf(),
);
assert_eq!(order(&one_answered), vec!["m1"]);
close(score(&one_answered, "m1"), 0.016_393_442_622_950_82);
}
#[test]
fn a_hole_in_the_ranks_is_read_as_the_engine_meant_it() {
let fused = fuse::<_, _, HashJoin<_>>(
vec![
list(&[("p", 1, Saw::Transcript), ("q", 100, Saw::Transcript)]),
list(&[("s", 1, Saw::Voiceprint), ("r", 2, Saw::Voiceprint)]),
],
Fusion::rrf(),
);
assert_eq!(
order(&fused),
vec!["p", "s", "r", "q"],
"reading list positions would answer [p, s, q, r]"
);
close(score(&fused, "q"), 0.006_25);
close(score(&fused, "r"), 0.016_129_032_258_064_516);
}
#[test]
fn the_damping_constant_decides_between_certainty_and_agreement() {
let lists = || {
vec![
list(&[("x", 1, Saw::Transcript), ("y", 3, Saw::Transcript)]),
list(&[("y", 3, Saw::Voiceprint)]),
]
};
let undamped = fuse::<_, _, HashJoin<_>>(lists(), Fusion::Rrf { k: 0 });
assert_eq!(order(&undamped), vec!["x", "y"]);
close(score(&undamped, "x"), 1.0);
close(score(&undamped, "y"), 0.666_666_666_666_666_6);
let shipped = fuse::<_, _, HashJoin<_>>(lists(), Fusion::rrf());
assert_eq!(order(&shipped), vec!["y", "x"]);
close(score(&shipped, "x"), 0.016_393_442_622_950_82);
close(score(&shipped, "y"), 0.031_746_031_746_031_744);
}
#[test]
fn a_members_second_sighting_of_a_root_is_dropped_whole() {
let fused = fuse::<_, _, HashJoin<_>>(
vec![
list(&[("m1", 1, Saw::Transcript), ("m1", 5, Saw::Transcript)]),
list(&[("m2", 1, Saw::Voiceprint)]),
],
Fusion::rrf(),
);
assert_eq!(order(&fused), vec!["m1", "m2"], "a tie, not a win");
close(score(&fused, "m1"), 0.016_393_442_622_950_82);
close(score(&fused, "m2"), 0.016_393_442_622_950_82);
assert_eq!(saw(&fused[0]), [Saw::Transcript]);
}
#[test]
fn adding_a_member_can_only_raise_a_score() {
let first = list(&[
("d", 1, Saw::Transcript),
("e", 2, Saw::Transcript),
("f", 3, Saw::Transcript),
]);
let second = list(&[("e", 1, Saw::Voiceprint), ("d", 5, Saw::Voiceprint)]);
let alone = fuse::<_, _, HashJoin<_>>(vec![first.clone()], Fusion::rrf());
let both = fuse::<_, _, HashJoin<_>>(vec![first, second], Fusion::rrf());
for root in ["d", "e", "f"] {
assert!(
score(&both, root) >= score(&alone, root),
"{root} lost score by being fused with more evidence"
);
}
assert!(score(&both, "d") > score(&alone, "d"), "ranked by both");
assert!(score(&both, "e") > score(&alone, "e"), "ranked by both");
close(score(&both, "f"), score(&alone, "f"));
}
#[test]
fn a_roots_position_can_fall_even_where_its_score_rose() {
let first = list(&[("d", 1, Saw::Transcript), ("e", 2, Saw::Transcript)]);
let second = list(&[("e", 1, Saw::Voiceprint), ("d", 5, Saw::Voiceprint)]);
let alone = fuse::<_, _, HashJoin<_>>(vec![first.clone()], Fusion::rrf());
assert_eq!(order(&alone), vec!["d", "e"]);
let both = fuse::<_, _, HashJoin<_>>(vec![first, second], Fusion::rrf());
assert_eq!(order(&both), vec!["e", "d"]);
close(score(&both, "d"), 0.031_778_058_007_566_21);
close(score(&both, "e"), 0.032_522_474_881_015_34);
assert!(
score(&both, "d") > score(&alone, "d"),
"the score rose while the position fell"
);
}
#[test]
fn the_equality_mechanism_fuses_the_same_answer() {
let lists = || {
vec![
list(&[("m1", 1, Saw::Transcript), ("m2", 4, Saw::Transcript)]),
list(&[("m2", 1, Saw::Voiceprint), ("m3", 2, Saw::Voiceprint)]),
]
};
let hashed = fuse::<_, _, HashJoin<_>>(lists(), Fusion::rrf());
let walked = fuse::<_, _, EqJoin<_>>(lists(), Fusion::rrf());
assert_eq!(hashed, walked);
}
#[test]
fn a_root_that_cannot_hash_still_fuses() {
let fused = fuse::<_, _, EqJoin<_>>(
vec![
vec![Rooted::new(Coordinate(0.5), 1, Saw::Transcript)],
vec![Rooted::new(Coordinate(0.5), 2, Saw::Voiceprint)],
],
Fusion::rrf(),
);
assert_eq!(fused.len(), 1, "one root, sighted twice");
close(fused[0].score().get(), 0.032_522_474_881_015_34);
}
#[test]
fn the_evidence_seat_is_bare() {
struct Opaque(Rc<str>);
let fused = fuse::<_, _, HashJoin<_>>(
vec![
vec![Rooted::new("m1", 9, Opaque(Rc::from("second")))],
vec![Rooted::new("m1", 1, Opaque(Rc::from("first")))],
],
Fusion::rrf(),
);
let names: Vec<&str> = fused[0]
.evidence()
.iter()
.map(|held| held.seen().0.as_ref())
.collect();
assert_eq!(names, vec!["first", "second"]);
}
#[test]
fn a_zero_rank_cannot_become_an_infinity() {
let fused = fuse::<_, _, HashJoin<_>>(
vec![vec![Rooted::new("m1", 0, Saw::Transcript)]],
Fusion::Rrf { k: 0 },
);
close(fused[0].score().get(), 1.0);
assert!(fused[0].score().get().is_finite());
}
#[test]
fn an_enormous_rank_saturates_instead_of_wrapping() {
let fused = fuse::<_, _, HashJoin<_>>(
vec![
vec![Rooted::new("far", u64::MAX, Saw::Transcript)],
vec![Rooted::new("near", 1, Saw::Voiceprint)],
],
Fusion::rrf(),
);
assert_eq!(order(&fused), vec!["near", "far"]);
assert!(score(&fused, "far") > 0.0, "still a contribution");
assert!(score(&fused, "far") < 1e-18, "and a vanishing one");
}