|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Test; |
|
4
|
|
|
|
|
5
|
|
|
use Hal\Component\Tree\Graph; |
|
6
|
|
|
use Hal\Component\Tree\GraphFactory; |
|
7
|
|
|
use Hal\Component\Tree\Node; |
|
8
|
|
|
use Hal\Component\Tree\Operator\SizeOfTree; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @group tree |
|
12
|
|
|
*/ |
|
13
|
|
|
class SizeOfTreeTest extends \PHPUnit\Framework\TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @expectedException LogicException |
|
18
|
|
|
* @expectedExceptionMessage Cannot get size informations of cyclic graph |
|
19
|
|
|
*/ |
|
20
|
|
|
public function testICannotGetInfoAboutGraphWhenItIsCyclic() |
|
21
|
|
|
{ |
|
22
|
|
|
$graph = new Graph(); |
|
23
|
|
|
$a = new Node('A'); |
|
24
|
|
|
$b = new Node('B'); |
|
25
|
|
|
|
|
26
|
|
|
$graph->insert($a)->insert($b); |
|
27
|
|
|
|
|
28
|
|
|
// case 1 |
|
29
|
|
|
$graph->addEdge($a, $b); // A -> B |
|
30
|
|
|
$graph->addEdge($b, $a); // B -> C |
|
31
|
|
|
|
|
32
|
|
|
$size = new SizeOfTree($graph); |
|
|
|
|
|
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
View Code Duplication |
public function testICanGetDepthOfNode() |
|
|
|
|
|
|
36
|
|
|
{ |
|
37
|
|
|
$graph = new Graph(); |
|
38
|
|
|
$a = new Node('A'); |
|
39
|
|
|
$b = new Node('B'); |
|
40
|
|
|
$c = new Node('C'); |
|
41
|
|
|
$d = new Node('D'); |
|
42
|
|
|
$e = new Node('E'); |
|
43
|
|
|
|
|
44
|
|
|
$graph->insert($a)->insert($b)->insert($c)->insert($d)->insert($e); |
|
45
|
|
|
|
|
46
|
|
|
// case 1 |
|
47
|
|
|
$graph->addEdge($a, $b); // A -> B |
|
48
|
|
|
$graph->addEdge($b, $c); // B -> C |
|
49
|
|
|
$graph->addEdge($c, $d); // C -> D |
|
50
|
|
|
$graph->addEdge($a, $e); // A -> E (node with multiple childs) |
|
51
|
|
|
|
|
52
|
|
|
$size = new SizeOfTree($graph); |
|
53
|
|
|
|
|
54
|
|
|
$this->assertEquals(0, $size->getDepthOfNode($a)); |
|
55
|
|
|
$this->assertEquals(1, $size->getDepthOfNode($b)); |
|
56
|
|
|
$this->assertEquals(2, $size->getDepthOfNode($c)); |
|
57
|
|
|
$this->assertEquals(3, $size->getDepthOfNode($d)); |
|
58
|
|
|
$this->assertEquals(1, $size->getDepthOfNode($e)); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
View Code Duplication |
public function testICanGetNbChildsOfNode() |
|
|
|
|
|
|
62
|
|
|
{ |
|
63
|
|
|
$graph = new Graph(); |
|
64
|
|
|
$a = new Node('A'); |
|
65
|
|
|
$b = new Node('B'); |
|
66
|
|
|
$c = new Node('C'); |
|
67
|
|
|
$d = new Node('D'); |
|
68
|
|
|
$e = new Node('E'); |
|
69
|
|
|
|
|
70
|
|
|
$graph->insert($a)->insert($b)->insert($c)->insert($d)->insert($e); |
|
71
|
|
|
|
|
72
|
|
|
// case 1 |
|
73
|
|
|
$graph->addEdge($a, $b); // A -> B |
|
74
|
|
|
$graph->addEdge($b, $c); // B -> C |
|
75
|
|
|
$graph->addEdge($c, $d); // C -> D |
|
76
|
|
|
$graph->addEdge($a, $e); // A -> E (node with multiple childs) |
|
77
|
|
|
|
|
78
|
|
|
$size = new SizeOfTree($graph); |
|
79
|
|
|
|
|
80
|
|
|
$this->assertEquals(4, $size->getNumberOfChilds($a)); |
|
81
|
|
|
$this->assertEquals(2, $size->getNumberOfChilds($b)); |
|
82
|
|
|
$this->assertEquals(1, $size->getNumberOfChilds($c)); |
|
83
|
|
|
$this->assertEquals(0, $size->getNumberOfChilds($d)); |
|
84
|
|
|
$this->assertEquals(0, $size->getNumberOfChilds($e)); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function testICanGetInfoAboutAverageHeightOfTree() |
|
88
|
|
|
{ |
|
89
|
|
|
$graph = new Graph(); |
|
90
|
|
|
$a = new Node('A'); |
|
91
|
|
|
$b = new Node('B'); |
|
92
|
|
|
$c = new Node('C'); |
|
93
|
|
|
$d = new Node('D'); |
|
94
|
|
|
$e = new Node('E'); |
|
95
|
|
|
$f = new Node('F'); |
|
96
|
|
|
$g = new Node('G'); |
|
97
|
|
|
$x = new Node('X'); |
|
98
|
|
|
$y = new Node('Y'); |
|
99
|
|
|
$z = new Node('Z'); |
|
100
|
|
|
|
|
101
|
|
|
$graph->insert($a)->insert($b)->insert($c)->insert($d)->insert($e)->insert($f)->insert($g); |
|
102
|
|
|
$graph->insert($x)->insert($y)->insert($z); |
|
103
|
|
|
|
|
104
|
|
|
// case 1 |
|
105
|
|
|
$graph->addEdge($a, $b); // A -> B |
|
106
|
|
|
$graph->addEdge($b, $c); // B -> C |
|
107
|
|
|
$graph->addEdge($c, $d); // C -> D |
|
108
|
|
|
$graph->addEdge($c, $f); // C -> F |
|
109
|
|
|
$graph->addEdge($f, $g); // F -> G |
|
110
|
|
|
$graph->addEdge($a, $e); // A -> E |
|
111
|
|
|
$graph->addEdge($x, $y); // X -> Y |
|
112
|
|
|
|
|
113
|
|
|
// longest branch A = 5 A -> B -> C -> F -> G |
|
114
|
|
|
// longest branch X = 2 X -> Y |
|
115
|
|
|
// longest branch Z = 1 Z |
|
116
|
|
|
|
|
117
|
|
|
$size = new SizeOfTree($graph); |
|
118
|
|
|
|
|
119
|
|
|
$this->assertEquals(5, $size->getLongestBranch($a)); |
|
120
|
|
|
$this->assertEquals(2, $size->getLongestBranch($x)); |
|
121
|
|
|
$this->assertEquals(1, $size->getLongestBranch($z)); |
|
122
|
|
|
|
|
123
|
|
|
$this->assertEquals(2.67, $size->getAverageHeightOfGraph()); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.