ConnectedComponentTest::testOneComponent()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
c 0
b 0
f 0
dl 0
loc 18
rs 9.9
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Fisharebest\Tests\Algorithm;
4
5
use Fisharebest\Algorithm\ConnectedComponent;
6
7
/**
8
 * @author    Greg Roach <[email protected]>
9
 * @copyright (c) 2021 Greg Roach <[email protected]>
10
 * @license   GPL-3.0+
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU General Public License as published by
14
 * the Free Software Foundation, either version 3 of the License, or
15
 * (at your option) any later version.
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
 * GNU General Public License for more details.
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program. If not, see <https://www.gnu.org/licenses>.
22
 */
23
class ConnectedComponentTest extends BaseTestCase
24
{
25
    /**
26
     * A graph with no components.
27
     *
28
     * @covers \Fisharebest\Algorithm\ConnectedComponent
29
     */
30
    public function testNoComponents()
31
    {
32
        $graph = array();
33
34
        $components = array();
35
36
        $algorithm = new ConnectedComponent($graph);
37
38
        $this->assertSame($components, $algorithm->findConnectedComponents());
39
    }
40
41
    /**
42
     * A graph with one component.
43
     *
44
     *    D----E
45
     *   / \    \
46
     *  /   \    \
47
     * A-----B---C
48
     *  \   /    /
49
     *   \ /    /
50
     *    F----/
51
     *
52
     * @covers \Fisharebest\Algorithm\ConnectedComponent
53
     */
54
    public function testOneComponent()
55
    {
56
        $graph = array(
57
            'A' => array('B' => 1, 'D' => 1, 'F' => 1),
58
            'B' => array('A' => 1, 'C' => 1, 'D' => 1, 'F' => 1),
59
            'C' => array('B' => 1, 'E' => 1, 'F' => 1),
60
            'D' => array('A' => 1, 'B' => 1, 'E' => 1),
61
            'E' => array('C' => 1, 'D' => 1),
62
            'F' => array('A' => 1, 'B' => 1, 'C' => 1),
63
        );
64
65
        $components = array(
66
            1 => array('A', 'B', 'C', 'D', 'E', 'F'),
67
        );
68
69
        $algorithm = new ConnectedComponent($graph);
70
71
        $this->assertSame($components, $algorithm->findConnectedComponents());
72
    }
73
74
    /**
75
     * A graph with two component.
76
     *
77
     *    D    E
78
     *   / \    \
79
     *  /   \    \
80
     * A-----B   C
81
     *  \   /
82
     *   \ /
83
     *    F
84
     *
85
     * @covers \Fisharebest\Algorithm\ConnectedComponent
86
     */
87
    public function testTwoComponent()
88
    {
89
        $graph = array(
90
            'A' => array('B' => 1, 'D' => 1, 'F' => 1),
91
            'B' => array('A' => 1, 'D' => 1, 'F' => 1),
92
            'C' => array('E' => 1),
93
            'D' => array('A' => 1, 'B' => 1),
94
            'E' => array('C' => 1),
95
            'F' => array('A' => 1, 'B' => 1),
96
        );
97
98
        $components = array(
99
            1 => array('A', 'B', 'D', 'F'),
100
            2 => array('C', 'E'),
101
        );
102
103
        $algorithm = new ConnectedComponent($graph);
104
105
        $this->assertSame($components, $algorithm->findConnectedComponents());
106
    }
107
108
    /**
109
     * A graph with two component.
110
     *
111
     * A   B
112
     *
113
     * @covers \Fisharebest\Algorithm\ConnectedComponent
114
     */
115
    public function testUnconnected()
116
    {
117
        $graph = array(
118
            'A' => array(),
119
            'B' => array(),
120
        );
121
122
        $components = array(
123
            1 => array('A'),
124
            2 => array('B'),
125
        );
126
127
        $algorithm = new ConnectedComponent($graph);
128
129
        $this->assertSame($components, $algorithm->findConnectedComponents());
130
    }
131
}
132