diff --git a/src/Edge/Base.php b/src/Edge/Base.php index 3761447c..9317566f 100644 --- a/src/Edge/Base.php +++ b/src/Edge/Base.php @@ -257,28 +257,6 @@ public function destroy() } } - /** - * create new clone of this edge between adjacent vertices - * - * @return self new edge - * @uses Graph::createEdgeClone() - */ - public function createEdgeClone() - { - return $this->getGraph()->createEdgeClone($this); - } - - /** - * create new clone of this edge inverted (in opposite direction) between adjacent vertices - * - * @return self new edge - * @uses Graph::createEdgeCloneInverted() - */ - public function createEdgeCloneInverted() - { - return $this->getGraph()->createEdgeCloneInverted($this); - } - /** * do NOT allow cloning of objects * diff --git a/src/Walk.php b/src/Walk.php index 35c86ba3..40b0d7f5 100644 --- a/src/Walk.php +++ b/src/Walk.php @@ -203,31 +203,6 @@ public function getGraph() return $this->getVertices()->getVertexFirst()->getGraph(); } - /** - * create new graph clone with only vertices and edges actually in the walk - * - * do not add duplicate vertices and edges for loops and intersections, etc. - * - * @return Graph - * @uses Walk::getEdges() - * @uses Graph::createGraphCloneEdges() - */ - public function createGraph() - { - // create new graph clone with only edges of walk - $graph = $this->getGraph()->createGraphCloneEdges($this->getEdges()); - $vertices = $this->getVertices()->getMap(); - // get all vertices - foreach ($graph->getVertices()->getMap() as $vid => $vertex) { - if (!isset($vertices[$vid])) { - // remove those not present in the walk (isolated vertices, etc.) - $vertex->destroy(); - } - } - - return $graph; - } - /** * return set of all Edges of walk (in sequence visited in walk, may contain duplicates) * diff --git a/tests/Edge/EdgeBaseTest.php b/tests/Edge/EdgeBaseTest.php index 5ed16b38..dd1e227f 100644 --- a/tests/Edge/EdgeBaseTest.php +++ b/tests/Edge/EdgeBaseTest.php @@ -71,24 +71,6 @@ public function testEdgeToInvalid() $this->edge->getVertexToFrom($v3); } - public function testClone() - { - $edge = $this->edge->createEdgeClone(); - - $this->assertEdgeEquals($this->edge, $edge); - } - - public function testCloneDoubleInvertedIsOriginal() - { - $edgeInverted = $this->edge->createEdgeCloneInverted(); - - $this->assertInstanceOf(get_class($this->edge), $edgeInverted); - - $edge = $edgeInverted->createEdgeCloneInverted(); - - $this->assertEdgeEquals($this->edge, $edge); - } - public function testLoop() { $edge = $this->createEdgeLoop(); diff --git a/tests/GraphTest.php b/tests/GraphTest.php index 502113c4..98eb46b8 100644 --- a/tests/GraphTest.php +++ b/tests/GraphTest.php @@ -298,6 +298,38 @@ public function testCreateVerticesContainsInvalid() $graph->createVertices(array(1, 2, array(), 3)); } + public function testCloneInvertedUndirectedIsAlmostOriginal() + { + // 1 -- 2 + $graph = new Graph(); + $v1 = $graph->createVertex(1); + $v2 = $graph->createVertex(2); + $edge = $graph->createEdgeUndirected($v1, $v2); + + $edgeInverted = $graph->createEdgeCloneInverted($edge); + + $this->assertInstanceOf(get_class($edge), $edgeInverted); + + $this->assertEquals($edge->getVertices()->getVector(), array_reverse($edgeInverted->getVertices()->getVector())); + } + + public function testCloneDoubleInvertedDirectedEdgeIsOriginal() + { + // 1 -> 2 + $graph = new Graph(); + $v1 = $graph->createVertex(1); + $v2 = $graph->createVertex(2); + $edge = $graph->createEdgeDirected($v1, $v2); + + $edgeInverted = $graph->createEdgeCloneInverted($edge); + + $this->assertInstanceOf(get_class($edge), $edgeInverted); + + $edgeInvertedAgain = $graph->createEdgeCloneInverted($edgeInverted); + + $this->assertEdgeEquals($edge, $edgeInvertedAgain); + } + public function testRemoveEdge() { // 1 -- 2 diff --git a/tests/WalkTest.php b/tests/WalkTest.php index 36fb0561..00823b06 100644 --- a/tests/WalkTest.php +++ b/tests/WalkTest.php @@ -36,9 +36,6 @@ public function testWalkPath() $this->assertSame(array($v1, $e1, $v2, $e2, $v3), $walk->getAlternatingSequence()); $this->assertTrue($walk->isValid()); - $graphClone = $walk->createGraph(); - $this->assertGraphEquals($graph, $graphClone); - return $walk; } @@ -74,19 +71,12 @@ public function testWalkWithinGraph() $this->assertSame(array($v1, $e1, $v2), $walk->getAlternatingSequence()); $this->assertTrue($walk->isValid()); - $graphExpected = new Graph(); - $graphExpected->createEdgeDirected($graphExpected->createVertex(1), $graphExpected->createVertex(2)); - - $this->assertGraphEquals($graphExpected, $walk->createGraph()); - // construct same partial walk "1 -- 2" $walkVertices = Walk::factoryFromVertices(array($v1, $v2)); $this->assertEquals(2, count($walkVertices->getVertices())); $this->assertEquals(1, count($walkVertices->getEdges())); - $this->assertGraphEquals($graphExpected, $walkVertices->createGraph()); - return $walk; } @@ -169,7 +159,7 @@ public function testWalkCycleInvalid() Walk::factoryCycleFromEdges(array($e1), $v1); } - public function testLoopCycle() + public function testFactoryCycleFromEdgesWithLoopCycle() { // 1 --\ // ^ | @@ -179,31 +169,59 @@ public function testLoopCycle() $e1 = $graph->createEdgeDirected($v1, $v1); $cycle = Walk::factoryCycleFromEdges(array($e1), $v1); - $this->assertGraphEquals($graph, $cycle->createGraph()); + + $this->assertCount(2, $cycle->getVertices()); + $this->assertCount(1, $cycle->getEdges()); + $this->assertSame($v1, $cycle->getVertices()->getVertexFirst()); + $this->assertSame($v1, $cycle->getVertices()->getVertexLast()); + $this->assertTrue($cycle->isValid()); + } + + public function testFactoryCycleFromPredecessorMapWithLoopCycle() + { + // 1 --\ + // ^ | + // \---/ + $graph = new Graph(); + $v1 = $graph->createVertex(1); + $graph->createEdgeDirected($v1, $v1); $cycle = Walk::factoryCycleFromPredecessorMap(array(1 => $v1), $v1); - $this->assertGraphEquals($graph, $cycle->createGraph()); + + $this->assertCount(2, $cycle->getVertices()); + $this->assertCount(1, $cycle->getEdges()); + $this->assertSame($v1, $cycle->getVertices()->getVertexFirst()); + $this->assertSame($v1, $cycle->getVertices()->getVertexLast()); + $this->assertTrue($cycle->isValid()); + } + + public function testFactoryCycleFromVerticesWithLoopCycle() + { + // 1 --\ + // ^ | + // \---/ + $graph = new Graph(); + $v1 = $graph->createVertex(1); + $graph->createEdgeDirected($v1, $v1); $cycle = Walk::factoryCycleFromVertices(array($v1, $v1)); - $this->assertGraphEquals($graph, $cycle->createGraph()); $this->assertCount(2, $cycle->getVertices()); $this->assertCount(1, $cycle->getEdges()); $this->assertSame($v1, $cycle->getVertices()->getVertexFirst()); $this->assertSame($v1, $cycle->getVertices()->getVertexLast()); $this->assertTrue($cycle->isValid()); - - return $v1; } + /** - * - * @param Vertex $v1 - * @depends testLoopCycle * @expectedException InvalidArgumentException */ - public function testFactoryCycleFromVerticesIncomplete(Vertex $v1) + public function testFactoryCycleFromVerticesThrowsWhenCycleIsIncomplete() { + $graph = new Graph(); + $v1 = $graph->createVertex(1); + // should actually be [v1, v1] Walk::factoryCycleFromVertices(array($v1)); } @@ -211,7 +229,7 @@ public function testFactoryCycleFromVerticesIncomplete(Vertex $v1) /** * @expectedException InvalidArgumentException */ - public function testInvalidPredecessors() + public function testFactoryCycleFromPredecessorMapThrowsForInvalidPredecessors() { $graph = new Graph(); $v1 = $graph->createVertex(1); @@ -222,6 +240,7 @@ public function testInvalidPredecessors() public function testFactoryFromVertices() { // 1 -- 2 + // | | // \----/ $graph = new Graph(); $v1 = $graph->createVertex(1);