diff --git a/backend.native/tests/build.gradle b/backend.native/tests/build.gradle index 8027c3d41f1..a0629b2486f 100644 --- a/backend.native/tests/build.gradle +++ b/backend.native/tests/build.gradle @@ -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" diff --git a/backend.native/tests/runtime/collections/array_list2.kt b/backend.native/tests/runtime/collections/array_list2.kt new file mode 100644 index 00000000000..dd8f6e6719f --- /dev/null +++ b/backend.native/tests/runtime/collections/array_list2.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 { + while (true) { + it.next() + } + } +} + +fun testIteratorPrevious() { + val a = arrayListOf("1", "2", "3", "4", "5") + val it = a.listIterator() + it.next() + assertFailsWith { + while (true) { + it.previous() + } + } +} + +@Test fun runTest() { + testIteratorNext() + testIteratorPrevious() + + println("OK") +} \ No newline at end of file diff --git a/runtime/src/main/kotlin/kotlin/collections/ArrayList.kt b/runtime/src/main/kotlin/kotlin/collections/ArrayList.kt index 9838b3737dc..ecaacab3190 100644 --- a/runtime/src/main/kotlin/kotlin/collections/ArrayList.kt +++ b/runtime/src/main/kotlin/kotlin/collections/ArrayList.kt @@ -308,13 +308,13 @@ actual class ArrayList 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] }