Debian Codegraph @

test/com/google/javascript/jscomp/graph/GraphColoringTest.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.Graph;
20 import com.google.javascript.jscomp.graph.GraphColoring;
21 import com.google.javascript.jscomp.graph.LinkedUndirectedGraph;
22 import com.google.javascript.jscomp.graph.GraphNode;
23 import com.google.javascript.jscomp.graph.Graph.GraphEdge;
24 import com.google.javascript.jscomp.graph.GraphColoring.Color;
25 import com.google.javascript.jscomp.graph.GraphColoring.GreedyGraphColoring;
26
27 import junit.framework.TestCase;
28
29 import java.util.Comparator;
30
31 /**
32 * Tests for {@link GraphColoring}.
33 *
34 */
35 public class GraphColoringTest extends TestCase {
36
37 public void testNoEdge() {
38 Graph<String, String> graph = LinkedUndirectedGraph.create();
39 for (int i = 0; i < 5; i++) {
40 graph.createNode("Node " + i);
41 // All node with same color.
42 GraphColoring<String, String> coloring =
43 new GreedyGraphColoring<String, String>(graph);
44 assertEquals(1, coloring.color());
45 validateColoring(graph);
46 for (int j = 0; j < i; j++) {
47 assertEquals("Node 0", coloring.getPartitionSuperNode("Node 0"));
48 }
49 }
50 }
51
52 public void testTwoNodesConnected() {
53 Graph<String, String> graph = LinkedUndirectedGraph.create();
54 graph.createNode("A");
55 graph.createNode("B");
56 graph.connect("A", "--", "B");
57 GraphColoring<String, String> coloring =
58 new GreedyGraphColoring<String, String>(graph);
59 assertEquals(2, coloring.color());
60 validateColoring(graph);
61 assertEquals("A", coloring.getPartitionSuperNode("A"));
62 assertEquals("B", coloring.getPartitionSuperNode("B"));
63 }
64
65 public void testGreedy() {
66 Graph<String, String> graph = LinkedUndirectedGraph.create();
67 graph.createNode("A");
68 graph.createNode("B");
69 graph.createNode("C");
70 graph.createNode("D");
71 graph.connect("A", "--", "C");
72 graph.connect("B", "--", "C");
73 graph.connect("B", "--", "D");
74 GraphColoring<String, String> coloring =
75 new GreedyGraphColoring<String, String>(graph);
76 assertEquals(2, coloring.color());
77 validateColoring(graph);
78 assertEquals("A", coloring.getPartitionSuperNode("A"));
79 assertEquals("A", coloring.getPartitionSuperNode("B"));
80 assertEquals("C", coloring.getPartitionSuperNode("C"));
81 }
82
83 public void testFullyConnected() {
84 final int count = 100;
85 Graph<String, String> graph = LinkedUndirectedGraph.create();
86 for (int i = 0; i < count; i++) {
87 graph.createNode("Node " + i);
88 for (int j = 0; j < count; j++) {
89 graph.createNode("Node " + j);
90 if (i != j) {
91 graph.connect("Node " + i, null, "Node " + j);
92 }
93 }
94 }
95 GraphColoring<String, String> coloring =
96 new GreedyGraphColoring<String, String>(graph);
97 assertEquals(count, coloring.color());
98 validateColoring(graph);
99 for (int i = 0; i < count; i++) {
100 assertEquals("Node " + i, coloring.getPartitionSuperNode("Node " + i));
101 }
102 }
103
104 public void testAllConnectedToOneNode() {
105 final int count = 10;
106 Graph<String, String> graph = LinkedUndirectedGraph.create();
107 graph.createNode("Center");
108 for (int i = 0; i < count; i++) {
109 graph.createNode("Node " + i);
110 graph.connect("Center", null, "Node " + i);
111 }
112 GraphColoring<String, String> coloring =
113 new GreedyGraphColoring<String, String>(graph);
114 assertEquals(2, coloring.color());
115 validateColoring(graph);
116 assertEquals("Center", coloring.getPartitionSuperNode("Center"));
117 for (int i = 0; i < count; i++) {
118 assertEquals("Node 0", coloring.getPartitionSuperNode("Node " + i));
119 }
120 }
121
122 public void testTwoFullyConnected() {
123 final int count = 100;
124 // A graph with two disconnected disjunct cliques.
125 Graph<String, String> graph = LinkedUndirectedGraph.create();
126 for (int i = 0; i < count; i++) {
127 graph.createNode("Node Left " + i);
128 graph.createNode("Node Right " + i);
129 for (int j = 0; j < count; j++) {
130 graph.createNode("Node Left " + j);
131 graph.createNode("Node Right " + j);
132 if (i != j) {
133 graph.connect("Node Left " + i, null, "Node Left " + j);
134 graph.connect("Node Right " + i, null, "Node Right " + j);
135 }
136 }
137 }
138 assertEquals(count, new GreedyGraphColoring<String, String>(graph).color());
139 validateColoring(graph);
140
141 // Connect the two cliques.
142 for (int i = 0; i < count; i++) {
143 graph.connect("Node Left " + i, null, "Node Right " + i);
144 }
145 // Think of two exactly same graph with the same coloring side by side.
146 // If we circularly shift the colors of one of the graph by 1, we can
147 // connect the isomorphic nodes and still have a valid coloring in the
148 // resulting graph.
149 assertEquals(count, new GreedyGraphColoring<String, String>(graph).color());
150 validateColoring(graph);
151 }
152
153 public void testDeterministic() {
154 // A pentagon.
155 Graph<String, String> graph = LinkedUndirectedGraph.create();
156 graph.createNode("A");
157 graph.createNode("B");
158 graph.createNode("C");
159 graph.createNode("D");
160 graph.createNode("E");
161 graph.connect("A", "-->", "B");
162 graph.connect("B", "-->", "C");
163 graph.connect("C", "-->", "D");
164 graph.connect("D", "-->", "E");
165 graph.connect("E", "-->", "A");
166
167 Comparator<String> lexicographic = new Comparator<String>() {
168 @Override
169 public int compare(String o1, String o2) {
170 return o1.toString().compareTo(o2.toString());
171 }
172 };
173 GraphColoring<String, String> coloring =
174 new GreedyGraphColoring<String, String>(graph, lexicographic);
175 assertEquals(3, coloring.color());
176 validateColoring(graph);
177 assertEquals("A", coloring.getPartitionSuperNode("A"));
178 assertEquals("A", coloring.getPartitionSuperNode("C"));
179
180 Comparator<String> biasD = new Comparator<String>() {
181 @Override
182 public int compare(String o1, String o2) {
183 return o1.replaceAll("D", "@").compareTo(o2.replaceAll("D", "@"));
184 }
185 };
186
187 coloring = new GreedyGraphColoring<String, String>(graph, biasD);
188 assertEquals(3, coloring.color());
189 validateColoring(graph);
190 assertEquals("A", coloring.getPartitionSuperNode("A"));
191 assertFalse("A".equals(coloring.getPartitionSuperNode("C")));
192 }
193
194 /**
195 * Validate that each node has been colored and connected nodes have different
196 * coloring.
197 */
198 private static <N, E> void validateColoring(Graph<N, E> graph) {
199 for (GraphNode<N, E> node : graph.getNodes()) {
200 assertTrue(node.getAnnotation() != null);
201 }
202 for (GraphEdge<N, E> edge : graph.getEdges()) {
203 Color c1 = edge.getNodeA().getAnnotation();
204 Color c2 = edge.getNodeB().getAnnotation();
205 assertTrue(c1 != null);
206 assertTrue(c2 != null);
207 assertTrue(!c1.equals(c2));
208 }
209 }
210 }