Skip to content

Commit

Permalink
fixed QueueArray Newest + Indexer
Browse files Browse the repository at this point in the history
  • Loading branch information
ZacharyPatten committed May 18, 2021
1 parent a3d9bb3 commit c9b2ad5
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Sources/Towel/DataStructures/Queue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ internal QueueArray(QueueArray<T> queue)
/// <exception cref="InvalidOperationException">Thrown when: _count &lt;= 0</exception>
public T Newest =>
_count <= 0 ? throw new InvalidOperationException("attempting to get the newest item in an empty queue") :
_array[(_start + _count) % _array.Length];
_array[(_start + _count - 1) % _array.Length];

/// <summary>
/// The current newest <typeparamref name="T"/> in the queue.
Expand All @@ -388,11 +388,11 @@ internal QueueArray(QueueArray<T> queue)
public T this[int index]
{
get =>
index <= 0 || index > _count ? throw new ArgumentOutOfRangeException(nameof(index), index, $"!(0 <= {nameof(index)} < {nameof(Count)})") :
index < 0 || index >= _count ? throw new ArgumentOutOfRangeException(nameof(index), index, $"!(0 <= {nameof(index)} < {nameof(Count)})") :
_array[(_start + index) % _array.Length];
set
{
if (index <= 0 || index > _count) throw new ArgumentOutOfRangeException(nameof(index), index, $"!(0 <= {nameof(index)} < {nameof(Count)})");
if (index < 0 || index >= _count) throw new ArgumentOutOfRangeException(nameof(index), index, $"!(0 <= {nameof(index)} < {nameof(Count)})");
_array[(_start + index) % _array.Length] = value;
}
}
Expand Down
57 changes: 57 additions & 0 deletions Tools/Towel_Testing/DataStructures/Queue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,63 @@ public static void IEnumerable_Testing<Queue>()
{
IQueueTester.IEnumerable_Testing<QueueArray<int>>();
}

[TestMethod] public void Indexer_Testing()
{
int max = 100;
QueueArray<int> queue = new();
Assert.ThrowsException<ArgumentOutOfRangeException>(() => queue[0]);
for (int i = 0; i < max; i++)
{
queue.Enqueue(i + 1);
Assert.ThrowsException<ArgumentOutOfRangeException>(() => queue[-1]);
Assert.ThrowsException<ArgumentOutOfRangeException>(() => queue[i + 1]);
for (int j = 0; j <= i; j++)
{
Assert.IsTrue(queue[j] == j + 1);
}
}
for (int i = 0; i < max; i++)
{
queue.Dequeue();
Assert.ThrowsException<ArgumentOutOfRangeException>(() => queue[-1]);
Assert.ThrowsException<ArgumentOutOfRangeException>(() => queue[max - i - 1]);
}
}

[TestMethod] public void Newest_Testing()
{
int max = 100;
QueueArray<int> queue = new();
Assert.ThrowsException<InvalidOperationException>(() => queue.Newest);
for (int i = 0; i < max; i++)
{
queue.Enqueue(i + 1);
Assert.IsTrue(queue.Newest == i + 1);
}
for (int i = 0; i < max - 1; i++)
{
queue.Dequeue();
Assert.IsTrue(queue.Newest == max);
}
}

[TestMethod] public void Oldest_Testing()
{
int max = 100;
QueueArray<int> queue = new();
Assert.ThrowsException<InvalidOperationException>(() => queue.Oldest);
for (int i = 0; i < max; i++)
{
queue.Enqueue(i + 1);
Assert.IsTrue(queue.Oldest == 1);
}
for (int i = 0; i < max - 1; i++)
{
queue.Dequeue();
Assert.IsTrue(queue.Oldest == i + 2);
}
}
}

[TestClass] public class QueueLinked_Testing
Expand Down

0 comments on commit c9b2ad5

Please sign in to comment.