use std::sync::Arc;
use super::{FoldSemantics, Max, Sum, Weighted};
use crate::{Embedding, FoldedQuery};
const DIM: usize = 4;
fn embedding(coordinates: [f32; DIM]) -> Embedding<f32, DIM> {
Embedding::new(Arc::from(coordinates.to_vec())).expect("the fixture declares DIM")
}
fn query(m: usize) -> FoldedQuery<f32, f32, DIM> {
FoldedQuery::new(
(0..m).map(|_| embedding([1.0, 0.0, 0.0, 0.0])).collect(),
0.0,
)
.expect("the fixture asks with at least one vector")
}
#[test]
fn sum_adds_every_answered_slot() {
let folded = Sum::new().score(&[Some(0.5_f32), Some(0.25), Some(0.125)]);
assert!((folded - 0.875).abs() < f32::EPSILON, "{folded}");
}
#[test]
fn sum_starts_from_the_first_present_slot() {
let folded = Sum::new().score(&[None, Some(0.5_f32), None, Some(0.25)]);
assert!((folded - 0.75).abs() < f32::EPSILON, "{folded}");
}
#[test]
fn max_takes_the_best_answered_slot() {
assert!((Max::new().score(&[Some(0.25_f32), Some(0.9), Some(0.5)]) - 0.9).abs() < f32::EPSILON);
assert!((Max::new().score(&[None, Some(0.4_f32)]) - 0.4).abs() < f32::EPSILON);
}
#[test]
fn max_keeps_what_it_holds_against_a_value_it_cannot_place() {
let folded = Max::new().score(&[Some(0.5_f32), Some(f32::NAN)]);
assert!((folded - 0.5).abs() < f32::EPSILON, "{folded}");
}
#[test]
fn the_two_shipped_folds_answer_the_same_slots_differently() {
let slots = [Some(0.6_f32), Some(0.6), None];
assert!((Sum::new().score(&slots) - 1.2).abs() < f32::EPSILON);
assert!((Max::new().score(&slots) - 0.6).abs() < f32::EPSILON);
}
#[test]
fn the_weighted_fold_scales_each_query_vectors_own_best() {
let weights = Weighted::for_query(&query(3), vec![2.0_f32, 1.0, 0.5])
.expect("three weights for three vectors");
let folded = weights.score(&[Some(0.5), Some(0.25), Some(0.5)]);
assert!((folded - 1.5).abs() < f32::EPSILON, "{folded}");
}
#[test]
fn an_unanswered_query_vector_contributes_no_term() {
let weights =
Weighted::for_query(&query(3), vec![2.0_f32, 100.0, 0.5]).expect("three weights for three");
let folded = weights.score(&[Some(0.5), None, Some(0.5)]);
assert!((folded - 1.25).abs() < f32::EPSILON, "{folded}");
}
#[test]
fn a_weight_list_that_is_not_the_querys_length_is_refused() {
assert!(Weighted::for_query(&query(3), vec![1.0_f32, 1.0]).is_none());
assert!(Weighted::for_query(&query(3), vec![1.0_f32, 1.0, 1.0, 1.0]).is_none());
assert!(Weighted::for_query(&query(1), Vec::<f32>::new()).is_none());
assert!(Weighted::for_query(&query(1), vec![1.0_f32]).is_some());
}
#[test]
fn the_weights_are_readable_in_the_order_the_vectors_were_asked() {
let weights =
Weighted::for_query(&query(2), vec![0.25_f32, 0.75]).expect("two weights for two vectors");
assert_eq!(weights.weights(), &[0.25, 0.75]);
}
#[test]
fn a_non_negative_weight_commutes_with_the_per_query_max() {
let members = [0.25_f32, 0.75, 0.5];
let weight = 3.0_f32;
let weighted_after = weight * members.iter().copied().fold(f32::MIN, f32::max);
let weighted_inside = members
.iter()
.map(|member| weight * member)
.fold(f32::MIN, f32::max);
assert!((weighted_after - weighted_inside).abs() < f32::EPSILON);
}
#[test]
fn a_fold_this_module_never_heard_of_is_one_impl() {
struct Leading;
impl<S: Clone + Send + Sync> FoldSemantics<S> for Leading {
fn score(&self, bests: &[Option<S>]) -> S {
bests
.iter()
.flatten()
.next()
.cloned()
.expect("an entity reaches a fold with at least one answer")
}
fn name(&self) -> &str {
"leading"
}
}
assert!((Leading.score(&[None, Some(0.25_f32), Some(0.9)]) - 0.25).abs() < f32::EPSILON);
}
#[test]
fn a_composite_currency_folds_without_a_framework_row() {
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
struct Fused(f32, u32);
impl core::ops::Add for Fused {
type Output = Self;
fn add(self, other: Self) -> Self {
Self(self.0 + other.0, self.1 + other.1)
}
}
let folded = Sum::new().score(&[Some(Fused(0.5, 1)), None, Some(Fused(0.25, 2))]);
assert_eq!(folded, Fused(0.75, 3));
}