@Test public void testDescendingIteratorRemoveLast() throws Exception { TreeSet6<Integer> set = new TreeSet6<Integer>(); int count = 1000; int[] truth = new int[count]; Random rnd = new Random(); for (int i = 0; i < count; i++) { truth[i] = rnd.nextInt(); set.add(truth[i]); } Arrays.sort(truth); // verify descending iterator int index = count; Iterator<Integer> iter = set.descendingIteratorCompatible(); while (iter.hasNext()) { index--; Integer test = iter.next(); Assert.assertEquals(truth[index], test.intValue()); if (index == 0) iter.remove(); } Assert.assertEquals(0, index); // do it again and verify missing. Start at top and // should go down to index 1 index = count; iter = set.descendingIteratorCompatible(); while (iter.hasNext()) { index--; Integer test = iter.next(); Assert.assertEquals(truth[index], test.intValue()); } Assert.assertEquals(1, index); }
@Test public void testDescendingIterator() throws Exception { TreeSet6<Integer> set = new TreeSet6<Integer>(); int count = 1000; int[] truth = new int[count]; Random rnd = new Random(); for (int i = 0; i < count; i++) { truth[i] = rnd.nextInt(); set.add(truth[i]); } Arrays.sort(truth); // verify ascending iterator int index = 0; Iterator<Integer> iter = set.iterator(); while (iter.hasNext()) { Integer test = iter.next(); Assert.assertEquals(truth[index], test.intValue()); index++; } Assert.assertEquals(count, index); // verify descending iterator index = count; iter = set.descendingIteratorCompatible(); while (iter.hasNext()) { index--; Integer test = iter.next(); Assert.assertEquals(truth[index], test.intValue()); } Assert.assertEquals(0, index); }
@Test public void testDescendingIteratorRemoveRandom() throws Exception { TreeSet6<Integer> set = new TreeSet6<Integer>(); int count = 1000; int[] truth = new int[count]; Random rnd = new Random(); ArrayList<Integer> afterremove = new ArrayList<Integer>(); for (int i = 0; i < count; i++) { truth[i] = rnd.nextInt(); set.add(truth[i]); } Arrays.sort(truth); // verify descending iterator int index = count; Iterator<Integer> iter = set.descendingIteratorCompatible(); while (iter.hasNext()) { index--; Integer test = iter.next(); Assert.assertEquals(truth[index], test.intValue()); if (rnd.nextDouble() < 0.5) iter.remove(); else afterremove.add(test); } Assert.assertEquals(0, index); // afterremove is in descending order already, so // walk up the indexes from 0. index = 0; iter = set.descendingIteratorCompatible(); while (iter.hasNext()) { Integer test = iter.next(); Assert.assertEquals(afterremove.get(index).intValue(), test.intValue()); index++; } Assert.assertEquals(afterremove.size(), index); }