BaseVerticesTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. use Fhaculty\Graph\Exception\OutOfBoundsException;
  3. use Fhaculty\Graph\Graph;
  4. use Fhaculty\Graph\Vertex;
  5. use Fhaculty\Graph\Set\Vertices;
  6. abstract class BaseVerticesTest extends TestCase
  7. {
  8. /**
  9. *
  10. * @param array $vertices
  11. * @return Vertices;
  12. */
  13. abstract protected function createVertices(array $vertices);
  14. public function testFactory()
  15. {
  16. $graph = new Graph();
  17. $vertex = $graph->createVertex();
  18. $verticesFromArray = $this->createVertices(array($vertex));
  19. $this->assertInstanceOf('Fhaculty\Graph\Set\Vertices', $verticesFromArray);
  20. $this->assertSame($vertex, $verticesFromArray->getVertexFirst());
  21. $verticesFromVertices = Vertices::factory($verticesFromArray);
  22. $this->assertSame($verticesFromArray, $verticesFromVertices);
  23. }
  24. public function testEmpty()
  25. {
  26. $vertices = $this->createVertices(array());
  27. $this->assertEquals(0, $vertices->count());
  28. $this->assertEquals(0, count($vertices));
  29. $this->assertEquals(array(), $vertices->getIds());
  30. $this->assertEquals(array(), $vertices->getMap());
  31. $this->assertEquals(array(), $vertices->getVector());
  32. $this->assertTrue($vertices->isEmpty());
  33. $this->assertTrue($vertices->getVertices()->isEmpty());
  34. $this->assertTrue($vertices->getVerticesOrder(Vertices::ORDER_ID)->isEmpty());
  35. $this->assertTrue($vertices->getVerticesDistinct()->isEmpty());
  36. $this->assertTrue($vertices->getVerticesMatch(function() { })->isEmpty());
  37. $this->assertFalse($vertices->hasDuplicates());
  38. return $vertices;
  39. }
  40. /**
  41. *
  42. * @param Vertices $vertices
  43. * @depends testEmpty
  44. * @expectedException UnderflowException
  45. */
  46. public function testEmptyDoesNotHaveFirst(Vertices $vertices)
  47. {
  48. $vertices->getVertexFirst();
  49. }
  50. /**
  51. *
  52. * @param Vertices $vertices
  53. * @depends testEmpty
  54. * @expectedException UnderflowException
  55. */
  56. public function testEmptyDoesNotHaveLast(Vertices $vertices)
  57. {
  58. $vertices->getVertexLast();
  59. }
  60. /**
  61. *
  62. * @param Vertices $vertices
  63. * @depends testEmpty
  64. * @expectedException UnderflowException
  65. */
  66. public function testEmptyDoesNotHaveOrdered(Vertices $vertices)
  67. {
  68. $vertices->getVertexOrder(Vertices::ORDER_ID);
  69. }
  70. public function testTwo()
  71. {
  72. $graph = new Graph();
  73. $v1 = $graph->createVertex(1);
  74. $v2 = $graph->createVertex(2);
  75. $vertices = $this->createVertices(array(1 => $v1, 2 => $v2));
  76. $this->assertTrue($vertices->hasVertexId(1));
  77. $this->assertTrue($vertices->hasVertexId(2));
  78. $this->assertFalse($vertices->hasVertexId(3));
  79. $this->assertEquals(2, count($vertices));
  80. $this->assertSame($v1, $vertices->getVertexFirst());
  81. $this->assertSame($v1, $vertices->getVertexId(1));
  82. $this->assertSame($v2, $vertices->getVertexLast());
  83. $this->assertSame($v2, $vertices->getVertexId(2));
  84. $this->assertEquals(1, $vertices->getIndexVertex($v1));
  85. $this->assertEquals(array(1, 2), $vertices->getIds());
  86. return $vertices;
  87. }
  88. /**
  89. *
  90. * @param Vertices $vertices
  91. * @depends testTwo
  92. * @expectedException OutOfBoundsException
  93. */
  94. public function testTwoDoesNotContainId3(Vertices $vertices)
  95. {
  96. $vertices->getVertexId(3);
  97. }
  98. /**
  99. *
  100. * @param Vertices $vertices
  101. * @depends testTwo
  102. * @expectedException OutOfBoundsException
  103. */
  104. public function testTwoDoesNotContainVertex3(Vertices $vertices)
  105. {
  106. $graph = new Graph();
  107. $v3 = $graph->createVertex(3);
  108. $vertices->getIndexVertex($v3);
  109. }
  110. /**
  111. *
  112. * @param Vertices $vertices
  113. * @depends testTwo
  114. */
  115. public function testTwoAsMap(Vertices $vertices)
  116. {
  117. $distinct = $vertices->getVerticesDistinct();
  118. $this->assertInstanceOf('Fhaculty\Graph\Set\VerticesMap', $distinct);
  119. $this->assertEquals(2, count($distinct));
  120. $this->assertEquals(array(1, 2), $distinct->getIds());
  121. }
  122. /**
  123. *
  124. * @param Vertices $vertices
  125. * @depends testTwo
  126. */
  127. public function testTwoRandom(Vertices $vertices)
  128. {
  129. $vertexRandom = $vertices->getVertexOrder(Vertices::ORDER_RANDOM);
  130. $this->assertInstanceOf('Fhaculty\Graph\Vertex', $vertexRandom);
  131. $this->assertTrue($vertices->hasVertexId($vertexRandom->getId()));
  132. $verticesRandom = $vertices->getVerticesOrder(Vertices::ORDER_RANDOM);
  133. $this->assertInstanceOf('Fhaculty\Graph\Set\Vertices', $verticesRandom);
  134. $this->assertEquals(2, count($verticesRandom));
  135. }
  136. /**
  137. *
  138. * @param Vertices $vertices
  139. * @depends testTwo
  140. */
  141. public function testTwoIterator(Vertices $vertices)
  142. {
  143. $this->assertInstanceOf('Iterator', $vertices->getIterator());
  144. $values = array_values(iterator_to_array($vertices));
  145. $this->assertEquals($vertices->getVector(), $values);
  146. }
  147. /**
  148. *
  149. * @param Vertices $vertices
  150. * @depends testTwo
  151. */
  152. public function testTwoMatch(Vertices $vertices)
  153. {
  154. $verticesMatch = $vertices->getVerticesMatch(array($this, 'returnTrue'));
  155. $this->assertEquals($vertices->getVector(), $verticesMatch->getVector());
  156. $vertexMatch = $vertices->getVertexMatch(array($this, 'returnTrue'));
  157. $this->assertEquals($vertices->getVertexFirst(), $vertexMatch);
  158. }
  159. public function returnTrue(Vertex $vertex)
  160. {
  161. return true;
  162. }
  163. public function testOrderByGroup()
  164. {
  165. $graph = new Graph();
  166. $graph->createVertex()->setGroup(1);
  167. $graph->createVertex()->setGroup(100);
  168. $graph->createVertex()->setGroup(5);
  169. $graph->createVertex()->setGroup(100);
  170. $graph->createVertex()->setGroup(100);
  171. $graph->createVertex()->setGroup(2);
  172. $biggest = $graph->createVertex()->setGroup(200);
  173. $vertices = $graph->getVertices();
  174. $verticesOrdered = $vertices->getVerticesOrder(Vertices::ORDER_GROUP);
  175. $this->assertInstanceOf('Fhaculty\Graph\Set\Vertices', $verticesOrdered);
  176. $this->assertEquals(1, $verticesOrdered->getVertexFirst()->getGroup());
  177. $this->assertEquals(200, $verticesOrdered->getVertexLast()->getGroup());
  178. $this->assertSame($biggest, $verticesOrdered->getVertexLast());
  179. $this->assertSame($biggest, $vertices->getVertexOrder(Vertices::ORDER_GROUP, true));
  180. $sumgroups = function(Vertex $vertex) {
  181. return $vertex->getGroup();
  182. };
  183. $this->assertSame(508, $vertices->getSumCallback($sumgroups));
  184. $this->assertSame(508, $verticesOrdered->getSumCallback($sumgroups));
  185. }
  186. /**
  187. *
  188. * @param Vertices $vertices
  189. * @depends testEmpty
  190. */
  191. public function testEmptyIntersectionSelf(Vertices $vertices)
  192. {
  193. $verticesIntersection = $vertices->getVerticesIntersection($vertices);
  194. $this->assertCount(0, $verticesIntersection);
  195. }
  196. /**
  197. *
  198. * @param Vertices $verticesEmpty
  199. * @param Vertices $verticesTwo
  200. * @depends testEmpty
  201. * @depends testTwo
  202. */
  203. public function testEmptyIntersectionTwo(Vertices $verticesEmpty, Vertices $verticesTwo)
  204. {
  205. $verticesIntersection = $verticesEmpty->getVerticesIntersection($verticesTwo);
  206. $this->assertCount(0, $verticesIntersection);
  207. }
  208. /**
  209. *
  210. * @param Vertices $vertices
  211. * @depends testTwo
  212. */
  213. public function testTwoIntersectionSelf(Vertices $vertices)
  214. {
  215. $verticesIntersection = $vertices->getVerticesIntersection($vertices);
  216. $this->assertCount(2, $verticesIntersection);
  217. $this->assertEquals($vertices->getMap(), $verticesIntersection->getMap());
  218. }
  219. /**
  220. *
  221. * @param Vertices $verticesTwo
  222. * @param Vertices $verticesEmpty
  223. * @depends testTwo
  224. * @depends testEmpty
  225. */
  226. public function testTwoIntersectionEmpty(Vertices $verticesTwo, Vertices $verticesEmpty)
  227. {
  228. $verticesIntersection = $verticesTwo->getVerticesIntersection($verticesEmpty);
  229. $this->assertCount(0, $verticesIntersection);
  230. }
  231. }