package lab.util; import static org.junit.Assert.*; import java.util.Date; import java.util.NoSuchElementException; import org.junit.Rule; import org.junit.Test; import org.junit.rules.Timeout; public class QueueTest { @Rule public Timeout globalTimeout = new Timeout(500); @Test public void testEnqueuePeek() { Queue q = new Queue(); try { q.dequeue(); } catch (NoSuchElementException x) { // we should end up here Date exp0 = new Date(); Date exp1 = new Date(exp0.getTime() + 10000); Date exp2 = new Date(exp1.getTime() + 10000); q.enqueue(exp0); q.enqueue(exp1); q.enqueue(exp2); Date got0 = q.peek(); assertSame("peek did not return a reference to the front object", exp0, got0); return; } catch (Exception x) { fail("peek threw the wrong type of exception"); } fail("peek on an empty queue should have thrown an exception"); } @Test public void testEnqueueDequeue() { Queue q = new Queue(); try { q.dequeue(); } catch (NoSuchElementException x) { // we should end up here Date exp0 = new Date(); Date exp1 = new Date(exp0.getTime() + 10000); Date exp2 = new Date(exp1.getTime() + 10000); q.enqueue(exp0); q.enqueue(exp1); q.enqueue(exp2); System.out.println(q); Date got0 = q.dequeue(); assertSame("dequeue did not return a reference to the enqueued object", exp0, got0); Date got1 = q.dequeue(); assertSame("dequeue did not return a reference to the enqueued object", exp1, got1); Date got2 = q.dequeue(); assertSame("dequeue did not return a reference to the enqueued object", exp2, got2); return; } catch (Exception x) { fail("dequeue threw the wrong type of exception"); } fail("dequeue on an empty queue should have thrown an exception"); } @Test public void testSize() { Queue q = new Queue(); assertEquals("default queue size should be zero", 0, q.size()); for (int i = 1; i < 10000; i++) { q.enqueue("" + i); assertEquals("queue size should be " + i, i, q.size()); } } @Test public void testIsEmpty() { Queue q = new Queue(); assertTrue("default queue should be empty", q.isEmpty()); q.enqueue("not empty"); assertFalse("queue with one element should not be empty", q.isEmpty()); q.dequeue(); assertTrue("empty queue should be empty", q.isEmpty()); } }