8000 Check index on bounds in ArrayList addAll and removeRange. · scala-wasm/scala-wasm@4913fc9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4913fc9

Browse files
tanishikingsjrd
authored andcommitted
Check index on bounds in ArrayList addAll and removeRange.
removeRange should throws an `IndexOutOfBoundsException` if fromIndex or toIndex is out of range. addAll should also throw when the index is not within bounds.
1 parent 619202c commit 4913fc9

File tree

3 files changed

+84
-2
lines changed

3 files changed

+84
-2
lines changed

javalib/src/main/scala/java/util/ArrayList.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,16 @@ class ArrayList[E] private (private[ArrayList] val inner: js.Array[E])
8181
override def addAll(index: Int, c: Collection[_ <: E]): Boolean = {
8282
c match {
8383
case other: ArrayList[_] =>
84+
checkIndexOnBounds(index)
8485
inner.splice(index, 0, other.inner.toSeq: _*)
8586
other.size() > 0
8687
case _ => super.addAll(index, c)
8788
}
8889
}
8990

90-
override protected def removeRange(fromIndex: Int, toIndex: Int): Unit =
91+
override protected def removeRange(fromIndex: Int, toIndex: Int): Unit = {
92+
if (fromIndex < 0 || toIndex > size() || toIndex < fromIndex)
93+
throw new IndexOutOfBoundsException()
9194
inner.splice(fromIndex, toIndex - fromIndex)
92-
95+
}
9396
}

test-suite/shared/src/test/scala/org/scalajs/testsuite/javalib/util/ArrayListTest.scala

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
package org.scalajs.testsuite.javalib.util
1414

1515
import org.junit.Test
16+
import org.junit.Assert._
17+
18+
import org.scalajs.testsuite.utils.AssertThrows.assertThrows
1619

1720
import java.{util => ju}
1821

@@ -29,6 +32,60 @@ class ArrayListTest extends AbstractListTest {
2932
al.ensureCapacity(34)
3033
al.trimToSize()
3134
}
35+
36+
@Test def removeRangeFromIdenticalIndices(): Unit = {
37+
val al = new ArrayListRangeRemovable[Int](
38+
TrivialImmutableCollection(-175, 24, 7, 44))
39+
val expected = Array[Int](-175, 24, 7, 44)
40+
al.removeRangeList(0, 0)
41+
assertTrue(al.toArray().sameElements(expected))
42+
al.removeRangeList(1, 1)
43+
assertTrue(al.toArray().sameElements(expected))
44+
al.removeRangeList(al.size, al.size) // no op
45+
assertTrue(al.toArray().sameElements(expected))
46+
}
47+
48+
@Test def removeRangeFromToInvalidIndices(): Unit = {
49+
val al = new ArrayListRangeRemovable[Int](
50+
TrivialImmutableCollection(175, -24, -7, -44))
51+
52+
assertThrows(
53+
classOf[java.lang.IndexOutOfBoundsException],
54+
al.removeRangeList(-1, 2)
55+
) // fromIndex < 0
56+
assertThrows(
57+
classOf[java.lang.IndexOutOfBoundsException],
58+
al.removeRangeList( 10000 0, al.size + 1)
59+
) // toIndex > size
60+
assertThrows(
61+
classOf[java.lang.IndexOutOfBoundsException],
62+
al.removeRangeList(2, -1)
63+
) // toIndex < fromIndex
64+
}
65+
66+
@Test def removeRangeFromToFirstTwoElements(): Unit = {
67+
val al = new ArrayListRangeRemovable[Int](
68+
TrivialImmutableCollection(284, -27, 995, 500, 267, 904))
69+
val expected = Array[Int](995, 500, 267, 904)
70+
al.removeRangeList(0, 2)
71+
assertTrue(al.toArray().sameElements(expected))
72+
}
73+
74+
@Test def removeRangeFromToTwoElementsFromMiddle(): Unit = {
75+
val al = new ArrayListRangeRemovable[Int](
76+
TrivialImmutableCollection(7, 9, -1, 20))
77+
val expected = Array[Int](7, 20)
78+
al.removeRangeList(1, 3)
79+
assertTrue(al.toArray().sameElements(expected))
80+
}
81+
82+
@Test def removeRangeFromToLastTwoElementsAtTail(): Unit = {
83+
val al = new ArrayListRangeRemovable[Int](
84+
TrivialImmutableCollection(50, 72, 650, 12, 7, 28, 3))
85+
val expected = Array[Int](50, 72, 650, 12, 7)
86+
al.removeRangeList(al.size - 2, al.size)
87+
assertTrue(al.toArray().sameElements(expected))
88+
}
3289
}
3390

3491
class ArrayListFactory extends AbstractListFactory {
@@ -37,4 +94,13 @@ class ArrayListFactory extends AbstractListFactory {
3794

3895
override def empty[E: ClassTag]: ju.ArrayList[E] =
3996
new ju.ArrayList[E]
97+
98+
override def fromElements[E: ClassTag](coll: E*): ju.ArrayList[E] =
99+
new ju.ArrayList[E](TrivialImmutableCollection(coll: _*))
100+
}
101+
102+
class ArrayListRangeRemovable[E](c: ju.Collection[_ <: E]) extends ju.ArrayList[E](c) {
103+
def removeRangeList(fromIndex: Int, toIndex: Int): Unit = {
104+
removeRange(fromIndex, toIndex)
105+
}
40106
}

test-suite/shared/src/test/scala/org/scalajs/testsuite/javalib/util/ListTest.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,19 @@ trait ListTest extends CollectionTest with CollectionsTestBase {
9696
assertThrows(classOf[IndexOutOfBoundsException], lst.get(lst.size))
9797
}
9898

99+
@Test def addAllIndexBounds(): Unit = {
100+
val al = factory.fromElements[String]("one", "two", "three")
101+
102+
val coll = factory.fromElements[String]("foo")
103+
assertThrows(classOf[IndexOutOfBoundsException], al.addAll(-1, coll))
104+
assertThrows(classOf[IndexOutOfBoundsException], al.addAll(al.size + 1, coll))
105+
106+
assertThrows(classOf[IndexOutOfBoundsException],
107+
al.addAll(-1, TrivialImmutableCollection("foo")))
108+
assertThrows(classOf[IndexOutOfBoundsException],
109+
al.addAll(al.size + 1, TrivialImmutableCollection("foo")))
110+
}
111+
99112
@Test def removeStringRemoveIndex(): Unit = {
100113
val lst = factory.empty[String]
101114

0 commit comments

Comments
 (0)
0