Skip to content

Commit

Permalink
change behavior of views returned by graph accessor methods that take…
Browse files Browse the repository at this point in the history
… a graph element as input: they now throw IllegalStateException when that element is removed from the graph

RELNOTES=change behavior of views returned by graph accessor methods that take a graph element as input: they now throw IllegalStateException when that element is removed from the graph
PiperOrigin-RevId: 600480069
  • Loading branch information
java-team-github-bot authored and Google Java Core Libraries committed Jan 22, 2024
1 parent 52654b7 commit 8dca776
Show file tree
Hide file tree
Showing 28 changed files with 1,329 additions and 328 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.common.graph;

import static com.google.common.graph.TestUtil.assertNodeNotInGraphErrorMessage;
import static com.google.common.graph.TestUtil.assertNodeRemovedFromGraphErrorMessage;
import static com.google.common.graph.TestUtil.assertStronglyEquivalent;
import static com.google.common.graph.TestUtil.sanityCheckSet;
import static com.google.common.truth.Truth.assertThat;
Expand Down Expand Up @@ -234,9 +235,8 @@ public void adjacentNodes_noAdjacentNodes() {

@Test
public void adjacentNodes_nodeNotInGraph() {
IllegalArgumentException e =
assertThrows(IllegalArgumentException.class, () -> graph.adjacentNodes(NODE_NOT_IN_GRAPH));
assertNodeNotInGraphErrorMessage(e);
assertNodeNotInGraphErrorMessage(
assertThrows(IllegalArgumentException.class, () -> graph.adjacentNodes(NODE_NOT_IN_GRAPH)));
}

@Test
Expand All @@ -247,9 +247,8 @@ public void predecessors_noPredecessors() {

@Test
public void predecessors_nodeNotInGraph() {
IllegalArgumentException e =
assertThrows(IllegalArgumentException.class, () -> graph.predecessors(NODE_NOT_IN_GRAPH));
assertNodeNotInGraphErrorMessage(e);
assertNodeNotInGraphErrorMessage(
assertThrows(IllegalArgumentException.class, () -> graph.predecessors(NODE_NOT_IN_GRAPH)));
}

@Test
Expand All @@ -260,9 +259,8 @@ public void successors_noSuccessors() {

@Test
public void successors_nodeNotInGraph() {
IllegalArgumentException e =
assertThrows(IllegalArgumentException.class, () -> graph.successors(NODE_NOT_IN_GRAPH));
assertNodeNotInGraphErrorMessage(e);
assertNodeNotInGraphErrorMessage(
assertThrows(IllegalArgumentException.class, () -> graph.successors(NODE_NOT_IN_GRAPH)));
}

@Test
Expand All @@ -273,9 +271,8 @@ public void incidentEdges_noIncidentEdges() {

@Test
public void incidentEdges_nodeNotInGraph() {
IllegalArgumentException e =
assertThrows(IllegalArgumentException.class, () -> graph.incidentEdges(NODE_NOT_IN_GRAPH));
assertNodeNotInGraphErrorMessage(e);
assertNodeNotInGraphErrorMessage(
assertThrows(IllegalArgumentException.class, () -> graph.incidentEdges(NODE_NOT_IN_GRAPH)));
}

@Test
Expand All @@ -293,9 +290,8 @@ public void degree_isolatedNode() {

@Test
public void degree_nodeNotInGraph() {
IllegalArgumentException e =
assertThrows(IllegalArgumentException.class, () -> graph.degree(NODE_NOT_IN_GRAPH));
assertNodeNotInGraphErrorMessage(e);
assertNodeNotInGraphErrorMessage(
assertThrows(IllegalArgumentException.class, () -> graph.degree(NODE_NOT_IN_GRAPH)));
}

@Test
Expand All @@ -306,9 +302,8 @@ public void inDegree_isolatedNode() {

@Test
public void inDegree_nodeNotInGraph() {
IllegalArgumentException e =
assertThrows(IllegalArgumentException.class, () -> graph.inDegree(NODE_NOT_IN_GRAPH));
assertNodeNotInGraphErrorMessage(e);
assertNodeNotInGraphErrorMessage(
assertThrows(IllegalArgumentException.class, () -> graph.inDegree(NODE_NOT_IN_GRAPH)));
}

@Test
Expand All @@ -319,9 +314,8 @@ public void outDegree_isolatedNode() {

@Test
public void outDegree_nodeNotInGraph() {
IllegalArgumentException e =
assertThrows(IllegalArgumentException.class, () -> graph.outDegree(NODE_NOT_IN_GRAPH));
assertNodeNotInGraphErrorMessage(e);
assertNodeNotInGraphErrorMessage(
assertThrows(IllegalArgumentException.class, () -> graph.outDegree(NODE_NOT_IN_GRAPH)));
}

@Test
Expand Down Expand Up @@ -351,8 +345,24 @@ public void removeNode_existingNode() {
assertThat(graphAsMutableGraph.removeNode(N1)).isTrue();
assertThat(graphAsMutableGraph.removeNode(N1)).isFalse();
assertThat(graph.nodes()).containsExactly(N2, N4);

assertThat(graph.adjacentNodes(N2)).isEmpty();
assertThat(graph.predecessors(N2)).isEmpty();
assertThat(graph.successors(N2)).isEmpty();
assertThat(graph.incidentEdges(N2)).isEmpty();
assertThat(graph.adjacentNodes(N4)).isEmpty();
assertThat(graph.predecessors(N4)).isEmpty();
assertThat(graph.successors(N4)).isEmpty();
assertThat(graph.incidentEdges(N4)).isEmpty();

assertNodeNotInGraphErrorMessage(
assertThrows(IllegalArgumentException.class, () -> graph.adjacentNodes(N1)));
assertNodeNotInGraphErrorMessage(
assertThrows(IllegalArgumentException.class, () -> graph.predecessors(N1)));
assertNodeNotInGraphErrorMessage(
assertThrows(IllegalArgumentException.class, () -> graph.successors(N1)));
assertNodeNotInGraphErrorMessage(
assertThrows(IllegalArgumentException.class, () -> graph.incidentEdges(N1)));
}

@Test
Expand Down Expand Up @@ -382,19 +392,48 @@ public void removeNode_nodeNotPresent() {
}

@Test
public void removeNode_queryAfterRemoval() {
public void queryAccessorSetAfterElementRemoval() {
assume().that(graphIsMutable()).isTrue();

putEdge(N1, N2);
putEdge(N2, N1);
Set<Integer> n1AdjacentNodes = graph.adjacentNodes(N1);
Set<Integer> n2AdjacentNodes = graph.adjacentNodes(N2);
Set<Integer> n1Predecessors = graph.predecessors(N1);
Set<Integer> n2Predecessors = graph.predecessors(N2);
Set<Integer> n1Successors = graph.successors(N1);
Set<Integer> n2Successors = graph.successors(N2);
Set<EndpointPair<Integer>> n1IncidentEdges = graph.incidentEdges(N1);
Set<EndpointPair<Integer>> n2IncidentEdges = graph.incidentEdges(N2);
assertThat(graphAsMutableGraph.removeNode(N1)).isTrue();
assertThat(n1AdjacentNodes).isEmpty();

// The choice of the size() method to call here is arbitrary. We assume that if any of the Set
// methods executes the validation check, they all will, and thus we only need to test one of
// them to ensure that the validation check happens and has the expected behavior.
assertNodeRemovedFromGraphErrorMessage(
assertThrows(IllegalStateException.class, n1AdjacentNodes::size));
assertNodeRemovedFromGraphErrorMessage(
assertThrows(IllegalStateException.class, n1Predecessors::size));
assertNodeRemovedFromGraphErrorMessage(
assertThrows(IllegalStateException.class, n1Successors::size));
assertNodeRemovedFromGraphErrorMessage(
assertThrows(IllegalStateException.class, n1IncidentEdges::size));

assertThat(n2AdjacentNodes).isEmpty();
IllegalArgumentException e =
assertThrows(IllegalArgumentException.class, () -> graph.adjacentNodes(N1));
assertNodeNotInGraphErrorMessage(e);
assertThat(n2Predecessors).isEmpty();
assertThat(n2Successors).isEmpty();
assertThat(n2IncidentEdges).isEmpty();
}

@Test
public void queryGraphAfterElementRemoval() {
assume().that(graphIsMutable()).isTrue();

putEdge(N1, N2);
putEdge(N2, N1);
assertThat(graphAsMutableGraph.removeNode(N1)).isTrue();
assertNodeNotInGraphErrorMessage(
assertThrows(IllegalArgumentException.class, () -> graph.adjacentNodes(N1)));
}

@Test
Expand Down
Loading

0 comments on commit 8dca776

Please sign in to comment.