Files
Chamilo/vendor/graphp/algorithms/tests/ConnectedComponentsTest.php
Xes 73154ae174
Behat tests 1.11.x 🐞 / PHP 7.4 Test on ubuntu-latest (push) Canceled after 0s
PHP-CS-Fixer / composer_install (7.4) (push) Canceled after 0s
Chamilo 1.11.40 (ZIP oficial v1.11.40)
Fuente: https://github.com/chamilo/chamilo-lms/releases/download/v1.11.40/chamilo-1.11.40.zip
sha256: 1cf4bf2cc7bae1ef1a1eff643235db1d552f78ddf4b6dd1e2d2dac9868679439
Snapshot independiente (rama huerfana); diffable vs 1.11.38. vendor incluido.
2026-08-06 17:59:45 +02:00

98 lines
2.8 KiB
PHP

<?php
use Fhaculty\Graph\Graph;
use Graphp\Algorithms\ConnectedComponents as AlgorithmConnected;
class ConnectedComponentsTest extends TestCase
{
public function testNullGraph()
{
$graph = new Graph();
$alg = new AlgorithmConnected($graph);
$this->assertEquals(0, $alg->getNumberOfComponents());
$this->assertFalse($alg->isSingle());
$this->assertCount(0, $alg->createGraphsComponents());
}
public function testGraphSingleTrivial()
{
$graph = new Graph();
$graph->createVertex(1);
$alg = new AlgorithmConnected($graph);
$this->assertEquals(1, $alg->getNumberOfComponents());
$this->assertTrue($alg->isSingle());
$graphs = $alg->createGraphsComponents();
$this->assertCount(1, $graphs);
$this->assertGraphEquals($graph, \reset($graphs));
}
public function testGraphEdgeDirections()
{
// 1 -- 2 -> 3 <- 4
$graph = new Graph();
$graph->createVertex(1)->createEdge($graph->createVertex(2));
$graph->getVertex(2)->createEdgeTo($graph->createVertex(3));
$graph->createVertex(4)->createEdgeTo($graph->getVertex(3));
$alg = new AlgorithmConnected($graph);
$this->assertEquals(1, $alg->getNumberOfComponents());
$this->assertTrue($alg->isSingle());
$graphs = $alg->createGraphsComponents();
$this->assertCount(1, $graphs);
$this->assertGraphEquals($graph, \reset($graphs));
$this->assertGraphEquals($graph, $alg->createGraphComponentVertex($graph->getVertex(1)));
}
public function testComponents()
{
// 1 -- 2, 3 -> 4, 5
$graph = new Graph();
$v1 = $graph->createVertex(1);
$v2 = $graph->createVertex(2);
$v3 = $graph->createVertex(3);
$v4 = $graph->createVertex(4);
$v5 = $graph->createVertex(5);
$v1->createEdge($v2);
$v3->createEdgeTo($v4);
$alg = new AlgorithmConnected($graph);
$this->assertEquals(3, $alg->getNumberOfComponents());
$this->assertFalse($alg->isSingle());
$graphs = $alg->createGraphsComponents();
$this->assertCount(3, $graphs);
$ge = new Graph();
$ge->createVertex(1)->createEdge($ge->createVertex(2));
$this->assertGraphEquals($ge, $alg->createGraphComponentVertex($v2));
$ge = new Graph();
$ge->createVertex(5);
$this->assertEquals($ge, $alg->createGraphComponentVertex($v5));
}
/**
* @expectedException InvalidArgumentException
*/
public function testInvalidVertexPassedToAlgorithm()
{
$graph = new Graph();
$graph2 = new Graph();
$v2 = $graph2->createVertex(12);
$alg = new AlgorithmConnected($graph);
$alg->createGraphComponentVertex($v2);
}
}