forked from graphp/graph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVertexTest.php
More file actions
192 lines (150 loc) · 6.69 KB
/
VertexTest.php
File metadata and controls
192 lines (150 loc) · 6.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
namespace Fhaculty\Graph\Tests;
use Fhaculty\Graph\Graph;
use Fhaculty\Graph\Tests\Attribute\AbstractAttributeAwareTest;
use Fhaculty\Graph\Vertex;
class VertexTest extends AbstractAttributeAwareTest
{
/** @var Graph */
private $graph;
/** @var Vertex */
private $vertex;
/**
* @before
*/
public function setUpVertex()
{
$this->graph = new Graph();
$this->vertex = $this->graph->createVertex(1);
}
public function testPrecondition()
{
$this->assertCount(1, $this->graph->getVertices());
$this->assertTrue($this->graph->hasVertex(1));
$this->assertFalse($this->graph->hasVertex(2));
$this->assertSame($this->vertex, $this->graph->getVertex(1));
}
public function testConstructor()
{
$v2 = new Vertex($this->graph, 2);
$this->assertCount(2, $this->graph->getVertices());
$this->assertTrue($this->graph->hasVertex(2));
$this->assertSame($v2, $this->graph->getVertex(2));
}
public function testCanNotConstructDuplicateVertex()
{
$this->setExpectedException('OverflowException');
$v2 = new Vertex($this->graph, 1);
}
public function testEdges()
{
// v1 -> v2, v1 -- v3, v1 <- v4
$v2 = $this->graph->createVertex(2);
$v3 = $this->graph->createVertex(3);
$v4 = $this->graph->createVertex(4);
$e1 = $this->vertex->createEdgeTo($v2);
$e2 = $this->vertex->createEdge($v3);
$e3 = $v4->createEdgeTo($this->vertex);
$this->assertEquals(array($e1, $e2, $e3), $this->vertex->getEdges()->getVector());
$this->assertEquals(array($e2, $e3), $this->vertex->getEdgesIn()->getVector());
$this->assertEquals(array($e1, $e2), $this->vertex->getEdgesOut()->getVector());
$this->assertTrue($this->vertex->hasEdgeTo($v2));
$this->assertTrue($this->vertex->hasEdgeTo($v3));
$this->assertFalse($this->vertex->hasEdgeTo($v4));
$this->assertFalse($this->vertex->hasEdgeFrom($v2));
$this->assertTrue($this->vertex->hasEdgeFrom($v3));
$this->assertTrue($this->vertex->hasEdgeFrom($v4));
$this->assertEquals(array($e1), $this->vertex->getEdgesTo($v2)->getVector());
$this->assertEquals(array($e2), $this->vertex->getEdgesTo($v3)->getVector());
$this->assertEquals(array(), $this->vertex->getEdgesTo($v4)->getVector());
$this->assertEquals(array(), $this->vertex->getEdgesFrom($v2)->getVector());
$this->assertEquals(array($e2), $this->vertex->getEdgesTo($v3)->getVector());
$this->assertEquals(array($e3), $this->vertex->getEdgesFrom($v4)->getVector());
$this->assertEquals(array($v2, $v3, $v4), $this->vertex->getVerticesEdge()->getVector());
$this->assertEquals(array($v2, $v3), $this->vertex->getVerticesEdgeTo()->getVector());
$this->assertEquals(array($v3, $v4), $this->vertex->getVerticesEdgeFrom()->getVector());
}
public function testUndirectedLoopEdgeReturnsEdgeTwiceInAndOut()
{
$edge = $this->vertex->createEdge($this->vertex);
$this->assertEquals(array($edge, $edge), $this->vertex->getEdges()->getVector());
$this->assertEquals(array($edge, $edge), $this->vertex->getEdgesIn()->getVector());
$this->assertEquals(array($edge, $edge), $this->vertex->getEdgesOut()->getVector());
$this->assertEquals(array($this->vertex, $this->vertex), $this->vertex->getVerticesEdge()->getVector());
$this->assertEquals(array($this->vertex, $this->vertex), $this->vertex->getVerticesEdgeTo()->getVector());
$this->assertEquals(array($this->vertex, $this->vertex), $this->vertex->getVerticesEdgeFrom()->getVector());
}
public function testDirectedLoopEdgeReturnsEdgeTwiceUndirectedAndOnceEachInAndOut()
{
$edge = $this->vertex->createEdgeTo($this->vertex);
$this->assertEquals(array($edge, $edge), $this->vertex->getEdges()->getVector());
$this->assertEquals(array($edge), $this->vertex->getEdgesIn()->getVector());
$this->assertEquals(array($edge), $this->vertex->getEdgesOut()->getVector());
$this->assertEquals(array($this->vertex, $this->vertex), $this->vertex->getVerticesEdge()->getVector());
$this->assertEquals(array($this->vertex), $this->vertex->getVerticesEdgeTo()->getVector());
$this->assertEquals(array($this->vertex), $this->vertex->getVerticesEdgeFrom()->getVector());
}
public function testBalance()
{
$this->vertex->setBalance(10);
$this->assertEquals(10, $this->vertex->getBalance());
}
public function testBalanceInvalid()
{
$this->setExpectedException('InvalidArgumentException');
$this->vertex->setBalance("10");
}
public function testGroup()
{
$this->vertex->setGroup(2);
$this->assertEquals(2, $this->vertex->getGroup());
}
public function testGroupInvalid()
{
$this->setExpectedException('InvalidArgumentException');
$this->vertex->setGroup("3");
}
public function testCreateEdgeOtherGraphFails()
{
$graphOther = new Graph();
$this->setExpectedException('InvalidArgumentException');
$this->vertex->createEdge($graphOther->createVertex(2));
}
public function testCreateEdgeToOtherGraphFails()
{
$graphOther = new Graph();
$this->setExpectedException('InvalidArgumentException');
$this->vertex->createEdgeTo($graphOther->createVertex(2));
}
public function testRemoveInvalidEdge()
{
// 2 -- 3
$v2 = $this->graph->createVertex(2);
$v3 = $this->graph->createVertex(3);
$edge = $v2->createEdge($v3);
$this->setExpectedException('InvalidArgumentException');
$this->vertex->removeEdge($edge);
}
public function testRemoveWithEdgeLoopUndirected()
{
// 1 -- 1
$edge = $this->vertex->createEdge($this->vertex);
$this->assertEquals(array(1 => $this->vertex), $this->graph->getVertices()->getMap());
$this->vertex->destroy();
$this->assertEquals(array(), $this->graph->getVertices()->getVector());
$this->assertEquals(array(), $this->graph->getEdges()->getVector());
}
public function testRemoveWithEdgeLoopDirected()
{
// 1 --> 1
$edge = $this->vertex->createEdgeTo($this->vertex);
$this->assertEquals(array(1 => $this->vertex), $this->graph->getVertices()->getMap());
$this->vertex->destroy();
$this->assertEquals(array(), $this->graph->getVertices()->getVector());
$this->assertEquals(array(), $this->graph->getEdges()->getVector());
}
protected function createAttributeAware()
{
return new Vertex(new Graph(), 1);
}
}