Debian Codegraph @

test/com/google/javascript/jscomp/graph/GraphReachabilityTest.java

raw symbols SCIP
1 /*
2 * Copyright 2008 The Closure Compiler 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.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 * Tests for {@link GraphReachability}.
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 }