public static final void test() { Random r = new Random(); String msg = ""; while (true) { ToolsArrayQueue<Integer> taq = new ToolsArrayQueue<Integer>(); LinkedList<Integer> ll = new LinkedList<Integer>(); for (int i = 0; i < 2000000; i++) { msg = ""; try { msg += assertequal("Size difference: LL = %d, TAQ = %d; ", ll.size(), taq.size()); msg += assertequal("Emptiness difference: LL = %d, TAQ = %d; ", ll.isEmpty(), taq.isEmpty()); Object llv, taqv; switch (r.nextInt(4)) { case 0: case 3: int rnd = r.nextInt(); llv = ll.offer(rnd); taqv = taq.offer(rnd); msg += assertequal("Offer: LL = %s, TAQ = %s; ", llv, taqv); break; case 1: llv = ll.peek(); taqv = taq.peek(); msg += assertequal("Peek: LL = %s, TAQ = %s; ", llv, taqv); break; case 2: llv = ll.poll(); taqv = taq.poll(); msg += assertequal("Poll: LL = %s, TAQ = %s; ", llv, taqv); break; } } catch (Exception e) { msg += "EXCEPTION [[" + e.toString() + "]]; "; e.printStackTrace(); } if (!msg.equals("") || (i % 100000 == 0)) System.out.println(String.format("%d: %s", i, msg)); } int size = taq.size(); System.out.println("size: " + size); Integer[] fromArr = (Integer[]) taq.toArray(new Integer[size]); Integer[] fromIt = new Integer[size]; int idx = 0; for (Integer ii : taq) { fromIt[idx++] = ii; } System.out.print(assertequal("fromIt size: expected %d got %d\n", size, idx)); for (int i = 0; i < size; i++) { msg = ""; msg += assertequal("arr/it: arr = %s, it = %s; ", fromArr[i], fromIt[i]); msg += assertequal("arr/Q: arr = %s, Q = %s; ", fromArr[i], taq.poll()); if (!msg.equals("")) System.out.println(String.format("%d: %s", i, msg)); } System.out.print(assertequal("size: expected %d got %d\n", 0, taq.size())); System.out.println("equality check done"); } }
// TODO can this be addAll? public boolean addQueue(ToolsArrayQueue<T> that) { int thatsize = that.size(); ensureCapacity(size() + thatsize); arraycopy( that.elements, that.elements.length, that.start, elements, elements.length, end, thatsize); end += thatsize; if (end >= elements.length) end -= elements.length; return true; }