diff --git a/README.md b/README.md index 356350a7..66f410a5 100644 --- a/README.md +++ b/README.md @@ -26,16 +26,14 @@ Once [installed](#install), let's initialize a sample graph: ```php createVertex('Rome'); -$madrid = $graph->createVertex('Madrid'); -$cologne = $graph->createVertex('Cologne'); +$rome = $graph->createVertex()->setAttribute('name', 'Rome'); +$madrid = $graph->createVertex()->setAttribute('name', 'Madrid'); +$cologne = $graph->createVertex()->setAttribute('name', 'Cologne'); // build some roads $graph->createEdgeDirected($cologne, $madrid); @@ -48,8 +46,8 @@ Let's see which city (Vertex) has a road (i.e. an edge pointing) to Rome: ```php foreach ($rome->getVerticesEdgeFrom() as $vertex) { - echo $vertex->getId().' leads to rome'.PHP_EOL; - // result: Madrid and Rome itself + echo $vertex->getAttribute('name') . ' leads to Rome' . PHP_EOL; + // result: Madrid and Rome itself lead to Rome } ``` diff --git a/src/Graph.php b/src/Graph.php index be3fcbea..483b46eb 100644 --- a/src/Graph.php +++ b/src/Graph.php @@ -6,11 +6,9 @@ use Graphp\Graph\Attribute\AttributeBagReference; use Graphp\Graph\Exception\InvalidArgumentException; use Graphp\Graph\Exception\OutOfBoundsException; -use Graphp\Graph\Exception\OverflowException; use Graphp\Graph\Set\DualAggregate; use Graphp\Graph\Set\Edges; use Graphp\Graph\Set\Vertices; -use Graphp\Graph\Set\VerticesMap; class Graph implements DualAggregate, AttributeAware { @@ -24,7 +22,7 @@ class Graph implements DualAggregate, AttributeAware public function __construct() { - $this->vertices = VerticesMap::factoryArrayReference($this->verticesStorage); + $this->vertices = Vertices::factoryArrayReference($this->verticesStorage); $this->edges = Edges::factoryArrayReference($this->edgesStorage); } @@ -51,24 +49,11 @@ public function getEdges() /** * create a new Vertex in the Graph * - * @param int|NULL $id new vertex ID to use (defaults to NULL: use next free numeric ID) - * @param bool $returnDuplicate normal operation is to throw an exception if given id already exists. pass true to return original vertex instead - * @return Vertex (chainable) - * @throws InvalidArgumentException if given vertex $id is invalid - * @throws OverflowException if given vertex $id already exists and $returnDuplicate is not set - * @uses Vertex::getId() + * @return Vertex */ - public function createVertex($id = NULL, $returnDuplicate = false) + public function createVertex() { - // no ID given - if ($id === NULL) { - $id = $this->getNextId(); - } - if ($returnDuplicate && $this->vertices->hasVertexId($id)) { - return $this->vertices->getVertexId($id); - } - - return new Vertex($this, $id); + return new Vertex($this); } /** @@ -105,86 +90,6 @@ public function createEdgeDirected(Vertex $source, Vertex $target) return new EdgeDirected($source, $target); } - /** - * create the given number of vertices or given array of Vertex IDs - * - * @param int|array $n number of vertices to create or array of Vertex IDs to create - * @return Vertices set of Vertices created - * @uses Graph::getNextId() - */ - public function createVertices($n) - { - $vertices = array(); - if (\is_int($n) && $n >= 0) { - for ($id = $this->getNextId(), $n += $id; $id < $n; ++$id) { - $vertices[$id] = new Vertex($this, $id); - } - } elseif (\is_array($n)) { - // array given => check to make sure all given IDs are available (atomic operation) - foreach ($n as $id) { - if (!\is_int($id) && !\is_string($id)) { - throw new InvalidArgumentException('All Vertex IDs have to be of type integer or string'); - } elseif ($this->vertices->hasVertexId($id)) { - throw new OverflowException('Given array of Vertex IDs contains an ID that already exists. Given IDs must be unique'); - } elseif (isset($vertices[$id])) { - throw new InvalidArgumentException('Given array of Vertex IDs contain duplicate IDs. Given IDs must be unique'); - } - - // temporary marker to check for duplicate IDs in the array - $vertices[$id] = false; - } - - // actually create all requested vertices - foreach ($n as $id) { - $vertices[$id] = new Vertex($this, $id); - } - } else { - throw new InvalidArgumentException('Invalid number of vertices given. Must be non-negative integer or an array of Vertex IDs'); - } - - return new Vertices($vertices); - } - - /** - * get next free/unused/available vertex ID - * - * its guaranteed there's NO other vertex with a greater ID - * - * @return int - */ - private function getNextId() - { - if (!$this->verticesStorage) { - return 0; - } - - // auto ID - return \max(\array_keys($this->verticesStorage))+1; - } - - /** - * returns the Vertex with identifier $id - * - * @param int|string $id identifier of Vertex - * @return Vertex - * @throws OutOfBoundsException if given vertex ID does not exist - */ - public function getVertex($id) - { - return $this->vertices->getVertexId($id); - } - - /** - * checks whether given vertex ID exists in this graph - * - * @param int|string $id identifier of Vertex - * @return bool - */ - public function hasVertex($id) - { - return $this->vertices->hasVertexId($id); - } - /** * adds a new Vertex to the Graph (MUST NOT be called manually!) * @@ -195,10 +100,7 @@ public function hasVertex($id) */ public function addVertex(Vertex $vertex) { - if (isset($this->verticesStorage[$vertex->getId()])) { - throw new OverflowException('ID must be unique'); - } - $this->verticesStorage[$vertex->getId()] = $vertex; + $this->verticesStorage[] = $vertex; } /** @@ -259,7 +161,7 @@ public function __clone() { $vertices = $this->verticesStorage; $this->verticesStorage = array(); - $this->vertices = VerticesMap::factoryArrayReference($this->verticesStorage); + $this->vertices = Vertices::factoryArrayReference($this->verticesStorage); $edges = $this->edgesStorage; $this->edgesStorage = array(); @@ -269,7 +171,7 @@ public function __clone() foreach ($vertices as $originalVertex) { \assert($originalVertex instanceof Vertex); - $vertex = new Vertex($this, $originalVertex->getId()); + $vertex = new Vertex($this); $vertex->getAttributeBag()->setAttributes($originalVertex->getAttributeBag()->getAttributes()); // create map with old vertex hash to new vertex object diff --git a/src/Set/Vertices.php b/src/Set/Vertices.php index 4692a126..5c3502fc 100644 --- a/src/Set/Vertices.php +++ b/src/Set/Vertices.php @@ -67,36 +67,6 @@ public function __construct(array $vertices = array()) $this->vertices = $vertices; } - /** - * get Vertex with the given vertex $id - * - * @param int|string $id - * @return Vertex - * @throws OutOfBoundsException if no Vertex with the given ID exists - * @uses self::getVertexMatch() - */ - public function getVertexId($id) - { - try { - return $this->getVertexMatch($this->getCallbackId($id)); - } - catch (UnderflowException $e) { - throw new OutOfBoundsException('Vertex ' . $id . ' does not exist', 0, $e); - } - } - - /** - * checks whether given vertex ID exists in this set of vertices - * - * @param int|string $id identifier of Vertex - * @return bool - * @uses self::hasVertexMatch() - */ - public function hasVertexId($id) - { - return $this->hasVertexMatch($this->getCallbackId($id)); - } - /** * get array index for given Vertex * @@ -353,41 +323,22 @@ public function getVertices() /** * get a new set of Vertices where each Vertex is distinct/unique * - * @return VerticesMap a new VerticesMap instance + * Vertex index/keys will be preserved from original array. + * + * @return Vertices * @uses self::getMap() */ public function getVerticesDistinct() - { - return new VerticesMap($this->getMap()); - } - - /** - * get a mapping array of Vertex ID => Vertex instance and thus remove duplicate vertices - * - * @return Vertex[] Vertex ID => Vertex instance - * @uses Vertex::getId() - */ - public function getMap() { $vertices = array(); - foreach ($this->vertices as $vertex) { - $vertices[$vertex->getId()] = $vertex; + foreach ($this->vertices as $vid => $vertex) { + // filter duplicate vertices + if (!\in_array($vertex, $vertices, true)) { + $vertices[$vid] = $vertex; + } } - return $vertices; - } - /** - * return array of Vertex IDs - * - * @return array - */ - public function getIds() - { - $ids = array(); - foreach ($this->vertices as $vertex) { - $ids []= $vertex->getId(); - } - return $ids; + return new self($vertices); } /** @@ -432,7 +383,16 @@ public function isEmpty() */ public function hasDuplicates() { - return (\count($this->vertices) !== \count($this->getMap())); + $found = array(); + foreach ($this->vertices as $vertex) { + if (\in_array($vertex, $found, true)) { + return true; + } + + $found[] = $vertex; + } + + return false; } /** @@ -466,13 +426,6 @@ public function getSumCallback($callback) return $sum; } - private function getCallbackId($id) - { - return function (Vertex $vertex) use ($id) { - return ($vertex->getId() == $id); - }; - } - private function getVertexMatchOrNull($callbackCheck) { $callbackCheck = $this->getCallback($callbackCheck); diff --git a/src/Set/VerticesMap.php b/src/Set/VerticesMap.php deleted file mode 100644 index f894ac64..00000000 --- a/src/Set/VerticesMap.php +++ /dev/null @@ -1,67 +0,0 @@ - Vertex instance mapping array - * - * Among others, using a mapped array significantly speeds up accessing vertices - * by ID. However, there's no way to store multiple vertices with the same ID - * (i.e. each Vertex ID has to be unique). - */ -class VerticesMap extends Vertices -{ - public function getMap() - { - return $this->vertices; - } - - public function getVertexId($id) - { - if (!isset($this->vertices[$id])) { - throw new OutOfBoundsException('Invalid vertex ID'); - } - return $this->vertices[$id]; - } - - public function hasVertexId($id) - { - return isset($this->vertices[$id]); - } - - public function getVerticesDistinct() - { - return $this; - } - - public function getIds() - { - return \array_keys($this->vertices); - } - - public function getIndexVertex(Vertex $vertex) - { - $id = $vertex->getId(); - if (!isset($this->vertices[$id]) || $this->vertices[$id] !== $vertex) { - throw new OutOfBoundsException(); - } - return $id; - } - - /** - * - * @return VerticesMap - */ - public function getVertices() - { - return $this; - } - - public function hasDuplicates() - { - return false; - } -} diff --git a/src/Vertex.php b/src/Vertex.php index bd24d304..ec6b9018 100644 --- a/src/Vertex.php +++ b/src/Vertex.php @@ -12,8 +12,6 @@ class Vertex implements EdgesAggregate, AttributeAware { - private $id; - /** * @var Edge[] */ @@ -29,17 +27,11 @@ class Vertex implements EdgesAggregate, AttributeAware /** * Create a new Vertex * - * @param Graph $graph graph to be added to - * @param string|int $id identifier used to uniquely identify this vertex in the graph + * @param Graph $graph graph to be added to * @see Graph::createVertex() to create new vertices */ - public function __construct(Graph $graph, $id) + public function __construct(Graph $graph) { - if (!\is_int($id) && !\is_string($id)) { - throw new InvalidArgumentException('Vertex ID has to be of type integer or string'); - } - - $this->id = $id; $this->graph = $graph; $graph->addVertex($this); @@ -55,16 +47,6 @@ public function getGraph() return $this->graph; } - /** - * returns id of this Vertex - * - * @return int|string - */ - public function getId() - { - return $this->id; - } - /** * add the given edge to list of connected edges (MUST NOT be called manually) * diff --git a/src/Walk.php b/src/Walk.php index f79ff9fc..b1071f84 100644 --- a/src/Walk.php +++ b/src/Walk.php @@ -73,57 +73,6 @@ public static function factoryFromVertices($vertices, $orderBy = null, $desc = f return new self($vertices, $edges); } - /** - * create new cycle instance from given predecessor map - * - * @param Vertex[] $predecessors map of vid => predecessor vertex instance - * @param Vertex $vertex start vertex to search predecessors from - * @param null|string|callable(Edge):number $orderBy - * @param bool $desc - * @return Walk - * @throws UnderflowException - * @see Edges::getEdgeOrder() for parameters $by and $desc - * @uses self::factoryFromVertices() - */ - public static function factoryCycleFromPredecessorMap(array $predecessors, Vertex $vertex, $orderBy = null, $desc = false) - { - // find a vertex in the cycle - $vid = $vertex->getId(); - $startVertices = array(); - do { - if (!isset($predecessors[$vid])) { - throw new InvalidArgumentException('Predecessor map is incomplete and does not form a cycle'); - } - - $startVertices[$vid] = $vertex; - - $vertex = $predecessors[$vid]; - $vid = $vertex->getId(); - } while (!isset($startVertices[$vid])); - - // find negative cycle - $vid = $vertex->getId(); - // build array of vertices in cycle - $vertices = array(); - do { - // add new vertex to cycle - $vertices[$vid] = $vertex; - - // get predecessor of vertex - $vertex = $predecessors[$vid]; - $vid = $vertex->getId(); - // continue until we find a vertex that's already in the circle (i.e. circle is closed) - } while (!isset($vertices[$vid])); - - // reverse cycle, because cycle is actually built in opposite direction due to checking predecessors - $vertices = \array_reverse($vertices, true); - - // additional edge from last vertex to first vertex - $vertices[] = \reset($vertices); - - return self::factoryCycleFromVertices($vertices, $orderBy, $desc); - } - /** * create new cycle instance with edges between given vertices * @@ -264,11 +213,11 @@ public function getAlternatingSequence() */ public function isValid() { - $vertices = $this->getGraph()->getVertices()->getMap(); + $vertices = \iterator_to_array($this->getGraph()->getVertices(), false); + // check source graph contains all vertices - foreach ($this->getVertices()->getMap() as $vid => $vertex) { - // make sure vertex ID exists and has not been replaced - if (!isset($vertices[$vid]) || $vertices[$vid] !== $vertex) { + foreach ($this->getVertices() as $vertex) { + if (!\in_array($vertex, $vertices, true)) { return false; } } diff --git a/tests/EdgeBaseTest.php b/tests/EdgeBaseTest.php index 8736cf54..4c77f187 100644 --- a/tests/EdgeBaseTest.php +++ b/tests/EdgeBaseTest.php @@ -5,11 +5,23 @@ use Graphp\Graph\Graph; use Graphp\Graph\Edge; use Graphp\Graph\Tests\Attribute\AbstractAttributeAwareTest; +use Graphp\Graph\Vertex; abstract class EdgeBaseTest extends AbstractAttributeAwareTest { + /** + * @var Graph + */ protected $graph; + + /** + * @var Vertex + */ protected $v1; + + /** + * @var Vertex + */ protected $v2; /** @@ -27,8 +39,8 @@ abstract protected function createEdgeLoop(); public function setUp() { $this->graph = new Graph(); - $this->v1 = $this->graph->createVertex(1); - $this->v2 = $this->graph->createVertex(2); + $this->v1 = $this->graph->createVertex(); + $this->v2 = $this->graph->createVertex(); $this->edge = $this->createEdgeUndirected(); } @@ -36,7 +48,6 @@ public function setUp() public function testEdgeVertices() { $this->assertEquals(array($this->v1, $this->v2), $this->edge->getVertices()->getVector()); - $this->assertEquals(array(1, 2), $this->edge->getVertices()->getIds()); $this->assertSame($this->graph, $this->edge->getGraph()); } @@ -46,7 +57,7 @@ public function testEdgeStartVertex() $this->assertTrue($this->edge->hasVertexStart($this->v1)); $this->assertTrue($this->edge->hasVertexTarget($this->v2)); - $v3 = $this->graph->createVertex(3); + $v3 = $this->graph->createVertex(); $this->assertFalse($this->edge->hasVertexStart($v3)); $this->assertFalse($this->edge->hasVertexTarget($v3)); @@ -57,7 +68,7 @@ public function testEdgeStartVertex() */ public function testEdgeFromInvalid() { - $v3 = $this->graph->createVertex(3); + $v3 = $this->graph->createVertex(); $this->edge->getVertexFromTo($v3); } @@ -66,7 +77,7 @@ public function testEdgeFromInvalid() */ public function testEdgeToInvalid() { - $v3 = $this->graph->createVertex(3); + $v3 = $this->graph->createVertex(); $this->edge->getVertexToFrom($v3); } diff --git a/tests/GraphTest.php b/tests/GraphTest.php index f752b54f..735754d1 100644 --- a/tests/GraphTest.php +++ b/tests/GraphTest.php @@ -2,25 +2,11 @@ namespace Graphp\Graph\Tests; -use Graphp\Graph\Exception\OverflowException; -use Graphp\Graph\Exception\InvalidArgumentException; use Graphp\Graph\Graph; use Graphp\Graph\Tests\Attribute\AbstractAttributeAwareTest; class GraphTest extends AbstractAttributeAwareTest { - /** - * @expectedException OutOfBoundsException - */ - public function testGetVertexNonexistant() - { - $graph = new Graph(); - $graph->getVertex('non-existant'); - } - - /** - * check to make sure we can actually create vertices with automatic IDs - */ public function testCanCreateVertex() { $graph = new Graph(); @@ -28,47 +14,6 @@ public function testCanCreateVertex() $this->assertInstanceOf('\Graphp\Graph\Vertex', $vertex); } - /** - * check to make sure we can actually create vertices with automatic IDs - */ - public function testCanCreateVertexId() - { - $graph = new Graph(); - $vertex = $graph->createVertex(11); - $this->assertInstanceOf('\Graphp\Graph\Vertex', $vertex); - $this->assertEquals(11, $vertex->getId()); - } - - /** - * fail to create two vertices with same ID - * @expectedException OverflowException - */ - public function testFailDuplicateVertex() - { - $graph = new Graph(); - $graph->createVertex(33); - $graph->createVertex(33); - } - - /** - * @expectedException InvalidArgumentException - */ - public function testCreateVertexWithInvalidIdThrows() - { - $graph = new Graph(); - $graph->createVertex(array('invalid')); - } - - public function testCreateDuplicateReturn() - { - $graph = new Graph(); - $v1 = $graph->createVertex(1); - - $v1again = $graph->createVertex(1, true); - - $this->assertSame($v1, $v1again); - } - /** * @expectedException InvalidArgumentException */ @@ -76,8 +21,8 @@ public function testCreateEdgeUndirectedWithVerticesFromOtherGraphThrows() { // 1, 2 $graph = new Graph(); - $v1 = $graph->createVertex(1); - $v2 = $graph->createVertex(2); + $v1 = $graph->createVertex(); + $v2 = $graph->createVertex(); $graph2 = new Graph(); $graph2->createEdgeUndirected($v1, $v2); @@ -90,52 +35,34 @@ public function testCreateEdgeDirectedWithVerticesFromOtherGraphThrows() { // 1, 2 $graph = new Graph(); - $v1 = $graph->createVertex(1); - $v2 = $graph->createVertex(2); + $v1 = $graph->createVertex(); + $v2 = $graph->createVertex(); $graph2 = new Graph(); $graph2->createEdgeDirected($v1, $v2); } - public function testHasVertex() - { - $graph = new Graph(); - $graph->createVertex(1); - $graph->createVertex('string'); - - // check integer IDs - $this->assertFalse($graph->hasVertex(2)); - $this->assertTrue($graph->hasVertex(1)); - - // check string IDs - $this->assertFalse($graph->hasVertex('non-existant')); - $this->assertTrue($graph->hasVertex('string')); - - // integer IDs can also be checked as string IDs - $this->assertTrue($graph->hasVertex('1')); - } - public function testCreateMultigraph() { $graph = new Graph(); - $v1 = $graph->createVertex(1); - $v2 = $graph->createVertex(2); + $v1 = $graph->createVertex(); + $v2 = $graph->createVertex(); $graph->createEdgeUndirected($v1, $v2); $graph->createEdgeUndirected($v1, $v2); $this->assertEquals(2, count($graph->getEdges())); $this->assertEquals(2, count($v1->getEdges())); - $this->assertEquals(array(2, 2), $v1->getVerticesEdge()->getIds()); + $this->assertEquals(array($v2, $v2), $v1->getVerticesEdge()->getVector()); } public function testCreateMixedGraph() { // v1 -- v2 -> v3 $graph = new Graph(); - $v1 = $graph->createVertex(1); - $v2 = $graph->createVertex(2); - $v3 = $graph->createVertex(3); + $v1 = $graph->createVertex(); + $v2 = $graph->createVertex(); + $v3 = $graph->createVertex(); $graph->createEdgeUndirected($v1, $v2); $graph->createEdgeDirected($v2, $v3); @@ -146,99 +73,16 @@ public function testCreateMixedGraph() $this->assertEquals(2, count($v2->getEdgesOut())); $this->assertEquals(1, count($v2->getEdgesIn())); - $this->assertEquals(array(1, 3), $v2->getVerticesEdgeTo()->getIds()); - $this->assertEquals(array(1), $v2->getVerticesEdgeFrom()->getIds()); - } - - public function testCreateVerticesNone() - { - $graph = new Graph(); - - $this->assertEquals(array(), $graph->createVertices(0)->getVector()); - $this->assertEquals(array(), $graph->createVertices(array())->getVector()); - - $this->assertEquals(0, count($graph->getVertices())); - } - - /** - * expect to fail for invalid number of vertices - * @expectedException InvalidArgumentException - * @dataProvider createVerticesFailProvider - */ - public function testCreateVerticesFail($number) - { - $graph = new Graph(); - $graph->createVertices($number); - } - - public static function createVerticesFailProvider() - { - return array( - array(-1), - array("10"), - array(0.5), - array(null), - array(array(1, 1)) - ); - } - - public function testCreateVerticesOkay() - { - $graph = new Graph(); - - $vertices = $graph->createVertices(2); - $this->assertCount(2, $vertices); - $this->assertEquals(array(0, 1), $graph->getVertices()->getIds()); - - $vertices = $graph->createVertices(array(7, 9)); - $this->assertCount(2, $vertices); - $this->assertEquals(array(0, 1, 7, 9), $graph->getVertices()->getIds()); - - $vertices = $graph->createVertices(3); - $this->assertCount(3, $vertices); - $this->assertEquals(array(0, 1, 7, 9, 10, 11, 12), $graph->getVertices()->getIds()); - } - - public function testCreateVerticesAtomic() - { - $graph = new Graph(); - - // create vertices 10-19 (inclusive) - $vertices = $graph->createVertices(range(10, 19)); - $this->assertCount(10, $vertices); - - try { - $graph->createVertices(array(9, 19, 20)); - $this->fail('Should be unable to create vertices because of duplicate IDs'); - } - catch (OverflowException $ignoreExpected) { - $this->assertEquals(10, count($graph->getVertices())); - } - - try { - $graph->createVertices(array(20, 21, 21)); - $this->fail('Should be unable to create vertices because of duplicate IDs'); - } - catch (InvalidArgumentException $ignoreExpected) { - $this->assertEquals(10, count($graph->getVertices())); - } - } - - /** - * @expectedException InvalidArgumentException - */ - public function testCreateVerticesContainsInvalid() - { - $graph = new Graph(); - $graph->createVertices(array(1, 2, array(), 3)); + $this->assertEquals(array($v1, $v3), $v2->getVerticesEdgeTo()->getVector()); + $this->assertEquals(array($v1), $v2->getVerticesEdgeFrom()->getVector()); } public function testRemoveEdge() { // 1 -- 2 $graph = new Graph(); - $v1 = $graph->createVertex(1); - $v2 = $graph->createVertex(2); + $v1 = $graph->createVertex(); + $v2 = $graph->createVertex(); $edge = $graph->createEdgeUndirected($v1, $v2); $this->assertEquals(array($edge), $graph->getEdges()->getVector()); @@ -252,13 +96,15 @@ public function testRemoveEdge() } /** - * @param Graph $graph * @expectedException InvalidArgumentException - * @depends testRemoveEdge */ - public function testRemoveEdgeInvalid(Graph $graph) + public function testRemoveEdgeInvalid() { - $edge = $graph->createEdgeUndirected($graph->getVertex(1), $graph->getVertex(2)); + // 1 -- 2 + $graph = new Graph(); + $v1 = $graph->createVertex(); + $v2 = $graph->createVertex(); + $edge = $graph->createEdgeUndirected($v1, $v2); $edge->destroy(); $edge->destroy(); @@ -267,9 +113,9 @@ public function testRemoveEdgeInvalid(Graph $graph) public function testRemoveVertex() { $graph = new Graph(); - $vertex = $graph->createVertex(1); + $vertex = $graph->createVertex(); - $this->assertEquals(array(1 => $vertex), $graph->getVertices()->getMap()); + $this->assertEquals(array($vertex), $graph->getVertices()->getVector()); $vertex->destroy(); @@ -282,7 +128,7 @@ public function testRemoveVertex() public function testRemoveVertexInvalid() { $graph = new Graph(); - $vertex = $graph->createVertex(1); + $vertex = $graph->createVertex(); $vertex->destroy(); $vertex->destroy(); @@ -342,7 +188,7 @@ public function testGraphCloneLoopGraphWithAttributes() // \--/ $graph = new Graph(); $graph->setAttribute('color', 'grey'); - $v = $graph->createVertex(123)->setAttribute('color', 'blue'); + $v = $graph->createVertex()->setAttribute('color', 'blue'); $graph->createEdgeDirected($v, $v)->setAttribute('color', 'red'); $newgraph = clone $graph; diff --git a/tests/Set/BaseVerticesTest.php b/tests/Set/BaseVerticesTest.php deleted file mode 100644 index e1efee94..00000000 --- a/tests/Set/BaseVerticesTest.php +++ /dev/null @@ -1,314 +0,0 @@ -createVertex(); - - $verticesFromArray = $this->createVertices(array($vertex)); - $this->assertInstanceOf('Graphp\Graph\Set\Vertices', $verticesFromArray); - $this->assertSame($vertex, $verticesFromArray->getVertexFirst()); - - $verticesFromVertices = Vertices::factory($verticesFromArray); - $this->assertSame($verticesFromArray, $verticesFromVertices); - } - - public function testEmpty() - { - $vertices = $this->createVertices(array()); - - $this->assertEquals(0, $vertices->count()); - $this->assertEquals(0, count($vertices)); - $this->assertEquals(array(), $vertices->getIds()); - $this->assertEquals(array(), $vertices->getMap()); - $this->assertEquals(array(), $vertices->getVector()); - $this->assertTrue($vertices->isEmpty()); - $this->assertTrue($vertices->getVertices()->isEmpty()); - $this->assertTrue($vertices->getVerticesOrder(function (Vertex $vertex) { - return $vertex->getId(); - })->isEmpty()); - $this->assertTrue($vertices->getVerticesOrder('id')->isEmpty()); - $this->assertTrue($vertices->getVerticesDistinct()->isEmpty()); - $this->assertTrue($vertices->getVerticesMatch(function() { })->isEmpty()); - $this->assertFalse($vertices->hasDuplicates()); - - return $vertices; - } - - /** - * - * @param Vertices $vertices - * @depends testEmpty - * @expectedException UnderflowException - */ - public function testEmptyDoesNotHaveFirst(Vertices $vertices) - { - $vertices->getVertexFirst(); - } - - /** - * - * @param Vertices $vertices - * @depends testEmpty - * @expectedException UnderflowException - */ - public function testEmptyDoesNotHaveLast(Vertices $vertices) - { - $vertices->getVertexLast(); - } - - /** - * - * @param Vertices $vertices - * @depends testEmpty - * @expectedException UnderflowException - */ - public function testEmptyDoesNotHaveRandom(Vertices $vertices) - { - $vertices->getVertexRandom(); - } - - /** - * - * @param Vertices $vertices - * @depends testEmpty - * @expectedException UnderflowException - */ - public function testEmptyDoesNotHaveOrdered(Vertices $vertices) - { - $vertices->getVertexOrder('group'); - } - - public function testTwo() - { - $graph = new Graph(); - $v1 = $graph->createVertex(1); - $v2 = $graph->createVertex(2); - - $vertices = $this->createVertices(array(1 => $v1, 2 => $v2)); - - $this->assertTrue($vertices->hasVertexId(1)); - $this->assertTrue($vertices->hasVertexId(2)); - $this->assertFalse($vertices->hasVertexId(3)); - $this->assertEquals(2, count($vertices)); - - $this->assertSame($v1, $vertices->getVertexFirst()); - $this->assertSame($v1, $vertices->getVertexId(1)); - - $this->assertSame($v2, $vertices->getVertexLast()); - $this->assertSame($v2, $vertices->getVertexId(2)); - - $this->assertEquals(1, $vertices->getIndexVertex($v1)); - $this->assertEquals(array(1, 2), $vertices->getIds()); - - return $vertices; - } - - /** - * - * @param Vertices $vertices - * @depends testTwo - * @expectedException OutOfBoundsException - */ - public function testTwoDoesNotContainId3(Vertices $vertices) - { - $vertices->getVertexId(3); - } - - /** - * - * @param Vertices $vertices - * @depends testTwo - * @expectedException OutOfBoundsException - */ - public function testTwoDoesNotContainVertex3(Vertices $vertices) - { - $graph = new Graph(); - $v3 = $graph->createVertex(3); - - $vertices->getIndexVertex($v3); - } - - /** - * - * @param Vertices $vertices - * @depends testTwo - */ - public function testTwoAsMap(Vertices $vertices) - { - $distinct = $vertices->getVerticesDistinct(); - - $this->assertInstanceOf('Graphp\Graph\Set\VerticesMap', $distinct); - $this->assertEquals(2, count($distinct)); - $this->assertEquals(array(1, 2), $distinct->getIds()); - } - - /** - * - * @param Vertices $vertices - * @depends testTwo - */ - public function testTwoRandom(Vertices $vertices) - { - $vertexRandom = $vertices->getVertexRandom(); - - $this->assertInstanceOf('Graphp\Graph\Vertex', $vertexRandom); - $this->assertTrue($vertices->hasVertexId($vertexRandom->getId())); - } - - /** - * - * @param Vertices $vertices - * @depends testTwo - */ - public function testTwoShuffled(Vertices $vertices) - { - $verticesRandom = $vertices->getVerticesShuffled(); - - $this->assertInstanceOf('Graphp\Graph\Set\Vertices', $verticesRandom); - $this->assertEquals(2, count($verticesRandom)); - } - - /** - * - * @param Vertices $vertices - * @depends testTwo - */ - public function testTwoIterator(Vertices $vertices) - { - $this->assertInstanceOf('Iterator', $vertices->getIterator()); - - $values = array_values(iterator_to_array($vertices)); - $this->assertEquals($vertices->getVector(), $values); - } - - /** - * - * @param Vertices $vertices - * @depends testTwo - */ - public function testTwoMatch(Vertices $vertices) - { - $verticesMatch = $vertices->getVerticesMatch(array($this, 'returnTrue')); - $this->assertEquals($vertices->getVector(), $verticesMatch->getVector()); - - $vertexMatch = $vertices->getVertexMatch(array($this, 'returnTrue')); - $this->assertEquals($vertices->getVertexFirst(), $vertexMatch); - } - - public function returnTrue(Vertex $vertex) - { - return true; - } - - public function testOrderByGroup() - { - $graph = new Graph(); - $graph->createVertex()->setAttribute('group', 1); - $graph->createVertex()->setAttribute('group', 100); - $graph->createVertex()->setAttribute('group', 5); - $graph->createVertex()->setAttribute('group', 100); - $graph->createVertex()->setAttribute('group', 100); - $graph->createVertex()->setAttribute('group', 2); - $biggest = $graph->createVertex()->setAttribute('group', 200); - - $vertices = $graph->getVertices(); - $verticesOrdered = $vertices->getVerticesOrder('group'); - - $this->assertInstanceOf('Graphp\Graph\Set\Vertices', $verticesOrdered); - $this->assertEquals(1, $verticesOrdered->getVertexFirst()->getAttribute('group')); - $this->assertEquals(200, $verticesOrdered->getVertexLast()->getAttribute('group')); - - $this->assertSame($biggest, $verticesOrdered->getVertexLast()); - $this->assertSame($biggest, $vertices->getVertexOrder(function (Vertex $vertex) { - return $vertex->getAttribute('group'); - }, true)); - $this->assertSame($biggest, $vertices->getVertexOrder('group', true)); - - $sumgroups = function(Vertex $vertex) { - return $vertex->getAttribute('group'); - }; - $this->assertSame(508, $vertices->getSumCallback($sumgroups)); - $this->assertSame(508, $verticesOrdered->getSumCallback($sumgroups)); - } - - public function testOrderByAttribute() - { - $graph = new Graph(); - $v1 = $graph->createVertex()->setAttribute('votes', 20); - $v2 = $graph->createVertex()->setAttribute('votes', 10); - - $vertices = $graph->getVertices()->getVerticesOrder('votes'); - - $this->assertInstanceOf('Graphp\Graph\Set\Vertices', $vertices); - $this->assertSame($v2, $vertices->getVertexFirst()); - $this->assertSame($v1, $vertices->getVertexLast()); - - $this->assertSame($v1, $vertices->getVertexOrder('votes', true)); - } - - /** - * - * @param Vertices $vertices - * @depends testEmpty - */ - public function testEmptyIntersectionSelf(Vertices $vertices) - { - $verticesIntersection = $vertices->getVerticesIntersection($vertices); - $this->assertCount(0, $verticesIntersection); - } - - /** - * - * @param Vertices $verticesEmpty - * @param Vertices $verticesTwo - * @depends testEmpty - * @depends testTwo - */ - public function testEmptyIntersectionTwo(Vertices $verticesEmpty, Vertices $verticesTwo) - { - $verticesIntersection = $verticesEmpty->getVerticesIntersection($verticesTwo); - $this->assertCount(0, $verticesIntersection); - } - - /** - * - * @param Vertices $vertices - * @depends testTwo - */ - public function testTwoIntersectionSelf(Vertices $vertices) - { - $verticesIntersection = $vertices->getVerticesIntersection($vertices); - $this->assertCount(2, $verticesIntersection); - $this->assertEquals($vertices->getMap(), $verticesIntersection->getMap()); - } - - /** - * - * @param Vertices $verticesTwo - * @param Vertices $verticesEmpty - * @depends testTwo - * @depends testEmpty - */ - public function testTwoIntersectionEmpty(Vertices $verticesTwo, Vertices $verticesEmpty) - { - $verticesIntersection = $verticesTwo->getVerticesIntersection($verticesEmpty); - $this->assertCount(0, $verticesIntersection); - } -} diff --git a/tests/Set/EdgesTest.php b/tests/Set/EdgesTest.php index 9ea3b40f..469b0f35 100644 --- a/tests/Set/EdgesTest.php +++ b/tests/Set/EdgesTest.php @@ -22,7 +22,7 @@ public function testFactory() { // 1 -> 1 $graph = new Graph(); - $v1 = $graph->createVertex(1); + $v1 = $graph->createVertex(); $e1 = $graph->createEdgeDirected($v1, $v1); $edgesFromArray = $this->createEdges(array($e1)); @@ -53,7 +53,6 @@ public function testEmpty() } /** - * * @param Edges $edges * @depends testEmpty * @expectedException UnderflowException @@ -64,7 +63,6 @@ public function testEmptyDoesNotHaveFirst(Edges $edges) } /** - * * @param Edges $edges * @depends testEmpty * @expectedException UnderflowException @@ -75,7 +73,6 @@ public function testEmptyDoesNotHaveLast(Edges $edges) } /** - * * @param Edges $edges * @depends testEmpty * @expectedException UnderflowException @@ -86,7 +83,6 @@ public function testEmptyDoesNotHaveRandom(Edges $edges) } /** - * * @param Edges $edges * @depends testEmpty * @expectedException UnderflowException @@ -100,9 +96,9 @@ public function testTwo() { // 1 -- 2 -- 3 $graph = new Graph(); - $v1 = $graph->createVertex(1); - $v2 = $graph->createVertex(2); - $v3 = $graph->createVertex(3); + $v1 = $graph->createVertex(); + $v2 = $graph->createVertex(); + $v3 = $graph->createVertex(); $e1 = $graph->createEdgeUndirected($v1, $v2); $e2 = $graph->createEdgeUndirected($v2, $v3); @@ -122,7 +118,6 @@ public function testTwo() } /** - * * @param Edges $edges * @depends testTwo * @expectedException OutOfBoundsException @@ -133,7 +128,6 @@ public function testTwoDoesNotContainIndex3(Edges $edges) } /** - * * @param Edges $edges * @depends testTwo * @expectedException OutOfBoundsException @@ -141,14 +135,13 @@ public function testTwoDoesNotContainIndex3(Edges $edges) public function testTwoDoesNotContainEdge3(Edges $edges) { $graph = new Graph(); - $v3 = $graph->createVertex(3); + $v3 = $graph->createVertex(); $e3 = $graph->createEdgeUndirected($v3, $v3); $edges->getIndexEdge($e3); } /** - * * @param Edges $edges * @depends testTwo */ @@ -161,7 +154,6 @@ public function testTwoAsMap(Edges $edges) } /** - * * @param Edges $edges * @depends testTwo */ @@ -174,7 +166,6 @@ public function testTwoRandom(Edges $edges) } /** - * * @param Edges $edges * @depends testTwo */ @@ -187,7 +178,6 @@ public function testTwoShuffled(Edges $edges) } /** - * * @param Edges $edges * @depends testTwo */ @@ -200,7 +190,6 @@ public function testTwoIterator(Edges $edges) } /** - * * @param Edges $edges * @depends testTwo */ @@ -214,7 +203,6 @@ public function testTwoMatch(Edges $edges) } /** - * * @param Edges $edges * @depends testTwo */ @@ -225,7 +213,6 @@ public function testTwoMatchEmpty(Edges $edges) } /** - * * @param Edges $edges * @depends testTwo * @expectedException UnderflowException @@ -248,8 +235,8 @@ public function returnFalse(Edge $edge) public function testOrderByGroup() { $graph = new Graph(); - $v1 = $graph->createVertex(1); - $v2 = $graph->createVertex(2); + $v1 = $graph->createVertex(); + $v2 = $graph->createVertex(); $graph->createEdgeUndirected($v1, $v2)->setAttribute('weight', 1); $graph->createEdgeUndirected($v1, $v2)->setAttribute('weight', 100); $graph->createEdgeUndirected($v1, $v2)->setAttribute('weight', 5); @@ -282,8 +269,8 @@ public function testOrderByGroup() public function testOrderByAttribute() { $graph = new Graph(); - $v1 = $graph->createVertex(1); - $v2 = $graph->createVertex(2); + $v1 = $graph->createVertex(); + $v2 = $graph->createVertex(); $e1 = $graph->createEdgeUndirected($v1, $v2)->setAttribute('weight', 20); $e2 = $graph->createEdgeUndirected($v1, $v2)->setAttribute('weight', 10); @@ -302,8 +289,8 @@ public function testOrderByAttribute() public function testIntersection() { $graph = new Graph(); - $v1 = $graph->createVertex(1); - $v2 = $graph->createVertex(2); + $v1 = $graph->createVertex(); + $v2 = $graph->createVertex(); $e1 = $graph->createEdgeUndirected($v1, $v2); $e2 = $graph->createEdgeUndirected($v1, $v2); $e3 = $graph->createEdgeUndirected($v1, $v2); @@ -319,8 +306,8 @@ public function testIntersection() public function testIntersectionDuplicates() { $graph = new Graph(); - $v1 = $graph->createVertex(1); - $v2 = $graph->createVertex(2); + $v1 = $graph->createVertex(); + $v2 = $graph->createVertex(); $e1 = $graph->createEdgeUndirected($v1, $v2); $edges1 = $this->createEdges(array($e1, $e1, $e1)); diff --git a/tests/Set/VerticesMapTest.php b/tests/Set/VerticesMapTest.php deleted file mode 100644 index 7575e4bc..00000000 --- a/tests/Set/VerticesMapTest.php +++ /dev/null @@ -1,13 +0,0 @@ -createVertex(); + + $verticesFromArray = $this->createVertices(array($vertex)); + $this->assertInstanceOf('Graphp\Graph\Set\Vertices', $verticesFromArray); + $this->assertSame($vertex, $verticesFromArray->getVertexFirst()); + + $verticesFromVertices = Vertices::factory($verticesFromArray); + $this->assertSame($verticesFromArray, $verticesFromVertices); + } + + public function testEmpty() + { + $vertices = $this->createVertices(array()); + + $this->assertEquals(0, $vertices->count()); + $this->assertEquals(0, count($vertices)); + $this->assertEquals(array(), $vertices->getVector()); + $this->assertTrue($vertices->isEmpty()); + $this->assertTrue($vertices->getVertices()->isEmpty()); + $this->assertTrue($vertices->getVerticesOrder(function (Vertex $vertex) { + return $vertex->getAttribute('id'); + })->isEmpty()); + $this->assertTrue($vertices->getVerticesOrder('id')->isEmpty()); + $this->assertTrue($vertices->getVerticesDistinct()->isEmpty()); + $this->assertTrue($vertices->getVerticesMatch(function() { })->isEmpty()); + $this->assertFalse($vertices->hasDuplicates()); + + return $vertices; + } + + /** + * @param Vertices $vertices + * @depends testEmpty + * @expectedException UnderflowException + */ + public function testEmptyDoesNotHaveFirst(Vertices $vertices) + { + $vertices->getVertexFirst(); + } + + /** + * @param Vertices $vertices + * @depends testEmpty + * @expectedException UnderflowException + */ + public function testEmptyDoesNotHaveLast(Vertices $vertices) + { + $vertices->getVertexLast(); + } + + /** + * @param Vertices $vertices + * @depends testEmpty + * @expectedException UnderflowException + */ + public function testEmptyDoesNotHaveRandom(Vertices $vertices) + { + $vertices->getVertexRandom(); + } + + /** + * @param Vertices $vertices + * @depends testEmpty + */ + public function testEmptyDoesNotHaveMatching(Vertices $vertices) + { + $this->assertFalse($vertices->hasVertexMatch(function () { return true; })); + } + + /** + * @param Vertices $vertices + * @depends testEmpty + * @expectedException UnderflowException + */ + public function testGetVertexMatchOneEmptyThrowsUnderflowException(Vertices $vertices) + { + $vertices->getVertexMatch(function () { return true; }); + } + + /** + * @param Vertices $vertices + * @depends testEmpty + * @expectedException UnderflowException + */ + public function testEmptyDoesNotHaveOrdered(Vertices $vertices) + { + $vertices->getVertexOrder('group'); + } + + public function testTwo() + { + $graph = new Graph(); + $v1 = $graph->createVertex(); + $v2 = $graph->createVertex(); + + $vertices = $this->createVertices(array(1 => $v1, 2 => $v2)); + $this->assertEquals(2, count($vertices)); + + $this->assertSame($v1, $vertices->getVertexFirst()); + + $this->assertSame($v2, $vertices->getVertexLast()); + + $this->assertEquals(1, $vertices->getIndexVertex($v1)); + + return $vertices; + } + + /** + * @param Vertices $vertices + * @depends testTwo + * @expectedException OutOfBoundsException + */ + public function testTwoDoesNotContainVertex3(Vertices $vertices) + { + $graph = new Graph(); + $v3 = $graph->createVertex(); + + $vertices->getIndexVertex($v3); + } + + /** + * @param Vertices $vertices + * @depends testTwo + */ + public function testTwoAsMap(Vertices $vertices) + { + $distinct = $vertices->getVerticesDistinct(); + + $this->assertInstanceOf('Graphp\Graph\Set\Vertices', $distinct); + $this->assertEquals(2, count($distinct)); + } + + /** + * @param Vertices $vertices + * @depends testTwo + */ + public function testTwoRandom(Vertices $vertices) + { + $vertexRandom = $vertices->getVertexRandom(); + + $this->assertInstanceOf('Graphp\Graph\Vertex', $vertexRandom); + $vertices->getIndexVertex($vertexRandom); + } + + /** + * @param Vertices $vertices + * @depends testTwo + */ + public function testTwoShuffled(Vertices $vertices) + { + $verticesRandom = $vertices->getVerticesShuffled(); + + $this->assertInstanceOf('Graphp\Graph\Set\Vertices', $verticesRandom); + $this->assertEquals(2, count($verticesRandom)); + } + + /** + * @param Vertices $vertices + * @depends testTwo + */ + public function testTwoIterator(Vertices $vertices) + { + $this->assertInstanceOf('Iterator', $vertices->getIterator()); + + $values = array_values(iterator_to_array($vertices)); + $this->assertEquals($vertices->getVector(), $values); + } + + /** + * @param Vertices $vertices + * @depends testTwo + */ + public function testTwoMatch(Vertices $vertices) + { + $verticesMatch = $vertices->getVerticesMatch(array($this, 'returnTrue')); + $this->assertEquals($vertices->getVector(), $verticesMatch->getVector()); + + $vertexMatch = $vertices->getVertexMatch(array($this, 'returnTrue')); + $this->assertEquals($vertices->getVertexFirst(), $vertexMatch); + } + + public function returnTrue(Vertex $vertex) + { + return true; + } + + public function testOrderByGroup() + { + $graph = new Graph(); + $graph->createVertex()->setAttribute('group', 1); + $graph->createVertex()->setAttribute('group', 100); + $graph->createVertex()->setAttribute('group', 5); + $graph->createVertex()->setAttribute('group', 100); + $graph->createVertex()->setAttribute('group', 100); + $graph->createVertex()->setAttribute('group', 2); + $biggest = $graph->createVertex()->setAttribute('group', 200); + + $vertices = $graph->getVertices(); + $verticesOrdered = $vertices->getVerticesOrder('group'); + + $this->assertInstanceOf('Graphp\Graph\Set\Vertices', $verticesOrdered); + $this->assertEquals(1, $verticesOrdered->getVertexFirst()->getAttribute('group')); + $this->assertEquals(200, $verticesOrdered->getVertexLast()->getAttribute('group')); + + $this->assertSame($biggest, $verticesOrdered->getVertexLast()); + $this->assertSame($biggest, $vertices->getVertexOrder(function (Vertex $vertex) { + return $vertex->getAttribute('group'); + }, true)); + $this->assertSame($biggest, $vertices->getVertexOrder('group', true)); + + $sumgroups = function(Vertex $vertex) { + return $vertex->getAttribute('group'); + }; + $this->assertSame(508, $vertices->getSumCallback($sumgroups)); + $this->assertSame(508, $verticesOrdered->getSumCallback($sumgroups)); + } + + public function testOrderByAttribute() + { + $graph = new Graph(); + $v1 = $graph->createVertex()->setAttribute('votes', 20); + $v2 = $graph->createVertex()->setAttribute('votes', 10); + + $vertices = $graph->getVertices()->getVerticesOrder('votes'); + + $this->assertInstanceOf('Graphp\Graph\Set\Vertices', $vertices); + $this->assertSame($v2, $vertices->getVertexFirst()); + $this->assertSame($v1, $vertices->getVertexLast()); + + $this->assertSame($v1, $vertices->getVertexOrder('votes', true)); + } + + /** + * @param Vertices $vertices + * @depends testEmpty + */ + public function testEmptyIntersectionSelf(Vertices $vertices) + { + $verticesIntersection = $vertices->getVerticesIntersection($vertices); + $this->assertCount(0, $verticesIntersection); + } + + /** + * @param Vertices $verticesEmpty + * @param Vertices $verticesTwo + * @depends testEmpty + * @depends testTwo + */ + public function testEmptyIntersectionTwo(Vertices $verticesEmpty, Vertices $verticesTwo) + { + $verticesIntersection = $verticesEmpty->getVerticesIntersection($verticesTwo); + $this->assertCount(0, $verticesIntersection); + } + + /** + * @param Vertices $vertices + * @depends testTwo + */ + public function testTwoIntersectionSelf(Vertices $vertices) + { + $verticesIntersection = $vertices->getVerticesIntersection($vertices); + $this->assertCount(2, $verticesIntersection); + $this->assertEquals($vertices->getVector(), $verticesIntersection->getVector()); + } + + /** + * @param Vertices $verticesTwo + * @param Vertices $verticesEmpty + * @depends testTwo + * @depends testEmpty + */ + public function testTwoIntersectionEmpty(Vertices $verticesTwo, Vertices $verticesEmpty) + { + $verticesIntersection = $verticesTwo->getVerticesIntersection($verticesEmpty); + $this->assertCount(0, $verticesIntersection); + } + public function testFactoryEmptyArray() { $vertices = Vertices::factory(array()); @@ -23,7 +305,7 @@ public function testFactoryEmptyArray() public function testDuplicates() { $graph = new Graph(); - $v1 = $graph->createVertex(1); + $v1 = $graph->createVertex(); $vertices = $this->createVertices(array($v1, $v1, $v1)); @@ -37,6 +319,6 @@ public function testDuplicates() $this->assertCount(1, $verticesDistinct); $this->assertFalse($verticesDistinct->hasDuplicates()); - $this->assertSame($verticesDistinct, $verticesDistinct->getVerticesDistinct()); + $this->assertSame($verticesDistinct->getVector(), $verticesDistinct->getVerticesDistinct()->getVector()); } } diff --git a/tests/TestCase.php b/tests/TestCase.php index c9b2b7d6..9b86a59e 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -25,12 +25,8 @@ protected function assertGraphEquals(Graph $expected, Graph $actual) $this->assertEquals($f($expected), $f($actual)); // next, assert that all vertices in both graphs are the same - // each vertex has a unique ID, therefor it's easy to search a matching partner // do not use assertVertexEquals() in order to not increase assertion counter - - foreach ($expected->getVertices()->getMap() as $vid => $vertex) { - $actual->getVertex($vid); - + foreach ($expected->getVertices() as $vertex) { if ($this->getVertexDump($vertex) !== $this->getVertexDump($vertex)) { $this->fail(); } @@ -70,8 +66,6 @@ protected function assertEdgeEquals(Edge $expected, Edge $actual) private function getVertexDump(Vertex $vertex) { $ret = get_class($vertex); - - $ret .= PHP_EOL . 'id: ' . $vertex->getId(); $ret .= PHP_EOL . 'attributes: ' . json_encode($vertex->getAttributeBag()->getAttributes()); return $ret; @@ -79,12 +73,13 @@ private function getVertexDump(Vertex $vertex) private function getEdgeDump(Edge $edge) { + $vertices = $edge->getGraph()->getVertices(); $ret = get_class($edge) . ' '; if ($edge instanceof EdgeDirected) { - $ret .= $edge->getVertexStart()->getId() . ' -> ' . $edge->getVertexEnd()->getId(); + $ret .= $vertices->getIndexVertex($edge->getVertexStart()) . ' -> ' . $vertices->getIndexVertex($edge->getVertexEnd()); } else { - $vertices = $edge->getVertices()->getIds(); - $ret .= $vertices[0] . ' -- ' . $vertices[1]; + $foo = $edge->getVertices()->getVector(); + $ret .= $vertices->getIndexVertex($foo[0]) . ' -- ' . $vertices->getIndexVertex($foo[1]); } $ret .= PHP_EOL . 'attributes: ' . json_encode($edge->getAttributeBag()->getAttributes()); diff --git a/tests/VertexTest.php b/tests/VertexTest.php index bde5cea7..b7322465 100644 --- a/tests/VertexTest.php +++ b/tests/VertexTest.php @@ -14,41 +14,29 @@ class VertexTest extends AbstractAttributeAwareTest public function setUp() { $this->graph = new Graph(); - $this->vertex = $this->graph->createVertex(1); + $this->vertex = $this->graph->createVertex(); } public function testPrecondition() { $this->assertCount(1, $this->graph->getVertices()); - $this->assertTrue($this->graph->hasVertex(1)); - $this->assertFalse($this->graph->hasVertex(2)); - $this->assertSame($this->vertex, $this->graph->getVertex(1)); + $this->assertEquals(array($this->vertex), $this->graph->getVertices()->getVector()); } public function testConstructor() { - $v2 = new Vertex($this->graph, 2); + $v2 = new Vertex($this->graph); $this->assertCount(2, $this->graph->getVertices()); - $this->assertTrue($this->graph->hasVertex(2)); - - $this->assertSame($v2, $this->graph->getVertex(2)); - } - - /** - * @expectedException OverflowException - */ - public function testCanNotConstructDuplicateVertex() - { - new Vertex($this->graph, 1); + $this->assertEquals(array($this->vertex, $v2), $this->graph->getVertices()->getVector()); } public function testEdges() { // v1 -> v2, v1 -- v3, v1 <- v4 - $v2 = $this->graph->createVertex(2); - $v3 = $this->graph->createVertex(3); - $v4 = $this->graph->createVertex(4); + $v2 = $this->graph->createVertex(); + $v3 = $this->graph->createVertex(); + $v4 = $this->graph->createVertex(); $e1 = $this->graph->createEdgeDirected($this->vertex, $v2); $e2 = $this->graph->createEdgeUndirected($this->vertex, $v3); $e3 = $this->graph->createEdgeDirected($v4, $this->vertex); @@ -111,17 +99,17 @@ public function testCreateEdgeOtherGraphFails() { $graphOther = new Graph(); - $this->graph->createEdgeUndirected($this->vertex, $graphOther->createVertex(2)); + $this->graph->createEdgeUndirected($this->vertex, $graphOther->createVertex()); } /** * @expectedException InvalidArgumentException */ - public function testcreateEdgeDirectedOtherGraphFails() + public function testCreateEdgeDirectedOtherGraphFails() { $graphOther = new Graph(); - $this->graph->createEdgeDirected($this->vertex, $graphOther->createVertex(2)); + $this->graph->createEdgeDirected($this->vertex, $graphOther->createVertex()); } /** @@ -130,8 +118,8 @@ public function testcreateEdgeDirectedOtherGraphFails() public function testRemoveInvalidEdge() { // 2 -- 3 - $v2 = $this->graph->createVertex(2); - $v3 = $this->graph->createVertex(3); + $v2 = $this->graph->createVertex(); + $v3 = $this->graph->createVertex(); $edge = $this->graph->createEdgeUndirected($v2, $v3); $this->vertex->removeEdge($edge); @@ -142,7 +130,7 @@ public function testRemoveWithEdgeLoopUndirected() // 1 -- 1 $this->graph->createEdgeUndirected($this->vertex, $this->vertex); - $this->assertEquals(array(1 => $this->vertex), $this->graph->getVertices()->getMap()); + $this->assertEquals(array($this->vertex), $this->graph->getVertices()->getVector()); $this->vertex->destroy(); @@ -155,7 +143,7 @@ public function testRemoveWithEdgeLoopDirected() // 1 --> 1 $this->graph->createEdgeDirected($this->vertex, $this->vertex); - $this->assertEquals(array(1 => $this->vertex), $this->graph->getVertices()->getMap()); + $this->assertEquals(array($this->vertex), $this->graph->getVertices()->getVector()); $this->vertex->destroy(); @@ -165,6 +153,6 @@ public function testRemoveWithEdgeLoopDirected() protected function createAttributeAware() { - return new Vertex(new Graph(), 1); + return new Vertex(new Graph()); } } diff --git a/tests/WalkTest.php b/tests/WalkTest.php index a58d955b..11e65197 100644 --- a/tests/WalkTest.php +++ b/tests/WalkTest.php @@ -20,9 +20,9 @@ public function testWalkPath() { // 1 -- 2 -- 3 $graph = new Graph(); - $v1 = $graph->createVertex(1); - $v2 = $graph->createVertex(2); - $v3 = $graph->createVertex(3); + $v1 = $graph->createVertex(); + $v2 = $graph->createVertex(); + $v3 = $graph->createVertex(); $e1 = $graph->createEdgeDirected($v1, $v2); $e2 = $graph->createEdgeDirected($v2, $v3); @@ -54,9 +54,9 @@ public function testWalkWithinGraph() { // 1 -- 2 -- 3 $graph = new Graph(); - $v1 = $graph->createVertex(1); - $v2 = $graph->createVertex(2); - $v3 = $graph->createVertex(3); + $v1 = $graph->createVertex(); + $v2 = $graph->createVertex(); + $v3 = $graph->createVertex(); $e1 = $graph->createEdgeDirected($v1, $v2); $graph->createEdgeDirected($v2, $v3); @@ -83,7 +83,7 @@ public function testWalkLoop() { // 1 -- 1 $graph = new Graph(); - $v1 = $graph->createVertex(1); + $v1 = $graph->createVertex(); $e1 = $graph->createEdgeUndirected($v1, $v1); $walk = Walk::factoryFromEdges(array($e1), $v1); @@ -116,7 +116,7 @@ public function testWalkLoopCycle() { // 1 -- 1 $graph = new Graph(); - $v1 = $graph->createVertex(1); + $v1 = $graph->createVertex(); $e1 = $graph->createEdgeUndirected($v1, $v1); $walk = Walk::factoryCycleFromEdges(array($e1), $v1); @@ -135,8 +135,8 @@ public function testWalkCycleFromVerticesIncomplete() { // 1 -- 2 -- 1 $graph = new Graph(); - $v1 = $graph->createVertex(1); - $v2 = $graph->createVertex(2); + $v1 = $graph->createVertex(); + $v2 = $graph->createVertex(); $graph->createEdgeUndirected($v1, $v2); $graph->createEdgeUndirected($v2, $v1); @@ -151,8 +151,8 @@ public function testWalkCycleInvalid() { // 1 -- 2 $graph = new Graph(); - $v1 = $graph->createVertex(1); - $v2 = $graph->createVertex(2); + $v1 = $graph->createVertex(); + $v2 = $graph->createVertex(); $e1 = $graph->createEdgeUndirected($v1, $v2); Walk::factoryCycleFromEdges(array($e1), $v1); @@ -164,7 +164,7 @@ public function testFactoryCycleFromEdgesWithLoopCycle() // ^ | // \---/ $graph = new Graph(); - $v1 = $graph->createVertex(1); + $v1 = $graph->createVertex(); $e1 = $graph->createEdgeDirected($v1, $v1); $cycle = Walk::factoryCycleFromEdges(array($e1), $v1); @@ -176,31 +176,13 @@ public function testFactoryCycleFromEdgesWithLoopCycle() $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->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); + $v1 = $graph->createVertex(); $graph->createEdgeDirected($v1, $v1); $cycle = Walk::factoryCycleFromVertices(array($v1, $v1)); @@ -219,31 +201,20 @@ public function testFactoryCycleFromVerticesWithLoopCycle() public function testFactoryCycleFromVerticesThrowsWhenCycleIsIncomplete() { $graph = new Graph(); - $v1 = $graph->createVertex(1); + $v1 = $graph->createVertex(); // should actually be [v1, v1] Walk::factoryCycleFromVertices(array($v1)); } - /** - * @expectedException InvalidArgumentException - */ - public function testFactoryCycleFromPredecessorMapThrowsForInvalidPredecessors() - { - $graph = new Graph(); - $v1 = $graph->createVertex(1); - - Walk::factoryCycleFromPredecessorMap(array(), $v1); - } - public function testFactoryFromVertices() { // 1 -- 2 // | | // \----/ $graph = new Graph(); - $v1 = $graph->createVertex(1); - $v2 = $graph->createVertex(2); + $v1 = $graph->createVertex(); + $v2 = $graph->createVertex(); $e1 = $graph->createEdgeUndirected($v1, $v2)->setAttribute('weight', 10); $e2 = $graph->createEdgeUndirected($v1, $v2)->setAttribute('weight', 20);