• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.common.graph;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.junit.runners.JUnit4;
24 
25 /** Tests for a directed {@link ConfigurableMutableGraph} allowing self-loops. */
26 @RunWith(JUnit4.class)
27 public class ConfigurableDirectedGraphTest extends ConfigurableSimpleDirectedGraphTest {
28 
29   @Override
createGraph()30   public MutableGraph<Integer> createGraph() {
31     return GraphBuilder.directed().allowsSelfLoops(true).build();
32   }
33 
34   @Test
adjacentNodes_selfLoop()35   public void adjacentNodes_selfLoop() {
36     putEdge(N1, N1);
37     putEdge(N1, N2);
38     assertThat(graph.adjacentNodes(N1)).containsExactly(N1, N2);
39   }
40 
41   @Test
predecessors_selfLoop()42   public void predecessors_selfLoop() {
43     putEdge(N1, N1);
44     assertThat(graph.predecessors(N1)).containsExactly(N1);
45     putEdge(N4, N1);
46     assertThat(graph.predecessors(N1)).containsExactly(N1, N4);
47   }
48 
49   @Test
successors_selfLoop()50   public void successors_selfLoop() {
51     putEdge(N1, N1);
52     assertThat(graph.successors(N1)).containsExactly(N1);
53     putEdge(N1, N2);
54     assertThat(graph.successors(N1)).containsExactly(N1, N2);
55   }
56 
57   @Test
incidentEdges_selfLoop()58   public void incidentEdges_selfLoop() {
59     putEdge(N1, N1);
60     assertThat(graph.incidentEdges(N1)).containsExactly(EndpointPair.ordered(N1, N1));
61     putEdge(N1, N2);
62     assertThat(graph.incidentEdges(N1))
63         .containsExactly(EndpointPair.ordered(N1, N1), EndpointPair.ordered(N1, N2));
64   }
65 
66   @Test
degree_selfLoop()67   public void degree_selfLoop() {
68     putEdge(N1, N1);
69     assertThat(graph.degree(N1)).isEqualTo(2);
70     putEdge(N1, N2);
71     assertThat(graph.degree(N1)).isEqualTo(3);
72   }
73 
74   @Test
inDegree_selfLoop()75   public void inDegree_selfLoop() {
76     putEdge(N1, N1);
77     assertThat(graph.inDegree(N1)).isEqualTo(1);
78     putEdge(N4, N1);
79     assertThat(graph.inDegree(N1)).isEqualTo(2);
80   }
81 
82   @Test
outDegree_selfLoop()83   public void outDegree_selfLoop() {
84     putEdge(N1, N1);
85     assertThat(graph.outDegree(N1)).isEqualTo(1);
86     putEdge(N1, N2);
87     assertThat(graph.outDegree(N1)).isEqualTo(2);
88   }
89 
90   @Override
91   @Test
addEdge_selfLoop()92   public void addEdge_selfLoop() {
93     assertThat(putEdge(N1, N1)).isTrue();
94     assertThat(graph.successors(N1)).containsExactly(N1);
95     assertThat(graph.predecessors(N1)).containsExactly(N1);
96   }
97 
98   @Test
addEdge_existingSelfLoopEdgeBetweenSameNodes()99   public void addEdge_existingSelfLoopEdgeBetweenSameNodes() {
100     putEdge(N1, N1);
101     assertThat(putEdge(N1, N1)).isFalse();
102   }
103 
104   @Test
removeNode_existingNodeWithSelfLoopEdge()105   public void removeNode_existingNodeWithSelfLoopEdge() {
106     addNode(N1);
107     putEdge(N1, N1);
108     assertThat(graph.removeNode(N1)).isTrue();
109     assertThat(graph.nodes()).isEmpty();
110   }
111 
112   @Test
removeEdge_existingSelfLoopEdge()113   public void removeEdge_existingSelfLoopEdge() {
114     putEdge(N1, N1);
115     assertThat(graph.removeEdge(N1, N1)).isTrue();
116     assertThat(graph.nodes()).containsExactly(N1);
117     assertThat(graph.successors(N1)).isEmpty();
118   }
119 }
120