Skip to content

Commit

Permalink
Fix inconsistent behavior in LongPairRangeSet (apache#10713)
Browse files Browse the repository at this point in the history
* fix inconsistent behavior

* add unit test

* fix unit test

* add unit test

(cherry picked from commit 4b2c673)
  • Loading branch information
315157973 authored and eolivelli committed Nov 24, 2021
1 parent 0bc7251 commit 4c83db2
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
import com.google.common.collect.TreeRangeSet;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;

/**
Expand Down Expand Up @@ -237,7 +239,11 @@ public boolean isEmpty() {

@Override
public Range<T> span() {
return set.span();
try {
return set.span();
} catch (NoSuchElementException e) {
return null;
}
}

@Override
Expand Down Expand Up @@ -266,7 +272,11 @@ public boolean contains(long key, long value) {

@Override
public Range<T> firstRange() {
return set.asRanges().iterator().next();
Iterator<Range<T>> iterable = set.asRanges().iterator();
if (iterable.hasNext()) {
return set.asRanges().iterator().next();
}
return null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.pulsar.common.util.collections;

import org.testng.annotations.Test;

import static org.testng.AssertJUnit.assertNull;
import static org.testng.AssertJUnit.assertTrue;
import static org.testng.AssertJUnit.fail;

public class DefaultRangeSetTest {
static final LongPairRangeSet.LongPairConsumer<LongPairRangeSet.LongPair> consumer =
LongPairRangeSet.LongPair::new;

@Test
public void testBehavior() {
LongPairRangeSet.DefaultRangeSet<LongPairRangeSet.LongPair> set =
new LongPairRangeSet.DefaultRangeSet<>(consumer);
ConcurrentOpenLongPairRangeSet<LongPairRangeSet.LongPair> rangeSet =
new ConcurrentOpenLongPairRangeSet<>(consumer);

assertNull(set.firstRange());
assertNull(set.lastRange());
assertNull(set.span());

assertNull(rangeSet.firstRange());
assertNull(rangeSet.lastRange());
assertNull(rangeSet.span());

try {
set.contains(null);
fail("should fail");
} catch (NullPointerException ignore) {
}
try {
rangeSet.contains(null);
fail("should fail");
} catch (NullPointerException ignore) {
}

rangeSet.addOpenClosed(9, 0, 10, 10);
set.addOpenClosed(9, 0, 10, 10);
assertTrue(rangeSet.contains(10,1));
assertTrue(set.contains(10,1));

}
}

0 comments on commit 4c83db2

Please sign in to comment.