graph.test 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. * @file
  4. * Provides unit tests for graph.inc.
  5. */
  6. /**
  7. * Unit tests for the graph handling features.
  8. */
  9. class GraphUnitTest extends DrupalUnitTestCase {
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'Graph',
  13. 'description' => 'Graph handling unit tests.',
  14. 'group' => 'System',
  15. );
  16. }
  17. function setUp() {
  18. require_once DRUPAL_ROOT . '/includes/graph.inc';
  19. parent::setUp();
  20. }
  21. /**
  22. * Test depth-first-search features.
  23. */
  24. function testDepthFirstSearch() {
  25. // The sample graph used is:
  26. // 1 --> 2 --> 3 5 ---> 6
  27. // | ^ ^
  28. // | | |
  29. // | | |
  30. // +---> 4 <-- 7 8 ---> 9
  31. $graph = $this->normalizeGraph(array(
  32. 1 => array(2),
  33. 2 => array(3, 4),
  34. 3 => array(),
  35. 4 => array(3),
  36. 5 => array(6),
  37. 7 => array(4, 5),
  38. 8 => array(9),
  39. 9 => array(),
  40. ));
  41. drupal_depth_first_search($graph);
  42. $expected_paths = array(
  43. 1 => array(2, 3, 4),
  44. 2 => array(3, 4),
  45. 3 => array(),
  46. 4 => array(3),
  47. 5 => array(6),
  48. 7 => array(4, 3, 5, 6),
  49. 8 => array(9),
  50. 9 => array(),
  51. );
  52. $this->assertPaths($graph, $expected_paths);
  53. $expected_reverse_paths = array(
  54. 1 => array(),
  55. 2 => array(1),
  56. 3 => array(2, 1, 4, 7),
  57. 4 => array(2, 1, 7),
  58. 5 => array(7),
  59. 7 => array(),
  60. 8 => array(),
  61. 9 => array(8),
  62. );
  63. $this->assertReversePaths($graph, $expected_reverse_paths);
  64. // Assert that DFS didn't created "missing" vertexes automatically.
  65. $this->assertFALSE(isset($graph[6]), 'Vertex 6 has not been created');
  66. $expected_components = array(
  67. array(1, 2, 3, 4, 5, 7),
  68. array(8, 9),
  69. );
  70. $this->assertComponents($graph, $expected_components);
  71. $expected_weights = array(
  72. array(1, 2, 3),
  73. array(2, 4, 3),
  74. array(7, 4, 3),
  75. array(7, 5),
  76. array(8, 9),
  77. );
  78. $this->assertWeights($graph, $expected_weights);
  79. }
  80. /**
  81. * Return a normalized version of a graph.
  82. */
  83. function normalizeGraph($graph) {
  84. $normalized_graph = array();
  85. foreach ($graph as $vertex => $edges) {
  86. // Create vertex even if it hasn't any edges.
  87. $normalized_graph[$vertex] = array();
  88. foreach ($edges as $edge) {
  89. $normalized_graph[$vertex]['edges'][$edge] = TRUE;
  90. }
  91. }
  92. return $normalized_graph;
  93. }
  94. /**
  95. * Verify expected paths in a graph.
  96. *
  97. * @param $graph
  98. * A graph array processed by drupal_depth_first_search().
  99. * @param $expected_paths
  100. * An associative array containing vertices with their expected paths.
  101. */
  102. function assertPaths($graph, $expected_paths) {
  103. foreach ($expected_paths as $vertex => $paths) {
  104. // Build an array with keys = $paths and values = TRUE.
  105. $expected = array_fill_keys($paths, TRUE);
  106. $result = isset($graph[$vertex]['paths']) ? $graph[$vertex]['paths'] : array();
  107. $this->assertEqual($expected, $result, format_string('Expected paths for vertex @vertex: @expected-paths, got @paths', array('@vertex' => $vertex, '@expected-paths' => $this->displayArray($expected, TRUE), '@paths' => $this->displayArray($result, TRUE))));
  108. }
  109. }
  110. /**
  111. * Verify expected reverse paths in a graph.
  112. *
  113. * @param $graph
  114. * A graph array processed by drupal_depth_first_search().
  115. * @param $expected_reverse_paths
  116. * An associative array containing vertices with their expected reverse
  117. * paths.
  118. */
  119. function assertReversePaths($graph, $expected_reverse_paths) {
  120. foreach ($expected_reverse_paths as $vertex => $paths) {
  121. // Build an array with keys = $paths and values = TRUE.
  122. $expected = array_fill_keys($paths, TRUE);
  123. $result = isset($graph[$vertex]['reverse_paths']) ? $graph[$vertex]['reverse_paths'] : array();
  124. $this->assertEqual($expected, $result, format_string('Expected reverse paths for vertex @vertex: @expected-paths, got @paths', array('@vertex' => $vertex, '@expected-paths' => $this->displayArray($expected, TRUE), '@paths' => $this->displayArray($result, TRUE))));
  125. }
  126. }
  127. /**
  128. * Verify expected components in a graph.
  129. *
  130. * @param $graph
  131. * A graph array processed by drupal_depth_first_search().
  132. * @param $expected_components
  133. * An array containing of components defined as a list of their vertices.
  134. */
  135. function assertComponents($graph, $expected_components) {
  136. $unassigned_vertices = array_fill_keys(array_keys($graph), TRUE);
  137. foreach ($expected_components as $component) {
  138. $result_components = array();
  139. foreach ($component as $vertex) {
  140. $result_components[] = $graph[$vertex]['component'];
  141. unset($unassigned_vertices[$vertex]);
  142. }
  143. $this->assertEqual(1, count(array_unique($result_components)), format_string('Expected one unique component for vertices @vertices, got @components', array('@vertices' => $this->displayArray($component), '@components' => $this->displayArray($result_components))));
  144. }
  145. $this->assertEqual(array(), $unassigned_vertices, format_string('Vertices not assigned to a component: @vertices', array('@vertices' => $this->displayArray($unassigned_vertices, TRUE))));
  146. }
  147. /**
  148. * Verify expected order in a graph.
  149. *
  150. * @param $graph
  151. * A graph array processed by drupal_depth_first_search().
  152. * @param $expected_orders
  153. * An array containing lists of vertices in their expected order.
  154. */
  155. function assertWeights($graph, $expected_orders) {
  156. foreach ($expected_orders as $order) {
  157. $previous_vertex = array_shift($order);
  158. foreach ($order as $vertex) {
  159. $this->assertTrue($graph[$previous_vertex]['weight'] < $graph[$vertex]['weight'], format_string('Weights of @previous-vertex and @vertex are correct relative to each other', array('@previous-vertex' => $previous_vertex, '@vertex' => $vertex)));
  160. }
  161. }
  162. }
  163. /**
  164. * Helper function to output vertices as comma-separated list.
  165. *
  166. * @param $paths
  167. * An array containing a list of vertices.
  168. * @param $keys
  169. * (optional) Whether to output the keys of $paths instead of the values.
  170. */
  171. function displayArray($paths, $keys = FALSE) {
  172. if (!empty($paths)) {
  173. return implode(', ', $keys ? array_keys($paths) : $paths);
  174. }
  175. else {
  176. return '(empty)';
  177. }
  178. }
  179. }