/core/src/test/php/net/xp_framework/unittest/util/collections/ArrayAccessTest.class.php

https://github.com/ghiata/xp-framework · PHP · 410 lines · 190 code · 38 blank · 182 comment · 0 complexity · edbb8a59d2d995dc6ac43f4dfb5d194a MD5 · raw file

  1. <?php namespace net\xp_framework\unittest\util\collections;
  2. use unittest\TestCase;
  3. use lang\types\String;
  4. use util\collections\HashTable;
  5. use util\collections\HashSet;
  6. use util\collections\Vector;
  7. /**
  8. * TestCase
  9. *
  10. * @see xp://util.collections.HashTable
  11. * @see xp://util.collections.HashSet
  12. * @see xp://util.collections.Vector
  13. */
  14. class ArrayAccessTest extends TestCase {
  15. /**
  16. * Tests array access operator is overloaded for reading
  17. *
  18. */
  19. #[@test]
  20. public function hashTableReadElement() {
  21. $c= new HashTable();
  22. $world= new String('world');
  23. $c->put(new String('hello'), $world);
  24. $this->assertEquals($world, $c[new String('hello')]);
  25. }
  26. /**
  27. * Tests array access operator is overloaded for reading
  28. *
  29. */
  30. #[@test]
  31. public function hashTableReadNonExistantElement() {
  32. $c= new HashTable();
  33. $this->assertEquals(null, $c[new String('hello')]);
  34. }
  35. /**
  36. * Tests array access operator is overloaded for reading
  37. *
  38. */
  39. #[@test, @expect('lang.IllegalArgumentException')]
  40. public function hashTableReadIllegalElement() {
  41. $c= create('new HashTable<string, Object>()');
  42. $c[STDIN];
  43. }
  44. /**
  45. * Tests array access operator is overloaded for writing
  46. *
  47. */
  48. #[@test]
  49. public function hashTableWriteElement() {
  50. $c= new HashTable();
  51. $world= new String('world');
  52. $c[new String('hello')]= $world;
  53. $this->assertEquals($world, $c->get(new String('hello')));
  54. }
  55. /**
  56. * Tests array access operator is overloaded for writing
  57. *
  58. */
  59. #[@test, @expect('lang.IllegalArgumentException')]
  60. public function hashTableWriteIllegalKey() {
  61. $c= create('new HashTable<string, Object>()');
  62. $c[STDIN]= new String('Hello');
  63. }
  64. /**
  65. * Tests array access operator is overloaded for writing
  66. *
  67. */
  68. #[@test, @expect('lang.IllegalArgumentException')]
  69. public function hashTableWriteIllegalValue() {
  70. $c= create('new HashTable<string, Object>()');
  71. $c['hello']= 'scalar';
  72. }
  73. /**
  74. * Tests array access operator is overloaded for isset()
  75. *
  76. */
  77. #[@test]
  78. public function hashTableTestElement() {
  79. $c= new HashTable();
  80. $c->put(new String('hello'), new String('world'));
  81. $this->assertTrue(isset($c[new String('hello')]));
  82. $this->assertFalse(isset($c[new String('world')]));
  83. }
  84. /**
  85. * Tests array access operator is overloaded for unset()
  86. *
  87. */
  88. #[@test]
  89. public function hashTableRemoveElement() {
  90. $c= new HashTable();
  91. $c->put(new String('hello'), new String('world'));
  92. $this->assertTrue(isset($c[new String('hello')]));
  93. unset($c[new String('hello')]);
  94. $this->assertFalse(isset($c[new String('hello')]));
  95. }
  96. /**
  97. * Tests array access operator is overloaded for reading
  98. *
  99. */
  100. #[@test]
  101. public function vectorReadElement() {
  102. $v= new Vector();
  103. $world= new String('world');
  104. $v->add($world);
  105. $this->assertEquals($world, $v[0]);
  106. }
  107. /**
  108. * Tests array access operator is overloaded for reading
  109. *
  110. */
  111. #[@test, @expect('lang.IndexOutOfBoundsException')]
  112. public function vectorReadNonExistantElement() {
  113. $v= new Vector();
  114. $v[0];
  115. }
  116. /**
  117. * Tests array access operator is overloaded for adding
  118. *
  119. */
  120. #[@test]
  121. public function vectorAddElement() {
  122. $v= new Vector();
  123. $world= new String('world');
  124. $v[]= $world;
  125. $this->assertEquals($world, $v[0]);
  126. }
  127. /**
  128. * Tests array access operator is overloaded for writing
  129. *
  130. */
  131. #[@test]
  132. public function vectorWriteElement() {
  133. $v= new Vector(array(new String('hello')));
  134. $world= new String('world');
  135. $v[0]= $world;
  136. $this->assertEquals($world, $v[0]);
  137. }
  138. /**
  139. * Tests array access operator is overloaded for writing
  140. *
  141. */
  142. #[@test, @expect('lang.IndexOutOfBoundsException')]
  143. public function vectorWriteElementBeyondBoundsKey() {
  144. $v= new Vector();
  145. $v[0]= new String('world');
  146. }
  147. /**
  148. * Tests array access operator is overloaded for writing
  149. *
  150. */
  151. #[@test, @expect('lang.IndexOutOfBoundsException')]
  152. public function vectorWriteElementNegativeKey() {
  153. $v= new Vector();
  154. $v[-1]= new String('world');
  155. }
  156. /**
  157. * Tests array access operator is overloaded for isset()
  158. *
  159. */
  160. #[@test]
  161. public function vectorTestElement() {
  162. $v= new Vector();
  163. $v[]= new String('world');
  164. $this->assertTrue(isset($v[0]));
  165. $this->assertFalse(isset($v[1]));
  166. $this->assertFalse(isset($v[-1]));
  167. }
  168. /**
  169. * Tests array access operator is overloaded for unset()
  170. *
  171. */
  172. #[@test]
  173. public function vectorRemoveElement() {
  174. $v= new Vector();
  175. $v[]= new String('world');
  176. unset($v[0]);
  177. $this->assertFalse(isset($v[0]));
  178. }
  179. /**
  180. * Tests Vector is usable in foreach()
  181. *
  182. */
  183. #[@test]
  184. public function vectorIsUsableInForeach() {
  185. $values= array(new String('hello'), new String('world'));
  186. foreach (new Vector($values) as $i => $value) {
  187. $this->assertEquals($values[$i], $value);
  188. }
  189. $this->assertEquals(sizeof($values)- 1, $i);
  190. }
  191. /**
  192. * Tests string class array access operator overloading
  193. *
  194. */
  195. #[@test]
  196. public function stringReadChar() {
  197. $s= new String('Hello');
  198. $this->assertEquals(new \lang\types\Character('H'), $s[0]);
  199. $this->assertEquals(new \lang\types\Character('e'), $s[1]);
  200. $this->assertEquals(new \lang\types\Character('l'), $s[2]);
  201. $this->assertEquals(new \lang\types\Character('l'), $s[3]);
  202. $this->assertEquals(new \lang\types\Character('o'), $s[4]);
  203. }
  204. /**
  205. * Tests string class array access operator overloading
  206. *
  207. */
  208. #[@test, @expect('lang.IndexOutOfBoundsException')]
  209. public function stringReadBeyondOffset() {
  210. $s= new String('Hello');
  211. $s[5];
  212. }
  213. /**
  214. * Tests string class array access operator overloading
  215. *
  216. */
  217. #[@test, @expect('lang.IndexOutOfBoundsException')]
  218. public function stringReadNegativeOffset() {
  219. $s= new String('Hello');
  220. $s[-1];
  221. }
  222. /**
  223. * Tests string class array access operator overloading
  224. *
  225. */
  226. #[@test]
  227. public function stringReadUtfChar() {
  228. $s= new String('Übercoder', 'iso-8859-1');
  229. $this->assertEquals(new \lang\types\Character('Ü', 'iso-8859-1'), $s[0]);
  230. }
  231. /**
  232. * Tests string class array access operator overloading
  233. *
  234. */
  235. #[@test]
  236. public function stringWriteChar() {
  237. $s= new String('Übercoder', 'iso-8859-1');
  238. $s[0]= 'U';
  239. $this->assertEquals(new String('Ubercoder'), $s);
  240. }
  241. /**
  242. * Tests string class array access operator overloading
  243. *
  244. */
  245. #[@test]
  246. public function stringWriteUtfChar() {
  247. $s= new String('Ubercoder');
  248. $s[0]= new \lang\types\Character('Ü', 'iso-8859-1');
  249. $this->assertEquals(new String('Übercoder', 'iso-8859-1'), $s);
  250. }
  251. /**
  252. * Tests string class array access operator overloading
  253. *
  254. */
  255. #[@test, @expect('lang.IllegalArgumentException')]
  256. public function stringWriteMoreThanOneChar() {
  257. $s= new String('Hallo');
  258. $s[0]= 'Halli H'; // Hoping somehow this would become "Halli Hallo":)
  259. }
  260. /**
  261. * Tests string class array access operator overloading
  262. *
  263. */
  264. #[@test, @expect('lang.IndexOutOfBoundsException')]
  265. public function stringWriteBeyondOffset() {
  266. $s= new String('Hello');
  267. $s[5]= 's';
  268. }
  269. /**
  270. * Tests string class array access operator overloading
  271. *
  272. */
  273. #[@test, @expect('lang.IndexOutOfBoundsException')]
  274. public function stringWriteNegativeOffset() {
  275. $s= new String('Hello');
  276. $s[-1]= "\x00";
  277. }
  278. /**
  279. * Tests string class array access operator overloading
  280. *
  281. */
  282. #[@test, @expect('lang.IllegalArgumentException')]
  283. public function stringAppend() {
  284. $s= new String('Hello');
  285. $s[]= ' '; // use concat() instead
  286. }
  287. /**
  288. * Tests string class array access operator overloading
  289. *
  290. */
  291. #[@test]
  292. public function stringTestChar() {
  293. $s= new String('Übercoder', 'iso-8859-1');
  294. $this->assertTrue(isset($s[0]));
  295. $this->assertTrue(isset($s[$s->length()- 1]));
  296. $this->assertFalse(isset($s[$s->length()]));
  297. $this->assertFalse(isset($s[-1]));
  298. }
  299. /**
  300. * Tests string class array access operator overloading
  301. *
  302. */
  303. #[@test]
  304. public function stringRemoveChar() {
  305. $s= new String('Übercoder', 'iso-8859-1');
  306. unset($s[0]);
  307. $this->assertEquals(new String('bercoder'), $s);
  308. }
  309. /**
  310. * Tests hashset array access operator overloading
  311. *
  312. */
  313. #[@test]
  314. public function hashSetAddElement() {
  315. $s= new HashSet();
  316. $s[]= new String('X');
  317. $this->assertTrue($s->contains(new String('X')));
  318. }
  319. /**
  320. * Tests hashset array access operator overloading
  321. *
  322. */
  323. #[@test, @expect('lang.IllegalArgumentException')]
  324. public function hashSetWriteElement() {
  325. $s= new HashSet();
  326. $s[0]= new String('X');
  327. }
  328. /**
  329. * Tests hashset array access operator overloading
  330. *
  331. */
  332. #[@test, @expect('lang.IllegalArgumentException')]
  333. public function hashSetReadElement() {
  334. $s= new HashSet();
  335. $s[]= new String('X');
  336. $x= $s[0];
  337. }
  338. /**
  339. * Tests hashset array access operator overloading
  340. *
  341. */
  342. #[@test]
  343. public function hashSetTestElement() {
  344. $s= new HashSet();
  345. $this->assertFalse(isset($s[new String('X')]));
  346. $s[]= new String('X');
  347. $this->assertTrue(isset($s[new String('X')]));
  348. }
  349. /**
  350. * Tests hashset array access operator overloading
  351. *
  352. */
  353. #[@test]
  354. public function hashSetRemoveElement() {
  355. $s= new HashSet();
  356. $s[]= new String('X');
  357. unset($s[new String('X')]);
  358. $this->assertFalse(isset($s[new String('X')]));
  359. }
  360. /**
  361. * Tests hashset array access operator overloading
  362. *
  363. */
  364. #[@test]
  365. public function hashSetUsableInForeach() {
  366. $s= new HashSet();
  367. $s->addAll(array(new String('0'), new String('1'), new String('2')));
  368. foreach ($s as $i => $element) {
  369. $this->assertEquals(new String($i), $element);
  370. }
  371. }
  372. }