ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# freddie2025's solution

## to Circular Buffer in the C# Track

Published at Apr 15 2019 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

A circular buffer, cyclic buffer or ring buffer is a data structure that uses a single, fixed-size buffer as if it were connected end-to-end.

A circular buffer first starts empty and of some predefined length. For example, this is a 7-element buffer:

``````[ ][ ][ ][ ][ ][ ][ ]
``````

Assume that a 1 is written into the middle of the buffer (exact starting location does not matter in a circular buffer):

``````[ ][ ][ ][1][ ][ ][ ]
``````

Then assume that two more elements are added â€” 2 & 3 â€” which get appended after the 1:

``````[ ][ ][ ][1][2][3][ ]
``````

If two elements are then removed from the buffer, the oldest values inside the buffer are removed. The two elements removed, in this case, are 1 & 2, leaving the buffer with just a 3:

``````[ ][ ][ ][ ][ ][3][ ]
``````

If the buffer has 7 elements then it is completely full:

``````[6][7][8][9][3][4][5]
``````

When the buffer is full an error will be raised, alerting the client that further writes are blocked until a slot becomes free.

When the buffer is full, the client can opt to overwrite the oldest data with a forced write. In this case, two more elements â€” A & B â€” are added and they overwrite the 3 & 4:

``````[6][7][8][9][A][B][5]
``````

3 & 4 have been replaced by A & B making 5 now the oldest data in the buffer. Finally, if two elements are removed then what would be returned is 5 & 6 yielding the buffer:

``````[ ][7][8][9][A][B][ ]
``````

Because there is space available, if the client again uses overwrite to store C & D then the space where 5 & 6 were stored previously will be used not the location of 7 & 8. 7 is still the oldest element and the buffer is once again full.

``````[D][7][8][9][A][B][C]
``````

## Running the tests

To run the tests, run the command `dotnet test` from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time. Once you get the first test passing, remove the `Skip` property from the next test and work on getting that test passing. Once none of the tests are skipped and they are all passing, you can submit your solution using `exercism submit CircularBuffer.cs`

## Further information

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

## Source

### CircularBufferTest.cs

``````// This file was auto-generated based on version 1.1.0 of the canonical data.

using System;
using Xunit;

public class CircularBufferTest
{
[Fact]
{
var buffer = new CircularBuffer<int>(capacity: 1);
}

[Fact(Skip = "Remove to run test")]
{
var buffer = new CircularBuffer<int>(capacity: 1);
buffer.Write(1);
}

[Fact(Skip = "Remove to run test")]
{
var buffer = new CircularBuffer<int>(capacity: 1);
buffer.Write(1);
}

[Fact(Skip = "Remove to run test")]
{
var buffer = new CircularBuffer<int>(capacity: 2);
buffer.Write(1);
buffer.Write(2);
}

[Fact(Skip = "Remove to run test")]
public void Full_buffer_cant_be_written_to()
{
var buffer = new CircularBuffer<int>(capacity: 1);
buffer.Write(1);
Assert.Throws<InvalidOperationException>(() => buffer.Write(2));
}

[Fact(Skip = "Remove to run test")]
{
var buffer = new CircularBuffer<int>(capacity: 1);
buffer.Write(1);
buffer.Write(2);
}

[Fact(Skip = "Remove to run test")]
{
var buffer = new CircularBuffer<int>(capacity: 3);
buffer.Write(1);
buffer.Write(2);
buffer.Write(3);
}

[Fact(Skip = "Remove to run test")]
{
var buffer = new CircularBuffer<int>(capacity: 1);
buffer.Write(1);
buffer.Clear();
}

[Fact(Skip = "Remove to run test")]
public void Clear_frees_up_capacity_for_another_write()
{
var buffer = new CircularBuffer<int>(capacity: 1);
buffer.Write(1);
buffer.Clear();
buffer.Write(2);
}

[Fact(Skip = "Remove to run test")]
public void Clear_does_nothing_on_empty_buffer()
{
var buffer = new CircularBuffer<int>(capacity: 1);
buffer.Clear();
buffer.Write(1);
}

[Fact(Skip = "Remove to run test")]
public void Overwrite_acts_like_write_on_non_full_buffer()
{
var buffer = new CircularBuffer<int>(capacity: 2);
buffer.Write(1);
buffer.Overwrite(2);
}

[Fact(Skip = "Remove to run test")]
public void Overwrite_replaces_the_oldest_item_on_full_buffer()
{
var buffer = new CircularBuffer<int>(capacity: 2);
buffer.Write(1);
buffer.Write(2);
buffer.Overwrite(3);
}

[Fact(Skip = "Remove to run test")]
{
var buffer = new CircularBuffer<int>(capacity: 3);
buffer.Write(1);
buffer.Write(2);
buffer.Write(3);
buffer.Write(4);
buffer.Overwrite(5);
}
}``````
``````ï»¿using System;
using System.Collections.Generic;

public class CircularBuffer<T>
{
private Queue<T> _queue;
private int _capacity;

public CircularBuffer(int capacity)
{
_queue = new Queue<T>();
_capacity = capacity;
}

{
return _queue.Dequeue();
}

public void Write(T value)
{
if(_queue.Count == _capacity)
throw new InvalidOperationException("Reached capacity");
else
_queue.Enqueue(value);
}

public void Overwrite(T value)
{
if(_queue.Count == _capacity) _queue.Dequeue();
Write(value);
}

public void Clear()
{
_queue.Clear();
}
}``````