if (!graphCopy.edges().isEmpty())
@Test public void removeNode_antiparallelEdges() { putEdge(N1, N2); putEdge(N2, N1); assertThat(graph.removeNode(N1)).isTrue(); assertThat(graph.nodes()).containsExactly(N2); assertThat(graph.edges()).isEmpty(); assertThat(graph.removeNode(N2)).isTrue(); assertThat(graph.nodes()).isEmpty(); assertThat(graph.edges()).isEmpty(); }
public void removeEdge_antiparallelEdges() { putEdge(N1, N2); putEdge(N2, N1); assertThat(graph.removeEdge(N1, N2)).isTrue(); assertThat(graph.successors(N1)).isEmpty(); assertThat(graph.predecessors(N1)).containsExactly(N2); assertThat(graph.edges()).hasSize(1); assertThat(graph.removeEdge(N2, N1)).isTrue(); assertThat(graph.successors(N1)).isEmpty(); assertThat(graph.predecessors(N1)).isEmpty(); assertThat(graph.edges()).isEmpty(); }
@Test public void removeEdge_antiparallelEdges() { putEdge(N1, N2); putEdge(N2, N1); // no-op assertThat(graph.removeEdge(N1, N2)).isTrue(); assertThat(graph.adjacentNodes(N1)).isEmpty(); assertThat(graph.edges()).isEmpty(); assertThat(graph.removeEdge(N2, N1)).isFalse(); } }
assertThat(graph.edges()).isEmpty(); AbstractGraphTest.validateGraph(graph); while (graph.edges().size() < NUM_EDGES) { graph.putEdge(getRandomElement(nodeList, gen), getRandomElement(nodeList, gen)); ArrayList<EndpointPair<Integer>> edgeList = new ArrayList<>(graph.edges()); assertThat(graph.edges()).hasSize(NUM_EDGES); AbstractGraphTest.validateGraph(graph); assertThat(graph.edges()).hasSize(NUM_EDGES - numEdgesToRemove); AbstractGraphTest.validateGraph(graph); assertThat(graph.edges()).isEmpty(); // no edges can remain if there's no nodes AbstractGraphTest.validateGraph(graph); assertThat(graph.edges()).hasSize(NUM_EDGES); AbstractGraphTest.validateGraph(graph);
@Test public void endpointPair_undirected_contains() { MutableGraph<Integer> undirectedGraph = GraphBuilder.undirected().allowsSelfLoops(true).build(); undirectedGraph.putEdge(N1, N1); undirectedGraph.putEdge(N1, N2); Set<EndpointPair<Integer>> edges = undirectedGraph.edges(); assertThat(edges).hasSize(2); assertThat(edges).contains(EndpointPair.unordered(N1, N1)); assertThat(edges).contains(EndpointPair.unordered(N1, N2)); assertThat(edges).contains(EndpointPair.unordered(N2, N1)); // equal to unordered(N1, N2) // ordered endpoints OK for undirected graph (because ordering is irrelevant) assertThat(edges).contains(EndpointPair.ordered(N1, N2)); assertThat(edges).doesNotContain(EndpointPair.unordered(N2, N2)); // edge not present assertThat(edges).doesNotContain(EndpointPair.unordered(N3, N4)); // nodes not in graph }
@Test public void endpointPair_directed_contains() { MutableGraph<Integer> directedGraph = GraphBuilder.directed().allowsSelfLoops(true).build(); directedGraph.putEdge(N1, N1); directedGraph.putEdge(N1, N2); Set<EndpointPair<Integer>> edges = directedGraph.edges(); assertThat(edges).hasSize(2); assertThat(edges).contains(EndpointPair.ordered(N1, N1)); assertThat(edges).contains(EndpointPair.ordered(N1, N2)); // unordered endpoints not OK for directed graph (undefined behavior) assertThat(edges).doesNotContain(EndpointPair.unordered(N1, N2)); assertThat(edges).doesNotContain(EndpointPair.ordered(N2, N1)); // wrong order assertThat(edges).doesNotContain(EndpointPair.ordered(N2, N2)); // edge not present assertThat(edges).doesNotContain(EndpointPair.ordered(N3, N4)); // nodes not in graph }
@Test public void endpointPair_unmodifiableView() { MutableGraph<Integer> directedGraph = GraphBuilder.directed().build(); Set<EndpointPair<Integer>> edges = directedGraph.edges(); directedGraph.putEdge(N1, N2); containsExactlySanityCheck(edges, EndpointPair.ordered(N1, N2)); directedGraph.putEdge(N2, N1); containsExactlySanityCheck(edges, EndpointPair.ordered(N1, N2), EndpointPair.ordered(N2, N1)); directedGraph.removeEdge(N1, N2); directedGraph.removeEdge(N2, N1); containsExactlySanityCheck(edges); try { edges.add(EndpointPair.ordered(N1, N2)); fail("Set returned by edges() should be unmodifiable"); } catch (UnsupportedOperationException expected) { } }
@Test public void endpointPair_undirectedGraph() { MutableGraph<Integer> undirectedGraph = GraphBuilder.undirected().allowsSelfLoops(true).build(); undirectedGraph.addNode(N0); undirectedGraph.putEdge(N1, N2); undirectedGraph.putEdge(N2, N1); // does nothing undirectedGraph.putEdge(N1, N3); undirectedGraph.putEdge(N4, N4); containsExactlySanityCheck( undirectedGraph.edges(), EndpointPair.unordered(N1, N2), EndpointPair.unordered(N1, N3), EndpointPair.unordered(N4, N4)); }
@Test public void endpointPair_directedGraph() { MutableGraph<Integer> directedGraph = GraphBuilder.directed().allowsSelfLoops(true).build(); directedGraph.addNode(N0); directedGraph.putEdge(N1, N2); directedGraph.putEdge(N2, N1); directedGraph.putEdge(N1, N3); directedGraph.putEdge(N4, N4); containsExactlySanityCheck( directedGraph.edges(), EndpointPair.ordered(N1, N2), EndpointPair.ordered(N2, N1), EndpointPair.ordered(N1, N3), EndpointPair.ordered(N4, N4)); }
@Override public Set<EndpointPair<N>> edges() { return delegate.edges(); }
@SuppressFBWarnings("RV_RETURN_VALUE_IGNORED") public static <T> MutableGraph<T> clone(final MutableGraph<T> graph) { MutableGraph<T> clone = GraphBuilder.from(graph).build(); for (T token : graph.nodes()) { clone.addNode(token); } for (EndpointPair<T> ep : graph.edges()) { clone.putEdge(ep.source(), ep.target()); } return clone; }
@SuppressFBWarnings("RV_RETURN_VALUE_IGNORED") public static <T> MutableGraph<T> clone(final MutableGraph<T> graph) { MutableGraph<T> clone = GraphBuilder.from(graph).build(); for (T token : graph.nodes()) { clone.addNode(token); } for (EndpointPair<T> ep : graph.edges()) { clone.putEdge(ep.source(), ep.target()); } return clone; }
@Test public void removeNode_antiparallelEdges() { putEdge(N1, N2); putEdge(N2, N1); assertThat(graph.removeNode(N1)).isTrue(); assertThat(graph.nodes()).containsExactly(N2); assertThat(graph.edges()).isEmpty(); assertThat(graph.removeNode(N2)).isTrue(); assertThat(graph.nodes()).isEmpty(); assertThat(graph.edges()).isEmpty(); }
public void removeEdge_antiparallelEdges() { putEdge(N1, N2); putEdge(N2, N1); assertThat(graph.removeEdge(N1, N2)).isTrue(); assertThat(graph.successors(N1)).isEmpty(); assertThat(graph.predecessors(N1)).containsExactly(N2); assertThat(graph.edges()).hasSize(1); assertThat(graph.removeEdge(N2, N1)).isTrue(); assertThat(graph.successors(N1)).isEmpty(); assertThat(graph.predecessors(N1)).isEmpty(); assertThat(graph.edges()).isEmpty(); } }
@Test public void removeEdge_antiparallelEdges() { putEdge(N1, N2); putEdge(N2, N1); // no-op assertThat(graph.removeEdge(N1, N2)).isTrue(); assertThat(graph.adjacentNodes(N1)).isEmpty(); assertThat(graph.edges()).isEmpty(); assertThat(graph.removeEdge(N2, N1)).isFalse(); } }
@Test public void endpointPair_containment() { MutableGraph<Integer> undirectedGraph = GraphBuilder.undirected().allowsSelfLoops(true).build(); undirectedGraph.putEdge(N1, N1); undirectedGraph.putEdge(N1, N2); Set<EndpointPair<Integer>> edges = undirectedGraph.edges(); assertThat(edges).hasSize(2); assertThat(edges).contains(EndpointPair.unordered(N1, N1)); assertThat(edges).contains(EndpointPair.unordered(N1, N2)); assertThat(edges).contains(EndpointPair.unordered(N2, N1)); // equal to unordered(N1, N2) assertThat(edges).doesNotContain(EndpointPair.unordered(N2, N2)); assertThat(edges).doesNotContain(EndpointPair.ordered(N1, N2)); // graph not directed assertThat(edges).doesNotContain(EndpointPair.unordered(N3, N4)); // nodes not in graph }
@Test public void endpointPair_unmodifiableView() { MutableGraph<Integer> directedGraph = GraphBuilder.directed().build(); Set<EndpointPair<Integer>> edges = directedGraph.edges(); directedGraph.putEdge(N1, N2); containsExactlySanityCheck(edges, EndpointPair.ordered(N1, N2)); directedGraph.putEdge(N2, N1); containsExactlySanityCheck(edges, EndpointPair.ordered(N1, N2), EndpointPair.ordered(N2, N1)); directedGraph.removeEdge(N1, N2); directedGraph.removeEdge(N2, N1); containsExactlySanityCheck(edges); try { edges.add(EndpointPair.ordered(N1, N2)); fail("Set returned by edges() should be unmodifiable"); } catch (UnsupportedOperationException expected) { } }
@Test public void endpointPair_undirectedGraph() { MutableGraph<Integer> undirectedGraph = GraphBuilder.undirected().allowsSelfLoops(true).build(); undirectedGraph.addNode(N0); undirectedGraph.putEdge(N1, N2); undirectedGraph.putEdge(N2, N1); // does nothing undirectedGraph.putEdge(N1, N3); undirectedGraph.putEdge(N4, N4); containsExactlySanityCheck( undirectedGraph.edges(), EndpointPair.unordered(N1, N2), EndpointPair.unordered(N1, N3), EndpointPair.unordered(N4, N4)); }
@Test public void endpointPair_directedGraph() { MutableGraph<Integer> directedGraph = GraphBuilder.directed().allowsSelfLoops(true).build(); directedGraph.addNode(N0); directedGraph.putEdge(N1, N2); directedGraph.putEdge(N2, N1); directedGraph.putEdge(N1, N3); directedGraph.putEdge(N4, N4); containsExactlySanityCheck( directedGraph.edges(), EndpointPair.ordered(N1, N2), EndpointPair.ordered(N2, N1), EndpointPair.ordered(N1, N3), EndpointPair.ordered(N4, N4)); }