use super::{EqJoin, HashJoin, Join};
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct Hashable(&'static str);
#[derive(Debug, Clone, PartialEq)]
struct Unhashable(f64);
#[test]
fn both_mechanisms_number_the_roots_densely_from_zero() {
fn walk<R: Clone, J: Join<R>>(roots: &[R]) -> Vec<Option<usize>> {
let mut join = J::empty();
roots.iter().map(|root| join.place(root)).collect()
}
let roots = [
Hashable("m1"),
Hashable("m2"),
Hashable("m1"),
Hashable("m3"),
Hashable("m2"),
Hashable("m1"),
];
let placed = vec![None, None, Some(0), None, Some(1), Some(0)];
assert_eq!(walk::<_, HashJoin<_>>(&roots), placed);
assert_eq!(walk::<_, EqJoin<_>>(&roots), placed);
}
#[test]
fn the_equality_mechanism_takes_a_root_that_cannot_hash() {
let mut join = EqJoin::empty();
assert_eq!(join.place(&Unhashable(0.5)), None);
assert_eq!(join.place(&Unhashable(0.25)), None);
assert_eq!(join.place(&Unhashable(0.5)), Some(0));
assert_eq!(join.place(&Unhashable(0.25)), Some(1));
}
#[test]
fn a_join_starts_empty() {
let mut hashed: HashJoin<Hashable> = Join::empty();
let mut walked: EqJoin<Hashable> = Join::empty();
assert_eq!(hashed.place(&Hashable("m1")), None);
assert_eq!(walked.place(&Hashable("m1")), None);
}