| 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.common.base.Predicates; |
| 20 |
import com.google.common.collect.Lists; |
| 21 |
|
| 22 |
import junit.framework.TestCase; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public class GraphPrunerTest extends TestCase { |
| 28 |
|
| 29 |
public void testThreeNodesConnected() { |
| 30 |
DiGraph<String, String> graph = LinkedDirectedGraph.create(); |
| 31 |
graph.createNode("A"); |
| 32 |
graph.createNode("B"); |
| 33 |
graph.createNode("C"); |
| 34 |
graph.connect("A", "--", "B"); |
| 35 |
graph.connect("B", "--", "C"); |
| 36 |
|
| 37 |
DiGraph<String, String> pruned = |
| 38 |
new GraphPruner<String, String>(graph).prune( |
| 39 |
Predicates.in(Lists.newArrayList("A", "C"))); |
| 40 |
assertEquals(2, pruned.getNodes().size()); |
| 41 |
assertTrue(pruned.isConnectedInDirection("A", "C")); |
| 42 |
} |
| 43 |
|
| 44 |
public void testThreeNodesDisconnected() { |
| 45 |
DiGraph<String, String> graph = LinkedDirectedGraph.create(); |
| 46 |
graph.createNode("A"); |
| 47 |
graph.createNode("B"); |
| 48 |
graph.createNode("C"); |
| 49 |
graph.connect("A", "--", "B"); |
| 50 |
graph.connect("C", "--", "B"); |
| 51 |
|
| 52 |
DiGraph<String, String> pruned = |
| 53 |
new GraphPruner<String, String>(graph).prune( |
| 54 |
Predicates.in(Lists.newArrayList("A", "C"))); |
| 55 |
assertEquals(2, pruned.getNodes().size()); |
| 56 |
assertFalse(pruned.isConnectedInDirection("A", "C")); |
| 57 |
} |
| 58 |
|
| 59 |
public void testFourNodesConnected1() { |
| 60 |
DiGraph<String, String> graph = LinkedDirectedGraph.create(); |
| 61 |
graph.createNode("A"); |
| 62 |
graph.createNode("B"); |
| 63 |
graph.createNode("C"); |
| 64 |
graph.createNode("D"); |
| 65 |
graph.connect("A", "--", "C"); |
| 66 |
graph.connect("B", "--", "C"); |
| 67 |
graph.connect("C", "--", "D"); |
| 68 |
graph.connect("A", "--", "D"); |
| 69 |
|
| 70 |
DiGraph<String, String> pruned = |
| 71 |
new GraphPruner<String, String>(graph).prune( |
| 72 |
Predicates.not(Predicates.equalTo("C"))); |
| 73 |
assertEquals(3, pruned.getNodes().size()); |
| 74 |
assertTrue(pruned.isConnectedInDirection("A", "D")); |
| 75 |
assertTrue(pruned.isConnectedInDirection("B", "D")); |
| 76 |
assertFalse(pruned.isConnectedInDirection("A", "B")); |
| 77 |
} |
| 78 |
|
| 79 |
public void testFourNodesConnected2() { |
| 80 |
DiGraph<String, String> graph = LinkedDirectedGraph.create(); |
| 81 |
graph.createNode("A"); |
| 82 |
graph.createNode("B"); |
| 83 |
graph.createNode("C"); |
| 84 |
graph.createNode("D"); |
| 85 |
graph.connect("A", "--", "B"); |
| 86 |
graph.connect("B", "--", "C"); |
| 87 |
graph.connect("C", "--", "D"); |
| 88 |
|
| 89 |
DiGraph<String, String> pruned = |
| 90 |
new GraphPruner<String, String>(graph).prune( |
| 91 |
Predicates.not(Predicates.in(Lists.newArrayList("B", "C")))); |
| 92 |
assertEquals(2, pruned.getNodes().size()); |
| 93 |
assertTrue(pruned.isConnectedInDirection("A", "D")); |
| 94 |
} |
| 95 |
|
| 96 |
public void testFiveNodesConnected() { |
| 97 |
DiGraph<String, String> graph = LinkedDirectedGraph.create(); |
| 98 |
graph.createNode("A"); |
| 99 |
graph.createNode("B"); |
| 100 |
graph.createNode("C"); |
| 101 |
graph.createNode("D"); |
| 102 |
graph.createNode("E"); |
| 103 |
graph.connect("A", "--", "B"); |
| 104 |
graph.connect("B", "--", "C"); |
| 105 |
graph.connect("C", "--", "D"); |
| 106 |
graph.connect("D", "--", "E"); |
| 107 |
graph.connect("D", "--", "B"); |
| 108 |
|
| 109 |
DiGraph<String, String> pruned = |
| 110 |
new GraphPruner<String, String>(graph).prune( |
| 111 |
Predicates.not(Predicates.in(Lists.newArrayList("B", "C", "D")))); |
| 112 |
assertEquals(2, pruned.getNodes().size()); |
| 113 |
assertTrue(pruned.isConnectedInDirection("A", "E")); |
| 114 |
} |
| 115 |
} |