Skip to content

Commit 88d8c2e

Browse files
authored
Correctly resize integer map (#2422)
1 parent 260cb22 commit 88d8c2e

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/main/java/com/comphenix/protocol/collections/IntegerMap.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ protected void ensureCapacity(int key) {
7676

7777
// Fast calculation of the new size.
7878
// See IntMath#ceilingPowerOfTwo in newer guava versions.
79-
int newLength = IntegerMath.nextPowerOfTwo(key);
80-
79+
int newLength = IntegerMath.nextPowerOfTwo(key + 1);
8180
this.array = Arrays.copyOf(array, newLength);
8281
}
8382

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.comphenix.protocol.collections;
2+
3+
import com.comphenix.protocol.utility.IntegerMath;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.junit.jupiter.api.Assertions.assertEquals;
7+
8+
class IntegerMapTest {
9+
@Test
10+
public void testNextPower() {
11+
assertEquals(128, IntegerMath.nextPowerOfTwo(127));
12+
assertEquals(128, IntegerMath.nextPowerOfTwo(128));
13+
assertEquals(256, IntegerMath.nextPowerOfTwo(129));
14+
}
15+
@Test
16+
public void testCapacityIncrement() {
17+
IntegerMap<Boolean> map = new IntegerMap<>();
18+
for (int i = 0; i < 512; i++) {
19+
map.put(i, false);
20+
}
21+
assertEquals(map.size(), 512);
22+
}
23+
}

0 commit comments

Comments
 (0)