|
| 1 | +// To run this test in isolation from root folder: |
| 2 | +// |
| 3 | +// $ gradle test --tests |
| 4 | +// javatests.com.williamfiset.algorithms.graphtheory.treealgorithms.TreeCenterTest |
| 5 | + |
| 6 | +package javatests.com.williamfiset.algorithms.graphtheory.treealgorithms; |
| 7 | + |
| 8 | +import static com.google.common.truth.Truth.assertThat; |
| 9 | +import static com.williamfiset.algorithms.graphtheory.treealgorithms.TreeCenter.addUndirectedEdge; |
| 10 | +import static com.williamfiset.algorithms.graphtheory.treealgorithms.TreeCenter.createEmptyTree; |
| 11 | +import static com.williamfiset.algorithms.graphtheory.treealgorithms.TreeCenter.findTreeCenters; |
| 12 | + |
| 13 | +import java.util.*; |
| 14 | +import org.junit.*; |
| 15 | + |
| 16 | +public class TreeCenterTest { |
| 17 | + |
| 18 | + @Test |
| 19 | + public void emptyTreeThrowsException() { |
| 20 | + assertThat(createEmptyTree(0)).isEmpty(); |
| 21 | + } |
| 22 | + |
| 23 | + @Test |
| 24 | + public void simpleTest1() { |
| 25 | + List<List<Integer>> graph = createEmptyTree(9); |
| 26 | + addUndirectedEdge(graph, 0, 1); |
| 27 | + addUndirectedEdge(graph, 2, 1); |
| 28 | + addUndirectedEdge(graph, 2, 3); |
| 29 | + addUndirectedEdge(graph, 3, 4); |
| 30 | + addUndirectedEdge(graph, 5, 3); |
| 31 | + addUndirectedEdge(graph, 2, 6); |
| 32 | + addUndirectedEdge(graph, 6, 7); |
| 33 | + addUndirectedEdge(graph, 6, 8); |
| 34 | + assertThat(findTreeCenters(graph)).containsExactly(2); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void singleton() { |
| 39 | + assertThat(findTreeCenters(createEmptyTree(1))).containsExactly(0); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void twoNodeTree() { |
| 44 | + List<List<Integer>> graph = createEmptyTree(2); |
| 45 | + addUndirectedEdge(graph, 0, 1); |
| 46 | + assertThat(findTreeCenters(graph)).containsExactly(0, 1); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void simpleTest2() { |
| 51 | + List<List<Integer>> graph = createEmptyTree(3); |
| 52 | + addUndirectedEdge(graph, 0, 1); |
| 53 | + addUndirectedEdge(graph, 1, 2); |
| 54 | + assertThat(findTreeCenters(graph)).containsExactly(1); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void simpleTest3() { |
| 59 | + List<List<Integer>> graph = createEmptyTree(4); |
| 60 | + addUndirectedEdge(graph, 0, 1); |
| 61 | + addUndirectedEdge(graph, 1, 2); |
| 62 | + addUndirectedEdge(graph, 2, 3); |
| 63 | + assertThat(findTreeCenters(graph)).containsExactly(1, 2); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void simpleTest4() { |
| 68 | + List<List<Integer>> graph = createEmptyTree(7); |
| 69 | + addUndirectedEdge(graph, 0, 1); |
| 70 | + addUndirectedEdge(graph, 1, 2); |
| 71 | + addUndirectedEdge(graph, 2, 3); |
| 72 | + addUndirectedEdge(graph, 3, 4); |
| 73 | + addUndirectedEdge(graph, 4, 5); |
| 74 | + addUndirectedEdge(graph, 4, 6); |
| 75 | + assertThat(findTreeCenters(graph)).containsExactly(2, 3); |
| 76 | + } |
| 77 | +} |
0 commit comments