/tests/classes/Iterator/Offset.php

https://github.com/Nycto/Round-Eights · PHP · 151 lines · 100 code · 23 blank · 28 comment · 1 complexity · 382fafec65d76cfffe7f7e584be16ffc MD5 · raw file

  1. <?php
  2. /**
  3. * Unit Test File
  4. *
  5. * @license Artistic License 2.0
  6. *
  7. * This file is part of Round Eights.
  8. *
  9. * Round Eights is free software: you can redistribute it and/or modify
  10. * it under the terms of the Artistic License as published by
  11. * the Open Source Initiative, either version 2.0 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * Round Eights is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * Artistic License for more details.
  18. *
  19. * You should have received a copy of the Artistic License
  20. * along with Round Eights. If not, see <http://www.RoundEights.com/license.php>
  21. * or <http://www.opensource.org/licenses/artistic-license-2.0.php>.
  22. *
  23. * @author James Frasca <James@RoundEights.com>
  24. * @copyright Copyright 2009, James Frasca, All Rights Reserved
  25. * @package UnitTests
  26. */
  27. require_once rtrim( __DIR__, "/" ) ."/../../general.php";
  28. /**
  29. * unit tests
  30. */
  31. class classes_Iterator_Offset extends PHPUnit_Framework_TestCase
  32. {
  33. public function testConstruct_UncountableNegative ()
  34. {
  35. $iterator = $this->getMock('Iterator');
  36. try {
  37. new \r8\Iterator\Offset( -1, $iterator );
  38. $this->fail("An expected exception was not thrown");
  39. }
  40. catch ( \r8\Exception\Index $err ) {
  41. $this->assertSame(
  42. "Negative offsets are only supported if Iterator implements the Countable interface",
  43. $err->getMessage()
  44. );
  45. }
  46. }
  47. public function testSeekable_Empty ()
  48. {
  49. $offset = new \r8\Iterator\Offset( 10, new \ArrayIterator( array() ) );
  50. \r8\Test\Constraint\Iterator::assert( array(), $offset );
  51. \r8\Test\Constraint\Iterator::assert( array(), $offset );
  52. \r8\Test\Constraint\Iterator::assert( array(), $offset );
  53. }
  54. public function testIterate_Seekable_FromZero ()
  55. {
  56. $offset = new \r8\Iterator\Offset( 0, new \ArrayIterator( array(1, 2, 3) ) );
  57. \r8\Test\Constraint\Iterator::assert( array(1, 2, 3), $offset );
  58. \r8\Test\Constraint\Iterator::assert( array(1, 2, 3), $offset );
  59. \r8\Test\Constraint\Iterator::assert( array(1, 2, 3), $offset );
  60. }
  61. public function testIterate_Seekable_WithOffset ()
  62. {
  63. $offset = new \r8\Iterator\Offset(
  64. 2,
  65. new \ArrayIterator( range("a", "e") )
  66. );
  67. \r8\Test\Constraint\Iterator::assert( array(2 => 'c', 3 => 'd', 4 => 'e'), $offset );
  68. \r8\Test\Constraint\Iterator::assert( array(2 => 'c', 3 => 'd', 4 => 'e'), $offset );
  69. \r8\Test\Constraint\Iterator::assert( array(2 => 'c', 3 => 'd', 4 => 'e'), $offset );
  70. }
  71. public function testIterate_Seekable_OutOfBounds ()
  72. {
  73. $offset = new \r8\Iterator\Offset(
  74. 50,
  75. new \ArrayIterator( range("a", "e") )
  76. );
  77. \r8\Test\Constraint\Iterator::assert( array(), $offset );
  78. \r8\Test\Constraint\Iterator::assert( array(), $offset );
  79. \r8\Test\Constraint\Iterator::assert( array(), $offset );
  80. }
  81. public function testIterate_Seekable_Negative ()
  82. {
  83. $offset = new \r8\Iterator\Offset(
  84. -2,
  85. new \ArrayIterator( range("a", "e") )
  86. );
  87. \r8\Test\Constraint\Iterator::assert( array(3 => 'd', 4 => 'e'), $offset );
  88. \r8\Test\Constraint\Iterator::assert( array(3 => 'd', 4 => 'e'), $offset );
  89. \r8\Test\Constraint\Iterator::assert( array(3 => 'd', 4 => 'e'), $offset );
  90. }
  91. public function testIterate_Unseekable_Empty ()
  92. {
  93. $offset = new \r8\Iterator\Offset(
  94. 10,
  95. new IteratorIterator( new \ArrayIterator( array() ) )
  96. );
  97. \r8\Test\Constraint\Iterator::assert( array(), $offset );
  98. \r8\Test\Constraint\Iterator::assert( array(), $offset );
  99. \r8\Test\Constraint\Iterator::assert( array(), $offset );
  100. }
  101. public function testIterate_Unseekable_FromZero ()
  102. {
  103. $offset = new \r8\Iterator\Offset( 0, new \ArrayIterator( array(1, 2, 3) ) );
  104. \r8\Test\Constraint\Iterator::assert( array(1, 2, 3), $offset );
  105. \r8\Test\Constraint\Iterator::assert( array(1, 2, 3), $offset );
  106. \r8\Test\Constraint\Iterator::assert( array(1, 2, 3), $offset );
  107. }
  108. public function testIterate_Unseekable_WithOffset ()
  109. {
  110. $offset = new \r8\Iterator\Offset(
  111. 2,
  112. new IteratorIterator( new \ArrayIterator( range("a", "e") ) )
  113. );
  114. \r8\Test\Constraint\Iterator::assert( array(2 => 'c', 3 => 'd', 4 => 'e'), $offset );
  115. \r8\Test\Constraint\Iterator::assert( array(2 => 'c', 3 => 'd', 4 => 'e'), $offset );
  116. \r8\Test\Constraint\Iterator::assert( array(2 => 'c', 3 => 'd', 4 => 'e'), $offset );
  117. }
  118. public function testIterate_Unseekable_OutOfBounds ()
  119. {
  120. $offset = new \r8\Iterator\Offset(
  121. 50,
  122. new IteratorIterator( new \ArrayIterator( range("a", "e") ) )
  123. );
  124. \r8\Test\Constraint\Iterator::assert( array(), $offset );
  125. \r8\Test\Constraint\Iterator::assert( array(), $offset );
  126. \r8\Test\Constraint\Iterator::assert( array(), $offset );
  127. }
  128. }