PageRenderTime 59ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/unit/DocBlox/BidirectionalIteratorTest.php

https://github.com/androa/Docblox
PHP | 236 lines | 104 code | 25 blank | 107 comment | 2 complexity | 2c4d57b044e4b98754c57f5a81d55aa0 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * DocBlox
  4. *
  5. * PHP Version 5
  6. *
  7. * @category DocBlox
  8. * @package Core
  9. * @subpackage Tests
  10. * @author Mike van Riel <mike.vanriel@naenius.com>
  11. * @copyright 2010-2011 Mike van Riel / Naenius (http://www.naenius.com)
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT
  13. * @link http://docblox-project.org
  14. */
  15. /**
  16. * Test class for BidirectionalArrayIterator.
  17. *
  18. * @category DocBlox
  19. * @package Core
  20. * @subpackage Tests
  21. * @author Mike van Riel <mike.vanriel@naenius.com>
  22. * @license http://www.opensource.org/licenses/mit-license.php MIT
  23. * @link http://docblox-project.org
  24. */
  25. class DocBlox_BidirectionalIteratorTest extends PHPUnit_Framework_TestCase
  26. {
  27. /** @var DocBlox_BidirectionalIterator */
  28. protected $fixture = null;
  29. /** @var string Expected serialized values */
  30. const SERIALIZED = 'a:10:{i:0;i:1;i:1;i:2;i:2;i:3;i:3;i:4;i:4;i:5;i:5;i:6;i:6;i:7;i:7;i:8;i:8;i:9;i:9;i:10;}';
  31. /**
  32. * Prepares the fixture for the test.
  33. *
  34. * @return void
  35. */
  36. protected function setUp()
  37. {
  38. $this->fixture = new DocBlox_BidirectionalIterator(array(
  39. 1,2,3,4,5,6,7,8,9,10
  40. ));
  41. }
  42. /**
  43. * Test if the result of the count keyword matches the number of elements.
  44. *
  45. * @return void
  46. */
  47. public function testCount()
  48. {
  49. $this->assertEquals(10, count($this->fixture));
  50. }
  51. /**
  52. * Tests whether setting an existing or new values succeeds.
  53. *
  54. * @return void
  55. */
  56. public function testSet()
  57. {
  58. $this->assertEquals(10, $this->fixture[9]);
  59. $this->fixture[9] = 100;
  60. $this->assertEquals(100, $this->fixture[9]);
  61. $this->setExpectedException('Exception');
  62. $this->fixture[11] = 11;
  63. }
  64. /**
  65. * Tests whether unsetting throws an exception.
  66. *
  67. * @return void
  68. */
  69. public function testUnset()
  70. {
  71. $this->setExpectedException('Exception');
  72. unset($this->fixture[1]);
  73. }
  74. /**
  75. * Tests whether getting a value returns the correct one.
  76. *
  77. * @return void
  78. */
  79. public function testGet()
  80. {
  81. $this->assertEquals(2, $this->fixture[1]);
  82. $this->assertEquals(null, $this->fixture[11]);
  83. }
  84. /**
  85. * Checks whether the isset method returns the correct result.
  86. *
  87. * @return void
  88. */
  89. public function testIsset()
  90. {
  91. $this->assertEquals(true, isset($this->fixture[1]));
  92. $this->assertEquals(false, isset($this->fixture[11]));
  93. }
  94. /**
  95. * Tests whether the seek method finds the correct value or false if no item is on that location.
  96. *
  97. * @return void
  98. */
  99. public function testSeek()
  100. {
  101. $this->assertEquals(2, $this->fixture->seek(1));
  102. $this->assertEquals(false, $this->fixture->seek(11));
  103. $fixture2 = new DocBlox_BidirectionalIterator(array_fill(0, 1000, 'a'));
  104. $times = array();
  105. for ($i = 0; $i < 1000; $i++)
  106. {
  107. $time = microtime(true);
  108. $fixture2->seek(rand(0, 999));
  109. $times[] = microtime(true) - $time;
  110. }
  111. $average = array_sum($times) / count($times);
  112. $this->assertLessThan(0.0002, $average);
  113. }
  114. /**
  115. * Tests whether the key method returns the right result.
  116. *
  117. * Note: for some reason the key() keyword does not work; this has been observed in other ArrayObjects as well.
  118. *
  119. * @return void
  120. */
  121. public function testKey()
  122. {
  123. $this->assertEquals(0, $this->fixture->key());
  124. // $this->assertEquals(0, key($this->fixture));
  125. $this->fixture->next();
  126. $this->assertEquals(1, $this->fixture->key());
  127. // $this->assertEquals(1, key($this->fixture));
  128. }
  129. /**
  130. * Tests whether the validity check also works.
  131. *
  132. * @return void
  133. */
  134. public function testValid()
  135. {
  136. $this->assertEquals(10, $this->fixture->seek(9));
  137. $this->assertEquals(true, $this->fixture->valid());
  138. $this->assertEquals(false, $this->fixture->next());
  139. $this->assertEquals(false, $this->fixture->valid());
  140. }
  141. /**
  142. * Tests whether next returns indeed the next token and shifts the pointer; or returns false is there is no next.
  143. *
  144. * @return void
  145. */
  146. public function testNext()
  147. {
  148. $this->assertEquals(false, $this->fixture->seek(11));
  149. $this->assertEquals(false, $this->fixture->next());
  150. $this->assertEquals(9, $this->fixture->seek(8));
  151. $this->assertEquals(10, $this->fixture->next());
  152. $this->assertEquals(false, $this->fixture->next()); // out of bounds
  153. $fixture2 = new DocBlox_BidirectionalIterator(array_fill(0, 1000, 'a'));
  154. $times = array();
  155. for($i=0;$i < 1000; $i++)
  156. {
  157. $time = microtime(true);
  158. $fixture2->next();
  159. $times[] = microtime(true) - $time;
  160. }
  161. $average = array_sum($times) / count($times);
  162. $this->assertLessThan(0.0002, $average);
  163. }
  164. /**
  165. * Tests whether previous returns indeed the previous token and shifts the pointer; or returns false is there
  166. * is no previous.
  167. *
  168. * @return void
  169. */
  170. public function testPrevious()
  171. {
  172. $this->assertEquals(false, $this->fixture->seek(11));
  173. $this->assertEquals(false, $this->fixture->previous());
  174. $this->assertEquals(2, $this->fixture->seek(1));
  175. $this->assertEquals(1, $this->fixture->previous());
  176. $this->assertEquals(false, $this->fixture->previous());
  177. }
  178. /**
  179. * Tests whether the current method returns the element contained underneath the current pointer.
  180. *
  181. * Note: for some reason the current() keyword does not work; this has been observed with other ArrayObjects.
  182. *
  183. * @return void
  184. */
  185. public function testCurrent()
  186. {
  187. $this->assertEquals(2, $this->fixture->seek(1));
  188. // $this->assertEquals(2, current($this->fixture));
  189. $this->assertEquals(2, $this->fixture->current());
  190. }
  191. /**
  192. * Tests whether the items are successfully serialized.
  193. *
  194. * @return void
  195. */
  196. public function testSerialize()
  197. {
  198. // $this->assertEquals(self::SERIALIZED, $this->fixture->serialize());
  199. }
  200. /**
  201. * Tests whether unserialization provides the right results.
  202. *
  203. * @return void
  204. */
  205. public function testUnserialize()
  206. {
  207. // $f2 = new DocBlox_BidirectionalIterator(array());
  208. // $f2->unserialize(self::SERIALIZED);
  209. // $this->assertEquals($this->fixture, $f2);
  210. }
  211. }