Skip to content

Instantly share code, notes, and snippets.

@serialseb
Created June 25, 2014 10:27
Show Gist options
  • Save serialseb/a35d6d01fa6895e730f5 to your computer and use it in GitHub Desktop.
Save serialseb/a35d6d01fa6895e730f5 to your computer and use it in GitHub Desktop.
public class cappedqueue : cappedcontext
{
static object[] examples =
{
new { members = new[] { 1, 2, 3, 4 }, cap = 3, expected = new[] { 2, 3, 4 } }
};
public cappedqueue(int[] members, int cap)
{
given_queue(members, cap);
when_reading();
}
public void result_is_correct(int[] expected)
{
result.Count().Is(expected.Length);
for (int i = 0; i < expected.Length; i++)
{
result[i].Is(expected[i]);
}
}
}
public class CappedQueue<T> : IEnumerable<T>
{
readonly int size;
public CappedQueue(int size)
{
this.size = size;
internalQueue = new Queue<T>(size);
}
Queue<T> internalQueue { get; set; }
public void Enqueue(T value)
{
if (internalQueue.Count >= size)
internalQueue.Dequeue();
internalQueue.Enqueue(value);
}
public int Count { get { return internalQueue.Count; }}
public T Dequeue()
{
return internalQueue.Dequeue();
}
public IEnumerator<T> GetEnumerator()
{
return internalQueue.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
public abstract class cappedcontext
{
protected CappedQueue<int> queue;
protected int[] result;
protected void when_reading()
{
result = queue.ToArray();
}
protected void given_queue(int[] members, int cap)
{
queue = new CappedQueue<int>(cap);
foreach (var val in members) queue.Enqueue(val);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment