Skip to content

Commit 181ce00

Browse files
committed
Add tests with 100% coverage for WalkProperty algorithm
1 parent 2e5a1b4 commit 181ce00

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed

tests/Property/WalkPropertyTest.php

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
<?php
2+
3+
use Fhaculty\Graph\Walk;
4+
5+
use Fhaculty\Graph\Graph;
6+
use Fhaculty\Graph\Algorithm\Property\WalkProperty;
7+
8+
class WalkPropertyTest extends TestCase
9+
{
10+
public function testTrivialGraph()
11+
{
12+
$graph = new Graph();
13+
$v1 = $graph->createVertex(1);
14+
15+
$walk = Walk::factoryFromEdges(array(), $v1);
16+
17+
$this->assertEquals(1, $walk->getNumberOfVertices());
18+
$this->assertEquals(0, $walk->getNumberOfEdges());
19+
20+
$alg = new WalkProperty($walk);
21+
22+
$this->assertFalse($alg->isLoop());
23+
$this->assertFalse($alg->hasLoop());
24+
25+
$this->assertFalse($alg->isCycle());
26+
$this->assertFalse($alg->hasCycle());
27+
28+
$this->assertTrue($alg->isPath());
29+
$this->assertTrue($alg->isSimple());
30+
31+
$this->assertTrue($alg->isEulerian());
32+
$this->assertTrue($alg->isHamiltonian());
33+
}
34+
35+
public function testLoop()
36+
{
37+
// 1 -- 1
38+
$graph = new Graph();
39+
$v1 = $graph->createVertex(1);
40+
$e1 = $v1->createEdge($v1);
41+
42+
$walk = Walk::factoryFromEdges(array($e1), $v1);
43+
44+
$alg = new WalkProperty($walk);
45+
46+
$this->assertTrue($alg->isLoop());
47+
$this->assertTrue($alg->hasLoop());
48+
49+
$this->assertTrue($alg->isCycle());
50+
$this->assertTrue($alg->hasCycle());
51+
52+
$this->assertTrue($alg->isPath());
53+
$this->assertTrue($alg->isSimple());
54+
55+
$this->assertTrue($alg->isEulerian());
56+
$this->assertTrue($alg->isHamiltonian());
57+
}
58+
59+
public function testCycle()
60+
{
61+
// 1 -- 2 -- 1
62+
$graph = new Graph();
63+
$v1 = $graph->createVertex(1);
64+
$v2 = $graph->createVertex(2);
65+
$e1 = $v1->createEdge($v2);
66+
$e2 = $v2->createEdge($v1);
67+
68+
$walk = Walk::factoryFromEdges(array($e1, $e2), $v1);
69+
70+
$this->assertEquals(3, $walk->getNumberOfVertices());
71+
$this->assertEquals(2, $walk->getNumberOfEdges());
72+
73+
$alg = new WalkProperty($walk);
74+
75+
$this->assertTrue($alg->isCycle());
76+
$this->assertTrue($alg->hasCycle());
77+
$this->assertTrue($alg->isPath());
78+
$this->assertTrue($alg->isSimple());
79+
80+
$this->assertTrue($alg->isEulerian());
81+
$this->assertTrue($alg->isHamiltonian());
82+
}
83+
84+
public function testDigon()
85+
{
86+
// 1 -> 2 -> 1
87+
$graph = new Graph();
88+
$v1 = $graph->createVertex(1);
89+
$v2 = $graph->createVertex(2);
90+
$e1 = $v1->createEdgeTo($v2);
91+
$e2 = $v2->createEdgeTo($v1);
92+
93+
$walk = Walk::factoryFromEdges(array($e1, $e2), $v1);
94+
95+
$alg = new WalkProperty($walk);
96+
97+
$this->assertTrue($alg->isDigon());
98+
}
99+
100+
public function testTriangle()
101+
{
102+
// 1 -> 2 -> 3 -> 1
103+
$graph = new Graph();
104+
$v1 = $graph->createVertex(1);
105+
$v2 = $graph->createVertex(2);
106+
$v3 = $graph->createVertex(3);
107+
$e1 = $v1->createEdgeTo($v2);
108+
$e2 = $v2->createEdgeTo($v3);
109+
$e3 = $v3->createEdgeTo($v1);
110+
111+
$walk = Walk::factoryFromEdges(array($e1, $e2, $e3), $v1);
112+
113+
$alg = new WalkProperty($walk);
114+
115+
$this->assertTrue($alg->isTriangle());
116+
}
117+
118+
public function testSimplePathWithinGraph()
119+
{
120+
// 1 -- 2 -- 2
121+
$graph = new Graph();
122+
$v1 = $graph->createVertex(1);
123+
$v2 = $graph->createVertex(2);
124+
$e1 = $v1->createEdge($v2);
125+
$e2 = $v2->createEdge($v2);
126+
127+
// only use "2 -- 2" part
128+
$walk = Walk::factoryFromEdges(array($e2), $v2);
129+
130+
$this->assertEquals(2, $walk->getNumberOfVertices());
131+
$this->assertEquals(1, $walk->getNumberOfEdges());
132+
133+
$alg = new WalkProperty($walk);
134+
135+
$this->assertTrue($alg->isCycle());
136+
$this->assertTrue($alg->hasCycle());
137+
$this->assertTrue($alg->isPath());
138+
$this->assertTrue($alg->isSimple());
139+
140+
$this->assertFalse($alg->isEulerian());
141+
$this->assertFalse($alg->isHamiltonian());
142+
}
143+
}

0 commit comments

Comments
 (0)