示例#1
0
 @Test
 public void incidentEdges_nodeNotInGraph() {
   try {
     network.incidentEdges(NODE_NOT_IN_GRAPH);
     fail(ERROR_NODE_NOT_IN_GRAPH);
   } catch (IllegalArgumentException e) {
     assertNodeNotInGraphErrorMessage(e);
   }
 }
示例#2
0
 @Test
 public void incidentEdges_oneEdge() {
   addEdge(E12, N1, N2);
   assertThat(network.incidentEdges(N2)).containsExactly(E12);
   assertThat(network.incidentEdges(N1)).containsExactly(E12);
 }
示例#3
0
 @Test
 public void incidentEdges_isolatedNode() {
   addNode(N1);
   assertThat(network.incidentEdges(N1)).isEmpty();
 }
示例#4
0
  @After
  public void validateGraphState() {
    new EqualsTester()
        .addEqualityGroup(network, Graphs.copyOf(network), ImmutableNetwork.copyOf(network))
        .testEquals();

    String networkString = network.toString();
    assertThat(networkString).contains("isDirected: " + network.isDirected());
    assertThat(networkString).contains("allowsParallelEdges: " + network.allowsParallelEdges());
    assertThat(networkString).contains("allowsSelfLoops: " + network.allowsSelfLoops());

    int nodeStart = networkString.indexOf("nodes:");
    int edgeStart = networkString.indexOf("edges:");
    String nodeString = networkString.substring(nodeStart, edgeStart);
    String edgeString = networkString.substring(edgeStart);

    Graph<Integer> asGraph = network.asGraph();
    assertThat(network.nodes()).isEqualTo(asGraph.nodes());
    assertThat(network.nodeOrder()).isEqualTo(asGraph.nodeOrder());
    assertThat(network.isDirected()).isEqualTo(asGraph.isDirected());
    assertThat(network.allowsSelfLoops()).isEqualTo(asGraph.allowsSelfLoops());

    for (String edge : network.edges()) {
      // TODO(b/27817069): Consider verifying the edge's incident nodes in the string.
      assertThat(edgeString).contains(edge);

      Iterator<Integer> endpointsIterator = network.incidentNodes(edge).iterator();
      Integer nodeA = endpointsIterator.next();
      Integer nodeB = endpointsIterator.next();
      assertThat(network.edgesConnecting(nodeA, nodeB)).contains(edge);
      assertThat(network.successors(nodeA)).contains(nodeB);
      assertThat(network.adjacentNodes(nodeA)).contains(nodeB);
      assertThat(network.outEdges(nodeA)).contains(edge);
      assertThat(network.incidentEdges(nodeA)).contains(edge);
      assertThat(network.predecessors(nodeB)).contains(nodeA);
      assertThat(network.adjacentNodes(nodeB)).contains(nodeA);
      assertThat(network.inEdges(nodeB)).contains(edge);
      assertThat(network.incidentEdges(nodeB)).contains(edge);

      for (Integer incidentNode : network.incidentNodes(edge)) {
        assertThat(network.nodes()).contains(incidentNode);
        for (String adjacentEdge : network.incidentEdges(incidentNode)) {
          assertTrue(
              edge.equals(adjacentEdge) || network.adjacentEdges(edge).contains(adjacentEdge));
        }
      }
    }

    for (Integer node : network.nodes()) {
      assertThat(nodeString).contains(node.toString());

      assertThat(network.adjacentNodes(node)).isEqualTo(asGraph.adjacentNodes(node));
      assertThat(network.predecessors(node)).isEqualTo(asGraph.predecessors(node));
      assertThat(network.successors(node)).isEqualTo(asGraph.successors(node));

      for (Integer otherNode : network.nodes()) {
        Set<String> edgesConnecting = network.edgesConnecting(node, otherNode);
        boolean isSelfLoop = node.equals(otherNode);
        if (network.isDirected() || !isSelfLoop) {
          assertThat(edgesConnecting)
              .isEqualTo(Sets.intersection(network.outEdges(node), network.inEdges(otherNode)));
        }
        if (!network.allowsParallelEdges()) {
          assertThat(edgesConnecting.size()).isAtMost(1);
        }
        if (!network.allowsSelfLoops() && isSelfLoop) {
          assertThat(edgesConnecting).isEmpty();
        }
        for (String edge : edgesConnecting) {
          assertThat(network.incidentNodes(edge)).isEqualTo(Endpoints.of(network, node, otherNode));
        }
      }

      for (String incidentEdge : network.incidentEdges(node)) {
        assertTrue(
            network.inEdges(node).contains(incidentEdge)
                || network.outEdges(node).contains(incidentEdge));
        assertThat(network.edges()).contains(incidentEdge);
        assertThat(network.incidentNodes(incidentEdge)).contains(node);
      }

      for (String inEdge : network.inEdges(node)) {
        assertThat(network.incidentEdges(node)).contains(inEdge);
        assertThat(network.outEdges(network.incidentNodes(inEdge).adjacentNode(node)))
            .contains(inEdge);
      }

      for (String outEdge : network.outEdges(node)) {
        assertThat(network.incidentEdges(node)).contains(outEdge);
        assertThat(network.inEdges(network.incidentNodes(outEdge).adjacentNode(node)))
            .contains(outEdge);
      }

      for (Integer adjacentNode : network.adjacentNodes(node)) {
        assertTrue(
            network.predecessors(node).contains(adjacentNode)
                || network.successors(node).contains(adjacentNode));
        assertTrue(
            !network.edgesConnecting(node, adjacentNode).isEmpty()
                || !network.edgesConnecting(adjacentNode, node).isEmpty());
      }

      for (Integer predecessor : network.predecessors(node)) {
        assertThat(network.successors(predecessor)).contains(node);
        assertThat(network.edgesConnecting(predecessor, node)).isNotEmpty();
      }

      for (Integer successor : network.successors(node)) {
        assertThat(network.predecessors(successor)).contains(node);
        assertThat(network.edgesConnecting(node, successor)).isNotEmpty();
      }
    }
  }