GraphTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. <?php
  2. use Fhaculty\Graph\Exception\RuntimeException;
  3. use Fhaculty\Graph\Vertex;
  4. use Fhaculty\Graph\Exception\OverflowException;
  5. use Fhaculty\Graph\Exception\InvalidArgumentException;
  6. use Fhaculty\Graph\Graph;
  7. class GraphTest extends AbstractAttributeAwareTest
  8. {
  9. public function setup()
  10. {
  11. $this->graph = new Graph();
  12. }
  13. public function testVertexClone()
  14. {
  15. $graph = new Graph();
  16. $vertex = $graph->createVertex(123)->setBalance(10)->setGroup(4);
  17. $newgraph = new Graph();
  18. $newvertex = $newgraph->createVertexClone($vertex);
  19. $this->assertVertexEquals($vertex, $newvertex);
  20. }
  21. /**
  22. * test to make sure vertex can not be cloned into same graph (due to duplicate id)
  23. *
  24. * @expectedException RuntimeException
  25. */
  26. public function testInvalidVertexClone()
  27. {
  28. $graph = new Graph();
  29. $vertex = $graph->createVertex(123);
  30. $graph->createVertexClone($vertex);
  31. }
  32. public function testGraphCloneEmpty()
  33. {
  34. $graph = new Graph();
  35. $newgraph = $graph->createGraphClone();
  36. $this->assertGraphEquals($graph, $newgraph);
  37. }
  38. /**
  39. * @expectedException OutOfBoundsException
  40. */
  41. public function testGetVertexNonexistant()
  42. {
  43. $graph = new Graph();
  44. $graph->getVertex('non-existant');
  45. }
  46. public function testGraphClone()
  47. {
  48. $graph = new Graph();
  49. $graph->createVertex(123)->setBalance(10)->setGroup(4);
  50. $newgraph = $graph->createGraphClone();
  51. $this->assertGraphEquals($graph, $newgraph);
  52. $graphClonedTwice = $newgraph->createGraphClone();
  53. $this->assertGraphEquals($graph, $graphClonedTwice);
  54. }
  55. public function testGraphCloneEdgeless()
  56. {
  57. $graph = new Graph();
  58. $graph->createVertex(1)->createEdgeTo($graph->createVertex(2));
  59. $graph->createVertex(3)->createEdge($graph->getVertex(2));
  60. $graphEdgeless = $graph->createGraphCloneEdgeless();
  61. $graphExpected = new Graph();
  62. $graphExpected->createVertex(1);
  63. $graphExpected->createVertex(2);
  64. $graphExpected->createVertex(3);
  65. $this->assertGraphEquals($graphExpected, $graphEdgeless);
  66. }
  67. /**
  68. * check to make sure we can actually create vertices with automatic IDs
  69. */
  70. public function testCanCreateVertex()
  71. {
  72. $graph = new Graph();
  73. $vertex = $graph->createVertex();
  74. $this->assertInstanceOf('\Fhaculty\Graph\Vertex', $vertex);
  75. }
  76. /**
  77. * check to make sure we can actually create vertices with automatic IDs
  78. */
  79. public function testCanCreateVertexId()
  80. {
  81. $graph = new Graph();
  82. $vertex = $graph->createVertex(11);
  83. $this->assertInstanceOf('\Fhaculty\Graph\Vertex', $vertex);
  84. $this->assertEquals(11, $vertex->getId());
  85. }
  86. /**
  87. * fail to create two vertices with same ID
  88. * @expectedException OverflowException
  89. */
  90. public function testFailDuplicateVertex()
  91. {
  92. $graph = new Graph();
  93. $graph->createVertex(33);
  94. $graph->createVertex(33);
  95. }
  96. /**
  97. * @expectedException InvalidArgumentException
  98. */
  99. public function testCreateInvalidId()
  100. {
  101. $graph = new Graph();
  102. $graph->createVertex(array('invalid'));
  103. }
  104. public function testCreateDuplicateReturn()
  105. {
  106. $graph = new Graph();
  107. $v1 = $graph->createVertex(1);
  108. $v1again = $graph->createVertex(1, true);
  109. $this->assertSame($v1, $v1again);
  110. }
  111. public function testHasVertex()
  112. {
  113. $graph = new Graph();
  114. $graph->createVertex(1);
  115. $graph->createVertex('string');
  116. // check integer IDs
  117. $this->assertFalse($graph->hasVertex(2));
  118. $this->assertTrue($graph->hasVertex(1));
  119. // check string IDs
  120. $this->assertFalse($graph->hasVertex('non-existant'));
  121. $this->assertTrue($graph->hasVertex('string'));
  122. // integer IDs can also be checked as string IDs
  123. $this->assertTrue($graph->hasVertex('1'));
  124. }
  125. public function testCreateMultigraph()
  126. {
  127. $graph = new Graph();
  128. $v1 = $graph->createVertex(1);
  129. $v2 = $graph->createVertex(2);
  130. $e1 = $v1->createEdge($v2);
  131. $e2 = $v1->createEdge($v2);
  132. $this->assertEquals(2, count($graph->getEdges()));
  133. $this->assertEquals(2, count($v1->getEdges()));
  134. $this->assertEquals(array(2, 2), $v1->getVerticesEdge()->getIds());
  135. }
  136. public function testCreateMixedGraph()
  137. {
  138. // v1 -- v2 -> v3
  139. $graph = new Graph();
  140. $v1 = $graph->createVertex(1);
  141. $v2 = $graph->createVertex(2);
  142. $v3 = $graph->createVertex(3);
  143. $v1->createEdge($v2);
  144. $v2->createEdgeTo($v3);
  145. $this->assertEquals(2, count($graph->getEdges()));
  146. $this->assertEquals(2, count($v2->getEdges()));
  147. $this->assertEquals(2, count($v2->getEdgesOut()));
  148. $this->assertEquals(1, count($v2->getEdgesIn()));
  149. $this->assertEquals(array(1, 3), $v2->getVerticesEdgeTo()->getIds());
  150. $this->assertEquals(array(1), $v2->getVerticesEdgeFrom()->getIds());
  151. }
  152. public function testCreateVerticesNone()
  153. {
  154. $graph = new Graph();
  155. $this->assertEquals(array(), $graph->createVertices(0)->getVector());
  156. $this->assertEquals(array(), $graph->createVertices(array())->getVector());
  157. $this->assertEquals(0, count($graph->getVertices()));
  158. }
  159. /**
  160. * expect to fail for invalid number of vertices
  161. * @expectedException InvalidArgumentException
  162. * @dataProvider testCreateVerticesFailProvider
  163. */
  164. public function testCreateVerticesFail($number)
  165. {
  166. $graph = new Graph();
  167. $graph->createVertices($number);
  168. }
  169. public static function testCreateVerticesFailProvider()
  170. {
  171. return array(
  172. array(-1),
  173. array("10"),
  174. array(0.5),
  175. array(null),
  176. array(array(1, 1))
  177. );
  178. }
  179. public function testCreateVerticesOkay()
  180. {
  181. $graph = new Graph();
  182. $vertices = $graph->createVertices(2);
  183. $this->assertCount(2, $vertices);
  184. $this->assertEquals(array(0, 1), $graph->getVertices()->getIds());
  185. $vertices = $graph->createVertices(array(7, 9));
  186. $this->assertCount(2, $vertices);
  187. $this->assertEquals(array(0, 1, 7, 9), $graph->getVertices()->getIds());
  188. $vertices = $graph->createVertices(3);
  189. $this->assertCount(3, $vertices);
  190. $this->assertEquals(array(0, 1, 7, 9, 10, 11, 12), $graph->getVertices()->getIds());
  191. }
  192. public function testCreateVerticesAtomic()
  193. {
  194. $graph = new Graph();
  195. // create vertices 10-19 (inclusive)
  196. $vertices = $graph->createVertices(range(10, 19));
  197. $this->assertCount(10, $vertices);
  198. try {
  199. $graph->createVertices(array(9, 19, 20));
  200. $this->fail('Should be unable to create vertices because of duplicate IDs');
  201. }
  202. catch (OverflowException $ignoreExpected) {
  203. $this->assertEquals(10, count($graph->getVertices()));
  204. }
  205. try {
  206. $graph->createVertices(array(20, 21, 21));
  207. $this->fail('Should be unable to create vertices because of duplicate IDs');
  208. }
  209. catch (InvalidArgumentException $ignoreExpected) {
  210. $this->assertEquals(10, count($graph->getVertices()));
  211. }
  212. }
  213. /**
  214. * @expectedException InvalidArgumentException
  215. */
  216. public function testCreateVerticesContainsInvalid()
  217. {
  218. $graph = new Graph();
  219. $graph->createVertices(array(1, 2, array(), 3));
  220. }
  221. public function testRemoveEdge()
  222. {
  223. // 1 -- 2
  224. $graph = new Graph();
  225. $v1 = $graph->createVertex(1);
  226. $v2 = $graph->createVertex(2);
  227. $edge = $v1->createEdge($v2);
  228. $this->assertEquals(array($edge), $graph->getEdges()->getVector());
  229. $edge->destroy();
  230. //$graph->removeEdge($edge);
  231. $this->assertEquals(array(), $graph->getEdges()->getVector());
  232. return $graph;
  233. }
  234. /**
  235. * @param Graph $graph
  236. * @expectedException InvalidArgumentException
  237. * @depends testRemoveEdge
  238. */
  239. public function testRemoveEdgeInvalid(Graph $graph)
  240. {
  241. $edge = $graph->getVertex(1)->createEdge($graph->getVertex(2));
  242. $edge->destroy();
  243. $edge->destroy();
  244. }
  245. public function testRemoveVertex()
  246. {
  247. $graph = new Graph();
  248. $vertex = $graph->createVertex(1);
  249. $this->assertEquals(array(1 => $vertex), $graph->getVertices()->getMap());
  250. $vertex->destroy();
  251. $this->assertEquals(array(), $graph->getVertices()->getVector());
  252. }
  253. /**
  254. * @expectedException InvalidArgumentException
  255. */
  256. public function testRemoveVertexInvalid()
  257. {
  258. $graph = new Graph();
  259. $vertex = $graph->createVertex(1);
  260. $vertex->destroy();
  261. $vertex->destroy();
  262. }
  263. public function testGetEdgesClones()
  264. {
  265. // 1 -> 2 -> 1
  266. $graph = new Graph();
  267. $v1 = $graph->createVertex(1);
  268. $v2 = $graph->createVertex(2);
  269. $e1 = $v1->createEdgeTo($v2);
  270. $e2 = $v2->createEdgeTo($v1);
  271. $graphClone = $graph->createGraphClone();
  272. $this->assertEdgeEquals($e1, $graphClone->getEdgeClone($e1));
  273. $this->assertEdgeEquals($e2, $graphClone->getEdgeCloneInverted($e1));
  274. }
  275. /**
  276. * @expectedException OverflowException
  277. */
  278. public function testEdgesFailParallel()
  279. {
  280. // 1 -> 2, 1 -> 2
  281. $graph = new Graph();
  282. $v1 = $graph->createVertex(1);
  283. $v2 = $graph->createVertex(2);
  284. $e1 = $v1->createEdgeTo($v2);
  285. $e2 = $v1->createEdgeTo($v2);
  286. // which one to return? e1? e2?
  287. $graph->getEdgeClone($e1);
  288. }
  289. /**
  290. * @expectedException UnderflowException
  291. */
  292. public function testEdgesFailEdgeless()
  293. {
  294. // 1 -> 2
  295. $graph = new Graph();
  296. $v1 = $graph->createVertex(1);
  297. $v2 = $graph->createVertex(2);
  298. $e1 = $v1->createEdgeTo($v2);
  299. $e2 = $v1->createEdgeTo($v2);
  300. $graphCloneEdgeless = $graph->createGraphCloneEdgeless();
  301. // nothing to return
  302. $graphCloneEdgeless->getEdgeClone($e1);
  303. }
  304. public function testCreateGraphCloneVertices()
  305. {
  306. // 1 -- 2 -- 3
  307. $graph = new Graph();
  308. $v1 = $graph->createVertex(1);
  309. $v2 = $graph->createVertex(2);
  310. $v3 = $graph->createVertex(3);
  311. $e1 = $v1->createEdgeTo($v2);
  312. $e2 = $v2->createEdgeTo($v3);
  313. $graphClone = $graph->createGraphCloneVertices(array(1 => $v1, 2 => $v2));
  314. $this->assertEquals(2, count($graphClone->getVertices()));
  315. $this->assertEquals(1, count($graphClone->getEdges()));
  316. }
  317. protected function createAttributeAware()
  318. {
  319. return new Graph();
  320. }
  321. }