Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/main/java/org/glavo/nbt/tag/ParentTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ final void ensureTagsCapacityForAdd() {

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

/// Adds all `tags` to this tag.
///
Expand Down
46 changes: 46 additions & 0 deletions src/test/java/org/glavo/nbt/tag/ListTagTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.glavo.nbt.tag;

import org.glavo.nbt.internal.ArrayAccessor;
import org.junit.jupiter.api.Test;

import java.util.function.Supplier;
Expand Down Expand Up @@ -238,4 +239,49 @@ void testCloneEqualsAndIndependentChildren() {
assertEquals("changed", cloneFirst.get());
assertContentNotEquals(tag, clone);
}

@Test
void testRemoveAt() {
ListTag<IntTag> list = new ListTag<>();
for (int i = 0; i < 9; i++) {
list.addTag(new IntTag(i));
}
assertEquals(new IntTag(6), list.removeTagAt(6));
assertEquals(8, list.size());
assertArrayEquals(new IntTag[]{
new IntTag(0), new IntTag(1), new IntTag(2),
new IntTag(3), new IntTag(4), new IntTag(5),
new IntTag(7), new IntTag(8), null,
null, null, null}, list.tags);
}

@Test
void testRemoveAtCapacity() {
ListTag<IntTag> list = new ListTag<>();
for (int i = 0; i < ArrayAccessor.DEFAULT_CAPACITY; i++) {
list.addTag(new IntTag(i));
}
assertEquals(new IntTag(6), list.removeTagAt(6));
assertEquals(ArrayAccessor.DEFAULT_CAPACITY - 1, list.size());
assertArrayEquals(new IntTag[]{
new IntTag(0), new IntTag(1), new IntTag(2),
new IntTag(3), new IntTag(4), new IntTag(5),
new IntTag(7), new IntTag(8), new IntTag(9),
new IntTag(10), new IntTag(11), null}, list.tags);
}

@Test
void testRemoveAtEnd() {
ListTag<IntTag> list = new ListTag<>();
for (int i = 0; i < ArrayAccessor.DEFAULT_CAPACITY; i++) {
list.addTag(new IntTag(i));
}
assertEquals(new IntTag(11), list.removeTagAt(11));
assertEquals(ArrayAccessor.DEFAULT_CAPACITY - 1, list.size());
assertArrayEquals(new IntTag[]{
new IntTag(0), new IntTag(1), new IntTag(2),
new IntTag(3), new IntTag(4), new IntTag(5),
new IntTag(6), new IntTag(7), new IntTag(8),
new IntTag(9), new IntTag(10), null}, list.tags);
}
}
Loading