Skip to content

Commit

Permalink
Change read() to return null on empty instead of Exception
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasnorre committed Feb 22, 2024
1 parent c07b00d commit bc9f553
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 29 deletions.
10 changes: 1 addition & 9 deletions exercises/practice/circular-buffer/.meta/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ class BufferFullError extends Exception
{
}

class BufferEmptyError extends Exception
{
}

class CircularBuffer
{
private int $capacity;
Expand All @@ -47,13 +43,10 @@ public function __construct($capacity)
$this->writePosition = 0;
}

/**
* @throws BufferEmptyError
*/
public function read()
{
if ($this->isEmpty()) {
throw new BufferEmptyError();
return null;
}
$value = $this->buffer[$this->readPosition];
$this->buffer[$this->readPosition] = null;
Expand All @@ -74,7 +67,6 @@ public function write($item): void
}

/**
* @throws BufferEmptyError
* @throws BufferFullError
*/
public function forceWrite($item): void
Expand Down
24 changes: 4 additions & 20 deletions exercises/practice/circular-buffer/CircularBufferTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,15 @@ class CircularBufferTest extends TestCase
{
/**
* uuid: 28268ed4-4ff3-45f3-820e-895b44d53dfa
* @throws BufferEmptyError
*/
public function testReadingEmptyBufferShouldFail(): void
{
$buffer = new CircularBuffer(1);
$this->expectException(BufferEmptyError::class);
$buffer->read();
$this->assertNull($buffer->read());
}

/**
* uuid: 2e6db04a-58a1-425d-ade8-ac30b5f318f3
* @throws BufferEmptyError
* @throws BufferFullError
*/
public function testCanReadAnItemJustWritten(): void
Expand All @@ -33,21 +30,18 @@ public function testCanReadAnItemJustWritten(): void

/**
* uuid: 90741fe8-a448-45ce-be2b-de009a24c144
* @throws BufferEmptyError
* @throws BufferFullError
*/
public function testEachItemMayOnlyBeReadOnce(): void
{
$buffer = new CircularBuffer(1);
$buffer->write('1');
$this->assertSame('1', $buffer->read());
$this->expectException(BufferEmptyError::class);
$buffer->read();
$this->assertNull($buffer->read());
}

/**
* uuid: be0e62d5-da9c-47a8-b037-5db21827baa7
* @throws BufferEmptyError
* @throws BufferFullError
*/
public function testItemsAreReadInTheOrderTheyAreWritten(): void
Expand All @@ -73,7 +67,6 @@ public function testFullBufferCantBeWrittenTo(): void

/**
* uuid: 547d192c-bbf0-4369-b8fa-fc37e71f2393
* @throws BufferEmptyError
* @throws BufferFullError
*/
public function testAReadFreesUpCapacityForAnotherWrite(): void
Expand All @@ -87,7 +80,6 @@ public function testAReadFreesUpCapacityForAnotherWrite(): void

/**
* uuid: 04a56659-3a81-4113-816b-6ecb659b4471
* @throws BufferEmptyError
* @throws BufferFullError
*/
public function testReadPositionIsMaintainedEvenAcrossMultipleWrites(): void
Expand All @@ -110,13 +102,11 @@ public function testItemsClearedOutOfBufferCantBeRead(): void
$buffer = new CircularBuffer(1);
$buffer->write('1');
$buffer->clear();
$this->expectException(BufferEmptyError::class);
$buffer->read();
$this->assertNull($buffer->read());
}

/**
* uuid: 45f3ae89-3470-49f3-b50e-362e4b330a59
* @throws BufferEmptyError
* @throws BufferFullError
*/
public function testClearFreesUpCapacityForAnotherWrite(): void
Expand All @@ -130,7 +120,6 @@ public function testClearFreesUpCapacityForAnotherWrite(): void

/**
* uuid: e1ac5170-a026-4725-bfbe-0cf332eddecd
* @throws BufferEmptyError
* @throws BufferFullError
*/
public function testClearDoesNothingOnEmptyBuffer(): void
Expand All @@ -143,7 +132,6 @@ public function testClearDoesNothingOnEmptyBuffer(): void

/**
* uuid: 9c2d4f26-3ec7-453f-a895-7e7ff8ae7b5b
* @throws BufferEmptyError
* @throws BufferFullError
*/
public function testForceWriteActsLikeWriteOnNonFullBuffer(): void
Expand All @@ -157,7 +145,6 @@ public function testForceWriteActsLikeWriteOnNonFullBuffer(): void

/**
* uuid: 880f916b-5039-475c-bd5c-83463c36a147
* @throws BufferEmptyError
* @throws BufferFullError
*/
public function testForceWriteReplacesTheOldestItemOnFullBuffer(): void
Expand All @@ -172,7 +159,6 @@ public function testForceWriteReplacesTheOldestItemOnFullBuffer(): void

/**
* uuid: bfecab5b-aca1-4fab-a2b0-cd4af2b053c3
* @throws BufferEmptyError
* @throws BufferFullError
*/
public function testForceWriteReplacesTheOldestItemRemainingInBufferFollowingARead(): void
Expand All @@ -191,7 +177,6 @@ public function testForceWriteReplacesTheOldestItemRemainingInBufferFollowingARe

/**
* uuid: 9cebe63a-c405-437b-8b62-e3fdc1ecec5a
* @throws BufferEmptyError
* @throws BufferFullError
*/
public function testInitialClearDoesNotAffectWrappingAround(): void
Expand All @@ -204,7 +189,6 @@ public function testInitialClearDoesNotAffectWrappingAround(): void
$buffer->forceWrite('4');
$this->assertSame('3', $buffer->read());
$this->assertSame('4', $buffer->read());
$this->expectException(BufferEmptyError::class);
$buffer->read();
$this->assertNull($buffer->read());
}
}

0 comments on commit bc9f553

Please sign in to comment.