Mercurial > hg > cmmr2012-drupal-site
comparison core/tests/Drupal/Tests/Component/Graph/GraphTest.php @ 0:c75dbcec494b
Initial commit from drush-created site
author | Chris Cannam |
---|---|
date | Thu, 05 Jul 2018 14:24:15 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c75dbcec494b |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\Tests\Component\Graph; | |
4 | |
5 use Drupal\Component\Graph\Graph; | |
6 use PHPUnit\Framework\TestCase; | |
7 | |
8 /** | |
9 * @coversDefaultClass \Drupal\Component\Graph\Graph | |
10 * @group Graph | |
11 */ | |
12 class GraphTest extends TestCase { | |
13 | |
14 /** | |
15 * Test depth-first-search features. | |
16 */ | |
17 public function testDepthFirstSearch() { | |
18 // The sample graph used is: | |
19 // 1 --> 2 --> 3 5 ---> 6 | |
20 // | ^ ^ | |
21 // | | | | |
22 // | | | | |
23 // +---> 4 <-- 7 8 ---> 9 | |
24 $graph = $this->normalizeGraph([ | |
25 1 => [2], | |
26 2 => [3, 4], | |
27 3 => [], | |
28 4 => [3], | |
29 5 => [6], | |
30 7 => [4, 5], | |
31 8 => [9], | |
32 9 => [], | |
33 ]); | |
34 $graph_object = new Graph($graph); | |
35 $graph = $graph_object->searchAndSort(); | |
36 | |
37 $expected_paths = [ | |
38 1 => [2, 3, 4], | |
39 2 => [3, 4], | |
40 3 => [], | |
41 4 => [3], | |
42 5 => [6], | |
43 7 => [4, 3, 5, 6], | |
44 8 => [9], | |
45 9 => [], | |
46 ]; | |
47 $this->assertPaths($graph, $expected_paths); | |
48 | |
49 $expected_reverse_paths = [ | |
50 1 => [], | |
51 2 => [1], | |
52 3 => [2, 1, 4, 7], | |
53 4 => [2, 1, 7], | |
54 5 => [7], | |
55 7 => [], | |
56 8 => [], | |
57 9 => [8], | |
58 ]; | |
59 $this->assertReversePaths($graph, $expected_reverse_paths); | |
60 | |
61 // Assert that DFS didn't created "missing" vertexes automatically. | |
62 $this->assertFalse(isset($graph[6]), 'Vertex 6 has not been created'); | |
63 | |
64 $expected_components = [ | |
65 [1, 2, 3, 4, 5, 7], | |
66 [8, 9], | |
67 ]; | |
68 $this->assertComponents($graph, $expected_components); | |
69 | |
70 $expected_weights = [ | |
71 [1, 2, 3], | |
72 [2, 4, 3], | |
73 [7, 4, 3], | |
74 [7, 5], | |
75 [8, 9], | |
76 ]; | |
77 $this->assertWeights($graph, $expected_weights); | |
78 } | |
79 | |
80 /** | |
81 * Normalizes a graph. | |
82 * | |
83 * @param $graph | |
84 * A graph array processed by \Drupal\Component\Graph\Graph::searchAndSort() | |
85 * | |
86 * @return array | |
87 * The normalized version of a graph. | |
88 */ | |
89 protected function normalizeGraph($graph) { | |
90 $normalized_graph = []; | |
91 foreach ($graph as $vertex => $edges) { | |
92 // Create vertex even if it hasn't any edges. | |
93 $normalized_graph[$vertex] = []; | |
94 foreach ($edges as $edge) { | |
95 $normalized_graph[$vertex]['edges'][$edge] = TRUE; | |
96 } | |
97 } | |
98 return $normalized_graph; | |
99 } | |
100 | |
101 /** | |
102 * Verify expected paths in a graph. | |
103 * | |
104 * @param $graph | |
105 * A graph array processed by \Drupal\Component\Graph\Graph::searchAndSort() | |
106 * @param $expected_paths | |
107 * An associative array containing vertices with their expected paths. | |
108 */ | |
109 protected function assertPaths($graph, $expected_paths) { | |
110 foreach ($expected_paths as $vertex => $paths) { | |
111 // Build an array with keys = $paths and values = TRUE. | |
112 $expected = array_fill_keys($paths, TRUE); | |
113 $result = isset($graph[$vertex]['paths']) ? $graph[$vertex]['paths'] : []; | |
114 $this->assertEquals($expected, $result, sprintf('Expected paths for vertex %s: %s, got %s', $vertex, $this->displayArray($expected, TRUE), $this->displayArray($result, TRUE))); | |
115 } | |
116 } | |
117 | |
118 /** | |
119 * Verify expected reverse paths in a graph. | |
120 * | |
121 * @param $graph | |
122 * A graph array processed by \Drupal\Component\Graph\Graph::searchAndSort() | |
123 * @param $expected_reverse_paths | |
124 * An associative array containing vertices with their expected reverse | |
125 * paths. | |
126 */ | |
127 protected function assertReversePaths($graph, $expected_reverse_paths) { | |
128 foreach ($expected_reverse_paths as $vertex => $paths) { | |
129 // Build an array with keys = $paths and values = TRUE. | |
130 $expected = array_fill_keys($paths, TRUE); | |
131 $result = isset($graph[$vertex]['reverse_paths']) ? $graph[$vertex]['reverse_paths'] : []; | |
132 $this->assertEquals($expected, $result, sprintf('Expected reverse paths for vertex %s: %s, got %s', $vertex, $this->displayArray($expected, TRUE), $this->displayArray($result, TRUE))); | |
133 } | |
134 } | |
135 | |
136 /** | |
137 * Verify expected components in a graph. | |
138 * | |
139 * @param $graph | |
140 * A graph array processed by \Drupal\Component\Graph\Graph::searchAndSort(). | |
141 * @param $expected_components | |
142 * An array containing of components defined as a list of their vertices. | |
143 */ | |
144 protected function assertComponents($graph, $expected_components) { | |
145 $unassigned_vertices = array_fill_keys(array_keys($graph), TRUE); | |
146 foreach ($expected_components as $component) { | |
147 $result_components = []; | |
148 foreach ($component as $vertex) { | |
149 $result_components[] = $graph[$vertex]['component']; | |
150 unset($unassigned_vertices[$vertex]); | |
151 } | |
152 $this->assertEquals(1, count(array_unique($result_components)), sprintf('Expected one unique component for vertices %s, got %s', $this->displayArray($component), $this->displayArray($result_components))); | |
153 } | |
154 $this->assertEquals([], $unassigned_vertices, sprintf('Vertices not assigned to a component: %s', $this->displayArray($unassigned_vertices, TRUE))); | |
155 } | |
156 | |
157 /** | |
158 * Verify expected order in a graph. | |
159 * | |
160 * @param $graph | |
161 * A graph array processed by \Drupal\Component\Graph\Graph::searchAndSort() | |
162 * @param $expected_orders | |
163 * An array containing lists of vertices in their expected order. | |
164 */ | |
165 protected function assertWeights($graph, $expected_orders) { | |
166 foreach ($expected_orders as $order) { | |
167 $previous_vertex = array_shift($order); | |
168 foreach ($order as $vertex) { | |
169 $this->assertTrue($graph[$previous_vertex]['weight'] < $graph[$vertex]['weight'], sprintf('Weights of %s and %s are correct relative to each other', $previous_vertex, $vertex)); | |
170 } | |
171 } | |
172 } | |
173 | |
174 /** | |
175 * Helper function to output vertices as comma-separated list. | |
176 * | |
177 * @param $paths | |
178 * An array containing a list of vertices. | |
179 * @param $keys | |
180 * (optional) Whether to output the keys of $paths instead of the values. | |
181 */ | |
182 protected function displayArray($paths, $keys = FALSE) { | |
183 if (!empty($paths)) { | |
184 return implode(', ', $keys ? array_keys($paths) : $paths); | |
185 } | |
186 else { | |
187 return '(empty)'; | |
188 } | |
189 } | |
190 | |
191 } |