EdgesTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <?php
  2. use Fhaculty\Graph\Exception\OutOfBoundsException;
  3. use Fhaculty\Graph\Graph;
  4. use Fhaculty\Graph\Vertex;
  5. use Fhaculty\Graph\Edge\Base as Edge;
  6. use Fhaculty\Graph\Set\Edges;
  7. class EdgesTest extends TestCase
  8. {
  9. /**
  10. *
  11. * @param array $edges
  12. * @return Edges;
  13. */
  14. protected function createEdges(array $edges)
  15. {
  16. return Edges::factory($edges);
  17. }
  18. public function testFactory()
  19. {
  20. // 1 -> 1
  21. $graph = new Graph();
  22. $v1 = $graph->createVertex(1);
  23. $e1 = $v1->createEdgeTo($v1);
  24. $edgesFromArray = $this->createEdges(array($e1));
  25. $this->assertInstanceOf('Fhaculty\Graph\Set\Edges', $edgesFromArray);
  26. $this->assertSame($e1, $edgesFromArray->getEdgeFirst());
  27. $edgesFromEdges = Edges::factory($edgesFromArray);
  28. $this->assertSame($edgesFromArray, $edgesFromEdges);
  29. }
  30. public function testEmpty()
  31. {
  32. $edges = $this->createEdges(array());
  33. $this->assertEquals(0, $edges->count());
  34. $this->assertEquals(0, count($edges));
  35. $this->assertEquals(array(), $edges->getVector());
  36. $this->assertTrue($edges->isEmpty());
  37. $this->assertTrue($edges->getEdges()->isEmpty());
  38. $this->assertTrue($edges->getEdgesOrder(Edges::ORDER_WEIGHT)->isEmpty());
  39. $this->assertTrue($edges->getEdgesDistinct()->isEmpty());
  40. $this->assertTrue($edges->getEdgesMatch(function() { })->isEmpty());
  41. return $edges;
  42. }
  43. /**
  44. *
  45. * @param Edges $edges
  46. * @depends testEmpty
  47. * @expectedException UnderflowException
  48. */
  49. public function testEmptyDoesNotHaveFirst(Edges $edges)
  50. {
  51. $edges->getEdgeFirst();
  52. }
  53. /**
  54. *
  55. * @param Edges $edges
  56. * @depends testEmpty
  57. * @expectedException UnderflowException
  58. */
  59. public function testEmptyDoesNotHaveLast(Edges $edges)
  60. {
  61. $edges->getEdgeLast();
  62. }
  63. /**
  64. *
  65. * @param Edges $edges
  66. * @depends testEmpty
  67. * @expectedException UnderflowException
  68. */
  69. public function testEmptyDoesNotHaveOrdered(Edges $edges)
  70. {
  71. $edges->getEdgeOrder(Edges::ORDER_WEIGHT);
  72. }
  73. public function testTwo()
  74. {
  75. // 1 -- 2 -- 3
  76. $graph = new Graph();
  77. $v1 = $graph->createVertex(1);
  78. $v2 = $graph->createVertex(2);
  79. $v3 = $graph->createVertex(3);
  80. $e1 = $v1->createEdge($v2);
  81. $e2 = $v2->createEdge($v3);
  82. $edges = $this->createEdges(array($e1, $e2));
  83. $this->assertEquals(2, count($edges));
  84. $this->assertSame($e1, $edges->getEdgeFirst());
  85. $this->assertSame($e1, $edges->getEdgeIndex(0));
  86. $this->assertSame($e2, $edges->getEdgeLast());
  87. $this->assertSame($e2, $edges->getEdgeIndex(1));
  88. $this->assertEquals(0, $edges->getIndexEdge($e1));
  89. return $edges;
  90. }
  91. /**
  92. *
  93. * @param Edges $edges
  94. * @depends testTwo
  95. * @expectedException OutOfBoundsException
  96. */
  97. public function testTwoDoesNotContainIndex3(Edges $edges)
  98. {
  99. $edges->getEdgeIndex(3);
  100. }
  101. /**
  102. *
  103. * @param Edges $edges
  104. * @depends testTwo
  105. * @expectedException OutOfBoundsException
  106. */
  107. public function testTwoDoesNotContainEdge3(Edges $edges)
  108. {
  109. $graph = new Graph();
  110. $v3 = $graph->createVertex(3);
  111. $e3 = $v3->createEdge($v3);
  112. $edges->getIndexEdge($e3);
  113. }
  114. /**
  115. *
  116. * @param Edges $edges
  117. * @depends testTwo
  118. */
  119. public function testTwoAsMap(Edges $edges)
  120. {
  121. $distinct = $edges->getEdgesDistinct();
  122. $this->assertInstanceOf('Fhaculty\Graph\Set\Edges', $distinct);
  123. $this->assertEquals(2, count($distinct));
  124. }
  125. /**
  126. *
  127. * @param Edges $edges
  128. * @depends testTwo
  129. */
  130. public function testTwoRandom(Edges $edges)
  131. {
  132. $edgeRandom = $edges->getEdgeOrder(Edges::ORDER_RANDOM);
  133. $this->assertInstanceOf('Fhaculty\Graph\Edge\Base', $edgeRandom);
  134. $edges->getEdgeIndex($edges->getIndexEdge($edgeRandom));
  135. $edgesRandom = $edges->getEdgesOrder(Edges::ORDER_RANDOM);
  136. $this->assertInstanceOf('Fhaculty\Graph\Set\Edges', $edgesRandom);
  137. $this->assertEquals(2, count($edgesRandom));
  138. }
  139. /**
  140. *
  141. * @param Edges $edges
  142. * @depends testTwo
  143. */
  144. public function testTwoIterator(Edges $edges)
  145. {
  146. $this->assertInstanceOf('Iterator', $edges->getIterator());
  147. $values = array_values(iterator_to_array($edges));
  148. $this->assertEquals($edges->getVector(), $values);
  149. }
  150. /**
  151. *
  152. * @param Edges $edges
  153. * @depends testTwo
  154. */
  155. public function testTwoMatch(Edges $edges)
  156. {
  157. $edgesMatch = $edges->getEdgesMatch(array($this, 'returnTrue'));
  158. $this->assertEquals($edges->getVector(), $edgesMatch->getVector());
  159. $edgeMatch = $edges->getEdgeMatch(array($this, 'returnTrue'));
  160. $this->assertEquals($edges->getEdgeFirst(), $edgeMatch);
  161. }
  162. /**
  163. *
  164. * @param Edges $edges
  165. * @depends testTwo
  166. */
  167. public function testTwoMatchEmpty(Edges $edges)
  168. {
  169. $edgesMatch = $edges->getEdgesMatch(array($this, 'returnFalse'));
  170. $this->assertCount(0, $edgesMatch);
  171. }
  172. /**
  173. *
  174. * @param Edges $edges
  175. * @depends testTwo
  176. * @expectedException UnderflowException
  177. */
  178. public function testTwoMatchFail(Edges $edges)
  179. {
  180. $edges->getEdgeMatch(array($this, 'returnFalse'));
  181. }
  182. public function returnTrue(Edge $edge)
  183. {
  184. return true;
  185. }
  186. public function returnFalse(Edge $edge)
  187. {
  188. return false;
  189. }
  190. /**
  191. * @expectedException InvalidArgumentException
  192. */
  193. public function testGetEdgeOrderInvalidSortBy()
  194. {
  195. // 1 -> 1
  196. $graph = new Graph();
  197. $v1 = $graph->createVertex(1);
  198. $v1->createEdgeTo($v1);
  199. $edges = $graph->getEdges();
  200. $edges->getEdgeOrder('not a valid callback');
  201. }
  202. /**
  203. * @expectedException InvalidArgumentException
  204. */
  205. public function testGetEdgesOrderInvalidSortBy()
  206. {
  207. $edges = $this->createEdges(array());
  208. $edges->getEdgesOrder('not a valid callback');
  209. }
  210. public function testOrderByGroup()
  211. {
  212. $graph = new Graph();
  213. $v1 = $graph->createVertex(1);
  214. $v2 = $graph->createVertex(2);
  215. $v1->createEdge($v2)->setWeight(1);
  216. $v1->createEdge($v2)->setWeight(100);
  217. $v1->createEdge($v2)->setWeight(5);
  218. $v1->createEdge($v2)->setWeight(100);
  219. $v1->createEdge($v2)->setWeight(100);
  220. $v1->createEdge($v2)->setWeight(2);
  221. $biggest = $v1->createEdge($v2)->setWeight(200);
  222. $edges = $graph->getEdges();
  223. $edgesOrdered = $edges->getEdgesOrder(Edges::ORDER_WEIGHT);
  224. $this->assertInstanceOf('Fhaculty\Graph\Set\Edges', $edgesOrdered);
  225. $this->assertEquals(1, $edgesOrdered->getEdgeFirst()->getWeight());
  226. $this->assertEquals(200, $edgesOrdered->getEdgeLast()->getWeight());
  227. $this->assertSame($biggest, $edgesOrdered->getEdgeLast());
  228. $this->assertSame($biggest, $edges->getEdgeOrder(Edges::ORDER_WEIGHT, true));
  229. $sumweights = function(Edge $edge) {
  230. return $edge->getWeight();
  231. };
  232. $this->assertSame(508, $edges->getSumCallback($sumweights));
  233. $this->assertSame(508, $edgesOrdered->getSumCallback($sumweights));
  234. }
  235. public function testIntersection()
  236. {
  237. $graph = new Graph();
  238. $v1 = $graph->createVertex(1);
  239. $v2 = $graph->createVertex(2);
  240. $e1 = $v1->createEdge($v2);
  241. $e2 = $v1->createEdge($v2);
  242. $e3 = $v1->createEdge($v2);
  243. $edges1 = $this->createEdges(array($e1, $e2));
  244. $edges2 = $this->createEdges(array($e2, $e3));
  245. $edges3 = $edges1->getEdgesIntersection($edges2);
  246. $this->assertCount(1, $edges3);
  247. $this->assertEquals($e2, $edges3->getEdgeFirst());
  248. }
  249. public function testIntersectionDuplicates()
  250. {
  251. $graph = new Graph();
  252. $v1 = $graph->createVertex(1);
  253. $v2 = $graph->createVertex(2);
  254. $e1 = $v1->createEdge($v2);
  255. $edges1 = $this->createEdges(array($e1, $e1, $e1));
  256. $edges2 = $this->createEdges(array($e1, $e1));
  257. $edges3 = $edges1->getEdgesIntersection($edges2);
  258. $this->assertCount(2, $edges3);
  259. }
  260. public function testIntersectionEmpty()
  261. {
  262. $edges1 = new Edges();
  263. $edges2 = new Edges();
  264. $edges3 = $edges1->getEdgesIntersection($edges2);
  265. $this->assertCount(0, $edges3);
  266. }
  267. }