Skip to content

Commit b7ac2b8

Browse files
author
Christian Lück
committed
Add tests for connected components (100% coverage)
1 parent e6458fa commit b7ac2b8

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

tests/ConnectedComponentsTest.php

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
use Fhaculty\Graph\Algorithm\ConnectedComponents as AlgorithmConnected;
4+
use Fhaculty\Graph\Graph;
5+
6+
class ConnectedComponentsTest extends TestCase
7+
{
8+
public function testNullGraph()
9+
{
10+
$graph = new Graph();
11+
12+
$alg = new AlgorithmConnected($graph);
13+
14+
$this->assertEquals(0, $alg->getNumberOfComponents());
15+
$this->assertFalse($alg->isSingle());
16+
$this->assertCount(0, $alg->createGraphsComponents());
17+
}
18+
19+
public function testGraphSingleTrivial()
20+
{
21+
$graph = new Graph();
22+
$graph->createVertex(1);
23+
24+
$alg = new AlgorithmConnected($graph);
25+
26+
$this->assertEquals(1, $alg->getNumberOfComponents());
27+
$this->assertTrue($alg->isSingle());
28+
29+
$graphs = $alg->createGraphsComponents();
30+
31+
$this->assertCount(1, $graphs);
32+
$this->assertGraphEquals($graph, reset($graphs));
33+
}
34+
35+
public function testGraphEdgeDirections()
36+
{
37+
// 1 -- 2 -> 3 <- 4
38+
$graph = new Graph();
39+
$graph->createVertex(1)->createEdge($graph->createVertex(2));
40+
$graph->getVertex(2)->createEdgeTo($graph->createVertex(3));
41+
$graph->createVertex(4)->createEdgeTo($graph->getVertex(3));
42+
43+
$alg = new AlgorithmConnected($graph);
44+
45+
$this->assertEquals(1, $alg->getNumberOfComponents());
46+
$this->assertTrue($alg->isSingle());
47+
48+
$graphs = $alg->createGraphsComponents();
49+
50+
$this->assertCount(1, $graphs);
51+
$this->assertGraphEquals($graph, reset($graphs));
52+
$this->assertGraphEquals($graph, $alg->createGraphComponentVertex($graph->getVertex(1)));
53+
}
54+
55+
public function testComponents()
56+
{
57+
// 1 -- 2, 3 -> 4, 5
58+
$graph = new Graph();
59+
$v1 = $graph->createVertex(1);
60+
$v2 = $graph->createVertex(2);
61+
$v3 = $graph->createVertex(3);
62+
$v4 = $graph->createVertex(4);
63+
$v5 = $graph->createVertex(5);
64+
$e1 = $v1->createEdge($v2);
65+
$e2 = $v3->createEdgeTo($v4);
66+
67+
$alg = new AlgorithmConnected($graph);
68+
69+
$this->assertEquals(3, $alg->getNumberOfComponents());
70+
$this->assertFalse($alg->isSingle());
71+
72+
$graphs = $alg->createGraphsComponents();
73+
$this->assertCount(3, $graphs);
74+
75+
$ge = new Graph();
76+
$ge->createVertex(1)->createEdge($ge->createVertex(2));
77+
$this->assertGraphEquals($ge, $alg->createGraphComponentVertex($v2));
78+
79+
$ge = new Graph();
80+
$ge->createVertex(5);
81+
$this->assertEquals($ge, $alg->createGraphComponentVertex($v5));
82+
}
83+
84+
/**
85+
* @expectedException InvalidArgumentException
86+
*/
87+
public function testInvalidVertexPassedToAlgorithm()
88+
{
89+
$graph = new Graph();
90+
91+
$graph2 = new Graph();
92+
$v2 = $graph2->createVertex(12);
93+
94+
$alg = new AlgorithmConnected($graph);
95+
$alg->createGraphComponentVertex($v2);
96+
}
97+
}

0 commit comments

Comments
 (0)