Skip to content

Commit e927c2b

Browse files
committed
Add proper unit tests for minimum spanning tree (~95% coverage)
1 parent c00e170 commit e927c2b

File tree

3 files changed

+193
-14
lines changed

3 files changed

+193
-14
lines changed
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
<?php
2+
3+
use Fhaculty\Graph\Graph;
4+
5+
use Fhaculty\Graph\Vertex;
6+
use Fhaculty\Graph\Loader\CompleteGraph as LoaderCompleteGraph;
7+
use Fhaculty\Graph\Algorithm\MinimumSpanningTree\Base as MstBase;
8+
9+
abstract class BaseMstTest extends TestCase
10+
{
11+
/**
12+
* @param Vertex $vertex
13+
* @return MstBase
14+
*/
15+
abstract protected function createAlg(Vertex $vertex);
16+
17+
public function testIsolatedVertex()
18+
{
19+
$graph = new Graph();
20+
$v1 = $graph->createVertex(1);
21+
22+
$alg = $this->createAlg($v1);
23+
24+
$this->assertCount(0, $alg->getEdges());
25+
$this->assertEquals(0, $alg->getWeight());
26+
27+
$graphMst = $alg->createGraph();
28+
$this->assertGraphEquals($graph, $graphMst);
29+
}
30+
31+
public function testSingleEdge()
32+
{
33+
// 1 --[3]-- 2
34+
$graph = new Graph();
35+
$v1 = $graph->createVertex(1);
36+
$v2 = $graph->createVertex(2);
37+
$v1->createEdge($v2)->setWeight(3);
38+
39+
$alg = $this->createAlg($v1);
40+
41+
$this->assertCount(1, $alg->getEdges());
42+
$this->assertEquals(3, $alg->getWeight());
43+
$this->assertGraphEquals($graph, $alg->createGraph());
44+
}
45+
46+
public function testSimpleGraph()
47+
{
48+
// 1 --[6]-- 2 --[9]-- 3 --[7]-- 4 --[8]-- 5
49+
$graph = new Graph();
50+
$v1 = $graph->createVertex(1);
51+
$v2 = $graph->createVertex(2);
52+
$v1->createEdge($v2)->setWeight(3);
53+
54+
$alg = $this->createAlg($v1);
55+
56+
$graphMst = $alg->createGraph();
57+
$this->assertGraphEquals($graph, $graphMst);
58+
}
59+
60+
public function testFindingCheapestEdge()
61+
{
62+
// /--[4]--\
63+
// / \
64+
// 1 ---[3]--- 2
65+
// \ /
66+
// \--[5]--/
67+
$graph = new Graph();
68+
$v1 = $graph->createVertex(1);
69+
$v2 = $graph->createVertex(2);
70+
$v1->createEdge($v2)->setWeight(4);
71+
$v1->createEdge($v2)->setWeight(3);
72+
$v1->createEdge($v2)->setWeight(5);
73+
74+
$alg = $this->createAlg($v1);
75+
$edges = $alg->getEdges();
76+
77+
$this->assertCount(1, $edges);
78+
$this->assertEquals(3, $edges->getEdgeFirst()->getWeight());
79+
$this->assertEquals(3, $alg->getWeight());
80+
}
81+
82+
public function testFindingCheapestTree()
83+
{
84+
// 1 --[4]-- 2 --[5]-- 3
85+
// \ /
86+
// \-------[6]-----/
87+
$graph = new Graph();
88+
$v1 = $graph->createVertex(1);
89+
$v2 = $graph->createVertex(2);
90+
$v3 = $graph->createVertex(3);
91+
$v1->createEdge($v2)->setWeight(4);
92+
$v2->createEdge($v3)->setWeight(5);
93+
$v3->createEdge($v1)->setWeight(6);
94+
95+
// 1 --[4]-- 2 -- [5] -- 3
96+
$graphExpected = new Graph();
97+
$ve1 = $graphExpected->createVertex(1);
98+
$ve2 = $graphExpected->createVertex(2);
99+
$ve3 = $graphExpected->createVertex(3);
100+
$ve1->createEdge($ve2)->setWeight(4);
101+
$ve2->createEdge($ve3)->setWeight(5);
102+
103+
$alg = $this->createAlg($v1);
104+
$this->assertCount(2, $alg->getEdges());
105+
$this->assertEquals(9, $alg->getWeight());
106+
$this->assertGraphEquals($graphExpected, $alg->createGraph());
107+
}
108+
109+
public function testMixedGraphDirectionIsIgnored()
110+
{
111+
// 1 --[6]-> 2 --[7]-- 3 --[8]-- 4 <-[9]-- 5
112+
$graph = new Graph();
113+
$v1 = $graph->createVertex(1);
114+
$v2 = $graph->createVertex(2);
115+
$v3 = $graph->createVertex(3);
116+
$v4 = $graph->createVertex(4);
117+
$v5 = $graph->createVertex(5);
118+
$v1->createEdgeTo($v2)->setWeight(6);
119+
$v2->createEdge($v3)->setWeight(7);
120+
$v4->createEdge($v3)->setWeight(8);
121+
$v5->createEdgeTo($v4)->setWeight(9);
122+
123+
$alg = $this->createAlg($v1);
124+
125+
$this->assertCount(4, $alg->getEdges());
126+
$this->assertEquals(30, $alg->getWeight());
127+
$this->assertGraphEquals($graph, $alg->createGraph());
128+
}
129+
130+
/**
131+
* @expectedException UnexpectedValueException
132+
*/
133+
public function testMultipleComponentsFail()
134+
{
135+
// 1 --[1]-- 2, 3 --[1]-- 4
136+
$graph = new Graph();
137+
$v1 = $graph->createVertex(1);
138+
$v2 = $graph->createVertex(2);
139+
$v3 = $graph->createVertex(3);
140+
$v4 = $graph->createVertex(4);
141+
$v1->createEdge($v2)->setWeight(1);
142+
$v3->createEdge($v4)->setWeight(1);
143+
144+
$alg = $this->createAlg($v1);
145+
$alg->getEdges();
146+
}
147+
148+
/**
149+
* @expectedException UnexpectedValueException
150+
*/
151+
public function testMultipleIsolatedVerticesFormMultipleComponentsFail()
152+
{
153+
// 1, 2
154+
$graph = new Graph();
155+
$v1 = $graph->createVertex(1);
156+
$v2 = $graph->createVertex(2);
157+
158+
$alg = $this->createAlg($v1);
159+
$alg->getEdges();
160+
}
161+
162+
163+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
use Fhaculty\Graph\Graph;
4+
use Fhaculty\Graph\Vertex;
5+
use Fhaculty\Graph\Algorithm\MinimumSpanningTree\Kruskal;
6+
7+
class KruskalTest extends BaseMstTest
8+
{
9+
protected function createAlg(Vertex $vertex)
10+
{
11+
return new Kruskal($vertex->getGraph());
12+
}
13+
14+
/**
15+
* @expectedException UnexpectedValueException
16+
*/
17+
public function testNullGraphIsNotConsideredToBeConnected()
18+
{
19+
$graph = new Graph();
20+
21+
$alg = new Kruskal($graph);
22+
$alg->getEdges();
23+
}
24+
}
Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
11
<?php
2-
use Fhaculty\Graph\Algorithm\MinimumSpanningTree\Prim as AlgorithmMSTPrim;
3-
use Fhaculty\Graph\Loader\CompleteGraph as LoaderCompleteGraph;
42

5-
class PrimTest extends PHPUnit_Framework_TestCase
6-
{
7-
public function testKnownComplete()
8-
{
9-
$this->assertCount(4, $this->getResultForComplete(5));
10-
}
3+
use Fhaculty\Graph\Vertex;
4+
use Fhaculty\Graph\Algorithm\MinimumSpanningTree\Prim;
115

12-
protected function getResultForComplete($n)
6+
class PrimTest extends BaseMstTest
7+
{
8+
protected function createAlg(Vertex $vertex)
139
{
14-
$loader = new LoaderCompleteGraph($n);
15-
$loader->setEnableDirectedEdges(false);
16-
$alg = new AlgorithmMSTPrim($loader->createGraph()->getVertex(1));
17-
18-
return $alg->getEdges();
10+
return new Prim($vertex);
1911
}
2012
}

0 commit comments

Comments
 (0)