use ingraph_codegen::aggregate::Measure as Word;
use super::{Counting, Measure, Summing};
use crate::Count;
#[test]
fn counting_answers_the_number_of_rows() {
assert_eq!(Counting::new().fold(&["a", "b", "c"]), Count::new(3));
assert_eq!(Counting::new().fold(&[7_u64]), Count::new(1));
}
#[test]
fn counting_folds_a_type_this_framework_never_heard_of() {
struct Opaque;
assert_eq!(Counting::new().fold(&[Opaque, Opaque]), Count::new(2));
}
#[test]
fn counting_answers_zero_for_a_group_with_no_rows() {
assert_eq!(Counting::new().fold(&[] as &[u64]), Count::new(0));
}
#[test]
fn summing_adds_every_row() {
assert_eq!(Summing::new().fold(&[10_u64, 20, 30]), 60);
assert_eq!(Summing::new().fold(&[7_i32]), 7);
}
#[test]
fn summing_starts_from_the_first_row_rather_than_an_identity() {
#[derive(Debug, Clone, Copy, PartialEq)]
struct Cents(i64);
impl core::ops::Add for Cents {
type Output = Self;
fn add(self, other: Self) -> Self {
Self(self.0 + other.0)
}
}
assert_eq!(
Summing::new().fold(&[Cents(125), Cents(-25), Cents(400)]),
Cents(500)
);
}
#[test]
#[should_panic(expected = "the `summing` measure was handed a group with no rows")]
fn summing_refuses_to_invent_a_total_for_a_group_with_no_rows() {
let _ = Summing::new().fold(&[] as &[u64]);
}
#[test]
fn the_two_shipped_measures_answer_the_same_group_differently() {
let rows = [4_u64, 4, 4];
assert_eq!(Counting::new().fold(&rows), Count::new(3));
assert_eq!(Summing::new().fold(&rows), 12);
}
#[test]
fn a_citizen_answers_to_the_word_the_grammar_spells_it_with() {
assert_eq!(
<Counting as Measure<u64>>::name(&Counting::new()),
Word::SHIPPED[0]
);
assert_eq!(
<Summing as Measure<u64>>::name(&Summing::new()),
Word::SHIPPED[1]
);
assert_eq!(
Word::SHIPPED.len(),
2,
"two citizens ship, and two words do"
);
}
#[test]
fn every_shipped_word_names_a_citizen_here() {
let counting = Counting::new();
let summing = Summing::new();
for word in Word::SHIPPED {
let named = Word::shipped(word).expect("a shipped word names a shipped fold");
let answered = match named {
Word::Counting => <Counting as Measure<u64>>::name(&counting),
Word::Summing => <Summing as Measure<u64>>::name(&summing),
Word::Declared(_) => unreachable!("a bare word is never a declared path"),
};
assert_eq!(answered, word);
}
}
#[test]
fn a_borrowed_measure_measures() {
fn through<T, M: Measure<T>>(measure: M, items: &[T]) -> (M::Value, String) {
let name = measure.name().to_owned();
(measure.fold(items), name)
}
let counting = Counting::new();
let summing = Summing::new();
let rows = [2_u64, 3, 5];
assert_eq!(
through(&counting, &rows),
(Count::new(3), "counting".to_owned())
);
assert_eq!(through(&summing, &rows), (10, "summing".to_owned()));
}
#[test]
fn an_erased_measure_measures() {
let erased: &dyn Measure<u64, Value = u64> = &Summing::new();
assert_eq!(erased.fold(&[1, 2, 3]), 6);
assert_eq!(erased.name(), "summing");
let boxed: Box<dyn Measure<u64, Value = Count>> = Box::new(Counting::new());
assert_eq!(boxed.fold(&[9, 9]), Count::new(2));
}
#[test]
fn a_measure_this_module_never_heard_of_is_one_impl() {
struct Distinct;
impl<T: Ord + Clone + Send + Sync> Measure<T> for Distinct {
type Value = u64;
fn fold(&self, items: &[T]) -> u64 {
let mut seen = items.to_vec();
seen.sort_unstable();
seen.dedup();
u64::try_from(seen.len()).unwrap_or(u64::MAX)
}
fn name(&self) -> &str {
"distinct"
}
}
assert_eq!(Distinct.fold(&[1_u64, 2, 2, 3, 3, 3]), 3);
assert_eq!(<Distinct as Measure<u64>>::name(&Distinct), "distinct");
}
#[test]
fn a_measure_over_one_item_type_needs_no_blanket() {
struct Longest;
impl Measure<String> for Longest {
type Value = usize;
fn fold(&self, items: &[String]) -> usize {
items.iter().map(String::len).max().unwrap_or_default()
}
fn name(&self) -> &str {
"longest"
}
}
assert_eq!(
Longest.fold(&["a".to_owned(), "abcd".to_owned(), "ab".to_owned()]),
4
);
}
#[test]
fn a_composite_column_sums_without_a_framework_row() {
#[derive(Debug, Clone, Copy, PartialEq)]
struct Frames(u32, u64);
impl core::ops::Add for Frames {
type Output = Self;
fn add(self, other: Self) -> Self {
Self(self.0 + other.0, self.1 + other.1)
}
}
assert_eq!(
Summing::new().fold(&[Frames(2, 100), Frames(3, 250)]),
Frames(5, 350)
);
}
#[test]
fn a_citizen_is_reached_through_its_constructor() {
assert_eq!(Counting::new(), Counting::default());
assert_eq!(Summing::new(), Summing::default());
}