Completed
Push — master ( a2e1dc...e269fd )
by Greg
02:20
created

ConnectedComponentTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
lcom 0
cbo 1
dl 0
loc 100
rs 10
c 1
b 0
f 0

4 Methods

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