Skip to content

Commit aaad73b

Browse files
authored
Fix #3: ParentTag#removeAt throws ArrayIndexOutOfBoundsException at Specific Sizes (#4)
1 parent d601b29 commit aaad73b

2 files changed

Lines changed: 49 additions & 2 deletions

File tree

‎src/main/java/org/glavo/nbt/tag/ParentTag.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ final void ensureTagsCapacityForAdd() {
9595

9696
int arrayEnd = Math.min(size, tags.length);
9797
if (index < arrayEnd - 1) {
98-
System.arraycopy(tags, index + 1, tags, index, size - index);
98+
System.arraycopy(tags, index + 1, tags, index, arrayEnd - index - 1);
99+
tags[arrayEnd - 1] = null;
99100
} else if (oldTag != null) {
100101
tags[index] = null;
101102
}
@@ -179,7 +180,7 @@ final void moveTagToLast(T tag) {
179180
/// If the `tag` is already a child of another tag, removes it from old parent and adds it to this tag.
180181
@Contract(value = "_ -> this", mutates = "this,param1")
181182
public abstract ParentTag<T> addTag(@Flow(targetIsContainer = true)
182-
T tag) throws IllegalArgumentException;
183+
T tag) throws IllegalArgumentException;
183184

184185
/// Adds all `tags` to this tag.
185186
///

‎src/test/java/org/glavo/nbt/tag/ListTagTest.java‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.glavo.nbt.tag;
1717

18+
import org.glavo.nbt.internal.ArrayAccessor;
1819
import org.junit.jupiter.api.Test;
1920

2021
import java.util.function.Supplier;
@@ -238,4 +239,49 @@ void testCloneEqualsAndIndependentChildren() {
238239
assertEquals("changed", cloneFirst.get());
239240
assertContentNotEquals(tag, clone);
240241
}
242+
243+
@Test
244+
void testRemoveAt() {
245+
ListTag<IntTag> list = new ListTag<>();
246+
for (int i = 0; i < 9; i++) {
247+
list.addTag(new IntTag(i));
248+
}
249+
assertEquals(new IntTag(6), list.removeTagAt(6));
250+
assertEquals(8, list.size());
251+
assertArrayEquals(new IntTag[]{
252+
new IntTag(0), new IntTag(1), new IntTag(2),
253+
new IntTag(3), new IntTag(4), new IntTag(5),
254+
new IntTag(7), new IntTag(8), null,
255+
null, null, null}, list.tags);
256+
}
257+
258+
@Test
259+
void testRemoveAtCapacity() {
260+
ListTag<IntTag> list = new ListTag<>();
261+
for (int i = 0; i < ArrayAccessor.DEFAULT_CAPACITY; i++) {
262+
list.addTag(new IntTag(i));
263+
}
264+
assertEquals(new IntTag(6), list.removeTagAt(6));
265+
assertEquals(ArrayAccessor.DEFAULT_CAPACITY - 1, list.size());
266+
assertArrayEquals(new IntTag[]{
267+
new IntTag(0), new IntTag(1), new IntTag(2),
268+
new IntTag(3), new IntTag(4), new IntTag(5),
269+
new IntTag(7), new IntTag(8), new IntTag(9),
270+
new IntTag(10), new IntTag(11), null}, list.tags);
271+
}
272+
273+
@Test
274+
void testRemoveAtEnd() {
275+
ListTag<IntTag> list = new ListTag<>();
276+
for (int i = 0; i < ArrayAccessor.DEFAULT_CAPACITY; i++) {
277+
list.addTag(new IntTag(i));
278+
}
279+
assertEquals(new IntTag(11), list.removeTagAt(11));
280+
assertEquals(ArrayAccessor.DEFAULT_CAPACITY - 1, list.size());
281+
assertArrayEquals(new IntTag[]{
282+
new IntTag(0), new IntTag(1), new IntTag(2),
283+
new IntTag(3), new IntTag(4), new IntTag(5),
284+
new IntTag(6), new IntTag(7), new IntTag(8),
285+
new IntTag(9), new IntTag(10), null}, list.tags);
286+
}
241287
}

0 commit comments

Comments
 (0)