Skip to content

Commit 7996792

Browse files
committed
Tests for parallel/loop/undirected edges (100% coverage)
1 parent 3e12b71 commit 7996792

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

tests/Tree/BaseDirectedTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ abstract protected function createGraphNonTree();
1313

1414
abstract protected function createGraphTree();
1515

16+
abstract protected function createGraphParallelEdge();
17+
1618
public function testEmptyGraph()
1719
{
1820
$graph = new Graph();
@@ -69,11 +71,15 @@ public function testGraphTree()
6971

7072
$this->assertTrue($tree->isTree());
7173
$this->assertSame($root, $tree->getVertexRoot());
74+
$this->assertSame(array_values($graph->getVertices()), array_values($tree->getVerticesSubtree($root)));
7275
$this->assertSame(array_values($nonRoot), array_values($tree->getVerticesChildren($root)));
76+
$this->assertSame(array_values($nonRoot), array_values($tree->getVerticesDescendant($root)));
7377
$this->assertSame(array_values($nonRoot), array_values($tree->getVerticesLeaf()));
7478
$this->assertSame(array(), array_values($tree->getVerticesInternal()));
7579
$this->assertSame($root, $tree->getVertexParent($c1));
7680
$this->assertSame(array(), $tree->getVerticesChildren($c1));
81+
$this->assertSame(array(), $tree->getVerticesDescendant($c1));
82+
$this->assertSame(array($c1), array_values($tree->getVerticesSubtree($c1)));
7783
$this->assertEquals(2, $tree->getDegree());
7884
$this->assertEquals(0, $tree->getDepthVertex($root));
7985
$this->assertEquals(1, $tree->getDepthVertex($c1));
@@ -116,4 +122,62 @@ public function testNonTreeVertexHasMoreThanOneParent()
116122

117123
$tree->getVertexParent($graph->getVertex('v3'));
118124
}
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+
}
119183
}

tests/Tree/InTreeTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,14 @@ protected function createGraphNonTree()
3535

3636
return $graph;
3737
}
38+
39+
protected function createGraphParallelEdge()
40+
{
41+
// v1 <- v2, v1 <- v2
42+
$graph = new Graph();
43+
$graph->createVertex('v2')->createEdgeTo($graph->createVertex('v1'));
44+
$graph->getVertex('v2')->createEdgeTo($graph->getVertex('v1'));
45+
46+
return $graph;
47+
}
3848
}

tests/Tree/OutTreeTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,14 @@ protected function createGraphNonTree()
3535

3636
return $graph;
3737
}
38+
39+
protected function createGraphParallelEdge()
40+
{
41+
// v1 -> v2, v1 -> v2
42+
$graph = new Graph();
43+
$graph->createVertex('v1')->createEdgeTo($graph->createVertex('v2'));
44+
$graph->getVertex('v1')->createEdgeTo($graph->getVertex('v2'));
45+
46+
return $graph;
47+
}
3848
}

0 commit comments

Comments
 (0)