| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp.graph; |
| 18 |
|
| 19 |
import com.google.javascript.jscomp.graph.Graph; |
| 20 |
import com.google.javascript.jscomp.graph.LinkedDirectedGraph; |
| 21 |
import com.google.javascript.jscomp.graph.LinkedUndirectedGraph; |
| 22 |
import com.google.javascript.jscomp.graph.Annotatable; |
| 23 |
import com.google.javascript.jscomp.graph.Annotation; |
| 24 |
import com.google.javascript.jscomp.graph.GraphNode; |
| 25 |
import com.google.javascript.jscomp.graph.SubGraph; |
| 26 |
import com.google.javascript.jscomp.graph.DiGraph; |
| 27 |
import com.google.javascript.jscomp.graph.Graph.GraphEdge; |
| 28 |
import com.google.javascript.jscomp.graph.UndiGraph; |
| 29 |
|
| 30 |
import junit.framework.TestCase; |
| 31 |
|
| 32 |
import java.util.HashSet; |
| 33 |
import java.util.List; |
| 34 |
import java.util.Set; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public class GraphTest extends TestCase { |
| 41 |
|
| 42 |
public void testDirectedSimple() { |
| 43 |
DiGraph<String, String> graph = |
| 44 |
LinkedDirectedGraph.create(); |
| 45 |
graph.createNode("a"); |
| 46 |
graph.createNode("b"); |
| 47 |
graph.createNode("c"); |
| 48 |
graph.connect("a", "->", "b"); |
| 49 |
assertTrue(graph.hasNode("a")); |
| 50 |
assertTrue(graph.hasNode("b")); |
| 51 |
assertTrue(graph.hasNode("c")); |
| 52 |
assertFalse(graph.hasNode("d")); |
| 53 |
assertTrue(graph.isConnected("a", "b")); |
| 54 |
assertTrue(graph.isConnected("b", "a")); |
| 55 |
assertFalse(graph.isConnected("a", "c")); |
| 56 |
assertFalse(graph.isConnected("b", "c")); |
| 57 |
assertFalse(graph.isConnected("c", "a")); |
| 58 |
assertFalse(graph.isConnected("c", "b")); |
| 59 |
assertFalse(graph.isConnected("a", "a")); |
| 60 |
assertFalse(graph.isConnected("b", "b")); |
| 61 |
assertFalse(graph.isConnected("b", "c")); |
| 62 |
assertTrue(graph.isConnectedInDirection("a", "b")); |
| 63 |
assertFalse(graph.isConnectedInDirection("b", "a")); |
| 64 |
assertFalse(graph.isConnectedInDirection("a", "c")); |
| 65 |
assertFalse(graph.isConnectedInDirection("b", "c")); |
| 66 |
assertFalse(graph.isConnectedInDirection("c", "a")); |
| 67 |
assertFalse(graph.isConnectedInDirection("c", "b")); |
| 68 |
|
| 69 |
|
| 70 |
graph.disconnect("a", "b"); |
| 71 |
assertFalse(graph.isConnected("a", "b")); |
| 72 |
assertFalse(graph.isConnected("b", "a")); |
| 73 |
|
| 74 |
|
| 75 |
graph.connect("a", "->", "b"); |
| 76 |
graph.connect("b", "->", "a"); |
| 77 |
graph.disconnect("a", "b"); |
| 78 |
assertFalse(graph.isConnected("a", "b")); |
| 79 |
assertFalse(graph.isConnected("b", "a")); |
| 80 |
|
| 81 |
|
| 82 |
graph.connect("a", "->", "b"); |
| 83 |
graph.connect("b", "->", "a"); |
| 84 |
graph.disconnectInDirection("a", "b"); |
| 85 |
assertTrue(graph.isConnected("b", "a")); |
| 86 |
assertTrue(graph.isConnected("a", "b")); |
| 87 |
assertFalse(graph.isConnectedInDirection("a", "b")); |
| 88 |
assertTrue(graph.isConnectedInDirection("b", "a")); |
| 89 |
} |
| 90 |
|
| 91 |
public void testUndirectedSimple() { |
| 92 |
UndiGraph<String, String> graph = |
| 93 |
LinkedUndirectedGraph.create(); |
| 94 |
graph.createNode("a"); |
| 95 |
graph.createNode("b"); |
| 96 |
graph.createNode("c"); |
| 97 |
graph.connect("a", "--", "b"); |
| 98 |
assertTrue(graph.hasNode("a")); |
| 99 |
assertTrue(graph.hasNode("b")); |
| 100 |
assertTrue(graph.hasNode("c")); |
| 101 |
assertFalse(graph.hasNode("d")); |
| 102 |
assertTrue(graph.isConnected("a", "b")); |
| 103 |
assertTrue(graph.isConnected("b", "a")); |
| 104 |
assertFalse(graph.isConnected("a", "c")); |
| 105 |
assertFalse(graph.isConnected("b", "c")); |
| 106 |
assertFalse(graph.isConnected("c", "a")); |
| 107 |
assertFalse(graph.isConnected("c", "b")); |
| 108 |
assertFalse(graph.isConnected("a", "a")); |
| 109 |
assertFalse(graph.isConnected("b", "b")); |
| 110 |
assertFalse(graph.isConnected("b", "c")); |
| 111 |
|
| 112 |
|
| 113 |
graph.disconnect("a", "b"); |
| 114 |
assertFalse(graph.isConnected("a", "b")); |
| 115 |
assertFalse(graph.isConnected("b", "a")); |
| 116 |
} |
| 117 |
|
| 118 |
public void testDirectedSelfLoop() { |
| 119 |
DiGraph<String, String> graph = |
| 120 |
LinkedDirectedGraph.create(); |
| 121 |
graph.createNode("a"); |
| 122 |
graph.createNode("b"); |
| 123 |
graph.connect("a", "->", "a"); |
| 124 |
assertTrue(graph.isConnected("a", "a")); |
| 125 |
assertFalse(graph.isConnected("a", "b")); |
| 126 |
assertFalse(graph.isConnected("b", "a")); |
| 127 |
assertTrue(graph.isConnectedInDirection("a", "a")); |
| 128 |
assertFalse(graph.isConnectedInDirection("a", "b")); |
| 129 |
assertFalse(graph.isConnectedInDirection("b", "a")); |
| 130 |
|
| 131 |
|
| 132 |
graph.disconnect("a", "a"); |
| 133 |
assertFalse(graph.isConnected("a", "a")); |
| 134 |
|
| 135 |
|
| 136 |
graph.connect("a", "->", "a"); |
| 137 |
graph.disconnect("a", "a"); |
| 138 |
assertFalse(graph.isConnected("a", "a")); |
| 139 |
assertFalse(graph.isConnected("a", "a")); |
| 140 |
|
| 141 |
|
| 142 |
graph.connect("a", "->", "a"); |
| 143 |
graph.disconnectInDirection("a", "a"); |
| 144 |
assertFalse(graph.isConnected("a", "a")); |
| 145 |
} |
| 146 |
|
| 147 |
public void testUndirectedSelfLoop() { |
| 148 |
UndiGraph<String, String> graph = |
| 149 |
LinkedUndirectedGraph.create(); |
| 150 |
graph.createNode("a"); |
| 151 |
graph.createNode("b"); |
| 152 |
graph.connect("a", "--", "a"); |
| 153 |
assertTrue(graph.isConnected("a", "a")); |
| 154 |
assertFalse(graph.isConnected("a", "b")); |
| 155 |
assertFalse(graph.isConnected("b", "a")); |
| 156 |
|
| 157 |
|
| 158 |
graph.disconnect("a", "a"); |
| 159 |
assertFalse(graph.isConnected("a", "a")); |
| 160 |
} |
| 161 |
|
| 162 |
public void testDirectedInAndOutEdges() { |
| 163 |
DiGraph<String, String> graph = |
| 164 |
LinkedDirectedGraph.create(); |
| 165 |
graph.createNode("a"); |
| 166 |
graph.createNode("b"); |
| 167 |
graph.createNode("c"); |
| 168 |
graph.createNode("d"); |
| 169 |
graph.connect("a", "->", "b"); |
| 170 |
graph.connect("a", "-->", "b"); |
| 171 |
graph.connect("a", "--->", "b"); |
| 172 |
graph.connect("a", "->", "c"); |
| 173 |
graph.connect("c", "->", "d"); |
| 174 |
assertSetEquals(graph.getDirectedSuccNodes("a"), "b", "c"); |
| 175 |
assertSetEquals(graph.getDirectedPredNodes("b"), "a"); |
| 176 |
assertSetEquals(graph.getDirectedPredNodes("c"), "a"); |
| 177 |
assertListCount(graph.getDirectedSuccNodes("a"), "b", 3); |
| 178 |
|
| 179 |
|
| 180 |
graph.disconnect("a", "b"); |
| 181 |
assertFalse(graph.isConnected("a", "b")); |
| 182 |
} |
| 183 |
|
| 184 |
public void testUndirectedNeighbors() { |
| 185 |
UndiGraph<String, String> graph = |
| 186 |
LinkedUndirectedGraph.create(); |
| 187 |
graph.createNode("a"); |
| 188 |
graph.createNode("b"); |
| 189 |
graph.createNode("c"); |
| 190 |
graph.createNode("d"); |
| 191 |
graph.connect("a", "-", "b"); |
| 192 |
graph.connect("a", "--", "b"); |
| 193 |
graph.connect("a", "---", "b"); |
| 194 |
graph.connect("a", "-", "c"); |
| 195 |
graph.connect("c", "-", "d"); |
| 196 |
assertSetEquals(graph.getNeighborNodes("a"), "b", "c"); |
| 197 |
assertSetEquals(graph.getNeighborNodes("b"), "a"); |
| 198 |
assertSetEquals(graph.getNeighborNodes("c"), "a", "d"); |
| 199 |
assertListCount(graph.getNeighborNodes("a"), "b", 3); |
| 200 |
|
| 201 |
|
| 202 |
graph.disconnect("a", "b"); |
| 203 |
assertFalse(graph.isConnected("a", "b")); |
| 204 |
} |
| 205 |
|
| 206 |
public void testDirectedGetFirstEdge() { |
| 207 |
DiGraph<String, String> graph = |
| 208 |
LinkedDirectedGraph.create(); |
| 209 |
graph.createNode("a"); |
| 210 |
graph.createNode("b"); |
| 211 |
graph.createNode("c"); |
| 212 |
graph.connect("a", "-", "b"); |
| 213 |
assertEquals(graph.getFirstEdge("a", "b").getValue(), "-"); |
| 214 |
assertEquals(graph.getFirstEdge("b", "a").getValue(), "-"); |
| 215 |
assertNull(graph.getFirstEdge("a", "c")); |
| 216 |
} |
| 217 |
|
| 218 |
public void testUndirectedGetFirstEdge() { |
| 219 |
UndiGraph<String, String> graph = |
| 220 |
LinkedUndirectedGraph.create(); |
| 221 |
graph.createNode("a"); |
| 222 |
graph.createNode("b"); |
| 223 |
graph.createNode("c"); |
| 224 |
graph.connect("a", "-", "b"); |
| 225 |
assertEquals(graph.getFirstEdge("a", "b").getValue(), "-"); |
| 226 |
assertEquals(graph.getFirstEdge("b", "a").getValue(), "-"); |
| 227 |
assertNull(graph.getFirstEdge("a", "c")); |
| 228 |
} |
| 229 |
|
| 230 |
public void testNodeAnnotations() { |
| 231 |
Graph<String, String> graph = LinkedUndirectedGraph.create(); |
| 232 |
GraphNode<String, String> a = graph.createNode("a"); |
| 233 |
GraphNode<String, String> b = graph.createNode("b"); |
| 234 |
checkAnnotations(graph, a, b); |
| 235 |
} |
| 236 |
|
| 237 |
public void testEdgeAnnotations() { |
| 238 |
Graph<String, String> graph = LinkedUndirectedGraph.create(); |
| 239 |
graph.createNode("1"); |
| 240 |
graph.createNode("2"); |
| 241 |
graph.createNode("3"); |
| 242 |
graph.connect("1", "a", "2"); |
| 243 |
graph.connect("2", "b", "3"); |
| 244 |
GraphEdge<String, String> a = graph.getEdges("1", "2").get(0); |
| 245 |
GraphEdge<String, String> b = graph.getEdges("2", "3").get(0); |
| 246 |
checkAnnotations(graph, a, b); |
| 247 |
} |
| 248 |
|
| 249 |
private static void checkAnnotations( |
| 250 |
Graph<String, String> graph, Annotatable a, Annotatable b) { |
| 251 |
final Annotation A = new Annotation() {}; |
| 252 |
final Annotation B = new Annotation() {}; |
| 253 |
|
| 254 |
|
| 255 |
assertNull(a.getAnnotation()); |
| 256 |
assertNull(b.getAnnotation()); |
| 257 |
|
| 258 |
|
| 259 |
a.setAnnotation(A); |
| 260 |
b.setAnnotation(B); |
| 261 |
assertSame(A, a.getAnnotation()); |
| 262 |
assertSame(B, b.getAnnotation()); |
| 263 |
|
| 264 |
|
| 265 |
graph.clearEdgeAnnotations(); |
| 266 |
graph.clearNodeAnnotations(); |
| 267 |
assertNull(a.getAnnotation()); |
| 268 |
assertNull(b.getAnnotation()); |
| 269 |
|
| 270 |
a.setAnnotation(A); |
| 271 |
b.setAnnotation(B); |
| 272 |
|
| 273 |
graph.pushEdgeAnnotations(); |
| 274 |
graph.pushNodeAnnotations(); |
| 275 |
assertNull(a.getAnnotation()); |
| 276 |
assertNull(b.getAnnotation()); |
| 277 |
a.setAnnotation(B); |
| 278 |
b.setAnnotation(B); |
| 279 |
graph.pushEdgeAnnotations(); |
| 280 |
graph.pushNodeAnnotations(); |
| 281 |
a.setAnnotation(B); |
| 282 |
b.setAnnotation(A); |
| 283 |
|
| 284 |
|
| 285 |
assertSame(B, a.getAnnotation()); |
| 286 |
assertSame(A, b.getAnnotation()); |
| 287 |
graph.popEdgeAnnotations(); |
| 288 |
graph.popNodeAnnotations(); |
| 289 |
assertSame(B, a.getAnnotation()); |
| 290 |
assertSame(B, b.getAnnotation()); |
| 291 |
graph.popEdgeAnnotations(); |
| 292 |
graph.popNodeAnnotations(); |
| 293 |
assertSame(A, a.getAnnotation()); |
| 294 |
assertSame(B, b.getAnnotation()); |
| 295 |
} |
| 296 |
|
| 297 |
public void testDegree() { |
| 298 |
testDirectedDegree(LinkedDirectedGraph.<String, String>create()); |
| 299 |
testDirectedDegree(LinkedUndirectedGraph.<String, String>create()); |
| 300 |
} |
| 301 |
|
| 302 |
public void testDirectedDegree(Graph<String, String> graph) { |
| 303 |
graph.createNode("a"); |
| 304 |
graph.createNode("b"); |
| 305 |
graph.createNode("c"); |
| 306 |
graph.createNode("d"); |
| 307 |
assertEquals(0, graph.getNodeDegree("a")); |
| 308 |
graph.connect("a", "-", "b"); |
| 309 |
assertEquals(1, graph.getNodeDegree("a")); |
| 310 |
graph.connect("b", "-", "c"); |
| 311 |
assertEquals(1, graph.getNodeDegree("a")); |
| 312 |
graph.connect("a", "-", "c"); |
| 313 |
assertEquals(2, graph.getNodeDegree("a")); |
| 314 |
graph.connect("d", "-", "a"); |
| 315 |
assertEquals(3, graph.getNodeDegree("a")); |
| 316 |
} |
| 317 |
|
| 318 |
public void testDirectedConnectIfNotFound() { |
| 319 |
testDirectedConnectIfNotFound( |
| 320 |
LinkedDirectedGraph.<String, String>create()); |
| 321 |
testDirectedConnectIfNotFound( |
| 322 |
LinkedUndirectedGraph.<String, String>create()); |
| 323 |
} |
| 324 |
|
| 325 |
public void testDirectedConnectIfNotFound(Graph<String, String> graph) { |
| 326 |
graph.createNode("a"); |
| 327 |
graph.createNode("b"); |
| 328 |
graph.connectIfNotFound("a", "-", "b"); |
| 329 |
assertEquals(1, graph.getNodeDegree("a")); |
| 330 |
graph.connectIfNotFound("a", "-", "b"); |
| 331 |
assertEquals(1, graph.getNodeDegree("a")); |
| 332 |
graph.connectIfNotFound("a", null, "b"); |
| 333 |
assertEquals(2, graph.getNodeDegree("a")); |
| 334 |
graph.connectIfNotFound("a", null, "b"); |
| 335 |
assertEquals(2, graph.getNodeDegree("a")); |
| 336 |
} |
| 337 |
|
| 338 |
public void testSimpleSubGraph() { |
| 339 |
UndiGraph<String, String> graph = |
| 340 |
LinkedUndirectedGraph.create(); |
| 341 |
graph.createNode("a"); |
| 342 |
graph.createNode("b"); |
| 343 |
graph.createNode("c"); |
| 344 |
graph.connect("a", "--", "b"); |
| 345 |
|
| 346 |
SubGraph<String, String> subGraph = graph.newSubGraph(); |
| 347 |
subGraph.addNode("a"); |
| 348 |
subGraph.addNode("b"); |
| 349 |
|
| 350 |
try { |
| 351 |
subGraph.addNode("d"); |
| 352 |
fail("SubGraph should not allow add for node that is not in graph."); |
| 353 |
} catch (IllegalArgumentException e) { |
| 354 |
|
| 355 |
} |
| 356 |
|
| 357 |
assertFalse(subGraph.isIndependentOf("a")); |
| 358 |
assertFalse(subGraph.isIndependentOf("b")); |
| 359 |
assertTrue(subGraph.isIndependentOf("c")); |
| 360 |
} |
| 361 |
|
| 362 |
private <T extends GraphNode<String, String>> void assertListCount( |
| 363 |
List<T> list, String target, int count) { |
| 364 |
for (GraphNode<String, String> node : list) { |
| 365 |
if (node.getValue().equals(target)) { |
| 366 |
count--; |
| 367 |
} |
| 368 |
} |
| 369 |
assertTrue(count == 0); |
| 370 |
} |
| 371 |
|
| 372 |
private <T extends GraphNode<String, String>> void assertSetEquals( |
| 373 |
List<T> list, String ... targets) { |
| 374 |
Set<String> set = new HashSet<String>(); |
| 375 |
for (GraphNode<String, String> node : list) { |
| 376 |
set.add(node.getValue()); |
| 377 |
} |
| 378 |
Set<String> otherSet = new HashSet<String>(); |
| 379 |
for (String target : targets) { |
| 380 |
otherSet.add(target); |
| 381 |
} |
| 382 |
assertTrue(otherSet.equals(set)); |
| 383 |
} |
| 384 |
} |