|
| 1 | +<?php |
| 2 | + |
| 3 | +use Fhaculty\Graph\Algorithm\Tree\BaseDirected; |
| 4 | +use Fhaculty\Graph\Exception\UnderflowException; |
| 5 | +use Fhaculty\Graph\Exception\UnexpectedValueException; |
| 6 | +use Fhaculty\Graph\Graph; |
| 7 | + |
| 8 | +abstract class BaseDirectedTest extends TestCase |
| 9 | +{ |
| 10 | + abstract protected function createTreeAlg(Graph $graph); |
| 11 | + |
| 12 | + abstract protected function createGraphNonTree(); |
| 13 | + |
| 14 | + abstract protected function createGraphTree(); |
| 15 | + |
| 16 | + public function testEmptyGraph() |
| 17 | + { |
| 18 | + $graph = new Graph(); |
| 19 | + |
| 20 | + $tree = $this->createTreeAlg($graph); |
| 21 | + $this->assertTrue($tree->isTree()); |
| 22 | + $this->assertEquals(array(), $tree->getVerticesLeaf()); |
| 23 | + $this->assertEquals(array(), $tree->getVerticesInternal()); |
| 24 | + |
| 25 | + return $tree; |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * @param BaseDirected $tree |
| 30 | + * @depends testEmptyGraph |
| 31 | + * @expectedException UnderflowException |
| 32 | + */ |
| 33 | + public function testEmptyGraphDoesNotHaveRootVertex(BaseDirected $tree) |
| 34 | + { |
| 35 | + $tree->getVertexRoot(); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @param BaseDirected $tree |
| 40 | + * @depends testEmptyGraph |
| 41 | + * @expectedException UnderflowException |
| 42 | + */ |
| 43 | + public function testEmptyGraphDoesNotHaveDegree(BaseDirected $tree) |
| 44 | + { |
| 45 | + $tree->getDegree(); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @param BaseDirected $tree |
| 50 | + * @depends testEmptyGraph |
| 51 | + * @expectedException UnderflowException |
| 52 | + */ |
| 53 | + public function testEmptyGraphDoesNotHaveHeight(BaseDirected $tree) |
| 54 | + { |
| 55 | + $tree->getHeight(); |
| 56 | + } |
| 57 | + |
| 58 | + public function testGraphTree() |
| 59 | + { |
| 60 | + $graph = $this->createGraphTree(); |
| 61 | + $root = $graph->getVertexFirst(); |
| 62 | + |
| 63 | + $nonRoot = $graph->getVertices(); |
| 64 | + unset($nonRoot[$root->getId()]); |
| 65 | + |
| 66 | + $c1 = current($nonRoot); |
| 67 | + |
| 68 | + $tree = $this->createTreeAlg($graph); |
| 69 | + |
| 70 | + $this->assertTrue($tree->isTree()); |
| 71 | + $this->assertSame($root, $tree->getVertexRoot()); |
| 72 | + $this->assertSame(array_values($nonRoot), array_values($tree->getVerticesChildren($root))); |
| 73 | + $this->assertSame(array_values($nonRoot), array_values($tree->getVerticesLeaf())); |
| 74 | + $this->assertSame(array(), array_values($tree->getVerticesInternal())); |
| 75 | + $this->assertSame($root, $tree->getVertexParent($c1)); |
| 76 | + $this->assertSame(array(), $tree->getVerticesChildren($c1)); |
| 77 | + $this->assertEquals(2, $tree->getDegree()); |
| 78 | + $this->assertEquals(0, $tree->getDepthVertex($root)); |
| 79 | + $this->assertEquals(1, $tree->getDepthVertex($c1)); |
| 80 | + $this->assertEquals(1, $tree->getHeight()); |
| 81 | + $this->assertEquals(1, $tree->getHeightVertex($root)); |
| 82 | + $this->assertEquals(0, $tree->getHeightvertex($c1)); |
| 83 | + |
| 84 | + return $tree; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * |
| 89 | + * @param BaseDirected $tree |
| 90 | + * @depends testGraphTree |
| 91 | + * @expectedException UnderflowException |
| 92 | + */ |
| 93 | + public function testGraphTreeRootDoesNotHaveParent(BaseDirected $tree) |
| 94 | + { |
| 95 | + $root = $tree->getVertexRoot(); |
| 96 | + $tree->getVertexParent($root); |
| 97 | + } |
| 98 | + |
| 99 | + public function testNonTree() |
| 100 | + { |
| 101 | + $graph = $this->createGraphNonTree(); |
| 102 | + |
| 103 | + $tree = $this->createTreeAlg($graph); |
| 104 | + |
| 105 | + $this->assertFalse($tree->isTree()); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @expectedException UnexpectedValueException |
| 110 | + */ |
| 111 | + public function testNonTreeVertexHasMoreThanOneParent() |
| 112 | + { |
| 113 | + $graph = $this->createGraphNonTree(); |
| 114 | + |
| 115 | + $tree = $this->createTreeAlg($graph); |
| 116 | + |
| 117 | + $tree->getVertexParent($graph->getVertex('v3')); |
| 118 | + } |
| 119 | +} |
0 commit comments