@Test public void removeEdge_edgeNotPresent() { addEdge(E12, N1, N2); ImmutableSet<String> edges = ImmutableSet.copyOf(network.edges()); assertFalse(network.removeEdge(EDGE_NOT_IN_GRAPH)); assertThat(network.edges()).containsExactlyElementsIn(edges); }
@Test public void addNode_existingNode() { addNode(N1); ImmutableSet<Integer> nodes = ImmutableSet.copyOf(network.nodes()); assertFalse(addNode(N1)); assertThat(network.nodes()).containsExactlyElementsIn(nodes); }
@Test public void removeNode_nodeNotPresent() { addNode(N1); ImmutableSet<Integer> nodes = ImmutableSet.copyOf(network.nodes()); assertFalse(network.removeNode(NODE_NOT_IN_GRAPH)); assertThat(network.nodes()).containsExactlyElementsIn(nodes); }
@Test public void edges_noEdges() { assertThat(network.edges()).isEmpty(); // Network with no edges, given disconnected nodes addNode(N1); addNode(N2); assertThat(network.edges()).isEmpty(); }
@Test public void removeNode_existingNode() { addEdge(E12, N1, N2); addEdge(E41, N4, N1); assertTrue(network.removeNode(N1)); assertThat(network.nodes()).containsExactly(N2, N4); assertThat(network.edges()).doesNotContain(E12); assertThat(network.edges()).doesNotContain(E41); }
@Test public void removeEdge_oneOfMany() { addEdge(E12, N1, N2); addEdge(E13, N1, N3); addEdge(E14, N1, N4); assertThat(network.edges()).containsExactly(E12, E13, E14); assertTrue(network.removeEdge(E13)); assertThat(network.edges()).containsExactly(E12, E14); }
@Test public void removeEdge_queryAfterRemoval() { addEdge(E12, N1, N2); Endpoints<Integer> unused = network.incidentNodes(E12); // ensure cache (if any) is populated assertTrue(network.removeEdge(E12)); try { network.incidentNodes(E12); fail(ERROR_EDGE_NOT_IN_GRAPH); } catch (IllegalArgumentException e) { assertEdgeNotInGraphErrorMessage(e); } }
@Test public void removeNode_queryAfterRemoval() { addNode(N1); Set<Integer> unused = network.adjacentNodes(N1); // ensure cache (if any) is populated assertTrue(network.removeNode(N1)); try { network.adjacentNodes(N1); fail(ERROR_NODE_NOT_IN_GRAPH); } catch (IllegalArgumentException e) { assertNodeNotInGraphErrorMessage(e); } }
@Test public void adjacentEdges_addEdges() { addEdge(E12, N1, N2); addEdge(E13, N1, N3); addEdge(E23, N2, N3); assertThat(network.adjacentEdges(E12)).containsExactly(E13, E23); }
@Test public void successors_nodeNotInGraph() { try { network.successors(NODE_NOT_IN_GRAPH); fail(ERROR_NODE_NOT_IN_GRAPH); } catch (IllegalArgumentException e) { assertNodeNotInGraphErrorMessage(e); } }
@Test public void adjacentEdges_nodeNotInGraph() { try { network.adjacentEdges(EDGE_NOT_IN_GRAPH); fail(ERROR_EDGE_NOT_IN_GRAPH); } catch (IllegalArgumentException e) { assertEdgeNotInGraphErrorMessage(e); } }
@Test public void edgesConnecting_nodesNotInGraph() { addNode(N1); addNode(N2); try { network.edgesConnecting(N1, NODE_NOT_IN_GRAPH); fail(ERROR_NODE_NOT_IN_GRAPH); } catch (IllegalArgumentException e) { assertNodeNotInGraphErrorMessage(e); } try { network.edgesConnecting(NODE_NOT_IN_GRAPH, N2); fail(ERROR_NODE_NOT_IN_GRAPH); } catch (IllegalArgumentException e) { assertNodeNotInGraphErrorMessage(e); } try { network.edgesConnecting(NODE_NOT_IN_GRAPH, NODE_NOT_IN_GRAPH); fail(ERROR_NODE_NOT_IN_GRAPH); } catch (IllegalArgumentException e) { assertNodeNotInGraphErrorMessage(e); } }
@Test public void nodes_noNodes() { assertThat(network.nodes()).isEmpty(); }
@Test public void addNode_newNode() { assertTrue(addNode(N1)); assertThat(network.nodes()).contains(N1); }
@Test public void successors_noSuccessors() { addNode(N1); assertThat(network.successors(N1)).isEmpty(); }
@Test public void predecessors_noPredecessors() { addNode(N1); assertThat(network.predecessors(N1)).isEmpty(); }
@Test public void incidentNodes_oneEdge() { addEdge(E12, N1, N2); assertThat(network.incidentNodes(E12)).containsExactly(N1, N2); }
/** * A proxy method that adds the edge {@code e} to the graph being tested. In case of Immutable * graph implementations, this method should add {@code e} to the graph builder and build a new * graph with the current builder state. * * <p>This method should be used in tests of specific implementations if you want to ensure * uniform behavior (including side effects) with how edges are added elsewhere in the tests. For * example, the existing implementations of this method explicitly add the supplied nodes to the * graph, and then call {@code graph.addEdge()} to connect the edge to the nodes; this is not part * of the contract of {@code graph.addEdge()} and is done for convenience. In cases where you want * to avoid such side effects (e.g., if you're testing what happens in your implementation if you * add an edge whose end-points don't already exist in the graph), you should <b>not</b> use this * method. * * @return {@code true} iff the graph was modified as a result of this call TODO(user): Consider * changing access modifier to be protected. */ @CanIgnoreReturnValue boolean addEdge(String e, Integer n1, Integer n2) { network.addNode(n1); network.addNode(n2); return network.addEdge(e, n1, n2); }
@Test public void edges_oneEdge() { addEdge(E12, N1, N2); assertThat(network.edges()).containsExactly(E12); }
@Test public void adjacentEdges_noAdjacentEdges() { addEdge(E12, N1, N2); assertThat(network.adjacentEdges(E12)).isEmpty(); }
@Test public void adjacentNodes_noAdjacentNodes() { addNode(N1); assertThat(network.adjacentNodes(N1)).isEmpty(); }
@Test public void adjacentNodes_oneEdge() { addEdge(E12, N1, N2); assertThat(network.adjacentNodes(N1)).containsExactly(N2); assertThat(network.adjacentNodes(N2)).containsExactly(N1); }
@Test public void nodes_oneNode() { addNode(N1); assertThat(network.nodes()).containsExactly(N1); }
@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(); } } }
@Test public void inEdges_noInEdges() { addNode(N1); assertThat(network.inEdges(N1)).isEmpty(); }
@Test public void outEdges_noOutEdges() { addNode(N1); assertThat(network.outEdges(N1)).isEmpty(); }
@Test public void incidentEdges_isolatedNode() { addNode(N1); assertThat(network.incidentEdges(N1)).isEmpty(); }
@Test public void edgesConnecting_disconnectedNodes() { addNode(N1); addNode(N2); assertThat(network.edgesConnecting(N1, N2)).isEmpty(); }
/** * A proxy method that adds the node {@code n} to the graph being tested. In case of Immutable * graph implementations, this method should add {@code n} to the graph builder and build a new * graph with the current builder state. * * @return {@code true} iff the graph was modified as a result of this call TODO(user): Consider * changing access modifier to be protected. */ @CanIgnoreReturnValue boolean addNode(Integer n) { return network.addNode(n); }