| 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.GraphReachability; |
| 20 |
import com.google.javascript.jscomp.graph.LinkedDirectedGraph; |
| 21 |
import com.google.javascript.jscomp.graph.DiGraph; |
| 22 |
|
| 23 |
import junit.framework.TestCase; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public class GraphReachabilityTest extends TestCase { |
| 30 |
GraphReachability<String, String> reachability = null; |
| 31 |
DiGraph<String, String> graph = null; |
| 32 |
|
| 33 |
public void testSimple() { |
| 34 |
graph = LinkedDirectedGraph.create(); |
| 35 |
graph.createNode("A"); |
| 36 |
reachability = new GraphReachability<String, String>(graph); |
| 37 |
reachability.compute("A"); |
| 38 |
assertReachable("A"); |
| 39 |
|
| 40 |
graph.createNode("B"); |
| 41 |
reachability = new GraphReachability<String, String>(graph); |
| 42 |
reachability.compute("A"); |
| 43 |
assertReachable("A"); |
| 44 |
assertNotReachable("B"); |
| 45 |
|
| 46 |
graph.connect("A", "--->", "B"); |
| 47 |
reachability = new GraphReachability<String, String>(graph); |
| 48 |
reachability.compute("B"); |
| 49 |
assertNotReachable("A"); |
| 50 |
assertReachable("B"); |
| 51 |
|
| 52 |
graph.connect("B", "--->", "A"); |
| 53 |
reachability = new GraphReachability<String, String>(graph); |
| 54 |
reachability.compute("B"); |
| 55 |
assertReachable("A"); |
| 56 |
assertReachable("B"); |
| 57 |
|
| 58 |
graph.createNode("C"); |
| 59 |
reachability = new GraphReachability<String, String>(graph); |
| 60 |
reachability.compute("A"); |
| 61 |
assertReachable("A"); |
| 62 |
assertReachable("B"); |
| 63 |
assertNotReachable("C"); |
| 64 |
|
| 65 |
graph.createNode("D"); |
| 66 |
graph.connect("C", "--->", "D"); |
| 67 |
reachability = new GraphReachability<String, String>(graph); |
| 68 |
reachability.compute("A"); |
| 69 |
assertReachable("A"); |
| 70 |
assertReachable("B"); |
| 71 |
assertNotReachable("C"); |
| 72 |
assertNotReachable("D"); |
| 73 |
reachability.recompute("C"); |
| 74 |
assertReachable("C"); |
| 75 |
assertReachable("D"); |
| 76 |
} |
| 77 |
|
| 78 |
public void assertReachable(String s) { |
| 79 |
assertSame(s + " should be reachable", graph.getNode(s).getAnnotation(), |
| 80 |
GraphReachability.REACHABLE); |
| 81 |
} |
| 82 |
|
| 83 |
public void assertNotReachable(String s) { |
| 84 |
assertNotSame(s + " should not be reachable", |
| 85 |
graph.getNode(s).getAnnotation(), GraphReachability.REACHABLE); |
| 86 |
} |
| 87 |
} |