@@ -13,6 +13,8 @@ abstract protected function createGraphNonTree();
13
13
14
14
abstract protected function createGraphTree ();
15
15
16
+ abstract protected function createGraphParallelEdge ();
17
+
16
18
public function testEmptyGraph ()
17
19
{
18
20
$ graph = new Graph ();
@@ -69,11 +71,15 @@ public function testGraphTree()
69
71
70
72
$ this ->assertTrue ($ tree ->isTree ());
71
73
$ this ->assertSame ($ root , $ tree ->getVertexRoot ());
74
+ $ this ->assertSame (array_values ($ graph ->getVertices ()), array_values ($ tree ->getVerticesSubtree ($ root )));
72
75
$ this ->assertSame (array_values ($ nonRoot ), array_values ($ tree ->getVerticesChildren ($ root )));
76
+ $ this ->assertSame (array_values ($ nonRoot ), array_values ($ tree ->getVerticesDescendant ($ root )));
73
77
$ this ->assertSame (array_values ($ nonRoot ), array_values ($ tree ->getVerticesLeaf ()));
74
78
$ this ->assertSame (array (), array_values ($ tree ->getVerticesInternal ()));
75
79
$ this ->assertSame ($ root , $ tree ->getVertexParent ($ c1 ));
76
80
$ this ->assertSame (array (), $ tree ->getVerticesChildren ($ c1 ));
81
+ $ this ->assertSame (array (), $ tree ->getVerticesDescendant ($ c1 ));
82
+ $ this ->assertSame (array ($ c1 ), array_values ($ tree ->getVerticesSubtree ($ c1 )));
77
83
$ this ->assertEquals (2 , $ tree ->getDegree ());
78
84
$ this ->assertEquals (0 , $ tree ->getDepthVertex ($ root ));
79
85
$ this ->assertEquals (1 , $ tree ->getDepthVertex ($ c1 ));
@@ -116,4 +122,62 @@ public function testNonTreeVertexHasMoreThanOneParent()
116
122
117
123
$ tree ->getVertexParent ($ graph ->getVertex ('v3 ' ));
118
124
}
125
+
126
+ public function testGraphWithParallelEdgeIsNotTree ()
127
+ {
128
+ $ graph = $ this ->createGraphParallelEdge ();
129
+
130
+ $ tree = $ this ->createTreeAlg ($ graph );
131
+
132
+ $ this ->assertFalse ($ tree ->isTree ());
133
+ }
134
+
135
+ public function testGraphWithLoopIsNotTree ()
136
+ {
137
+ // v1 -> v1
138
+ $ graph = new Graph ();
139
+ $ graph ->createVertex ('v1 ' )->createEdgeTo ($ graph ->getVertex ('v1 ' ));
140
+
141
+ $ tree = $ this ->createTreeAlg ($ graph );
142
+
143
+ $ this ->assertFalse ($ tree ->isTree ());
144
+ }
145
+
146
+ /**
147
+ * @expectedException UnexpectedValueException
148
+ */
149
+ public function testGraphWithLoopCanNotGetSubgraph ()
150
+ {
151
+ // v1 -> v1
152
+ $ graph = new Graph ();
153
+ $ graph ->createVertex ('v1 ' )->createEdgeTo ($ graph ->getVertex ('v1 ' ));
154
+
155
+ $ tree = $ this ->createTreeAlg ($ graph );
156
+
157
+ $ tree ->getVerticesSubtree ($ graph ->getVertex ('v1 ' ));
158
+ }
159
+
160
+ public function testGraphWithUndirectedEdgeIsNotTree ()
161
+ {
162
+ // v1 -- v2
163
+ $ graph = new Graph ();
164
+ $ graph ->createVertex ('v1 ' )->createEdge ($ graph ->createVertex ('v2 ' ));
165
+
166
+ $ tree = $ this ->createTreeAlg ($ graph );
167
+
168
+ $ this ->assertFalse ($ tree ->isTree ());
169
+ }
170
+
171
+ public function testGraphWithMixedEdgesIsNotTree ()
172
+ {
173
+ // v1 -> v2 -- v3 -> v4
174
+ $ graph = new Graph ();
175
+ $ graph ->createVertex ('v1 ' )->createEdgeTo ($ graph ->createVertex ('v2 ' ));
176
+ $ graph ->getVertex ('v2 ' )->createEdge ($ graph ->createVertex ('v3 ' ));
177
+ $ graph ->getVertex ('v3 ' )->createEdgeTo ($ graph ->createVertex ('v4 ' ));
178
+
179
+ $ tree = $ this ->createTreeAlg ($ graph );
180
+
181
+ $ this ->assertFalse ($ tree ->isTree ());
182
+ }
119
183
}
0 commit comments