FrankenNetworkX

Guide/Evidence

Testing

3 min readFrom the README

On this page 1
Conformance Testing Methodology

The Python parity suite (pytest tests/python/) is the canonical truth. It is organized by parity flavor:

  • test_*_parity.py: direct NetworkX-vs-FrankenNetworkX comparisons on fixed inputs.
  • test_*_conformance.py: broad fixture-matrix parity sweeps.
  • test_*_metamorphic.py: algebraic invariants (e.g., shortest-path triangle inequality, max-flow/min-cut duality, degree-sum identity, König's theorem, MST cycle property).
  • test_*_hypothesis.py: property-based testing with hypothesis over randomized graphs.
  • test_*_golden.py: frozen output snapshots that lock in current behavior against regression.
  • test_thread_safety.py: concurrent dispatch under GIL release.
  • test_error_messages.py: exception class + wording parity.

Run it through the guarded runner, which caps address space, RSS and wall time (an unguarded whole-suite run once drove a shared host to 148 GB RSS), and prefer targeted files or shards over one monolithic invocation:

maturin develop --release --features pyo3/abi3-py310
./scripts/run_pytest_guarded.sh tests/python/test_error_messages.py -v
./scripts/run_pytest_guarded.sh tests/python/test_*shortest*path*.py -v   # one family

Cross-validate against NetworkX's own test suite with fnx as the backend. The script runs it from a scratch directory under an address-space limit and holds it to the ratchet; --target runs one NetworkX test module in seconds:

.venv/bin/python scripts/run_upstream_networkx_suite.py                    # full run, check the ratchet
.venv/bin/python scripts/run_upstream_networkx_suite.py --target networkx.algorithms.tests.test_dag
.venv/bin/python scripts/run_upstream_networkx_suite.py --update           # after fixes: drop what now passes, raise the floor

Do not add -W ignore to a manual run: NetworkX's own test_pajek.py::test_ignored_attribute then fails on plain NetworkX too.

Conformance Testing Methodology

The Python test files (about 1,100) implement five complementary testing strategies, each catching a different class of bug:

1. Direct parity (test_*_parity.py). Fix an input graph, call both fnx.<func>(G) and nx.<func>(G_nx), assert equality. Catches "I got the wrong answer." The most basic and most numerous family.

def test_dijkstra_path_parity():
    G_fnx = fnx.path_graph(20)
    G_nx  = nx.path_graph(20)
    assert fnx.dijkstra_path(G_fnx, 0, 19) == nx.dijkstra_path(G_nx, 0, 19)

2. Conformance matrix (test_*_conformance.py). Sweep over a fixture matrix: {path, cycle, complete, star, BA, ER, WS, bipartite} × {small, medium} × {weighted, unweighted}. Catches "I got the wrong answer on a kind of graph I didn't think to test individually." The conformance harness inside fnx-conformance writes structured logs and replay commands so a mismatch ships with a one-line reproducer.

3. Metamorphic invariants (test_*_metamorphic.py). These tests don't compare to a reference at all; they assert mathematical identities the algorithm must obey on any input. Catches "I got an answer that looks plausible but violates a structural law." Examples shipping today:

  • Shortest path triangle inequality. d(a,c) ≤ d(a,b) + d(b,c) for all triples.
  • Max-flow / min-cut duality. max_flow(s,t).value == min_cut(s,t).value on every directed graph.
  • Degree sum identity. sum(degree.values()) == 2 * G.number_of_edges() on undirected.
  • König's theorem. len(max_matching) == len(min_vertex_cover) on bipartite graphs.
  • MST cycle property. Removing any MST edge and re-adding the cheapest edge in the resulting cut yields a graph of equal or greater weight.
  • PageRank stochastic invariant. sum(pagerank.values()) ≈ 1.0 to numerical tolerance.

4. Property-based (test_*_hypothesis.py). Use hypothesis to synthesize randomized graphs over arbitrary topologies and assert the same parity/metamorphic checks. Catches "I got the right answer on every hand-crafted fixture but miss a weird shape." Hypothesis shrinks failing inputs to minimal counterexamples automatically.

5. Golden snapshots (test_*_golden.py). Freeze the current output of a sensitive function on a fixed input. Any future change to the algorithm output for that fixed input fails the test loudly. Catches "I refactored the kernel and accidentally changed observable behavior." Used for tie-break-sensitive algorithms (is_planar on K3,3 / Petersen / K5, dag_longest_path tie-break, directed-distance metrics).

The five flavors live side by side in tests/python/ and run through the guarded runner in shards; four files are part of the DSR quality run today (see Quality Gates).

Beyond Python: the fnx-conformance Rust crate runs a curated differential harness comparing fnx outputs against the legacy NetworkX oracle (the pure-Python legacy_networkx_code/ reference copy) over a hardened fixture matrix. It emits structured JSON logs into artifacts/conformance/latest/, which the dashboard generator turns into per-family parity reports that the RaptorQ tooling can encode for replay. It is run on demand, not as a gate.