Files
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

207 lines
6.4 KiB
PHP

<?php
use Fhaculty\Graph\Graph;
use Graphp\Algorithms\ResidualGraph;
class ResidualGraphTest extends TestCase
{
public function testGraphEmpty()
{
$graph = new Graph();
$alg = new ResidualGraph($graph);
$residual = $alg->createGraph();
$this->assertGraphEquals($graph, $residual);
}
/**
* test an edge with capacity unused
*/
public function testEdgeUnused()
{
$graph = new Graph();
$graph->createVertex(0)->createEdgeTo($graph->createVertex(1))->setFlow(0)
->setCapacity(2)
->setWeight(3);
$alg = new ResidualGraph($graph);
$residual = $alg->createGraph();
$this->assertGraphEquals($graph, $residual);
}
/**
* test an edge with capacity completely used
*/
public function testEdgeUsed()
{
$graph = new Graph();
$graph->createVertex(0)->createEdgeTo($graph->createVertex(1))->setFlow(2)
->setCapacity(2)
->setWeight(3);
$alg = new ResidualGraph($graph);
$residual = $alg->createGraph();
$expected = new Graph();
$expected->createVertex(1)->createEdgeTo($expected->createVertex(0))->setFlow(0)
->setCapacity(2)
->setWeight(-3);
$this->assertGraphEquals($expected, $residual);
}
/**
* test an edge with capacity remaining
*/
public function testEdgePartial()
{
$graph = new Graph();
$graph->createVertex(0)->createEdgeTo($graph->createVertex(1))->setFlow(1)
->setCapacity(2)
->setWeight(3);
$alg = new ResidualGraph($graph);
$residual = $alg->createGraph();
$expected = new Graph();
$expected->createVertex(0);
$expected->createVertex(1);
// remaining edge
$expected->getVertex(0)->createEdgeTo($expected->getVertex(1))->setFlow(0)
->setCapacity(1)
->setWeight(3);
// back edge
$expected->getVertex(1)->createEdgeTo($expected->getVertex(0))->setFlow(0)
->setCapacity(1)
->setWeight(-3);
$this->assertGraphEquals($expected, $residual);
}
public function testResidualGraphCanOptionallyKeepNullCapacityForEdgeWithZeroFlow()
{
// 1 -[0/2]-> 2
$graph = new Graph();
$v1 = $graph->createVertex(1);
$v2 = $graph->createVertex(2);
$v1->createEdgeTo($v2)->setFlow(0)->setCapacity(2);
// 1 -[0/2]-> 2
// ^ |
// \--[0/0]---/
$expected = new Graph();
$v1 = $expected->createVertex(1);
$v2 = $expected->createVertex(2);
$v1->createEdgeTo($v2)->setFlow(0)->setCapacity(2);
$v2->createEdgeTo($v1)->setFlow(0)->setCapacity(0);
$alg = new ResidualGraph($graph);
$alg->setKeepNullCapacity(true);
$residual = $alg->createGraph();
$this->assertGraphEquals($expected, $residual);
}
public function testResidualGraphCanOptionallyKeepNullCapacityForEdgeWithZeroCapacityRemaining()
{
// 1 -[2/2]-> 2
$graph = new Graph();
$v1 = $graph->createVertex(1);
$v2 = $graph->createVertex(2);
$v1->createEdgeTo($v2)->setFlow(2)->setCapacity(2);
// 1 -[0/0]-> 2
// ^ |
// \--[0/2]---/
$expected = new Graph();
$v1 = $expected->createVertex(1);
$v2 = $expected->createVertex(2);
$v1->createEdgeTo($v2)->setFlow(0)->setCapacity(0);
$v2->createEdgeTo($v1)->setFlow(0)->setCapacity(2);
$alg = new ResidualGraph($graph);
$alg->setKeepNullCapacity(true);
$residual = $alg->createGraph();
$this->assertGraphEquals($expected, $residual);
}
public function testParallelEdgesCanBeMerged()
{
// 1 -[1/2]-> 2
// | ^
// \--[2/3]---/
$graph = new Graph();
$v1 = $graph->createVertex(1);
$v2 = $graph->createVertex(2);
$v1->createEdgeTo($v2)->setFlow(1)->setCapacity(2);
$v1->createEdgeTo($v2)->setFlow(2)->setCapacity(3);
// 1 -[0/2]-> 2
// ^ |
// \--[0/3]---/
$expected = new Graph();
$v1 = $expected->createVertex(1);
$v2 = $expected->createVertex(2);
$v1->createEdgeTo($v2)->setFlow(0)->setCapacity(2);
$v2->createEdgeTo($v1)->setFlow(0)->setCapacity(3);
$alg = new ResidualGraph($graph);
$alg->setMergeParallelEdges(true);
$residual = $alg->createGraph();
$this->assertGraphEquals($expected, $residual);
}
/**
* expect exception for undirected edges
* @expectedException UnexpectedValueException
*/
public function testInvalidUndirected()
{
$graph = new Graph();
$graph->createVertex()->createEdge($graph->createVertex())->setFlow(1)
->setCapacity(2);
$alg = new ResidualGraph($graph);
$alg->createGraph();
}
/**
* expect exception for edges with no flow
* @expectedException UnexpectedValueException
*/
public function testInvalidNoFlow()
{
$graph = new Graph();
$graph->createVertex()->createEdgeTo($graph->createVertex())->setCapacity(1);
$alg = new ResidualGraph($graph);
$alg->createGraph();
}
/**
* expect exception for edges with no capacity
* @expectedException UnexpectedValueException
*/
public function testInvalidNoCapacity()
{
$graph = new Graph();
$graph->createVertex()->createEdgeTo($graph->createVertex())->setFlow(1);
$alg = new ResidualGraph($graph);
$alg->createGraph();
}
}