Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Feb 1, 2026. It is now read-only.

Commit c2d3cf0

Browse files
committed
test for TopologicalSorter
1 parent a8222ff commit c2d3cf0

2 files changed

Lines changed: 63 additions & 7 deletions

File tree

Structures/Graph/Manipulator/TopologicalSorter.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,9 @@ class Structures_Graph_Manipulator_TopologicalSorter {
6262
* This is a variant of Structures_Graph::inDegree which does
6363
* not count nodes marked as visited.
6464
*
65-
* @access private
6665
* @return integer Number of non-visited nodes that link to this one
6766
*/
68-
function _nonVisitedInDegree(&$node) {
67+
protected static function _nonVisitedInDegree(&$node) {
6968
$result = 0;
7069
$graphNodes =& $node->_graph->getNodes();
7170
foreach (array_keys($graphNodes) as $key) {
@@ -78,9 +77,8 @@ function _nonVisitedInDegree(&$node) {
7877

7978
/* _sort {{{ */
8079
/**
81-
* @access private
82-
*/
83-
function _sort(&$graph) {
80+
*/
81+
protected static function _sort(&$graph) {
8482
// Mark every node as not visited
8583
$nodes =& $graph->getNodes();
8684
$nodeKeys = array_keys($nodes);
@@ -126,9 +124,8 @@ function _sort(&$graph) {
126124
* the given topological level.
127125
*
128126
* @return array The graph's nodes, sorted by topological order.
129-
* @access public
130127
*/
131-
function sort(&$graph) {
128+
public static function sort(&$graph) {
132129
// We only sort graphs
133130
if (!is_a($graph, 'Structures_Graph')) return Pear::raiseError('Structures_Graph_Manipulator_TopologicalSorter::sort received an object that is not a Structures_Graph', STRUCTURES_GRAPH_ERROR_GENERIC);
134131
if (!Structures_Graph_Manipulator_AcyclicTest::isAcyclic($graph)) return Pear::raiseError('Structures_Graph_Manipulator_TopologicalSorter::sort received an graph that has cycles', STRUCTURES_GRAPH_ERROR_GENERIC);

tests/TopologicalSorterTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
require_once dirname(__FILE__) . '/helper.inc';
3+
require_once 'Structures/Graph/Manipulator/TopologicalSorter.php';
4+
5+
class TopologicalSorterTest extends PHPUnit_Framework_TestCase
6+
{
7+
public function testSort()
8+
{
9+
$graph = new Structures_Graph();
10+
11+
$name1 = 'node1';
12+
$node1 = new Structures_Graph_Node();
13+
$node1->setData($name1);
14+
$graph->addNode($node1);
15+
16+
$name11 = 'node11';
17+
$node11 = new Structures_Graph_Node();
18+
$node11->setData($name11);
19+
$graph->addNode($node11);
20+
$node1->connectTo($node11);
21+
22+
$name12 = 'node12';
23+
$node12 = new Structures_Graph_Node();
24+
$node12->setData($name12);
25+
$graph->addNode($node12);
26+
$node1->connectTo($node12);
27+
28+
$name121 = 'node121';
29+
$node121 = new Structures_Graph_Node();
30+
$node121->setData($name121);
31+
$graph->addNode($node121);
32+
$node12->connectTo($node121);
33+
34+
$name2 = 'node2';
35+
$node2 = new Structures_Graph_Node();
36+
$node2->setData($name2);
37+
$graph->addNode($node2);
38+
39+
$name21 = 'node21';
40+
$node21 = new Structures_Graph_Node();
41+
$node21->setData($name21);
42+
$graph->addNode($node21);
43+
$node2->connectTo($node21);
44+
45+
$nodes = Structures_Graph_Manipulator_TopologicalSorter::sort($graph);
46+
$this->assertCount(2, $nodes[0]);
47+
$this->assertEquals('node1', $nodes[0][0]->getData());
48+
$this->assertEquals('node2', $nodes[0][1]->getData());
49+
50+
$this->assertCount(3, $nodes[1]);
51+
$this->assertEquals('node11', $nodes[1][0]->getData());
52+
$this->assertEquals('node12', $nodes[1][1]->getData());
53+
$this->assertEquals('node21', $nodes[1][2]->getData());
54+
55+
$this->assertCount(1, $nodes[2]);
56+
$this->assertEquals('node121', $nodes[2][0]->getData());
57+
}
58+
}
59+
?>

0 commit comments

Comments
 (0)