use crate::{ArmWeights, ExpandOrigin, HashJoin, LaneIdent, RoutedProbe};
use super::{FanStream, Fusion, RankedStream, Rooted, fuse_fanned};
static EN: &LaneIdent = LaneIdent::new("en");
static JA: &LaneIdent = LaneIdent::new("ja");
fn cell(
lane: &'static LaneIdent,
arm: usize,
arm_weight: f64,
lane_weight: f64,
ranked: &[(u32, u64, u8)],
) -> FanStream<u32, u8> {
FanStream::new(
lane,
arm,
arm_weight,
lane_weight,
ranked
.iter()
.map(|&(root, rank, seen)| Rooted::new(root, rank, seen))
.collect(),
)
}
fn roots(grid: Vec<FanStream<u32, u8>>) -> Vec<u32> {
fuse_fanned::<u32, u8, HashJoin<u32>>(grid, Fusion::rrf())
.into_iter()
.map(|row| row.into_components().1)
.collect()
}
#[test]
fn a_cells_coefficient_is_the_two_numbers_multiplied() {
let composed = cell(JA, 1, ArmWeights::DEFAULT_ARM_WEIGHT, 2.0, &[(7, 1, 0)]);
assert!((composed.weight() - 1.0).abs() < 1e-12);
assert_eq!(composed.arm(), 1);
assert_eq!(composed.lane(), JA);
assert!(
(cell(EN, 0, ArmWeights::ORIGINAL, 2.0, &[]).weight() - 2.0).abs() < 1e-12,
"the original arm carries the lane's emphasis whole",
);
assert!(
(cell(EN, 2, 0.25, RoutedProbe::<()>::UNWEIGHTED, &[]).weight() - 0.25).abs() < 1e-12,
"an unweighted lane carries the arm's own number whole",
);
}
#[test]
fn the_axis_a_tower_did_not_open_is_the_identity_on_that_side() {
let uncut = FanStream::uncut(
1,
ArmWeights::DEFAULT_ARM_WEIGHT,
vec![Rooted::new(1_u32, 1, 0_u8)],
);
assert_eq!(uncut.lane(), LaneIdent::UND);
assert!((uncut.weight() - ArmWeights::DEFAULT_ARM_WEIGHT).abs() < 1e-12);
let unwidened = FanStream::unwidened(JA, 2.0, vec![Rooted::new(1_u32, 1, 0_u8)]);
assert_eq!(unwidened.arm(), 0);
assert!((unwidened.weight() - 2.0).abs() < 1e-12);
}
#[test]
fn an_arms_weight_is_read_off_its_origin() {
let shipped = ArmWeights::new();
assert!((shipped.weight(&ExpandOrigin::Original) - ArmWeights::ORIGINAL).abs() < 1e-12);
assert!(
(shipped.weight(&ExpandOrigin::expander("synonyms")) - ArmWeights::DEFAULT_ARM_WEIGHT).abs()
< 1e-12
);
let named = ArmWeights::new().with_paraphrase(0.9);
assert_eq!(named.paraphrase(), Some(0.9));
assert!((named.weight(&ExpandOrigin::expander("translate")) - 0.9).abs() < 1e-12);
assert!((named.weight(&ExpandOrigin::Original) - ArmWeights::ORIGINAL).abs() < 1e-12);
let mut narrowed = named;
narrowed.set_paraphrase(None);
assert_eq!(narrowed, ArmWeights::DEFAULT);
}
#[test]
fn one_familys_two_arms_are_one_witness() {
let grid = vec![
cell(EN, 0, ArmWeights::ORIGINAL, 1.0, &[(1, 3, 0)]),
cell(EN, 1, ArmWeights::ORIGINAL, 1.0, &[(1, 1, 0), (2, 2, 0)]),
];
let fused = fuse_fanned::<u32, u8, HashJoin<u32>>(grid, Fusion::rrf());
let one = fused
.iter()
.find(|row| *row.root_ref() == 1)
.expect("the deduplicated root is still answered");
assert_eq!(one.evidence().len(), 1);
assert_eq!(one.evidence()[0].ordinal(), 1);
assert!((one.score().get() - 1.0 / 61.0).abs() < 1e-12);
}
#[test]
fn one_root_under_two_evidences_survives_twice() {
let grid = vec![
cell(EN, 0, ArmWeights::ORIGINAL, 1.0, &[(1, 1, 7)]),
cell(EN, 1, ArmWeights::ORIGINAL, 1.0, &[(1, 1, 9)]),
];
let fused = fuse_fanned::<u32, u8, HashJoin<u32>>(grid, Fusion::rrf());
let one = fused
.iter()
.find(|row| *row.root_ref() == 1)
.expect("the root is answered");
assert_eq!(one.evidence().len(), 2);
assert!((one.score().get() - 2.0 / 61.0).abs() < 1e-12);
}
#[test]
fn two_lanes_that_found_one_pair_are_two_witnesses() {
let grid = vec![
cell(EN, 0, ArmWeights::ORIGINAL, 1.0, &[(1, 1, 0)]),
cell(JA, 0, ArmWeights::ORIGINAL, 1.0, &[(1, 1, 0)]),
];
let fused = fuse_fanned::<u32, u8, HashJoin<u32>>(grid, Fusion::rrf());
assert_eq!(fused[0].evidence().len(), 2);
assert!((fused[0].score().get() - 2.0 / 61.0).abs() < 1e-12);
}
#[test]
fn one_arms_own_repeats_are_not_the_familys_business() {
let grid = vec![
cell(EN, 0, ArmWeights::ORIGINAL, 1.0, &[(1, 1, 0)]),
cell(EN, 0, ArmWeights::ORIGINAL, 1.0, &[(1, 4, 0)]),
];
let fused = fuse_fanned::<u32, u8, HashJoin<u32>>(grid, Fusion::rrf());
assert_eq!(fused[0].evidence().len(), 2);
assert!((fused[0].score().get() - (1.0 / 61.0 + 1.0 / 64.0)).abs() < 1e-12);
}
#[test]
fn nothing_found_is_nothing_answered() {
assert!(roots(Vec::new()).is_empty());
assert!(roots(vec![cell(EN, 0, 1.0, 1.0, &[])]).is_empty());
}
#[test]
fn a_vec_of_rooted_hits_is_a_ranked_stream() {
let stream: Vec<Rooted<u32, u8>> = vec![Rooted::new(1, 1, 0), Rooted::new(2, 2, 0)];
let ranked = RankedStream::into_ranked(stream.clone());
assert_eq!(ranked, stream);
}