You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DirectedGraphTest.java 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright 2015 Decebal Suiu
  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. package org.pf4j.util;
  17. import org.junit.jupiter.api.BeforeAll;
  18. import org.junit.jupiter.api.Test;
  19. import java.util.Arrays;
  20. import java.util.HashMap;
  21. import java.util.List;
  22. import java.util.Map;
  23. import static org.junit.jupiter.api.Assertions.assertEquals;
  24. /**
  25. * @author Decebal Suiu
  26. */
  27. public class DirectedGraphTest {
  28. private static DirectedGraph<Character> graph;
  29. @BeforeAll
  30. public static void setUp() {
  31. graph = new DirectedGraph<>();
  32. // add vertex
  33. graph.addVertex('A');
  34. graph.addVertex('B');
  35. graph.addVertex('C');
  36. graph.addVertex('D');
  37. graph.addVertex('E');
  38. graph.addVertex('F');
  39. graph.addVertex('G');
  40. // add edges
  41. graph.addEdge('A', 'B');
  42. graph.addEdge('B', 'C');
  43. graph.addEdge('B', 'F');
  44. graph.addEdge('D', 'E');
  45. graph.addEdge('F', 'G');
  46. }
  47. @Test
  48. public void reverseTopologicalSort() {
  49. List<Character> result = graph.reverseTopologicalSort();
  50. List<Character> expected = Arrays.asList('C', 'G', 'F', 'B', 'A', 'E', 'D');
  51. assertEquals(expected, result);
  52. }
  53. @Test
  54. public void topologicalSort() {
  55. List<Character> result = graph.topologicalSort();
  56. List<Character> expected = Arrays.asList('D', 'E', 'A', 'B', 'F', 'G', 'C');
  57. assertEquals(expected, result);
  58. }
  59. @Test
  60. public void inDegree() {
  61. Map<Character, Integer> result = graph.inDegree();
  62. Map<Character, Integer> expected = new HashMap<>(7);
  63. expected.put('A', 0);
  64. expected.put('B', 1);
  65. expected.put('C', 1);
  66. expected.put('D', 0);
  67. expected.put('E', 1);
  68. expected.put('F', 1);
  69. expected.put('G', 1);
  70. assertEquals(expected, result);
  71. }
  72. @Test
  73. public void outDegree() {
  74. Map<Character, Integer> result = graph.outDegree();
  75. Map<Character, Integer> expected = new HashMap<>(7);
  76. expected.put('A', 1);
  77. expected.put('B', 2);
  78. expected.put('C', 0);
  79. expected.put('D', 1);
  80. expected.put('E', 0);
  81. expected.put('F', 1);
  82. expected.put('G', 0);
  83. assertEquals(expected, result);
  84. }
  85. }