File tree Expand file tree Collapse file tree 2 files changed +13
-1
lines changed
Expand file tree Collapse file tree 2 files changed +13
-1
lines changed Original file line number Diff line number Diff line change 66import java .util .Iterator ;
77import java .util .Random ;
88
9+ import org .jetbrains .annotations .TestOnly ;
910import processing .core .PApplet ;
1011
1112
@@ -164,13 +165,14 @@ public void clear() {
164165 * @webBrief Get an entry at a particular index
165166 */
166167 public int get (int index ) {
167- if (index >= this .count ) {
168+ if (index >= this .count || index < 0 ) {
168169 throw new ArrayIndexOutOfBoundsException (index );
169170 }
170171 return data [index ];
171172 }
172173
173174
175+
174176 /**
175177 * Set the entry at a particular index.
176178 *
Original file line number Diff line number Diff line change @@ -151,6 +151,16 @@ public void testPopOnEmptyIntListThrowsException() {
151151 assertEquals ("Can't call pop() on an empty list" , exception .getMessage ());
152152 }
153153
154+ @ Test
155+ public void testGetOnNegativeIndexIntListThrowsException () {
156+ IntList testedList = new IntList ();
157+ ArrayIndexOutOfBoundsException exception = assertThrows (ArrayIndexOutOfBoundsException .class , () -> {
158+ testedList .get (-1 );
159+ });
160+
161+ assertEquals ("Array index out of range: -1" , exception .getMessage ());
162+ }
163+
154164 @ Test
155165 public void testRemoveWithIndexGreaterThanSize () {
156166 IntList testedList = new IntList ();
You can’t perform that action at this time.
0 commit comments