public void testCreateCopy() {
    for (Range<Integer> range1 : QUERY_RANGES) {
      for (Range<Integer> range2 : QUERY_RANGES) {
        TreeRangeSet<Integer> rangeSet = TreeRangeSet.create();
        rangeSet.add(range1);
        rangeSet.add(range2);

        assertEquals(rangeSet, TreeRangeSet.create(rangeSet));
      }
    }
  }
  public void testAllSingleRangesComplementAgainstRemove() {
    for (Range<Integer> range : QUERY_RANGES) {
      TreeRangeSet<Integer> rangeSet = TreeRangeSet.create();
      rangeSet.add(range);

      TreeRangeSet<Integer> complement = TreeRangeSet.create();
      complement.add(Range.<Integer>all());
      complement.remove(range);

      assertEquals(complement, rangeSet.complement());
      ASSERT.that(rangeSet.complement().asRanges()).has().allFrom(complement.asRanges()).inOrder();
    }
  }
 public void testRemoveNoOverlap() {
   TreeRangeSet<Integer> rangeSet = TreeRangeSet.create();
   rangeSet.add(Range.closed(3, 6));
   rangeSet.remove(Range.closedOpen(1, 3));
   testInvariants(rangeSet);
   ASSERT.that(rangeSet.asRanges()).has().allOf(Range.closed(3, 6)).inOrder();
 }
 public void testRemoveExact() {
   TreeRangeSet<Integer> rangeSet = TreeRangeSet.create();
   rangeSet.add(Range.closed(3, 6));
   rangeSet.remove(Range.closed(3, 6));
   testInvariants(rangeSet);
   ASSERT.that(rangeSet.asRanges()).isEmpty();
 }
 public void testAddEmpty() {
   TreeRangeSet<Integer> rangeSet = TreeRangeSet.create();
   rangeSet.add(Range.closedOpen(3, 3));
   testInvariants(rangeSet);
   ASSERT.that(rangeSet.asRanges()).isEmpty();
   ASSERT.that(rangeSet.complement().asRanges()).has().allOf(Range.<Integer>all()).inOrder();
 }
 public void testRemovePartFromAboveUpperBound() {
   TreeRangeSet<Integer> rangeSet = TreeRangeSet.create();
   rangeSet.add(Range.closed(3, 6));
   rangeSet.remove(Range.closed(6, 9));
   testInvariants(rangeSet);
   ASSERT.that(rangeSet.asRanges()).has().allOf(Range.closedOpen(3, 6)).inOrder();
 }
  public void put(Range<R> range, K value) {
    RangeMap<R, List<K>> subRangeMap = map.subRangeMap(range);
    Map<Range<R>, List<K>> submap = subRangeMap.asMapOfRanges();
    if (submap.isEmpty()) {
      ArrayList<K> list = new ArrayList<K>();
      list.add(value);
      map.put(range, list);
    } else {

      RangeSet<R> notCoveredSpan = TreeRangeSet.create();
      notCoveredSpan.add(range);

      List<Pair<Range<R>, List<K>>> newElements = new ArrayList<Pair<Range<R>, List<K>>>();
      for (Entry<Range<R>, List<K>> mapEntry : submap.entrySet()) {
        List<K> newValue = new ArrayList<K>(mapEntry.getValue());
        newValue.add(value);

        newElements.add(new ImmutablePair<Range<R>, List<K>>(mapEntry.getKey(), newValue));
        notCoveredSpan.remove(mapEntry.getKey());
      }

      for (Pair<Range<R>, List<K>> el : newElements) {
        map.put(el.getLeft(), el.getRight());
      }

      if (!notCoveredSpan.isEmpty()) {
        for (Range<R> notYetCoveredSpan : notCoveredSpan.asRanges()) {
          ArrayList<K> list = new ArrayList<K>();
          list.add(value);
          map.put(notYetCoveredSpan, list);
        }
      }
    }
  }
 public void testRemoveAllExtendingBothDirections() {
   TreeRangeSet<Integer> rangeSet = TreeRangeSet.create();
   rangeSet.add(Range.closed(3, 6));
   rangeSet.remove(Range.closed(2, 7));
   testInvariants(rangeSet);
   ASSERT.that(rangeSet.asRanges()).isEmpty();
 }
 public void testRemovePartFromBelowLowerBound() {
   TreeRangeSet<Integer> rangeSet = TreeRangeSet.create();
   rangeSet.add(Range.closed(3, 6));
   rangeSet.remove(Range.closed(1, 3));
   testInvariants(rangeSet);
   ASSERT.that(rangeSet.asRanges()).has().allOf(Range.openClosed(3, 6)).inOrder();
 }
 public void testRemoveAllFromAboveUpperBound() {
   TreeRangeSet<Integer> rangeSet = TreeRangeSet.create();
   rangeSet.add(Range.closed(3, 6));
   rangeSet.remove(Range.closed(3, 7));
   testInvariants(rangeSet);
   ASSERT.that(rangeSet.asRanges()).isEmpty();
 }
 public void testRangeContaining1() {
   RangeSet<Integer> rangeSet = TreeRangeSet.create();
   rangeSet.add(Range.closed(3, 10));
   assertEquals(Range.closed(3, 10), rangeSet.rangeContaining(5));
   assertTrue(rangeSet.contains(5));
   assertNull(rangeSet.rangeContaining(1));
   assertFalse(rangeSet.contains(1));
 }
 public void testAllSingleRangesEnclosing() {
   for (Range<Integer> range : QUERY_RANGES) {
     TreeRangeSet<Integer> rangeSet = TreeRangeSet.create();
     rangeSet.add(range);
     testEnclosing(rangeSet);
     testEnclosing(rangeSet.complement());
   }
 }
 private RangeSet<Integer> expectedSubRangeSet(
     RangeSet<Integer> rangeSet, Range<Integer> subRange) {
   RangeSet<Integer> expected = TreeRangeSet.create();
   for (Range<Integer> range : rangeSet.asRanges()) {
     if (range.isConnected(subRange)) {
       expected.add(range.intersection(subRange));
     }
   }
   return expected;
 }
 public void testComplement() {
   for (Range<Integer> range1 : QUERY_RANGES) {
     for (Range<Integer> range2 : QUERY_RANGES) {
       TreeRangeSet<Integer> rangeSet = TreeRangeSet.create();
       rangeSet.add(range1);
       rangeSet.add(range2);
       testViewAgainstExpected(expectedComplement(rangeSet), rangeSet.complement());
     }
   }
 }
 public void testRangeContaining2() {
   RangeSet<Integer> rangeSet = TreeRangeSet.create();
   rangeSet.add(Range.closed(3, 10));
   rangeSet.remove(Range.open(5, 7));
   assertEquals(Range.closed(3, 5), rangeSet.rangeContaining(5));
   assertTrue(rangeSet.contains(5));
   assertEquals(Range.closed(7, 10), rangeSet.rangeContaining(8));
   assertTrue(rangeSet.contains(8));
   assertNull(rangeSet.rangeContaining(6));
   assertFalse(rangeSet.contains(6));
 }
 public void testRemoveMiddle() {
   TreeRangeSet<Integer> rangeSet = TreeRangeSet.create();
   rangeSet.add(Range.atMost(6));
   rangeSet.remove(Range.closedOpen(3, 4));
   testInvariants(rangeSet);
   ASSERT.that(rangeSet.asRanges()).has().allOf(Range.lessThan(3), Range.closed(4, 6)).inOrder();
   ASSERT
       .that(rangeSet.complement().asRanges())
       .has()
       .allOf(Range.closedOpen(3, 4), Range.greaterThan(6))
       .inOrder();
 }
 public void testRemovePartSharingUpperBound() {
   TreeRangeSet<Integer> rangeSet = TreeRangeSet.create();
   rangeSet.add(Range.closed(3, 5));
   rangeSet.remove(Range.openClosed(3, 5));
   testInvariants(rangeSet);
   ASSERT.that(rangeSet.asRanges()).has().item(Range.singleton(3));
   ASSERT
       .that(rangeSet.complement().asRanges())
       .has()
       .allOf(Range.lessThan(3), Range.greaterThan(3))
       .inOrder();
 }
 public void testRemoveEmpty() {
   TreeRangeSet<Integer> rangeSet = TreeRangeSet.create();
   rangeSet.add(Range.closed(1, 6));
   rangeSet.remove(Range.closedOpen(3, 3));
   testInvariants(rangeSet);
   ASSERT.that(rangeSet.asRanges()).has().item(Range.closed(1, 6));
   ASSERT
       .that(rangeSet.complement().asRanges())
       .has()
       .allOf(Range.lessThan(1), Range.greaterThan(6))
       .inOrder();
 }
 public void testExtendBothDirections() {
   TreeRangeSet<Integer> rangeSet = TreeRangeSet.create();
   rangeSet.add(Range.closed(3, 4));
   rangeSet.add(Range.closed(1, 6));
   testInvariants(rangeSet);
   ASSERT.that(rangeSet.asRanges()).has().item(Range.closed(1, 6));
   ASSERT
       .that(rangeSet.complement().asRanges())
       .has()
       .allOf(Range.lessThan(1), Range.greaterThan(6))
       .inOrder();
 }
  @Test
  public void whenCreateRangeSet_thenCreated() {
    final RangeSet<Integer> rangeSet = TreeRangeSet.create();
    rangeSet.add(Range.closed(1, 10));
    rangeSet.add(Range.closed(12, 15));

    assertEquals(2, rangeSet.asRanges().size());

    rangeSet.add(Range.closed(10, 12));
    assertTrue(rangeSet.encloses(Range.closed(1, 15)));
    assertEquals(1, rangeSet.asRanges().size());
  }
 public void testMergesConnectedDisjoint() {
   TreeRangeSet<Integer> rangeSet = TreeRangeSet.create();
   rangeSet.add(Range.closed(1, 4));
   rangeSet.add(Range.open(4, 6));
   testInvariants(rangeSet);
   ASSERT.that(rangeSet.asRanges()).has().item(Range.closedOpen(1, 6));
   ASSERT
       .that(rangeSet.complement().asRanges())
       .has()
       .allOf(Range.lessThan(1), Range.atLeast(6))
       .inOrder();
 }
 public void testIgnoresSmallerSharingUpperBound() {
   TreeRangeSet<Integer> rangeSet = TreeRangeSet.create();
   rangeSet.add(Range.closed(1, 6));
   rangeSet.add(Range.closed(3, 6));
   testInvariants(rangeSet);
   ASSERT.that(rangeSet.asRanges()).has().item(Range.closed(1, 6));
   ASSERT
       .that(rangeSet.complement().asRanges())
       .has()
       .allOf(Range.lessThan(1), Range.greaterThan(6))
       .inOrder();
 }
