# artemkorsakov's solution

## to Circular Buffer in the C# Track

Published at Apr 21 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]
public void Reading_empty_buffer_should_fail()
{
var buffer = new CircularBuffer<int>(capacity: 1);
Assert.Throws<InvalidOperationException>(() => buffer.Read());
}

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

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

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

[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")]
public void A_read_frees_up_capacity_for_another_write()
{
var buffer = new CircularBuffer<int>(capacity: 1);
buffer.Write(1);
Assert.Equal(1, buffer.Read());
buffer.Write(2);
Assert.Equal(2, buffer.Read());
}

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

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

[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);
Assert.Equal(2, buffer.Read());
}

[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);
Assert.Equal(1, buffer.Read());
}

[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);
Assert.Equal(1, buffer.Read());
Assert.Equal(2, buffer.Read());
}

[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);
Assert.Equal(2, buffer.Read());
Assert.Equal(3, buffer.Read());
}

[Fact(Skip = "Remove to run test")]
public void Overwrite_replaces_the_oldest_item_remaining_in_buffer_following_a_read()
{
var buffer = new CircularBuffer<int>(capacity: 3);
buffer.Write(1);
buffer.Write(2);
buffer.Write(3);
Assert.Equal(1, buffer.Read());
buffer.Write(4);
buffer.Overwrite(5);
Assert.Equal(3, buffer.Read());
Assert.Equal(4, buffer.Read());
Assert.Equal(5, buffer.Read());
}
}``````
``````﻿using System;
using System.Collections.Generic;

public class CircularBuffer<T>
{
private readonly int capacity;
private Queue<T> queue;

public CircularBuffer(int capacity)
{
if (capacity < 1)
{
throw new InvalidOperationException("Capacity must be more than 0.");
}

this.capacity = capacity;
Clear();
}

public T Read()
{
if (queue.Count == 0)
{
throw new InvalidOperationException("Buffer is empty!");
}

return queue.Dequeue();
}

public void Write(T value)
{
if (queue.Count >= capacity)
{
throw new InvalidOperationException("Buffer is full!");
}

queue.Enqueue(value);
}

public void Overwrite(T value)
{
if (queue.Count >= capacity)
{
queue.Dequeue();
}

queue.Enqueue(value);
}

public void Clear()
{
queue = new Queue<T>();
}
}``````

## Community comments

Find this solution interesting? Ask the author a question to learn more.

### What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

• What compromises have been made?
• Are there new concepts here that you could read more about to improve your understanding?