@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 testCeiling() throws Exception { TreeSet6<String> set = new TreeSet6<String>(); set.add("bbb"); set.add("aaa"); set.add("ccc"); String s; s = set.ceilingCompatible("bbb"); System.out.println("ceiling of bbb is : " + s); Assert.assertTrue("bbb".equals(s)); s = set.ceilingCompatible("b"); System.out.println("ceiling of b is : " + s); Assert.assertTrue("bbb".equals(s)); }
@Test public void testFloor() throws Exception { TreeSet6<String> set = new TreeSet6<String>(); set.add("bbb"); set.add("aaa"); set.add("ccc"); String s; s = set.floorCompatible("ccc"); System.out.println("floor of ccc is : " + s); Assert.assertTrue("ccc".equals(s)); s = set.floorCompatible("b"); System.out.println("floor of b is : " + s); Assert.assertTrue("aaa".equals(s)); }
@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); }