use of org.apache.pulsar.common.util.collections.ConcurrentLongPairSet.LongPair in project incubator-pulsar by apache.
the class ConcurrentLongPairSetTest method testIteration.
@Test
public void testIteration() {
ConcurrentLongPairSet set = new ConcurrentLongPairSet();
assertEquals(set.items(), Collections.emptyList());
set.add(0l, 0l);
assertTrue(set.items().iterator().next().equals(new LongPair(0l, 0l)));
set.remove(0l, 0l);
assertEquals(set.items(), Collections.emptyList());
set.add(0l, 0l);
set.add(1l, 1l);
set.add(2l, 2l);
List<LongPair> values = new ArrayList<>(set.items());
values.sort(null);
assertEquals(values, Lists.newArrayList(new LongPair(0, 0), new LongPair(1, 1), new LongPair(2, 2)));
set.clear();
assertTrue(set.isEmpty());
}
use of org.apache.pulsar.common.util.collections.ConcurrentLongPairSet.LongPair in project incubator-pulsar by apache.
the class ConcurrentLongPairSetTest method testRemoval.
@Test
public void testRemoval() {
ConcurrentLongPairSet set = new ConcurrentLongPairSet();
set.add(0, 0);
set.add(1, 1);
set.add(3, 3);
set.add(6, 6);
set.add(7, 7);
List<LongPair> values = new ArrayList<>(set.items());
values.sort(null);
assertEquals(values, Lists.newArrayList(new LongPair(0, 0), new LongPair(1, 1), new LongPair(3, 3), new LongPair(6, 6), new LongPair(7, 7)));
set.forEach((first, second) -> {
if (first < 5) {
set.remove(first, second);
}
});
assertEquals(set.size(), values.size() - 3);
values = new ArrayList<>(set.items());
values.sort(null);
assertEquals(values, Lists.newArrayList(new LongPair(6, 6), new LongPair(7, 7)));
}
use of org.apache.pulsar.common.util.collections.ConcurrentLongPairSet.LongPair in project incubator-pulsar by apache.
the class ConcurrentLongPairSetTest method testRehashingRemoval.
@Test
public void testRehashingRemoval() {
int n = 16;
ConcurrentLongPairSet set = new ConcurrentLongPairSet(n / 2, 1);
assertEquals(set.capacity(), n);
assertEquals(set.size(), 0);
int insertItems = 1000 * n;
for (int i = 0; i < insertItems; i++) {
set.add(i, -1);
}
int newSize = (int) Math.pow(2, 32 - Integer.numberOfLeadingZeros(insertItems - 1));
assertEquals(set.capacity(), newSize * 2);
assertEquals(set.size(), insertItems);
Set<LongPair> pairs = new HashSet<>();
set.forEach((first, second) -> {
pairs.add(new LongPair(first, second));
});
pairs.forEach(pair -> set.remove(pair.first, -1));
assertEquals(set.capacity(), newSize * 2);
assertEquals(set.size(), 0);
}
use of org.apache.pulsar.common.util.collections.ConcurrentLongPairSet.LongPair in project incubator-pulsar by apache.
the class ConcurrentLongPairSetTest method testItems.
@Test
public void testItems() {
ConcurrentLongPairSet set = new ConcurrentLongPairSet();
int n = 100;
int limit = 10;
for (int i = 0; i < n; i++) {
set.add(i, i);
}
Set<LongPair> items = set.items();
Set<LongPair> limitItems = set.items(limit);
assertEquals(items.size(), n);
assertEquals(limitItems.size(), limit);
int totalRemovedItems = set.removeIf((first, second) -> limitItems.contains((new LongPair(first, second))));
assertEquals(limitItems.size(), totalRemovedItems);
assertEquals(set.size(), n - limit);
}
use of org.apache.pulsar.common.util.collections.ConcurrentLongPairSet.LongPair in project incubator-pulsar by apache.
the class ConcurrentLongPairSetTest method testIfRemoval.
@Test
public void testIfRemoval() {
ConcurrentLongPairSet set = new ConcurrentLongPairSet();
set.add(0, 0);
set.add(1, 1);
set.add(3, 3);
set.add(6, 6);
set.add(7, 7);
List<LongPair> values = new ArrayList<>(set.items());
values.sort(null);
assertEquals(values, Lists.newArrayList(new LongPair(0, 0), new LongPair(1, 1), new LongPair(3, 3), new LongPair(6, 6), new LongPair(7, 7)));
int removeItems = set.removeIf((first, second) -> first < 5);
assertEquals(3, removeItems);
assertEquals(set.size(), values.size() - 3);
values = new ArrayList<>(set.items());
values.sort(null);
assertEquals(values, Lists.newArrayList(new LongPair(6, 6), new LongPair(7, 7)));
}
Aggregations