13
13
package org .scalajs .testsuite .javalib .util
14
14
15
15
import org .junit .Test
16
+ import org .junit .Assert ._
17
+
18
+ import org .scalajs .testsuite .utils .AssertThrows .assertThrows
16
19
17
20
import java .{util => ju }
18
21
@@ -29,6 +32,60 @@ class ArrayListTest extends AbstractListTest {
29
32
al.ensureCapacity(34 )
30
33
al.trimToSize()
31
34
}
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
+ }
32
89
}
33
90
34
91
class ArrayListFactory extends AbstractListFactory {
@@ -37,4 +94,13 @@ class ArrayListFactory extends AbstractListFactory {
37
94
38
95
override def empty [E : ClassTag ]: ju.ArrayList [E ] =
39
96
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
+ }
40
106
}
0 commit comments