Skip to content

Commit 179735c

Browse files
committed
Add tests with 100% coverage for Algorithm\Bipartit
1 parent 29ae882 commit 179735c

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

tests/BipartitTest.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
use Fhaculty\Graph\Algorithm\Bipartit as AlgorithmBipartit;
4+
use Fhaculty\Graph\Graph;
5+
6+
class BipartitTest extends TestCase
7+
{
8+
public function testGraphEmpty()
9+
{
10+
$graph = new Graph();
11+
12+
$alg = new AlgorithmBipartit($graph);
13+
14+
$this->assertTrue($alg->isBipartit());
15+
$this->assertEquals(array(), $alg->getColors());
16+
$this->assertEquals(array(0 => array(), 1 => array()), $alg->getColorVertices());
17+
}
18+
19+
public function testGraphPairIsBipartit()
20+
{
21+
// 1 -> 2
22+
$graph = new Graph();
23+
$v1 = $graph->createVertex(1);
24+
$v2 = $graph->createVertex(2);
25+
$v1->createEdgeTo($v2);
26+
27+
$alg = new AlgorithmBipartit($graph);
28+
29+
$this->assertTrue($alg->isBipartit());
30+
$this->assertEquals(array(1 => 0, 2 => 1), $alg->getColors());
31+
$this->assertEquals(array(0 => array(1 => $v1), 1 => array(2 => $v2)), $alg->getColorVertices());
32+
33+
return $alg;
34+
}
35+
36+
/**
37+
*
38+
* @param AlgorithmBipartit $alg
39+
* @depends testGraphPairIsBipartit
40+
*/
41+
public function testGraphPairBipartitGroups(AlgorithmBipartit $alg)
42+
{
43+
// graph does not have any groups assigned, so its groups are not bipartit
44+
$this->assertFalse($alg->isBipartitGroups());
45+
46+
// create a cloned graph with groups assigned according to bipartition
47+
$graph = $alg->createGraphGroups();
48+
49+
$this->assertInstanceOf('Fhaculty\Graph\Graph', $graph);
50+
51+
$alg2 = new AlgorithmBipartit($graph);
52+
$this->assertTrue($alg2->isBipartitGroups());
53+
}
54+
55+
public function testGraphTriangleCycleIsNotBipartit()
56+
{
57+
// 1 -> 2 --> 3 --> 1
58+
$graph = new Graph();
59+
$v1 = $graph->createVertex(1);
60+
$v2 = $graph->createVertex(2);
61+
$v3 = $graph->createVertex(3);
62+
$v1->createEdgeTo($v2);
63+
$v2->createEdgeTo($v3);
64+
$v3->createEdgeTo($v1);
65+
66+
$alg = new AlgorithmBipartit($graph);
67+
68+
$this->assertFalse($alg->isBipartit());
69+
70+
return $alg;
71+
}
72+
73+
/**
74+
*
75+
* @param AlgorithmBipartit $alg
76+
* @expectedException UnexpectedValueException
77+
* @depends testGraphTriangleCycleIsNotBipartit
78+
*/
79+
public function testGraphTriangleCycleColorsInvalid(AlgorithmBipartit $alg)
80+
{
81+
$alg->getColors();
82+
}
83+
84+
/**
85+
*
86+
* @param AlgorithmBipartit $alg
87+
* @expectedException UnexpectedValueException
88+
* @depends testGraphTriangleCycleIsNotBipartit
89+
*/
90+
public function testGraphTriangleCycleColorVerticesInvalid(AlgorithmBipartit $alg)
91+
{
92+
$alg->getColorVertices();
93+
}
94+
}

0 commit comments

Comments
 (0)