From 88d8c2eb1d40c663fb6f1dc149fe513bbdd80bd5 Mon Sep 17 00:00:00 2001 From: Lukas Alt Date: Thu, 8 Jun 2023 15:08:57 +0200 Subject: [PATCH] Correctly resize integer map (#2422) --- .../protocol/collections/IntegerMap.java | 3 +-- .../protocol/collections/IntegerMapTest.java | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 src/test/java/com/comphenix/protocol/collections/IntegerMapTest.java diff --git a/src/main/java/com/comphenix/protocol/collections/IntegerMap.java b/src/main/java/com/comphenix/protocol/collections/IntegerMap.java index bb2887c9..799f4747 100644 --- a/src/main/java/com/comphenix/protocol/collections/IntegerMap.java +++ b/src/main/java/com/comphenix/protocol/collections/IntegerMap.java @@ -76,8 +76,7 @@ public class IntegerMap { // 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); } diff --git a/src/test/java/com/comphenix/protocol/collections/IntegerMapTest.java b/src/test/java/com/comphenix/protocol/collections/IntegerMapTest.java new file mode 100644 index 00000000..6809e724 --- /dev/null +++ b/src/test/java/com/comphenix/protocol/collections/IntegerMapTest.java @@ -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 map = new IntegerMap<>(); + for (int i = 0; i < 512; i++) { + map.put(i, false); + } + assertEquals(map.size(), 512); + } +} \ No newline at end of file