Correctly resize integer map (#2422)

This commit is contained in:
Lukas Alt 2023-06-08 15:08:57 +02:00 committed by GitHub
parent 260cb22f53
commit 88d8c2eb1d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 2 deletions

View File

@ -76,8 +76,7 @@ public class IntegerMap<T> {
// 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);
}

View File

@ -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);
}
}