From 2ed09eef52d4808210a8356c7a1e94ec06bb10b2 Mon Sep 17 00:00:00 2001 From: smarasca5 Date: Wed, 18 Feb 2026 19:57:57 -0500 Subject: [PATCH] IntList get lower bounds check now enforced, test added --- core/src/processing/data/IntList.java | 4 +++- core/test/processing/data/IntListTest.java | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) 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();