ingraph 0.1.0

A declaration-driven indexing framework: records and nodes are written as Rust types, and the storage rows, the dataflow between them and the GraphQL query surface are emitted from the declaration.
Documentation
use core::hash::{Hash, Hasher};

use super::*;

/// **THE INTERVAL, at both ends and just outside them.**
///
/// The closed interval is what the type promises, so both endpoints are admitted and the first
/// representable value past each is not. A `<` written where `<=` was meant would pass every test
/// that only checked a midpoint.
#[test]
fn the_interval_is_closed_at_both_ends_and_nowhere_wider() {
  assert_eq!(NormCoord::new(0.0).map(NormCoord::get), Some(0.0));
  assert_eq!(NormCoord::new(1.0).map(NormCoord::get), Some(1.0));
  assert_eq!(NormCoord::new(0.5).map(NormCoord::get), Some(0.5));

  assert!(
    NormCoord::new(f32::from_bits(1)).is_some(),
    "the smallest positive subnormal is inside the interval, and nothing here rounds it away"
  );
  assert_eq!(
    NormCoord::new(-f32::from_bits(1)),
    None,
    "the smallest negative subnormal is below zero"
  );
  assert_eq!(NormCoord::new(1.000_000_1), None);
  assert_eq!(NormCoord::new(-0.000_000_1), None);
  assert_eq!(NormCoord::new(2.0), None);
  assert_eq!(NormCoord::new(-1.0), None);
}

/// **The three values that are not numbers**, refused by name.
///
/// `NaN` is the one that matters most, because it is what the total order below is bought with: a
/// `NaN` admitted here makes [`Ord::cmp`]'s `expect` reachable, which is a panic in a comparison
/// somebody is sorting with.
#[test]
fn a_value_that_is_not_a_number_is_refused() {
  assert_eq!(NormCoord::new(f32::NAN), None);
  assert_eq!(NormCoord::new(f32::INFINITY), None);
  assert_eq!(NormCoord::new(f32::NEG_INFINITY), None);
  assert_eq!(NormCoord::new(-f32::NAN), None);
}

/// **The `-0.0` NORMALISATION, and the `Hash`/`Eq` contract it is there to keep.**
///
/// The pair is the one place at this width where two values compare equal and carry different bits.
/// Admitting `-0.0` unchanged would satisfy every range check and break the hash contract silently,
/// which is a value that vanishes from a `HashSet` it was just inserted into.
#[test]
fn a_negative_zero_is_admitted_and_arrives_as_the_positive_one() {
  let signed = NormCoord::new(-0.0).expect("negative zero is in the interval");

  assert_eq!(signed, NormCoord::ZERO);
  assert!(signed.get().is_sign_positive());
  assert_eq!(signed.get().to_bits(), 0.0_f32.to_bits());
}

/// The contract the normalisation exists FOR, checked rather than assumed.
#[test]
fn two_equal_coordinates_hash_alike() {
  fn hashed(coord: NormCoord) -> u64 {
    let mut hasher = std::collections::hash_map::DefaultHasher::new();
    coord.hash(&mut hasher);
    hasher.finish()
  }

  let signed = NormCoord::new(-0.0).expect("negative zero is in the interval");

  assert_eq!(signed, NormCoord::ZERO);
  assert_eq!(hashed(signed), hashed(NormCoord::ZERO));
}

/// **The order is TOTAL**, which is what the refusal above buys and what a scalar filter's four
/// bound operators are well-defined over.
#[test]
fn admitted_coordinates_are_totally_ordered() {
  let mut frame = [
    NormCoord::new(0.75).unwrap(),
    NormCoord::ZERO,
    NormCoord::ONE,
    NormCoord::new(0.25).unwrap(),
  ];
  frame.sort();

  assert_eq!(
    frame.map(NormCoord::get),
    [0.0, 0.25, 0.75, 1.0],
    "sorting a coordinate never reaches `cmp`'s panic arm"
  );
}

/// **THE CURSOR ROUND TRIP, and the forged-cursor refusal beside it.**
///
/// A cursor is a string a CLIENT hands back, so the read has to refuse what the constructor would:
/// a forged one carrying `2.0` must not become a coordinate the type says cannot exist.
#[test]
fn a_coordinate_round_trips_through_a_cursor_and_a_forged_one_is_refused() {
  let mut bytes = Vec::new();
  let written = NormCoord::new(0.625).unwrap();
  crate::CursorValue::write_cursor(&written, &mut bytes);

  assert_eq!(
    <NormCoord as crate::CursorValue>::read_cursor(&bytes),
    Some(written)
  );

  let mut forged = Vec::new();
  crate::CursorValue::write_cursor(&2.0_f32, &mut forged);

  assert_eq!(
    <NormCoord as crate::CursorValue>::read_cursor(&forged),
    None,
    "a forged cursor meets the constructor's own refusal"
  );
}

/// The two widenings, and the ONE direction each runs in. `From<f32> for NormCoord` is absent on
/// purpose — see the impl's own note — and its absence is what keeps a bare float from becoming a
/// coordinate without a caller writing the word.
#[test]
fn a_coordinate_widens_to_both_float_widths_and_neither_narrows_back() {
  let coord = NormCoord::new(0.25).unwrap();

  assert_eq!(f32::from(coord), 0.25_f32);
  assert_eq!(f64::from(coord), 0.25_f64);
}