code: def _test_spanner(G, spanner, stretch, weight=None):
      """Test whether a spanner is valid.
  
      This function tests whether the given spanner is a subgraph of the
      given graph G with the same node set. It also tests for all shortest
      paths whether they adhere to the given stretch.
  
      Parameters
      ----------
      G : NetworkX graph
          The original graph for which the spanner was constructed.
  
      spanner : NetworkX graph
          The spanner to be tested.
  
      stretch : float
          The proclaimed stretch of the spanner.
  
      weight : object
          The edge attribute to use as distance.
      """
      # check node set
      assert set(G.nodes()) == set(spanner.nodes())
  
      # check edge set and weights
      for u, v in spanner.edges():
          assert G.has_edge(u, v)
          if weight:
              assert spanner[u][v][weight] == G[u][v][weight]
  
      # check connectivity and stretch
      original_length = dict(nx.shortest_path_length(G, weight=weight))
      spanner_length = dict(
          nx.shortest_path_length(spanner, weight=weight)
      )
      for u in G.nodes():
          for v in G.nodes():
              if u in original_length and v in original_length[u]:
                  assert (
                      spanner_length[u][v]
                      <= stretch * original_length[u][v]
                  )
  
file_path: networkx/networkx/algorithms/tests/test_sparsifiers.py
imports: import pytest
  import networkx
  from networkx.utils import py_random_state
label_desc: Use image node_id 1 for calling a global function with example usage: _test_spanner(G, spanner, stretch, weight) without return types