use of org.apache.pulsar.common.util.collections.ConcurrentLongPairSet 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 in project incubator-pulsar by apache.
the class ConcurrentLongPairSetTest method testRehashingWithDeletes.
@Test
public void testRehashingWithDeletes() {
int n = 16;
ConcurrentLongPairSet set = new ConcurrentLongPairSet(n / 2, 1);
assertEquals(set.capacity(), n);
assertEquals(set.size(), 0);
for (int i = 0; i < n / 2; i++) {
set.add(i, i);
}
for (int i = 0; i < n / 2; i++) {
assertTrue(set.remove(i, i));
}
assertEquals(set.capacity(), n);
assertEquals(set.size(), 0);
for (int i = n; i < (2 * n); i++) {
set.add(i, i);
}
assertEquals(set.capacity(), 2 * n);
assertEquals(set.size(), n);
}
use of org.apache.pulsar.common.util.collections.ConcurrentLongPairSet in project incubator-pulsar by apache.
the class ConcurrentLongPairSetTest method testRemove.
@Test
public void testRemove() {
ConcurrentLongPairSet set = new ConcurrentLongPairSet();
assertTrue(set.isEmpty());
assertTrue(set.add(1, 1));
assertFalse(set.isEmpty());
assertFalse(set.remove(1, 0));
assertFalse(set.isEmpty());
assertTrue(set.remove(1, 1));
assertTrue(set.isEmpty());
}
use of org.apache.pulsar.common.util.collections.ConcurrentLongPairSet 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)));
}
use of org.apache.pulsar.common.util.collections.ConcurrentLongPairSet in project incubator-pulsar by apache.
the class ConcurrentLongPairSetTest method testHashConflictWithDeletion.
@Test
public void testHashConflictWithDeletion() {
final int Buckets = 16;
ConcurrentLongPairSet set = new ConcurrentLongPairSet(Buckets, 1);
// Pick 2 keys that fall into the same bucket
long key1 = 1;
long key2 = 538515;
int bucket1 = ConcurrentLongPairSet.signSafeMod(ConcurrentLongPairSet.hash(key1, key1), Buckets);
int bucket2 = ConcurrentLongPairSet.signSafeMod(ConcurrentLongPairSet.hash(key2, key2), Buckets);
assertEquals(bucket1, bucket2);
assertTrue(set.add(key1, key1));
assertTrue(set.add(key2, key2));
assertEquals(set.size(), 2);
assertTrue(set.remove(key1, key1));
assertEquals(set.size(), 1);
assertTrue(set.add(key1, key1));
assertEquals(set.size(), 2);
assertTrue(set.remove(key1, key1));
assertEquals(set.size(), 1);
assertFalse(set.add(key2, key2));
assertTrue(set.contains(key2, key2));
assertEquals(set.size(), 1);
assertTrue(set.remove(key2, key2));
assertTrue(set.isEmpty());
}
Aggregations