CharDeque { public void compact() { if (tail == head) { data = new char[1]; head = 0; tail = 0; mask = 0; return; } int size = size(); int newCapacity = size + 1; if (!isPowerOfTwo(newCapacity)) { newCapacity = nextPowerOfTwo(newCapacity); } char[] newData = new char[newCapacity]; fullNonEmptyCopy(newData); head = 0; tail = size; data = newData; mask = newData.length - 1; } CharDeque();  CharDeque(int initialCapacity); void clear(); char[] consumeAllQueue(); void consumeAllNonEmptyQueue(char[] dst); void consumeQueue(char[] dst, int offset, int length); boolean isEmpty(); int size(); void compact(); void addFirst(char element); void addFirst(Charray chars); void addLast(char element); void addLast(Charray chars); char get(int index); char getFirst(); char getLast(); char removeFirst(); char removeLast();  }
@Test public void testCompact() { final char first = 'a', last = 'z'; final int n = last - first; for (char c = first; c <= last; c++) { deque.addLast(c); } deque.clear(); assertTrue(deque.isEmpty()); deque.compact(); assertTrue(deque.isEmpty()); for (char c = first; c <= last; c++) { deque.addLast(c); System.out.println(c + " => " + deque.size()); } deque.removeFirst(); assertFalse(deque.isEmpty(), "The dequeue should NOT be empty!"); assertEquals(n, deque.size()); deque.compact(); assertFalse(deque.isEmpty(), "The dequeue should NOT be empty!"); assertEquals(n, deque.size()); deque.addLast('€'); assertFalse(deque.isEmpty(), "The dequeue should NOT be empty!"); assertEquals(n + 1, deque.size()); }