Fix bug in ArrayDeque implementation

This commit is contained in:
Alexey Andreev 2020-02-04 17:36:46 +03:00
parent f4ec4872c5
commit fa198244dd
2 changed files with 22 additions and 4 deletions

View File

@ -269,10 +269,10 @@ public class TArrayDeque<E> extends TAbstractCollection<E> implements TDeque<E>
for (int i = index + 1; i < tail; ++i) {
array[i - 1] = array[i];
}
array[--tail] = null;
if (tail == 0) {
if (--tail < 0) {
tail += array.length;
}
array[tail] = null;
}
}
}

View File

@ -25,7 +25,6 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.teavm.junit.TeaVMTestRunner;
@RunWith(TeaVMTestRunner.class)
public class ArrayDequeTest {
@Test
public void addsToFront() {
@ -132,7 +131,7 @@ public class ArrayDequeTest {
@Test
public void eachRemovedObjectShouldReduceTheSizeByOne() {
TArrayDeque<Object> arrayDeque = new TArrayDeque<>();
ArrayDeque<Object> arrayDeque = new ArrayDeque<>();
Object object1 = new Object();
Object object2 = new Object();
Object object3 = new Object();
@ -235,4 +234,23 @@ public class ArrayDequeTest {
Assert.assertTrue(arrayDeque4.size() == 1);
Assert.assertTrue(arrayDeque4.contains(object1));
}
@Test
public void removeElementInWrappedArray() {
TArrayDeque<Object> arrayDeque = new TArrayDeque<>(8);
for (int i = 0; i < 4; ++i) {
arrayDeque.addLast(0);
}
for (int i = 0; i < 4; ++i) {
arrayDeque.removeFirst();
}
for (int i = 0; i < 6; ++i) {
arrayDeque.addLast(i);
}
for (int i = 5; i >= 0; --i) {
arrayDeque.remove(i);
arrayDeque.addLast(23);
arrayDeque.removeLast();
}
}
}