Beispiel #1
0
 public void testDescendingSubSetContents2() {
   NavigableSet set = dset5();
   SortedSet sm = set.subSet(m2, m3);
   assertEquals(1, sm.size());
   assertEquals(m2, sm.first());
   assertEquals(m2, sm.last());
   assertFalse(sm.contains(m1));
   assertTrue(sm.contains(m2));
   assertFalse(sm.contains(m3));
   assertFalse(sm.contains(m4));
   assertFalse(sm.contains(m5));
   Iterator i = sm.iterator();
   Object k;
   k = (Integer) (i.next());
   assertEquals(m2, k);
   assertFalse(i.hasNext());
   Iterator j = sm.iterator();
   j.next();
   j.remove();
   assertFalse(set.contains(m2));
   assertEquals(4, set.size());
   assertEquals(0, sm.size());
   assertTrue(sm.isEmpty());
   assertFalse(sm.remove(m3));
   assertEquals(4, set.size());
 }
Beispiel #2
0
 public void testSubSetContents2() {
   NavigableSet set = set5();
   SortedSet sm = set.subSet(two, three);
   assertEquals(1, sm.size());
   assertEquals(two, sm.first());
   assertEquals(two, sm.last());
   assertFalse(sm.contains(one));
   assertTrue(sm.contains(two));
   assertFalse(sm.contains(three));
   assertFalse(sm.contains(four));
   assertFalse(sm.contains(five));
   Iterator i = sm.iterator();
   Object k;
   k = (Integer) (i.next());
   assertEquals(two, k);
   assertFalse(i.hasNext());
   Iterator j = sm.iterator();
   j.next();
   j.remove();
   assertFalse(set.contains(two));
   assertEquals(4, set.size());
   assertEquals(0, sm.size());
   assertTrue(sm.isEmpty());
   assertFalse(sm.remove(three));
   assertEquals(4, set.size());
 }
 private static void testAllSlices(
     String id,
     NavigableSet<Integer> btree,
     NavigableSet<Integer> canon,
     boolean ascending,
     List<ListenableFuture<?>> results) {
   testOneSlice(id, btree, canon, results);
   for (Integer lb : range(canon.size(), Integer.MIN_VALUE, ascending)) {
     // test head/tail sets
     testOneSlice(
         String.format("%s->[..%d)", id, lb),
         btree.headSet(lb, true),
         canon.headSet(lb, true),
         results);
     testOneSlice(
         String.format("%s->(..%d)", id, lb),
         btree.headSet(lb, false),
         canon.headSet(lb, false),
         results);
     testOneSlice(
         String.format("%s->(%d..]", id, lb),
         btree.tailSet(lb, true),
         canon.tailSet(lb, true),
         results);
     testOneSlice(
         String.format("%s->(%d..]", id, lb),
         btree.tailSet(lb, false),
         canon.tailSet(lb, false),
         results);
     for (Integer ub : range(canon.size(), lb, ascending)) {
       // test subsets
       testOneSlice(
           String.format("%s->[%d..%d]", id, lb, ub),
           btree.subSet(lb, true, ub, true),
           canon.subSet(lb, true, ub, true),
           results);
       testOneSlice(
           String.format("%s->(%d..%d]", id, lb, ub),
           btree.subSet(lb, false, ub, true),
           canon.subSet(lb, false, ub, true),
           results);
       testOneSlice(
           String.format("%s->[%d..%d)", id, lb, ub),
           btree.subSet(lb, true, ub, false),
           canon.subSet(lb, true, ub, false),
           results);
       testOneSlice(
           String.format("%s->(%d..%d)", id, lb, ub),
           btree.subSet(lb, false, ub, false),
           canon.subSet(lb, false, ub, false),
           results);
     }
   }
 }
