LoopTest.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. use Graphp\Algorithms\Loop as AlgorithmLoop;
  3. use Fhaculty\Graph\Graph;
  4. class LoopTest extends TestCase
  5. {
  6. public function testGraphEmpty()
  7. {
  8. $graph = new Graph();
  9. $alg = new AlgorithmLoop($graph);
  10. $this->assertFalse($alg->hasLoop());
  11. }
  12. public function testGraphWithMixedCircuitIsNotConsideredLoop()
  13. {
  14. // 1 -> 2
  15. // 2 -- 1
  16. $graph = new Graph();
  17. $v1 = $graph->createVertex(1);
  18. $v2 = $graph->createVertex(2);
  19. $v1->createEdgeTo($v2);
  20. $v2->createEdge($v1);
  21. $alg = new AlgorithmLoop($graph);
  22. $this->assertFalse($alg->hasLoop());
  23. $this->assertFalse($alg->hasLoopVertex($v1));
  24. $this->assertFalse($alg->hasLoopVertex($v2));
  25. }
  26. public function testGraphUndirectedLoop()
  27. {
  28. // 1 -- 1
  29. $graph = new Graph();
  30. $graph->createVertex(1)->createEdge($v1 = $graph->getVertex(1));
  31. $alg = new AlgorithmLoop($graph);
  32. $this->assertTrue($alg->hasLoop());
  33. $this->assertTrue($alg->hasLoopVertex($v1));
  34. }
  35. public function testGraphDirectedLoop()
  36. {
  37. // 1 -> 1
  38. $graph = new Graph();
  39. $graph->createVertex(1)->createEdgeTo($v1 = $graph->getVertex(1));
  40. $alg = new AlgorithmLoop($graph);
  41. $this->assertTrue($alg->hasLoop());
  42. $this->assertTrue($alg->hasLoopVertex($v1));
  43. }
  44. }