/* * Aresh Saharkhiz * saharkiz@gmail.com * Associate Professor / Mapua Institute of Technology / Philippines */ using System; // Introduction to Software Testing, www.introsoftwaretesting.com // Authors: Paul Ammann & Jeff Offutt // Chapter 4, section 4.2, page 164 // Note: BoundedQueue is a minor variation of Queue from Chapter 2 public class BoundedQueue { // Overview: a BoundedQueue is a mutable, bounded FIFO data structure // of fixed size, set in the constructor // A typical BoundedQueue is [], [o1], or [o1, o2], where neither o1 nor o2 // are ever null. Older elements are listed before newer ones. private object[] elements; private int size, front, back; private static int capacity; public BoundedQueue(int cap) { // Effects: If cap < 0 throw Illegal ArgumentException // else instantiate BoundedQueue with capacity cap. if (cap < 0) { throw new System.ArgumentException("BoundedQueue constructor"); } capacity = cap; elements = new object[capacity]; size = 0; front = 0; back = 0; } //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: public void enqueue(Object o) throws NullPointerException, IllegalStateException public virtual void enqueue(object o) { // Modifies: this // Effects: If argument is null throw NullPointerException // else if this is full, throw IllegalStateException, // else make o the newest element of this if (o == null) { throw new System.NullReferenceException("BoundedQueue.enqueue"); } else if (size == capacity) { throw new InvalidOperationException("BoundedQueue.enqueue"); } else { size++; elements[back] = o; back = (back + 1) % capacity; } } //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: public Object dequeue() throws IllegalStateException public virtual object dequeue() { // Modifies: this // Effects: If queue is empty, throw IllegalStateException, // else remove and return oldest element of this if (size == 0) { throw new InvalidOperationException("BoundedQueue.dequeue"); } else { size--; object o = elements[(front % capacity)]; elements[front] = null; front = (front + 1) % capacity; return o; } } public virtual bool Empty { get { return (size == 0); } } public virtual bool Full { get { return (size == capacity); } } public override string ToString() { string result = "["; for (int i = 0; i < size; i++) { result += elements[(front + i) % capacity].ToString(); if (i < size - 1) { result += ", "; } } result += "]"; return result; } }