@Test
public void compacts() {
ByteArrayOrdinalMap map = new ByteArrayOrdinalMap();
ByteDataBuffer buf = new ByteDataBuffer();
/// add 1000 entries
for(int i=0;i<1000;i++) {
VarInt.writeVInt(buf, 10000 + i);
map.getOrAssignOrdinal(buf);
buf.reset();
}
/// mark half of the entries used
ThreadSafeBitSet bitSet = new ThreadSafeBitSet(10);
for(int i=0;i<1000;i+=2) {
bitSet.set(i);
}
/// compact away the unused entries
map.compact(bitSet);
/// ensure that the used entries are still available
for(int i=0;i<1000;i+=2) {
VarInt.writeVInt(buf, 10000 + i);
Assert.assertEquals(i, map.getOrAssignOrdinal(buf));
buf.reset();
}
/// track the ordinals which are assigned to new values
Set<Integer> newlyAssignedOrdinals = new HashSet<Integer>();
for(int i=1;i<1000;i+=2) {
VarInt.writeVInt(buf, 50230532 + i);
int newOrdinal = map.getOrAssignOrdinal(buf);
newlyAssignedOrdinals.add(newOrdinal);
buf.reset();
}
/// those ordinals should be the recycled ones after the compact.
for(int i=1;i<1000;i+=2) {
Assert.assertTrue(newlyAssignedOrdinals.contains(i));