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::*;

/// Euclid over the domain both constructors guarantee, including the `a = 0` case that makes the
/// reduction total.
#[test]
fn the_divisor_is_positive_for_every_pair_a_constructor_admits() {
  for (a, b, expected) in [
    (0, 1, 1),
    (0, 90_000, 90_000),
    (1, 1, 1),
    (2, 4, 2),
    (60_000, 2002, 2),
    (i32::MAX, i32::MAX, i32::MAX),
    (1 << 30, 2, 2),
  ] {
    assert_eq!(gcd(a, b), expected, "gcd({a}, {b})");
    assert!(gcd(a, b) > 0, "the quotient below must be total");
  }
}

/// The three affixes differ only in where the wildcard sits, and nothing else is added to the
/// operand.
#[cfg(feature = "sqlx")]
#[test]
fn each_affix_wraps_the_operand_at_its_own_end() {
  let of = |operand, affix| {
    pattern(Some(operand), affix)
      .expect("the alphabet is digits and a slash")
      .expect("an operand was given")
  };

  assert_eq!(of("1/", Affix::Prefix).as_str(), "1/%");
  assert_eq!(of("90000", Affix::Suffix).as_str(), "%90000");
  assert_eq!(of("/9", Affix::Contains).as_str(), "%/9%");
  assert_eq!(of("", Affix::Contains).as_str(), "%%");
}

/// An absent operand is an absent pattern — not an empty one, which would match everything.
#[cfg(feature = "sqlx")]
#[test]
fn an_absent_operand_builds_no_pattern() {
  assert_eq!(pattern(None, Affix::Contains), Ok(None));
}

/// **The alphabet gate is what removes the `ESCAPE` clause**, and it names the character that broke
/// it so the caller can say which of its own operands was at fault.
#[cfg(feature = "sqlx")]
#[test]
fn a_character_the_rendering_cannot_hold_is_refused_by_name() {
  for outside in ['%', '_', '\\', 'e', ' ', '-', '.'] {
    let operand = format!("1{outside}");

    for affix in [Affix::Prefix, Affix::Suffix, Affix::Contains] {
      assert_eq!(pattern(Some(&operand), affix), Err(outside), "{operand:?}");
    }
  }

  // The FIRST offender is the one reported, so a client fixes them in the order it wrote them.
  assert_eq!(pattern(Some("%_"), Affix::Contains), Err('%'));
}

/// Every pattern a real operand produces stays inside the inline representation, which is what
/// carrying `Utf8Bytes` instead of a `String` buys.
///
/// The widest an operand over this alphabet can usefully be is a whole rendering —
/// `2147483647/2147483647`, 21 bytes — which with two wildcards is exactly the 23-byte ceiling.
#[cfg(feature = "sqlx")]
#[test]
fn a_pattern_stays_inside_the_inline_representation() {
  let widest = format!("{}/{}", i32::MAX, i32::MAX);

  for operand in ["1/", "/90000", "/9", "1001/30000", widest.as_str()] {
    let built = pattern(Some(operand), Affix::Contains)
      .expect("the alphabet holds")
      .expect("an operand was given");

    assert!(
      built.is_inline(),
      "`{built}` ({} bytes) spilled to the heap",
      built.len()
    );
  }
}