diff --git a/core/src/processing/data/IntList.java b/core/src/processing/data/IntList.java index afb4c6cd61..83cac6b3f7 100644 --- a/core/src/processing/data/IntList.java +++ b/core/src/processing/data/IntList.java @@ -6,6 +6,7 @@ import java.util.Iterator; import java.util.Random; +import org.jetbrains.annotations.TestOnly; import processing.core.PApplet; @@ -164,13 +165,14 @@ public void clear() { * @webBrief Get an entry at a particular index */ public int get(int index) { - if (index >= this.count) { + if (index >= this.count || index < 0) { throw new ArrayIndexOutOfBoundsException(index); } return data[index]; } + /** * Set the entry at a particular index. * diff --git a/core/test/processing/data/IntListTest.java b/core/test/processing/data/IntListTest.java index 3c422d84e4..c98f7a675f 100644 --- a/core/test/processing/data/IntListTest.java +++ b/core/test/processing/data/IntListTest.java @@ -151,6 +151,16 @@ public void testPopOnEmptyIntListThrowsException() { assertEquals("Can't call pop() on an empty list", exception.getMessage()); } + @Test + public void testGetOnNegativeIndexIntListThrowsException() { + IntList testedList = new IntList(); + ArrayIndexOutOfBoundsException exception = assertThrows(ArrayIndexOutOfBoundsException.class, () -> { + testedList.get(-1); + }); + + assertEquals("Array index out of range: -1", exception.getMessage()); + } + @Test public void testRemoveWithIndexGreaterThanSize() { IntList testedList = new IntList();