Skip to content

Commit

Permalink
Correctly resize integer map
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas Alt committed Jun 8, 2023
1 parent c1ceb47 commit fa54f90
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ protected void ensureCapacity(int key) {

// Fast calculation of the new size.
// See IntMath#ceilingPowerOfTwo in newer guava versions.
int newLength = IntegerMath.nextPowerOfTwo(key);

int newLength = IntegerMath.nextPowerOfTwo(key + 1);
this.array = Arrays.copyOf(array, newLength);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.comphenix.protocol.collections;

import com.comphenix.protocol.utility.IntegerMath;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

class IntegerMapTest {
@Test
public void testNextPower() {
assertEquals(128, IntegerMath.nextPowerOfTwo(127));
assertEquals(128, IntegerMath.nextPowerOfTwo(128));
assertEquals(256, IntegerMath.nextPowerOfTwo(129));
}
@Test
public void testCapacityIncrement() {
IntegerMap<Boolean> map = new IntegerMap<>();
for (int i = 0; i < 512; i++) {
map.put(i, false);
}
assertEquals(map.size(), 512);
}
}

0 comments on commit fa54f90

Please sign in to comment.