Skip to content

Commit a9dc7fd

Browse files
committed
Add unit tests for directed trees (100% coverage)
1 parent 6d931fb commit a9dc7fd

File tree

3 files changed

+195
-0
lines changed

3 files changed

+195
-0
lines changed

tests/Tree/BaseDirectedTest.php

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
3+
use Fhaculty\Graph\Algorithm\Tree\BaseDirected;
4+
use Fhaculty\Graph\Exception\UnderflowException;
5+
use Fhaculty\Graph\Exception\UnexpectedValueException;
6+
use Fhaculty\Graph\Graph;
7+
8+
abstract class BaseDirectedTest extends TestCase
9+
{
10+
abstract protected function createTreeAlg(Graph $graph);
11+
12+
abstract protected function createGraphNonTree();
13+
14+
abstract protected function createGraphTree();
15+
16+
public function testEmptyGraph()
17+
{
18+
$graph = new Graph();
19+
20+
$tree = $this->createTreeAlg($graph);
21+
$this->assertTrue($tree->isTree());
22+
$this->assertEquals(array(), $tree->getVerticesLeaf());
23+
$this->assertEquals(array(), $tree->getVerticesInternal());
24+
25+
return $tree;
26+
}
27+
28+
/**
29+
* @param BaseDirected $tree
30+
* @depends testEmptyGraph
31+
* @expectedException UnderflowException
32+
*/
33+
public function testEmptyGraphDoesNotHaveRootVertex(BaseDirected $tree)
34+
{
35+
$tree->getVertexRoot();
36+
}
37+
38+
/**
39+
* @param BaseDirected $tree
40+
* @depends testEmptyGraph
41+
* @expectedException UnderflowException
42+
*/
43+
public function testEmptyGraphDoesNotHaveDegree(BaseDirected $tree)
44+
{
45+
$tree->getDegree();
46+
}
47+
48+
/**
49+
* @param BaseDirected $tree
50+
* @depends testEmptyGraph
51+
* @expectedException UnderflowException
52+
*/
53+
public function testEmptyGraphDoesNotHaveHeight(BaseDirected $tree)
54+
{
55+
$tree->getHeight();
56+
}
57+
58+
public function testGraphTree()
59+
{
60+
$graph = $this->createGraphTree();
61+
$root = $graph->getVertexFirst();
62+
63+
$nonRoot = $graph->getVertices();
64+
unset($nonRoot[$root->getId()]);
65+
66+
$c1 = current($nonRoot);
67+
68+
$tree = $this->createTreeAlg($graph);
69+
70+
$this->assertTrue($tree->isTree());
71+
$this->assertSame($root, $tree->getVertexRoot());
72+
$this->assertSame(array_values($nonRoot), array_values($tree->getVerticesChildren($root)));
73+
$this->assertSame(array_values($nonRoot), array_values($tree->getVerticesLeaf()));
74+
$this->assertSame(array(), array_values($tree->getVerticesInternal()));
75+
$this->assertSame($root, $tree->getVertexParent($c1));
76+
$this->assertSame(array(), $tree->getVerticesChildren($c1));
77+
$this->assertEquals(2, $tree->getDegree());
78+
$this->assertEquals(0, $tree->getDepthVertex($root));
79+
$this->assertEquals(1, $tree->getDepthVertex($c1));
80+
$this->assertEquals(1, $tree->getHeight());
81+
$this->assertEquals(1, $tree->getHeightVertex($root));
82+
$this->assertEquals(0, $tree->getHeightvertex($c1));
83+
84+
return $tree;
85+
}
86+
87+
/**
88+
*
89+
* @param BaseDirected $tree
90+
* @depends testGraphTree
91+
* @expectedException UnderflowException
92+
*/
93+
public function testGraphTreeRootDoesNotHaveParent(BaseDirected $tree)
94+
{
95+
$root = $tree->getVertexRoot();
96+
$tree->getVertexParent($root);
97+
}
98+
99+
public function testNonTree()
100+
{
101+
$graph = $this->createGraphNonTree();
102+
103+
$tree = $this->createTreeAlg($graph);
104+
105+
$this->assertFalse($tree->isTree());
106+
}
107+
108+
/**
109+
* @expectedException UnexpectedValueException
110+
*/
111+
public function testNonTreeVertexHasMoreThanOneParent()
112+
{
113+
$graph = $this->createGraphNonTree();
114+
115+
$tree = $this->createTreeAlg($graph);
116+
117+
$tree->getVertexParent($graph->getVertex('v3'));
118+
}
119+
}

tests/Tree/InTreeTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
use Fhaculty\Graph\Graph;
4+
use Fhaculty\Graph\Algorithm\Tree\InTree;
5+
6+
class InTreeTest extends BaseDirectedTest
7+
{
8+
protected function createGraphTree()
9+
{
10+
// c1 -> root <- c2
11+
$graph = new Graph();
12+
$root = $graph->createVertex();
13+
14+
$c1 = $graph->createVertex();
15+
$c1->createEdgeTo($root);
16+
17+
$c2 = $graph->createVertex();
18+
$c2->createEdgeTo($root);
19+
20+
return $graph;
21+
}
22+
23+
protected function createTreeAlg(Graph $graph)
24+
{
25+
return new InTree($graph);
26+
}
27+
28+
protected function createGraphNonTree()
29+
{
30+
// v1 -> v2 <- v3 -> v4
31+
$graph = new Graph();
32+
$graph->createVertex('v1')->createEdgeTo($graph->createVertex('v2'));
33+
$graph->createVertex('v3')->createEdgeTo($graph->getVertex('v2'));
34+
$graph->getVertex('v3')->createEdgeTo($graph->createVertex('v4'));
35+
36+
return $graph;
37+
}
38+
}

tests/Tree/OutTreeTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
use Fhaculty\Graph\Graph;
4+
use Fhaculty\Graph\Algorithm\Tree\OutTree;
5+
6+
class OutTreeTest extends BaseDirectedTest
7+
{
8+
protected function createGraphTree()
9+
{
10+
// c1 <- root -> c2
11+
$graph = new Graph();
12+
$root = $graph->createVertex();
13+
14+
$c1 = $graph->createVertex();
15+
$root->createEdgeTo($c1);
16+
17+
$c2 = $graph->createVertex();
18+
$root->createEdgeTo($c2);
19+
20+
return $graph;
21+
}
22+
23+
protected function createTreeAlg(Graph $graph)
24+
{
25+
return new OutTree($graph);
26+
}
27+
28+
protected function createGraphNonTree()
29+
{
30+
// v1 -> v3 <- v2 -> v4
31+
$graph = new Graph();
32+
$graph->createVertex('v1')->createEdgeTo($graph->createVertex('v3'));
33+
$graph->createVertex('v2')->createEdgeTo($graph->getVertex('v3'));
34+
$graph->getVertex('v2')->createEdgeTo($graph->createVertex('v4'));
35+
36+
return $graph;
37+
}
38+
}

0 commit comments

Comments
 (0)