OrderedHashMapTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Form\Tests\Util;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Form\Util\OrderedHashMap;
  13. /**
  14. * @author Bernhard Schussek <bschussek@gmail.com>
  15. */
  16. class OrderedHashMapTest extends TestCase
  17. {
  18. public function testGet()
  19. {
  20. $map = new OrderedHashMap();
  21. $map['first'] = 1;
  22. $this->assertSame(1, $map['first']);
  23. }
  24. /**
  25. * @expectedException \OutOfBoundsException
  26. */
  27. public function testGetNonExistingFails()
  28. {
  29. $map = new OrderedHashMap();
  30. $map['first'];
  31. }
  32. public function testInsertStringKeys()
  33. {
  34. $map = new OrderedHashMap();
  35. $map['first'] = 1;
  36. $map['second'] = 2;
  37. $this->assertSame(array('first' => 1, 'second' => 2), iterator_to_array($map));
  38. }
  39. public function testInsertNullKeys()
  40. {
  41. $map = new OrderedHashMap();
  42. $map[] = 1;
  43. $map['foo'] = 2;
  44. $map[] = 3;
  45. $this->assertSame(array(0 => 1, 'foo' => 2, 1 => 3), iterator_to_array($map));
  46. }
  47. public function testInsertLooselyEqualKeys()
  48. {
  49. $map = new OrderedHashMap();
  50. $map['1 as a string'] = '1 as a string';
  51. $map[1] = 1;
  52. $this->assertSame(array('1 as a string' => '1 as a string', 1 => 1), iterator_to_array($map));
  53. }
  54. /**
  55. * Updates should not change the position of an element, otherwise we could
  56. * turn foreach loops into endless loops if they change the current
  57. * element.
  58. *
  59. * foreach ($map as $index => $value) {
  60. * $map[$index] = $value + 1;
  61. * }
  62. *
  63. * And we don't want this, right? :)
  64. */
  65. public function testUpdateDoesNotChangeElementPosition()
  66. {
  67. $map = new OrderedHashMap();
  68. $map['first'] = 1;
  69. $map['second'] = 2;
  70. $map['first'] = 1;
  71. $this->assertSame(array('first' => 1, 'second' => 2), iterator_to_array($map));
  72. }
  73. public function testIsset()
  74. {
  75. $map = new OrderedHashMap();
  76. $map['first'] = 1;
  77. $this->assertArrayHasKey('first', $map);
  78. }
  79. public function testIssetReturnsFalseForNonExisting()
  80. {
  81. $map = new OrderedHashMap();
  82. $this->assertArrayNotHasKey('first', $map);
  83. }
  84. public function testIssetReturnsFalseForNull()
  85. {
  86. $map = new OrderedHashMap();
  87. $map['first'] = null;
  88. $this->assertArrayNotHasKey('first', $map);
  89. }
  90. public function testUnset()
  91. {
  92. $map = new OrderedHashMap();
  93. $map['first'] = 1;
  94. $map['second'] = 2;
  95. unset($map['first']);
  96. $this->assertSame(array('second' => 2), iterator_to_array($map));
  97. }
  98. public function testUnsetFromLooselyEqualKeysHashMap()
  99. {
  100. $map = new OrderedHashMap();
  101. $map['1 as a string'] = '1 as a string';
  102. $map[1] = 1;
  103. unset($map[1]);
  104. $this->assertSame(array('1 as a string' => '1 as a string'), iterator_to_array($map));
  105. }
  106. public function testUnsetNonExistingSucceeds()
  107. {
  108. $map = new OrderedHashMap();
  109. $map['second'] = 2;
  110. unset($map['first']);
  111. $this->assertSame(array('second' => 2), iterator_to_array($map));
  112. }
  113. public function testEmptyIteration()
  114. {
  115. $map = new OrderedHashMap();
  116. $it = $map->getIterator();
  117. $it->rewind();
  118. $this->assertFalse($it->valid());
  119. $this->assertNull($it->key());
  120. $this->assertNull($it->current());
  121. }
  122. public function testIterationSupportsInsertion()
  123. {
  124. $map = new OrderedHashMap(array('first' => 1));
  125. $it = $map->getIterator();
  126. $it->rewind();
  127. $this->assertTrue($it->valid());
  128. $this->assertSame('first', $it->key());
  129. $this->assertSame(1, $it->current());
  130. // dynamic modification
  131. $map['added'] = 2;
  132. // iterator is unchanged
  133. $this->assertTrue($it->valid());
  134. $this->assertSame('first', $it->key());
  135. $this->assertSame(1, $it->current());
  136. // continue iteration
  137. $it->next();
  138. $this->assertTrue($it->valid());
  139. $this->assertSame('added', $it->key());
  140. $this->assertSame(2, $it->current());
  141. // end of map
  142. $it->next();
  143. $this->assertFalse($it->valid());
  144. $this->assertNull($it->key());
  145. $this->assertNull($it->current());
  146. }
  147. public function testIterationSupportsDeletionAndInsertion()
  148. {
  149. $map = new OrderedHashMap(array('first' => 1, 'removed' => 2));
  150. $it = $map->getIterator();
  151. $it->rewind();
  152. $this->assertTrue($it->valid());
  153. $this->assertSame('first', $it->key());
  154. $this->assertSame(1, $it->current());
  155. // dynamic modification
  156. unset($map['removed']);
  157. $map['added'] = 3;
  158. // iterator is unchanged
  159. $this->assertTrue($it->valid());
  160. $this->assertSame('first', $it->key());
  161. $this->assertSame(1, $it->current());
  162. // continue iteration
  163. $it->next();
  164. $this->assertTrue($it->valid());
  165. $this->assertSame('added', $it->key());
  166. $this->assertSame(3, $it->current());
  167. // end of map
  168. $it->next();
  169. $this->assertFalse($it->valid());
  170. $this->assertNull($it->key());
  171. $this->assertNull($it->current());
  172. }
  173. public function testIterationSupportsDeletionOfCurrentElement()
  174. {
  175. $map = new OrderedHashMap(array('removed' => 1, 'next' => 2));
  176. $it = $map->getIterator();
  177. $it->rewind();
  178. $this->assertTrue($it->valid());
  179. $this->assertSame('removed', $it->key());
  180. $this->assertSame(1, $it->current());
  181. unset($map['removed']);
  182. // iterator is unchanged
  183. $this->assertTrue($it->valid());
  184. $this->assertSame('removed', $it->key());
  185. $this->assertSame(1, $it->current());
  186. // continue iteration
  187. $it->next();
  188. $this->assertTrue($it->valid());
  189. $this->assertSame('next', $it->key());
  190. $this->assertSame(2, $it->current());
  191. // end of map
  192. $it->next();
  193. $this->assertFalse($it->valid());
  194. $this->assertNull($it->key());
  195. $this->assertNull($it->current());
  196. }
  197. public function testIterationIgnoresReplacementOfCurrentElement()
  198. {
  199. $map = new OrderedHashMap(array('replaced' => 1, 'next' => 2));
  200. $it = $map->getIterator();
  201. $it->rewind();
  202. $this->assertTrue($it->valid());
  203. $this->assertSame('replaced', $it->key());
  204. $this->assertSame(1, $it->current());
  205. $map['replaced'] = 3;
  206. // iterator is unchanged
  207. $this->assertTrue($it->valid());
  208. $this->assertSame('replaced', $it->key());
  209. $this->assertSame(1, $it->current());
  210. // continue iteration
  211. $it->next();
  212. $this->assertTrue($it->valid());
  213. $this->assertSame('next', $it->key());
  214. $this->assertSame(2, $it->current());
  215. // end of map
  216. $it->next();
  217. $this->assertFalse($it->valid());
  218. $this->assertNull($it->key());
  219. $this->assertNull($it->current());
  220. }
  221. public function testIterationSupportsDeletionOfCurrentAndLastElement()
  222. {
  223. $map = new OrderedHashMap(array('removed' => 1));
  224. $it = $map->getIterator();
  225. $it->rewind();
  226. $this->assertTrue($it->valid());
  227. $this->assertSame('removed', $it->key());
  228. $this->assertSame(1, $it->current());
  229. unset($map['removed']);
  230. // iterator is unchanged
  231. $this->assertTrue($it->valid());
  232. $this->assertSame('removed', $it->key());
  233. $this->assertSame(1, $it->current());
  234. // end of map
  235. $it->next();
  236. $this->assertFalse($it->valid());
  237. $this->assertNull($it->key());
  238. $this->assertNull($it->current());
  239. }
  240. public function testIterationIgnoresReplacementOfCurrentAndLastElement()
  241. {
  242. $map = new OrderedHashMap(array('replaced' => 1));
  243. $it = $map->getIterator();
  244. $it->rewind();
  245. $this->assertTrue($it->valid());
  246. $this->assertSame('replaced', $it->key());
  247. $this->assertSame(1, $it->current());
  248. $map['replaced'] = 2;
  249. // iterator is unchanged
  250. $this->assertTrue($it->valid());
  251. $this->assertSame('replaced', $it->key());
  252. $this->assertSame(1, $it->current());
  253. // end of map
  254. $it->next();
  255. $this->assertFalse($it->valid());
  256. $this->assertNull($it->key());
  257. $this->assertNull($it->current());
  258. }
  259. public function testIterationSupportsDeletionOfPreviousElement()
  260. {
  261. $map = new OrderedHashMap(array('removed' => 1, 'next' => 2, 'onemore' => 3));
  262. $it = $map->getIterator();
  263. $it->rewind();
  264. $this->assertTrue($it->valid());
  265. $this->assertSame('removed', $it->key());
  266. $this->assertSame(1, $it->current());
  267. // continue iteration
  268. $it->next();
  269. $this->assertTrue($it->valid());
  270. $this->assertSame('next', $it->key());
  271. $this->assertSame(2, $it->current());
  272. unset($map['removed']);
  273. // iterator is unchanged
  274. $this->assertTrue($it->valid());
  275. $this->assertSame('next', $it->key());
  276. $this->assertSame(2, $it->current());
  277. // continue iteration
  278. $it->next();
  279. $this->assertTrue($it->valid());
  280. $this->assertSame('onemore', $it->key());
  281. $this->assertSame(3, $it->current());
  282. // end of map
  283. $it->next();
  284. $this->assertFalse($it->valid());
  285. $this->assertNull($it->key());
  286. $this->assertNull($it->current());
  287. }
  288. public function testIterationIgnoresReplacementOfPreviousElement()
  289. {
  290. $map = new OrderedHashMap(array('replaced' => 1, 'next' => 2, 'onemore' => 3));
  291. $it = $map->getIterator();
  292. $it->rewind();
  293. $this->assertTrue($it->valid());
  294. $this->assertSame('replaced', $it->key());
  295. $this->assertSame(1, $it->current());
  296. // continue iteration
  297. $it->next();
  298. $this->assertTrue($it->valid());
  299. $this->assertSame('next', $it->key());
  300. $this->assertSame(2, $it->current());
  301. $map['replaced'] = 4;
  302. // iterator is unchanged
  303. $this->assertTrue($it->valid());
  304. $this->assertSame('next', $it->key());
  305. $this->assertSame(2, $it->current());
  306. // continue iteration
  307. $it->next();
  308. $this->assertTrue($it->valid());
  309. $this->assertSame('onemore', $it->key());
  310. $this->assertSame(3, $it->current());
  311. // end of map
  312. $it->next();
  313. $this->assertFalse($it->valid());
  314. $this->assertNull($it->key());
  315. $this->assertNull($it->current());
  316. }
  317. public function testIterationSupportsDeletionOfMultiplePreviousElements()
  318. {
  319. $map = new OrderedHashMap(array('removed' => 1, 'alsoremoved' => 2, 'next' => 3, 'onemore' => 4));
  320. $it = $map->getIterator();
  321. $it->rewind();
  322. $this->assertTrue($it->valid());
  323. $this->assertSame('removed', $it->key());
  324. $this->assertSame(1, $it->current());
  325. // continue iteration
  326. $it->next();
  327. $this->assertTrue($it->valid());
  328. $this->assertSame('alsoremoved', $it->key());
  329. $this->assertSame(2, $it->current());
  330. // continue iteration
  331. $it->next();
  332. $this->assertTrue($it->valid());
  333. $this->assertSame('next', $it->key());
  334. $this->assertSame(3, $it->current());
  335. unset($map['removed'], $map['alsoremoved']);
  336. // iterator is unchanged
  337. $this->assertTrue($it->valid());
  338. $this->assertSame('next', $it->key());
  339. $this->assertSame(3, $it->current());
  340. // continue iteration
  341. $it->next();
  342. $this->assertTrue($it->valid());
  343. $this->assertSame('onemore', $it->key());
  344. $this->assertSame(4, $it->current());
  345. // end of map
  346. $it->next();
  347. $this->assertFalse($it->valid());
  348. $this->assertNull($it->key());
  349. $this->assertNull($it->current());
  350. }
  351. public function testParallelIteration()
  352. {
  353. $map = new OrderedHashMap(array('first' => 1, 'second' => 2));
  354. $it1 = $map->getIterator();
  355. $it2 = $map->getIterator();
  356. $it1->rewind();
  357. $this->assertTrue($it1->valid());
  358. $this->assertSame('first', $it1->key());
  359. $this->assertSame(1, $it1->current());
  360. $it2->rewind();
  361. $this->assertTrue($it2->valid());
  362. $this->assertSame('first', $it2->key());
  363. $this->assertSame(1, $it2->current());
  364. // 1: continue iteration
  365. $it1->next();
  366. $this->assertTrue($it1->valid());
  367. $this->assertSame('second', $it1->key());
  368. $this->assertSame(2, $it1->current());
  369. // 2: remains unchanged
  370. $this->assertTrue($it2->valid());
  371. $this->assertSame('first', $it2->key());
  372. $this->assertSame(1, $it2->current());
  373. // 1: advance to end of map
  374. $it1->next();
  375. $this->assertFalse($it1->valid());
  376. $this->assertNull($it1->key());
  377. $this->assertNull($it1->current());
  378. // 2: remains unchanged
  379. $this->assertTrue($it2->valid());
  380. $this->assertSame('first', $it2->key());
  381. $this->assertSame(1, $it2->current());
  382. // 2: continue iteration
  383. $it2->next();
  384. $this->assertTrue($it2->valid());
  385. $this->assertSame('second', $it2->key());
  386. $this->assertSame(2, $it2->current());
  387. // 1: remains unchanged
  388. $this->assertFalse($it1->valid());
  389. $this->assertNull($it1->key());
  390. $this->assertNull($it1->current());
  391. // 2: advance to end of map
  392. $it2->next();
  393. $this->assertFalse($it2->valid());
  394. $this->assertNull($it2->key());
  395. $this->assertNull($it2->current());
  396. // 1: remains unchanged
  397. $this->assertFalse($it1->valid());
  398. $this->assertNull($it1->key());
  399. $this->assertNull($it1->current());
  400. }
  401. public function testCount()
  402. {
  403. $map = new OrderedHashMap();
  404. $map[] = 1;
  405. $map['foo'] = 2;
  406. unset($map[0]);
  407. $map[] = 3;
  408. $this->assertCount(2, $map);
  409. }
  410. }