CircularBufferTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. declare(strict_types=1);
  3. require_once 'CircularBuffer.php';
  4. use PHPUnit\Framework\TestCase;
  5. class CircularBufferTest extends TestCase
  6. {
  7. /**
  8. * uuid: 28268ed4-4ff3-45f3-820e-895b44d53dfa
  9. */
  10. public function testReadingEmptyBufferShouldFail(): void
  11. {
  12. $buffer = new CircularBuffer(1);
  13. $this->expectException(BufferEmptyError::class);
  14. $buffer->read();
  15. }
  16. /**
  17. * uuid: 2e6db04a-58a1-425d-ade8-ac30b5f318f3
  18. */
  19. public function testCanReadAnItemJustWritten(): void
  20. {
  21. $buffer = new CircularBuffer(1);
  22. $buffer->write('1');
  23. $this->assertSame('1', $buffer->read());
  24. }
  25. /**
  26. * uuid: 90741fe8-a448-45ce-be2b-de009a24c144
  27. */
  28. public function testEachItemMayOnlyBeReadOnce(): void
  29. {
  30. $buffer = new CircularBuffer(1);
  31. $buffer->write('1');
  32. $this->assertSame('1', $buffer->read());
  33. $this->expectException(BufferEmptyError::class);
  34. $buffer->read();
  35. }
  36. /**
  37. * uuid: be0e62d5-da9c-47a8-b037-5db21827baa7
  38. */
  39. public function testItemsAreReadInTheOrderTheyAreWritten(): void
  40. {
  41. $buffer = new CircularBuffer(2);
  42. $buffer->write('1');
  43. $buffer->write('2');
  44. $this->assertSame('1', $buffer->read());
  45. $this->assertSame('2', $buffer->read());
  46. }
  47. /**
  48. * uuid: 2af22046-3e44-4235-bfe6-05ba60439d38
  49. */
  50. public function testFullBufferCantBeWrittenTo(): void
  51. {
  52. $buffer = new CircularBuffer(1);
  53. $buffer->write('1');
  54. $this->expectException(BufferFullError::class);
  55. $buffer->write('2');
  56. }
  57. /**
  58. * uuid: 547d192c-bbf0-4369-b8fa-fc37e71f2393
  59. */
  60. public function testAReadFreesUpCapacityForAnotherWrite(): void
  61. {
  62. $buffer = new CircularBuffer(1);
  63. $buffer->write('1');
  64. $this->assertSame('1', $buffer->read());
  65. $buffer->write('2');
  66. $this->assertSame('2', $buffer->read());
  67. }
  68. /**
  69. * uuid: 04a56659-3a81-4113-816b-6ecb659b4471
  70. */
  71. public function testReadPositionIsMaintainedEvenAcrossMultipleWrites(): void
  72. {
  73. $buffer = new CircularBuffer(3);
  74. $buffer->write('1');
  75. $buffer->write('2');
  76. $this->assertSame('1', $buffer->read());
  77. $buffer->write('3');
  78. $this->assertSame('2', $buffer->read());
  79. $this->assertSame('3', $buffer->read());
  80. }
  81. /**
  82. * uuid: 60c3a19a-81a7-43d7-bb0a-f07242b1111f
  83. */
  84. public function testItemsClearedOutOfBufferCantBeRead(): void
  85. {
  86. $buffer = new CircularBuffer(1);
  87. $buffer->write('1');
  88. $buffer->clear();
  89. $this->expectException(BufferEmptyError::class);
  90. $buffer->read();
  91. }
  92. /**
  93. * uuid: 45f3ae89-3470-49f3-b50e-362e4b330a59
  94. */
  95. public function testClearFreesUpCapacityForAnotherWrite(): void
  96. {
  97. $buffer = new CircularBuffer(1);
  98. $buffer->write('1');
  99. $buffer->clear();
  100. $buffer->write('2');
  101. $this->assertSame('2', $buffer->read());
  102. }
  103. /**
  104. * uuid: e1ac5170-a026-4725-bfbe-0cf332eddecd
  105. */
  106. public function testClearDoesNothingOnEmptyBuffer(): void
  107. {
  108. $buffer = new CircularBuffer(1);
  109. $buffer->clear();
  110. $buffer->write('1');
  111. $this->assertSame('1', $buffer->read());
  112. }
  113. /**
  114. * uuid: 9c2d4f26-3ec7-453f-a895-7e7ff8ae7b5b
  115. */
  116. public function testForceWriteActsLikeWriteOnNonFullBuffer(): void
  117. {
  118. $buffer = new CircularBuffer(2);
  119. $buffer->write('1');
  120. $buffer->forceWrite('2');
  121. $this->assertSame('1', $buffer->read());
  122. $this->assertSame('2', $buffer->read());
  123. }
  124. /**
  125. * uuid: 880f916b-5039-475c-bd5c-83463c36a147
  126. */
  127. public function testForceWriteReplacesTheOldestItemOnFullBuffer(): void
  128. {
  129. $buffer = new CircularBuffer(2);
  130. $buffer->write('1');
  131. $buffer->write('2');
  132. $buffer->forceWrite('3');
  133. $this->assertSame('2', $buffer->read());
  134. $this->assertSame('3', $buffer->read());
  135. }
  136. /**
  137. * uuid: bfecab5b-aca1-4fab-a2b0-cd4af2b053c3
  138. */
  139. public function testForceWriteReplacesTheOldestItemRemainingInBufferFollowingARead(): void
  140. {
  141. $buffer = new CircularBuffer(3);
  142. $buffer->write('1');
  143. $buffer->write('2');
  144. $buffer->write('3');
  145. $this->assertSame('1', $buffer->read());
  146. $buffer->write('4');
  147. $buffer->forceWrite('5');
  148. $this->assertSame('3', $buffer->read());
  149. $this->assertSame('4', $buffer->read());
  150. $this->assertSame('5', $buffer->read());
  151. }
  152. /**
  153. * uuid: 9cebe63a-c405-437b-8b62-e3fdc1ecec5a
  154. */
  155. public function testInitialClearDoesNotAffectWrappingAround(): void
  156. {
  157. $buffer = new CircularBuffer(2);
  158. $buffer->clear();
  159. $buffer->write('1');
  160. $buffer->write('2');
  161. $buffer->forceWrite('3');
  162. $buffer->forceWrite('4');
  163. $this->assertSame('3', $buffer->read());
  164. $this->assertSame('4', $buffer->read());
  165. $this->expectException(BufferEmptyError::class);
  166. $buffer->read();
  167. }
  168. }