use std::rc::Rc;
use super::{Fused, FusedScore, Rooted, Sighting};
#[derive(Debug, PartialEq)]
struct Heavy(Rc<str>);
fn score(value: f64) -> FusedScore {
FusedScore::new(value).expect("not a NaN")
}
fn saw<R>(row: &Fused<R, &'static str>) -> Vec<&'static str> {
row.evidence().iter().map(|held| *held.seen()).collect()
}
#[test]
fn a_rooted_hit_decomposes_into_what_it_was_built_from() {
let hit = Rooted::new(Heavy(Rc::from("m1")), 7, Heavy(Rc::from("transcript")));
assert_eq!(hit.root_ref(), &Heavy(Rc::from("m1")));
assert_eq!(hit.rank(), 7);
assert_eq!(hit.evidence_ref(), &Heavy(Rc::from("transcript")));
let (root, rank, evidence) = hit.into_components();
assert_eq!(root, Heavy(Rc::from("m1")));
assert_eq!(rank, 7);
assert_eq!(evidence, Heavy(Rc::from("transcript")));
}
#[test]
fn a_fused_row_decomposes_and_keeps_its_evidence_order() {
let row = Fused::new(
score(0.032_522_474_881_015_34),
Heavy(Rc::from("m2")),
vec![
Sighting::new(0.25, 1, "voiceprint"),
Sighting::new(0.125, 4, "transcript"),
],
);
assert_eq!(row.score(), score(0.032_522_474_881_015_34));
assert_eq!(row.root_ref(), &Heavy(Rc::from("m2")));
assert_eq!(saw(&row), ["voiceprint", "transcript"]);
assert!((row.evidence()[0].contribution() - 0.25).abs() < f64::EPSILON);
assert_eq!(row.evidence()[0].ordinal(), 1);
let (fused, root, evidence) = row.into_components();
assert_eq!(fused.get(), 0.032_522_474_881_015_34);
assert_eq!(root, Heavy(Rc::from("m2")));
assert_eq!(
evidence,
vec![
Sighting::new(0.25, 1, "voiceprint"),
Sighting::new(0.125, 4, "transcript"),
],
);
}
#[test]
fn the_copy_accessor_answers_where_the_root_is_copy() {
let row = Fused::new(
score(0.015_625),
"m4",
vec![Sighting::new(0.5, 1, "voiceprint")],
);
assert_eq!(row.root(), "m4");
assert_eq!(row.root_ref(), &"m4");
}
#[test]
fn mapping_the_root_carries_the_score_and_the_evidence() {
let keyed = Fused::new(
score(0.032_002_048_131_080_39),
"m3",
vec![Sighting::new(0.5, 2, "transcript")],
);
let hydrated = keyed.map_root(Heavy(Rc::from("the whole entity")));
assert_eq!(hydrated.score(), score(0.032_002_048_131_080_39));
assert_eq!(hydrated.root_ref(), &Heavy(Rc::from("the whole entity")));
assert_eq!(saw(&hydrated), ["transcript"]);
assert!(
(hydrated.evidence()[0].contribution() - 0.5).abs() < f64::EPSILON,
"the carry dropped the fold's own number",
);
assert_eq!(
hydrated.evidence()[0].ordinal(),
2,
"the carry dropped the rank the fold computed from",
);
}