public void testRandomAddOnQueue() { CircularQueue<Integer> q = new CircularQueue<>(); // Create a queue with 5 elements and capacity 8; for (int i = 0; i < 5; i++) { q.offer(i); } q.add(0, 100); q.add(3, 200); q.add(7, 300); Iterator<Integer> i = q.iterator(); assertEquals(8, q.size()); assertEquals(new Integer(100), i.next()); assertEquals(new Integer(0), i.next()); assertEquals(new Integer(1), i.next()); assertEquals(new Integer(200), i.next()); assertEquals(new Integer(2), i.next()); assertEquals(new Integer(3), i.next()); assertEquals(new Integer(4), i.next()); assertEquals(new Integer(300), i.next()); try { i.next(); fail(); } catch (Exception e) { // an exception signifies a successfull test case assertTrue(true); } }
public void testExpandAndShrink() throws Exception { CircularQueue<Integer> q = new CircularQueue<>(); for (int i = 0; i < 1024; i++) { q.offer(i); } assertEquals(1024, q.capacity()); for (int i = 0; i < 512; i++) { q.offer(i); q.poll(); } assertEquals(2048, q.capacity()); for (int i = 0; i < 1024; i++) { q.poll(); } assertEquals(4, q.capacity()); }
private CircularQueue<Integer> getRotatedQueue() { CircularQueue<Integer> q = new CircularQueue<>(); // Ensure capacity: 16 for (int i = 0; i < 16; i++) { q.offer(-1); } q.clear(); // Rotate it for (int i = 0; i < 12; i++) { q.offer(-1); q.poll(); } // Now push items for (int i = 0; i < 8; i++) { q.offer(i); } return q; }
public void testExpandingRotation() { CircularQueue<Integer> q = new CircularQueue<>(); // DEFAULT_CAPACITY = 4 for (int i = 0; i < 10; i++) { testRotation0(q); // make expansion happen int oldCapacity = q.capacity(); for (int j = q.capacity(); j >= 0; j--) { q.offer(++pushCount); } assertTrue(q.capacity() > oldCapacity); testRotation0(q); } }
private void testRotation0(CircularQueue<Integer> q) { for (int i = 0; i < q.capacity() * 7 / 4; i++) { q.offer(++pushCount); assertEquals(++popCount, q.poll().intValue()); } }