Throw NoSuchElementException instead of IOOBE in ArrayList's iterator (#1694)

This commit is contained in:
Pavel Punegov
2018-06-18 10:37:11 +03:00
committed by Nikolay Igotti
parent 36fe143ac3
commit 5e6e23dd53
3 changed files with 39 additions and 2 deletions
+6
View File
@@ -1654,6 +1654,12 @@ task array_list1(type: RunKonanTest) {
source = "runtime/collections/array_list1.kt"
}
task array_list2(type: RunKonanTest) {
disabled = (project.testTarget == 'wasm32') // uses exceptions
goldValue = "OK\n"
source = "runtime/collections/array_list2.kt"
}
task hash_map0(type: RunKonanTest) {
goldValue = "OK\n"
source = "runtime/collections/hash_map0.kt"
@@ -0,0 +1,31 @@
package runtime.collections.array_list2
import kotlin.test.*
fun testIteratorNext() {
val a = arrayListOf("1", "2", "3", "4", "5")
val it = a.listIterator()
assertFailsWith<NoSuchElementException> {
while (true) {
it.next()
}
}
}
fun testIteratorPrevious() {
val a = arrayListOf("1", "2", "3", "4", "5")
val it = a.listIterator()
it.next()
assertFailsWith<NoSuchElementException> {
while (true) {
it.previous()
}
}
}
@Test fun runTest() {
testIteratorNext()
testIteratorPrevious()
println("OK")
}
@@ -308,13 +308,13 @@ actual class ArrayList<E> private constructor(
override fun nextIndex(): Int = index
override fun previous(): E {
if (index <= 0) throw IndexOutOfBoundsException()
if (index <= 0) throw NoSuchElementException()
lastIndex = --index
return list.array[list.offset + lastIndex]
}
override fun next(): E {
if (index >= list.length) throw IndexOutOfBoundsException()
if (index >= list.length) throw NoSuchElementException()
lastIndex = index++
return list.array[list.offset + lastIndex]
}