Beispiel #4
0
  @GwtIncompatible("NavigableSet")
  public void testUnmodifiableNavigableSet() {
    TreeSet<Integer> mod = Sets.newTreeSet();
    mod.add(1);
    mod.add(2);
    mod.add(3);

    NavigableSet<Integer> unmod = unmodifiableNavigableSet(mod);

    /* Unmodifiable is a view. */
    mod.add(4);
    assertTrue(unmod.contains(4));
    assertTrue(unmod.descendingSet().contains(4));

    ensureNotDirectlyModifiable(unmod);
    ensureNotDirectlyModifiable(unmod.descendingSet());
    ensureNotDirectlyModifiable(unmod.headSet(2));
    ensureNotDirectlyModifiable(unmod.headSet(2, true));
    ensureNotDirectlyModifiable(unmod.tailSet(2));
    ensureNotDirectlyModifiable(unmod.tailSet(2, true));
    ensureNotDirectlyModifiable(unmod.subSet(1, 3));
    ensureNotDirectlyModifiable(unmod.subSet(1, true, 3, true));

    /* UnsupportedOperationException on indirect modifications. */
    NavigableSet<Integer> reverse = unmod.descendingSet();
    try {
      reverse.add(4);
      fail("UnsupportedOperationException expected");
    } catch (UnsupportedOperationException expected) {
    }
    try {
      reverse.addAll(Collections.singleton(4));
      fail("UnsupportedOperationException expected");
    } catch (UnsupportedOperationException expected) {
    }
    try {
      reverse.remove(4);
      fail("UnsupportedOperationException expected");
    } catch (UnsupportedOperationException expected) {
    }
  }
  @SuppressWarnings("EmptyCatchBlock")
  public void testUnmodifiability() {
    TreeSet<Integer> mod = Sets.newTreeSet();
    mod.add(1);
    mod.add(2);
    mod.add(3);

    NavigableSet<Integer> unmod = new UnmodifiableNavigableSet<Integer>(mod);

    mod.add(4);
    assertTrue(unmod.contains(4));
    assertTrue(unmod.descendingSet().contains(4));

    ensureNotDirectlyModifiable(unmod);
    ensureNotDirectlyModifiable(unmod.descendingSet());
    ensureNotDirectlyModifiable(unmod.headSet(2));
    ensureNotDirectlyModifiable(unmod.headSet(2, true));
    ensureNotDirectlyModifiable(unmod.tailSet(2));
    ensureNotDirectlyModifiable(unmod.tailSet(2, true));
    ensureNotDirectlyModifiable(unmod.subSet(1, 3));
    ensureNotDirectlyModifiable(unmod.subSet(1, true, 3, true));

    NavigableSet<Integer> reverse = unmod.descendingSet();
    try {
      reverse.add(4);
      fail("UnsupportedOperationException expected");
    } catch (UnsupportedOperationException expected) {
    }
    try {
      reverse.addAll(Collections.singleton(4));
      fail("UnsupportedOperationException expected");
    } catch (UnsupportedOperationException expected) {
    }
    try {
      reverse.remove(4);
      fail("UnsupportedOperationException expected");
    } catch (UnsupportedOperationException expected) {
    }
  }
 @Override
 public NavigableSet<ProtocolWaveletDelta> requestTransformedHistory(
     ProtocolHashedVersion versionStart, ProtocolHashedVersion versionEnd)
     throws WaveletStateException {
   acquireReadLock();
   try {
     assertStateOk();
     // TODO: ### validate requested range.
     // TODO: #### make immutable.
     return transformedDeltas.subSet(
         transformedDeltas.floor(emptyDeltaAtVersion(versionStart.getVersion())),
         true,
         emptyDeltaAtVersion(versionEnd.getVersion()),
         false);
   } finally {
     releaseReadLock();
   }
 }
    RandomSelection select(boolean narrow, boolean mixInNotPresentItems, boolean permitReversal) {
      ThreadLocalRandom random = ThreadLocalRandom.current();
      NavigableSet<Integer> canonicalSet = this.canonical;
      BTreeSet<Integer> testAsSet = this.test;
      List<Integer> canonicalList = new ArrayList<>(canonicalSet);
      BTreeSet<Integer> testAsList = this.test;

      Assert.assertEquals(canonicalSet.size(), testAsSet.size());
      Assert.assertEquals(canonicalList.size(), testAsList.size());

      // sometimes select keys first, so we cover full range
      List<Integer> allKeys = randomKeys(canonical, mixInNotPresentItems);
      List<Integer> keys = allKeys;

      int narrowCount = random.nextInt(3);
      while (narrow && canonicalList.size() > 10 && keys.size() > 10 && narrowCount-- > 0) {
        boolean useLb = random.nextBoolean();
        boolean useUb = random.nextBoolean();
        if (!(useLb | useUb)) continue;

        // select a range smaller than the total span when we have more narrowing iterations left
        int indexRange = keys.size() / (narrowCount + 1);

        boolean lbInclusive = true;
        Integer lbKey = canonicalList.get(0);
        int lbKeyIndex = 0, lbIndex = 0;
        boolean ubInclusive = true;
        Integer ubKey = canonicalList.get(canonicalList.size() - 1);
        int ubKeyIndex = keys.size(), ubIndex = canonicalList.size();

        if (useLb) {
          lbKeyIndex = random.nextInt(0, indexRange - 1);
          Integer candidate = keys.get(lbKeyIndex);
          if (useLb = (candidate > lbKey && candidate <= ubKey)) {
            lbInclusive = random.nextBoolean();
            lbKey = keys.get(lbKeyIndex);
            lbIndex = Collections.binarySearch(canonicalList, lbKey);
            if (lbIndex >= 0 && !lbInclusive) lbIndex++;
            else if (lbIndex < 0) lbIndex = -1 - lbIndex;
          }
        }
        if (useUb) {
          ubKeyIndex =
              random.nextInt(Math.max(lbKeyIndex, keys.size() - indexRange), keys.size() - 1);
          Integer candidate = keys.get(ubKeyIndex);
          if (useUb = (candidate < ubKey && candidate >= lbKey)) {
            ubInclusive = random.nextBoolean();
            ubKey = keys.get(ubKeyIndex);
            ubIndex = Collections.binarySearch(canonicalList, ubKey);
            if (ubIndex >= 0 && ubInclusive) {
              ubIndex++;
            } else if (ubIndex < 0) ubIndex = -1 - ubIndex;
          }
        }
        if (ubIndex < lbIndex) {
          ubIndex = lbIndex;
          ubKey = lbKey;
          ubInclusive = false;
        }

        canonicalSet =
            !useLb
                ? canonicalSet.headSet(ubKey, ubInclusive)
                : !useUb
                    ? canonicalSet.tailSet(lbKey, lbInclusive)
                    : canonicalSet.subSet(lbKey, lbInclusive, ubKey, ubInclusive);
        testAsSet =
            !useLb
                ? testAsSet.headSet(ubKey, ubInclusive)
                : !useUb
                    ? testAsSet.tailSet(lbKey, lbInclusive)
                    : testAsSet.subSet(lbKey, lbInclusive, ubKey, ubInclusive);

        keys = keys.subList(lbKeyIndex, ubKeyIndex);
        canonicalList = canonicalList.subList(lbIndex, ubIndex);
        testAsList = testAsList.subList(lbIndex, ubIndex);

        Assert.assertEquals(canonicalSet.size(), testAsSet.size());
        Assert.assertEquals(canonicalList.size(), testAsList.size());
      }

      // possibly restore full set of keys, to test case where we are provided existing keys that
      // are out of bounds
      if (keys != allKeys && random.nextBoolean()) keys = allKeys;

      Comparator<Integer> comparator = naturalOrder();
      if (permitReversal && random.nextBoolean()) {
        if (allKeys != keys) keys = new ArrayList<>(keys);
        if (canonicalSet != canonical) canonicalList = new ArrayList<>(canonicalList);
        Collections.reverse(keys);
        Collections.reverse(canonicalList);
        testAsList = testAsList.descendingSet();

        canonicalSet = canonicalSet.descendingSet();
        testAsSet = testAsSet.descendingSet();
        comparator = reverseOrder();
      }

      Assert.assertEquals(canonicalSet.size(), testAsSet.size());
      Assert.assertEquals(canonicalList.size(), testAsList.size());
      if (!canonicalSet.isEmpty()) {
        Assert.assertEquals(canonicalSet.first(), canonicalList.get(0));
        Assert.assertEquals(canonicalSet.last(), canonicalList.get(canonicalList.size() - 1));
        Assert.assertEquals(canonicalSet.first(), testAsSet.first());
        Assert.assertEquals(canonicalSet.last(), testAsSet.last());
        Assert.assertEquals(canonicalSet.first(), testAsList.get(0));
        Assert.assertEquals(canonicalSet.last(), testAsList.get(testAsList.size() - 1));
      }

      return new RandomSelection(
          keys, canonicalSet, testAsSet, canonicalList, testAsList, comparator);
    }
Beispiel #8
0
 // return a subset exclusively greater than lowerBound and inclusively less than upperBound
 public static NavigableSet<SerialNumber> getExclusiveToInclusiveSubset(
     NavigableSet<SerialNumber> compatibleProducts,
     SerialNumber lowerBound,
     SerialNumber upperBound) {
   return compatibleProducts.subSet(lowerBound, false, upperBound, true);
 }