|
| 1 | +<?php |
| 2 | + |
| 3 | +use Fhaculty\Graph\Algorithm\Groups as AlgorithmGroups; |
| 4 | +use Fhaculty\Graph\Graph; |
| 5 | + |
| 6 | +class GroupsTest extends TestCase |
| 7 | +{ |
| 8 | + public function testGraphEmpty() |
| 9 | + { |
| 10 | + $graph = new Graph(); |
| 11 | + |
| 12 | + $alg = new AlgorithmGroups($graph); |
| 13 | + |
| 14 | + $this->assertEquals(array(), $alg->getGroups()); |
| 15 | + $this->assertEquals(0, $alg->getNumberOfGroups()); |
| 16 | + |
| 17 | + $this->assertEquals(array(), $alg->getVerticesGroup(123)); |
| 18 | + |
| 19 | + $this->assertFalse($alg->isBipartit()); |
| 20 | + } |
| 21 | + |
| 22 | + public function testGraphPairIsBipartit() |
| 23 | + { |
| 24 | + // 1 -> 2 |
| 25 | + $graph = new Graph(); |
| 26 | + $v1 = $graph->createVertex(1)->setGroup(1); |
| 27 | + $v2 = $graph->createVertex(2)->setGroup(2); |
| 28 | + $v1->createEdgeTo($v2); |
| 29 | + |
| 30 | + $alg = new AlgorithmGroups($graph); |
| 31 | + |
| 32 | + $this->assertEquals(array(1, 2), $alg->getGroups()); |
| 33 | + $this->assertEquals(2, $alg->getNumberOfGroups()); |
| 34 | + |
| 35 | + $this->assertEquals(array(), $alg->getVerticesGroup(123)); |
| 36 | + $this->assertEquals(array(1 => $v1), $alg->getVerticesGroup(1)); |
| 37 | + |
| 38 | + $this->assertTrue($alg->isBipartit()); |
| 39 | + } |
| 40 | + |
| 41 | + public function testGraphTriangleCycleIsNotBipartit() |
| 42 | + { |
| 43 | + // 1 -> 2 -> 3 -> 1 |
| 44 | + $graph = new Graph(); |
| 45 | + $v1 = $graph->createVertex(1)->setGroup(1); |
| 46 | + $v2 = $graph->createVertex(2)->setGroup(2); |
| 47 | + $v3 = $graph->createVertex(3)->setGroup(1); |
| 48 | + $v1->createEdgeTo($v2); |
| 49 | + $v2->createEdgeTo($v3); |
| 50 | + $v3->createEdgeTo($v1); |
| 51 | + |
| 52 | + $alg = new AlgorithmGroups($graph); |
| 53 | + |
| 54 | + $this->assertEquals(array(1, 2), $alg->getGroups()); |
| 55 | + $this->assertEquals(2, $alg->getNumberOfGroups()); |
| 56 | + |
| 57 | + $this->assertEquals(array(), $alg->getVerticesGroup(123)); |
| 58 | + $this->assertEquals(array(1 => $v1, 3 => $v3), $alg->getVerticesGroup(1)); |
| 59 | + |
| 60 | + $this->assertFalse($alg->isBipartit()); |
| 61 | + } |
| 62 | +} |
0 commit comments