/* * 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))); }
/** Check if next works correctly */ public void testNext() { // Move down one at a time // Check if this is being done correctly iter = list.listIterator(); Object next; for (int i = 0; i < numElements - 1; i++) { next = iter.next(); assertTrue(next.equals(i)); assertTrue(iter.hasNext()); } next = iter.next(); assertTrue(next.equals(numElements - 1)); assertFalse(iter.hasNext()); }
public void testSet() { // Double the values in the list using set iter = list.listIterator(); while (iter.hasNext()) { int next = (Integer) iter.next(); next *= 2; iter.set(next); } // Check if the doubling got done right assertEquals(list.size(), numElements); for (int i = 0; i < numElements; i++) assertTrue(list.get(i).equals(2 * i)); }