|
| 1 | +<?php |
| 2 | + |
| 3 | +use Fhaculty\Graph\Graph; |
| 4 | + |
| 5 | +use Fhaculty\Graph\Algorithm\Tree\Undirected; |
| 6 | + |
| 7 | +class UndirectedTest extends TestCase |
| 8 | +{ |
| 9 | + protected function createTree(Graph $graph) |
| 10 | + { |
| 11 | + return new Undirected($graph); |
| 12 | + } |
| 13 | + |
| 14 | + public function testGraphEmpty() |
| 15 | + { |
| 16 | + $graph = new Graph(); |
| 17 | + |
| 18 | + $tree = $this->createTree($graph); |
| 19 | + |
| 20 | + $this->assertTrue($tree->isTree()); |
| 21 | + $this->assertSame(array(), $tree->getVerticesInternal()); |
| 22 | + $this->assertSame(array(), $tree->getVerticesLeaf()); |
| 23 | + } |
| 24 | + |
| 25 | + public function testGraphTrivial() |
| 26 | + { |
| 27 | + $graph = new Graph(); |
| 28 | + $graph->createVertex('v1'); |
| 29 | + |
| 30 | + $tree = $this->createTree($graph); |
| 31 | + $this->assertTrue($tree->isTree()); |
| 32 | + $this->assertSame(array(), $tree->getVerticesInternal()); |
| 33 | + $this->assertSame(array(), $tree->getVerticesLeaf()); |
| 34 | + } |
| 35 | + |
| 36 | + public function testGraphSimplePair() |
| 37 | + { |
| 38 | + // v1 -- v2 |
| 39 | + $graph = new Graph(); |
| 40 | + $graph->createVertex('v1')->createEdge($graph->createVertex('v2')); |
| 41 | + |
| 42 | + $tree = $this->createTree($graph); |
| 43 | + $this->assertTrue($tree->isTree()); |
| 44 | + $this->assertSame(array(), $tree->getVerticesInternal()); |
| 45 | + $this->assertSame($graph->getVertices(), $tree->getVerticesLeaf()); |
| 46 | + } |
| 47 | + |
| 48 | + public function testGraphSimpleLine() |
| 49 | + { |
| 50 | + // v1 -- v2 -- v3 |
| 51 | + $graph = new Graph(); |
| 52 | + $graph->createVertex('v1')->createEdge($graph->createVertex('v2')); |
| 53 | + $graph->getVertex('v2')->createEdge($graph->createVertex('v3')); |
| 54 | + |
| 55 | + $tree = $this->createTree($graph); |
| 56 | + $this->assertTrue($tree->isTree()); |
| 57 | + $this->assertSame(array($graph->getVertex('v2')), array_values($tree->getVerticesInternal())); |
| 58 | + $this->assertSame(array($graph->getVertex('v1'), $graph->getVertex('v3')), array_values($tree->getVerticesLeaf())); |
| 59 | + } |
| 60 | + |
| 61 | + public function testGraphPairParallelIsNotTree() |
| 62 | + { |
| 63 | + // v1 -- v2 -- v1 |
| 64 | + $graph = new Graph(); |
| 65 | + $graph->createVertex('v1')->createEdge($graph->createVertex('v2')); |
| 66 | + $graph->getVertex('v1')->createEdge($graph->getVertex('v2')); |
| 67 | + |
| 68 | + $tree = $this->createTree($graph); |
| 69 | + $this->assertFalse($tree->isTree()); |
| 70 | + } |
| 71 | + |
| 72 | + public function testGraphLoopIsNotTree() |
| 73 | + { |
| 74 | + // v1 -- v1 |
| 75 | + $graph = new Graph(); |
| 76 | + $graph->createVertex('v1')->createEdge($graph->getVertex('v1')); |
| 77 | + |
| 78 | + $tree = $this->createTree($graph); |
| 79 | + $this->assertFalse($tree->isTree()); |
| 80 | + } |
| 81 | + |
| 82 | + public function testGraphCycleIsNotTree() |
| 83 | + { |
| 84 | + // v1 -- v2 -- v3 -- v1 |
| 85 | + $graph = new Graph(); |
| 86 | + $graph->createVertex('v1')->createEdge($graph->createVertex('v2')); |
| 87 | + $graph->getVertex('v2')->createEdge($graph->createVertex('v3')); |
| 88 | + $graph->getVertex('v3')->createEdge($graph->getVertex('v1')); |
| 89 | + |
| 90 | + $tree = $this->createTree($graph); |
| 91 | + $this->assertFalse($tree->isTree()); |
| 92 | + } |
| 93 | + |
| 94 | + public function testGraphDirectedIsNotTree() |
| 95 | + { |
| 96 | + // v1 -> v2 |
| 97 | + $graph = new Graph(); |
| 98 | + $graph->createVertex('v1')->createEdgeTo($graph->createVertex('v2')); |
| 99 | + |
| 100 | + $tree = $this->createTree($graph); |
| 101 | + $this->assertFalse($tree->isTree()); |
| 102 | + } |
| 103 | + |
| 104 | + public function testGraphMixedIsNotTree() |
| 105 | + { |
| 106 | + // v1 -- v2 -> v3 |
| 107 | + $graph = new Graph(); |
| 108 | + $graph->createVertex('v1')->createEdge($graph->createVertex('v2')); |
| 109 | + $graph->getVertex('v2')->createEdgeTo($graph->createVertex('v3')); |
| 110 | + |
| 111 | + $tree = $this->createTree($graph); |
| 112 | + $this->assertFalse($tree->isTree()); |
| 113 | + } |
| 114 | +} |
0 commit comments