Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions Containers.Test/RingBufferTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,53 @@ public void Clear_AllowsReuseAfterClearing()
Assert.AreEqual(1, buffer.Count);
Assert.AreEqual(7, buffer.At(0));
}

[TestMethod]
public void Constructor_NonPositiveLength_Throws()
{
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => new RingBuffer<int>(0));
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => new RingBuffer<int>(-1));
}

[TestMethod]
public void Constructor_PrefillValue_NonPositiveLength_Throws()
{
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => new RingBuffer<int>(42, 0));
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => new RingBuffer<int>(42, -1));
}

[TestMethod]
public void Constructor_PrefillItems_NonPositiveLength_Throws()
{
int[] items = [1, 2, 3];
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => new RingBuffer<int>(items, 0));
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => new RingBuffer<int>(items, -1));
}

[TestMethod]
public void Resize_NonPositiveLength_Throws()
{
RingBuffer<int> buffer = new(3);
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => buffer.Resize(0));
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => buffer.Resize(-1));
}

[TestMethod]
public void Resample_NonPositiveLength_Throws()
{
RingBuffer<int> buffer = new(3);
buffer.PushBack(1);
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => buffer.Resample(0));
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => buffer.Resample(-1));
}

[TestMethod]
public void Constructor_MinimumLength_IsUsable()
{
RingBuffer<int> buffer = new(1);
buffer.PushBack(1);
buffer.PushBack(2);
Assert.AreEqual(1, buffer.Count);
Assert.AreEqual(2, buffer.At(0));
}
}
8 changes: 8 additions & 0 deletions Containers/RingBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public class RingBuffer<T> : IReadOnlyList<T>
/// Initializes a new instance of the <see cref="RingBuffer{T}"/> class with the specified length.
/// </summary>
/// <param name="length">The number of elements the buffer should store.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="length"/> is less than one.</exception>
public RingBuffer(int length) => AllocateBuffer(length);

/// <summary>
Expand All @@ -86,6 +87,7 @@ public class RingBuffer<T> : IReadOnlyList<T>
/// </summary>
/// <param name="items">The items to prefill the buffer with.</param>
/// <param name="length">The number of elements the buffer should store.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="length"/> is less than one.</exception>
public RingBuffer(IEnumerable<T> items, int length)
: this(length)
{
Expand All @@ -111,6 +113,7 @@ public RingBuffer(IEnumerable<T> items, int length)
/// </summary>
/// <param name="value">The value to prefill the buffer with.</param>
/// <param name="length">The number of elements the buffer should store.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="length"/> is less than one.</exception>
public RingBuffer(T value, int length)
: this(length)
{
Expand All @@ -124,8 +127,11 @@ public RingBuffer(T value, int length)
/// Allocates and initializes the internal buffer with the specified length.
/// </summary>
/// <param name="length">The number of elements to allocate space for.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="length"/> is less than one.</exception>
private void AllocateBuffer(int length)
{
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(length);

Length = length;
Capacity = NextPower2(Length);
Buffer = new T[Capacity];
Expand Down Expand Up @@ -218,6 +224,7 @@ private static int NextPower2(int v)
/// Resizes the buffer to the specified length, discarding all current contents.
/// </summary>
/// <param name="length">The new length of the buffer.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="length"/> is less than one.</exception>
public void Resize(int length) => AllocateBuffer(length);

/// <summary>
Expand All @@ -234,6 +241,7 @@ private static int NextPower2(int v)
/// If the buffer is empty, this method will resize the buffer without adding any elements.
/// </remarks>
/// <param name="length">The new length of the buffer.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="length"/> is less than one.</exception>
public void Resample(int length)
{
// Save the current count of valid elements
Expand Down