/** Check if previous is working correctly. */ public void testPrevious() { iter = list.listIterator(numElements); Object prev; for (int i = 0; i < numElements - 1; i++) { prev = iter.previous(); assertTrue(prev.equals(numElements - i - 1)); assertTrue(iter.hasPrevious()); } prev = iter.previous(); assertTrue(prev.equals(0)); assertFalse(iter.hasPrevious()); }
/* * Do the Samba! Perform a random sequence of next and previous * and check if you are at the correct point in the list */ public void testSamba() { Random random = new Random(); final int sambaSteps = 1; iter = list.listIterator(numElements / 2); // Position roughly in the middle int pos = numElements / 2; for (int i = 0; i < sambaSteps; i++) { boolean next = random.nextBoolean(); // Randomly jump ahead or back // pos keeps track of where you should be if (next && iter.hasNext()) { iter.next(); pos = pos + 1; } else if (!next && iter.hasPrevious()) { iter.previous(); pos = pos - 1; } } // Check to see if you are where you ought to be assertTrue((pos == numElements) || (iter.next().equals(pos))); }