Debian Codegraph @

test/com/google/javascript/jscomp/FixedPointGraphTraversalTest.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;
18
19 import com.google.javascript.jscomp.graph.FixedPointGraphTraversal;
20 import com.google.javascript.jscomp.graph.LinkedDirectedGraph;
21 import com.google.javascript.jscomp.graph.FixedPointGraphTraversal.EdgeCallback;
22 import com.google.javascript.jscomp.graph.DiGraph;
23
24 import junit.framework.TestCase;
25
26 /**
27 * Test for FixedPointGraphTraversal.
28 * @author [email protected] (Nick Santos)
29 */
30 public class FixedPointGraphTraversalTest extends TestCase {
31
32 // The maximum value of a counter that counts as a "change"
33 // to the state of the graph, for the purposes of fixed-point
34 // computation.
35 private int maxChange = 0;
36
37 private class Counter {
38 int value = 0;
39 }
40
41 private class CounterIncrementer implements EdgeCallback<Counter, String> {
42 @Override
43 public boolean traverseEdge(Counter source, String e, Counter dest) {
44 dest.value++;
45 return dest.value <= maxChange;
46 }
47 }
48
49 private DiGraph<Counter, String> graph;
50
51 private Counter A, B, C, D, E;
52 private CounterIncrementer callback = new CounterIncrementer();
53 private FixedPointGraphTraversal<Counter, String> traversal =
54 new FixedPointGraphTraversal<Counter, String>(callback);
55
56 // Create a new graph of the following form:
57 //
58 // A
59 // / \
60 // | B
61 // / \ /
62 // C D
63 // \ /
64 // E
65 //
66 // with all edges pointing downwards, and an "up-edge" from E to D.
67 @Override
68 public void setUp() {
69 A = new Counter();
70 B = new Counter();
71 C = new Counter();
72 D = new Counter();
73 E = new Counter();
74
75 graph = LinkedDirectedGraph.create();
76 graph.createDirectedGraphNode(A);
77 graph.createDirectedGraphNode(B);
78 graph.createDirectedGraphNode(C);
79 graph.createDirectedGraphNode(D);
80 graph.createDirectedGraphNode(E);
81
82 graph.connect(A, "->", B);
83 graph.connect(A, "->", C);
84 graph.connect(A, "->", D);
85 graph.connect(B, "->", D);
86 graph.connect(C, "->", E);
87 graph.connect(D, "->", E);
88 graph.connect(E, "->", D);
89 }
90
91 public void testGraph1() {
92 maxChange = 0;
93 traversal.computeFixedPoint(graph, A);
94
95 assertEquals(0, A.value);
96 assertEquals(1, B.value);
97 assertEquals(1, C.value);
98 assertEquals(1, D.value);
99 assertEquals(0, E.value);
100 }
101
102 public void testGraph2() {
103 maxChange = 0;
104 traversal.computeFixedPoint(graph, D);
105
106 assertEquals(0, A.value);
107 assertEquals(0, B.value);
108 assertEquals(0, C.value);
109 assertEquals(0, D.value);
110 assertEquals(1, E.value);
111 }
112
113 public void testGraph3() {
114 maxChange = 1;
115 traversal.computeFixedPoint(graph, A);
116
117 assertEquals(0, A.value);
118 assertEquals(1, B.value);
119 assertEquals(1, C.value);
120 assertEquals(3, D.value);
121 assertEquals(2, E.value);
122 }
123
124 public void testGraph4() {
125 maxChange = 1;
126 traversal.computeFixedPoint(graph, D);
127
128 assertEquals(0, A.value);
129 assertEquals(0, B.value);
130 assertEquals(0, C.value);
131 assertEquals(1, D.value);
132 assertEquals(2, E.value);
133 }
134
135 public void testGraph5() {
136 maxChange = 5;
137 traversal.computeFixedPoint(graph, A);
138
139 assertEquals(0, A.value);
140 assertEquals(1, B.value);
141 assertEquals(1, C.value);
142 assertEquals(6, D.value);
143 assertEquals(5, E.value);
144 }
145
146 public void testGraph6() {
147 maxChange = 5;
148 traversal.computeFixedPoint(graph, B);
149
150 assertEquals(0, A.value);
151 assertEquals(0, B.value);
152 assertEquals(0, C.value);
153 assertEquals(6, D.value);
154 assertEquals(5, E.value);
155 }
156
157 public void testGraph8() {
158 maxChange = 2;
159 traversal.computeFixedPoint(graph, A);
160
161 try {
162 traversal = new FixedPointGraphTraversal<Counter, String>(
163 new EdgeCallback<Counter, String>() {
164 @Override
165 public boolean traverseEdge(Counter source, String e, Counter dest) {
166 return true;
167 }
168 });
169 traversal.computeFixedPoint(graph, A);
170 fail("Expecting Error: " +
171 FixedPointGraphTraversal.NON_HALTING_ERROR_MSG);
172 } catch (IllegalStateException e) {
173 assertEquals(e.getMessage(),
174 FixedPointGraphTraversal.NON_HALTING_ERROR_MSG);
175 }
176 }
177
178 public void testGraph9() {
179 maxChange = 0;
180
181 // when the graph traversal is done for the whole graph, we're actually
182 // counting the number of "in" edges for each node.
183 traversal.computeFixedPoint(graph);
184
185 assertEquals(0, A.value);
186 assertEquals(1, B.value);
187 assertEquals(1, C.value);
188 assertEquals(3, D.value);
189 assertEquals(2, E.value);
190 }
191
192 public void testGraph10() {
193 // Test a graph with self-edges.
194 maxChange = 5;
195
196 A = new Counter();
197 B = new Counter();
198
199 graph = LinkedDirectedGraph.create();
200 graph.createDirectedGraphNode(A);
201 graph.createDirectedGraphNode(B);
202
203 graph.connect(A, "->", A);
204 graph.connect(A, "->", B);
205
206 traversal.computeFixedPoint(graph);
207
208 assertEquals(6, A.value);
209 assertEquals(6, B.value);
210 }
211 }