Esempio n. 23
0
 private static RangeSet<Integer> characterRangesToTokenRanges(
     JavaInput javaInput, List<Range<Integer>> characterRanges) throws FormatterException {
   RangeSet<Integer> tokenRangeSet = TreeRangeSet.create();
   for (Range<Integer> characterRange0 : characterRanges) {
     Range<Integer> characterRange = characterRange0.canonical(DiscreteDomain.integers());
     tokenRangeSet.add(
         javaInput.characterRangeToTokenRange(
             characterRange.lowerEndpoint(),
             characterRange.upperEndpoint() - characterRange.lowerEndpoint()));
   }
   return tokenRangeSet;
 }
 public void testSubRangeSet() {
   for (Range<Integer> range1 : QUERY_RANGES) {
     for (Range<Integer> range2 : QUERY_RANGES) {
       TreeRangeSet<Integer> rangeSet = TreeRangeSet.create();
       rangeSet.add(range1);
       rangeSet.add(range2);
       for (Range<Integer> subRange : QUERY_RANGES) {
         testViewAgainstExpected(
             expectedSubRangeSet(rangeSet, subRange), rangeSet.subRangeSet(subRange));
       }
     }
   }
 }
 public void testFillHoleWithOverlap() {
   TreeRangeSet<Integer> rangeSet = TreeRangeSet.create();
   rangeSet.add(Range.closedOpen(1, 3));
   rangeSet.add(Range.closedOpen(4, 6));
   rangeSet.add(Range.closedOpen(2, 5));
   testInvariants(rangeSet);
   ASSERT.that(rangeSet.asRanges()).has().item(Range.closedOpen(1, 6));
   ASSERT
       .that(rangeSet.complement().asRanges())
       .has()
       .allOf(Range.lessThan(1), Range.atLeast(6))
       .inOrder();
 }
 @Override
 public ImmutableRangeSet<Integer> lookahead(Language lang) {
   Frequency ff = lang.lali.consumesInput(first);
   switch (ff) {
     case NEVER:
       return lang.lali.lookahead(second);
     case ALWAYS:
       return lang.lali.lookahead(first);
     case SOMETIMES:
       TreeRangeSet<Integer> r = TreeRangeSet.create();
       r.addAll(lang.lali.lookahead(first));
       r.addAll(lang.lali.lookahead(second));
       return ImmutableRangeSet.copyOf(r);
   }
   throw new AssertionError(ff.name());
 }
