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 super::{Count, NegativeCount};

/// The plain identity every reader assumes: what goes in comes out, at both spellings of the way
/// out.
#[test]
fn a_count_is_the_magnitude_it_was_minted_from() {
  assert_eq!(Count::new(0).get(), 0);
  assert_eq!(Count::new(3).get(), 3);
  assert_eq!(u64::from(Count::new(u64::MAX)), u64::MAX);
}

/// **THE STORED FORM IS THE SIGNED ONE, AND THE SATURATION IS THE ARM NO COUNT REACHES.**
///
/// The three magnitudes that matter: an ordinary count, the largest value the signed form holds
/// exactly, and the first one past it. The third is the arm the type's own note calls unreachable —
/// a group of more than 9,223,372,036,854,775,807 rows is not a thing a store holds or a slice
/// counts — and it is pinned here rather than left as prose, because *unreachable* is a claim about
/// callers and this is a claim about the FUNCTION.
#[test]
fn the_stored_form_saturates_rather_than_truncating() {
  assert_eq!(Count::new(0).to_signed(), 0);
  assert_eq!(Count::new(7).to_signed(), 7);

  let ceiling = i64::MAX.unsigned_abs();
  assert_eq!(Count::new(ceiling).to_signed(), i64::MAX);
  assert_eq!(Count::new(ceiling + 1).to_signed(), i64::MAX);
  assert_eq!(Count::new(u64::MAX).to_signed(), i64::MAX);
}

/// **THE PAIR IS A ROUND TRIP** and not a one-way bind helper, which is what makes the encoding one
/// authority rather than two spellings that agree today — `BeU64`'s own argument, at the type that
/// borrowed its shape.
#[test]
fn the_two_directions_round_trip_below_the_ceiling() {
  for magnitude in [0_u64, 1, 42, 5_000_000_000, i64::MAX.unsigned_abs()] {
    let count = Count::new(magnitude);

    assert_eq!(
      Count::from_signed(count.to_signed()),
      Ok(count),
      "the signed form reads back as the count that wrote it"
    );
  }
}

/// **A NEGATIVE STORED VALUE IS A CORRUPT ROW, AND IT IS REFUSED BY NAME.**
///
/// Nothing that wrote through [`Count::to_signed`] can produce one — the narrowing has no negative
/// arm at all, which the test above pins — so this is a row a hand edit, a shared column or a bit
/// flip left behind. The refusal carries the value, which is the evidence a reader looking at such a
/// row wants; a clamp to zero would answer a question nobody asked and leave no trace.
#[test]
fn a_negative_column_is_refused_by_name_rather_than_clamped() {
  assert_eq!(Count::from_signed(-1), Err(NegativeCount { stored: -1 }));
  assert_eq!(
    Count::from_signed(i64::MIN),
    Err(NegativeCount { stored: i64::MIN })
  );

  let refused = Count::from_signed(-7).expect_err("a negative column is not a cardinality");
  assert_eq!(refused.stored(), -7);
  assert!(
    refused.to_string().contains("-7"),
    "the sentence names the value it refused: {refused}"
  );
}

/// The order is the magnitude's, so a `Count` column sorts the way the number does — which is what
/// every dialect's own ordering over the stored form has to agree with.
#[test]
fn counts_order_by_magnitude() {
  let mut counts = [
    Count::new(9),
    Count::new(0),
    Count::new(u64::MAX),
    Count::new(1),
  ];
  counts.sort_unstable();

  assert_eq!(
    counts,
    [
      Count::new(0),
      Count::new(1),
      Count::new(9),
      Count::new(u64::MAX)
    ]
  );
}