GroupsTest.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. use Graphp\Algorithms\Groups as AlgorithmGroups;
  3. use Fhaculty\Graph\Graph;
  4. class GroupsTest extends TestCase
  5. {
  6. public function testGraphEmpty()
  7. {
  8. $graph = new Graph();
  9. $alg = new AlgorithmGroups($graph);
  10. $this->assertEquals(array(), $alg->getGroups());
  11. $this->assertEquals(0, $alg->getNumberOfGroups());
  12. $this->assertTrue($alg->getVerticesGroup(123)->isEmpty());
  13. $this->assertFalse($alg->isBipartit());
  14. }
  15. public function testGraphPairIsBipartit()
  16. {
  17. // 1 -> 2
  18. $graph = new Graph();
  19. $v1 = $graph->createVertex(1)->setGroup(1);
  20. $v2 = $graph->createVertex(2)->setGroup(2);
  21. $v1->createEdgeTo($v2);
  22. $alg = new AlgorithmGroups($graph);
  23. $this->assertEquals(array(1, 2), $alg->getGroups());
  24. $this->assertEquals(2, $alg->getNumberOfGroups());
  25. $this->assertTrue($alg->getVerticesGroup(123)->isEmpty());
  26. $this->assertEquals(array(1 => $v1), $alg->getVerticesGroup(1)->getMap());
  27. $this->assertTrue($alg->isBipartit());
  28. }
  29. public function testGraphTriangleCycleIsNotBipartit()
  30. {
  31. // 1 -> 2 -> 3 -> 1
  32. $graph = new Graph();
  33. $v1 = $graph->createVertex(1)->setGroup(1);
  34. $v2 = $graph->createVertex(2)->setGroup(2);
  35. $v3 = $graph->createVertex(3)->setGroup(1);
  36. $v1->createEdgeTo($v2);
  37. $v2->createEdgeTo($v3);
  38. $v3->createEdgeTo($v1);
  39. $alg = new AlgorithmGroups($graph);
  40. $this->assertEquals(array(1, 2), $alg->getGroups());
  41. $this->assertEquals(2, $alg->getNumberOfGroups());
  42. $this->assertTrue($alg->getVerticesGroup(123)->isEmpty());
  43. $this->assertEquals(array(1 => $v1, 3 => $v3), $alg->getVerticesGroup(1)->getMap());
  44. $this->assertFalse($alg->isBipartit());
  45. }
  46. }