Esempio n. 27
0
 /**
  * Format an input string (a Java compilation unit) into an output string.
  *
  * @param input the input string
  * @return the output string
  * @throws FormatterException if the input string cannot be parsed
  */
 public String formatSource(String input) throws FormatterException {
   JavaInput javaInput = new JavaInput(STDIN_FILENAME, input);
   JavaOutput javaOutput = new JavaOutput(javaInput, new JavaCommentsHelper());
   List<FormatterDiagnostic> errors = new ArrayList<>();
   format(javaInput, javaOutput, MAX_WIDTH, errors, 1);
   if (!errors.isEmpty()) {
     throw new FormatterException(errors);
   }
   StringBuilder result = new StringBuilder(input.length());
   RangeSet<Integer> lineRangeSet = TreeRangeSet.create();
   lineRangeSet.add(Range.<Integer>all());
   try {
     javaOutput.writeMerged(result, lineRangeSet);
   } catch (IOException ignored) {
     throw new AssertionError("IOException impossible for StringWriter");
   }
   return result.toString();
 }
  public void testRangesByUpperBound() {
    for (Range<Integer> range1 : QUERY_RANGES) {
      for (Range<Integer> range2 : QUERY_RANGES) {
        TreeRangeSet<Integer> rangeSet = TreeRangeSet.create();
        rangeSet.add(range1);
        rangeSet.add(range2);

        NavigableMap<Cut<Integer>, Range<Integer>> expectedRangesByUpperBound = Maps.newTreeMap();
        for (Range<Integer> range : rangeSet.asRanges()) {
          expectedRangesByUpperBound.put(range.upperBound, range);
        }
        testNavigationAgainstExpected(
            expectedRangesByUpperBound,
            new TreeRangeSet.RangesByUpperBound<Integer>(rangeSet.rangesByLowerBound),
            CUTS_TO_TEST);
      }
    }
  }
 private static void doPairTest(Range<Integer> a, Range<Integer> b) {
   TreeRangeSet<Integer> rangeSet = TreeRangeSet.create();
   rangeSet.add(a);
   rangeSet.add(b);
   if (a.isEmpty() && b.isEmpty()) {
     ASSERT.that(rangeSet.asRanges()).isEmpty();
   } else if (a.isEmpty()) {
     ASSERT.that(rangeSet.asRanges()).has().item(b);
   } else if (b.isEmpty()) {
     ASSERT.that(rangeSet.asRanges()).has().item(a);
   } else if (a.isConnected(b)) {
     ASSERT.that(rangeSet.asRanges()).has().allOf(a.span(b)).inOrder();
   } else {
     if (a.lowerEndpoint() < b.lowerEndpoint()) {
       ASSERT.that(rangeSet.asRanges()).has().allOf(a, b).inOrder();
     } else {
       ASSERT.that(rangeSet.asRanges()).has().allOf(b, a).inOrder();
     }
   }
 }
 private RangeSet<Integer> expectedComplement(RangeSet<Integer> rangeSet) {
   RangeSet<Integer> expected = TreeRangeSet.create();
   expected.add(Range.<Integer>all());
   expected.removeAll(rangeSet);
   return expected;
 }