Check for index-out-of-bound in elementAt function (KT-30051)
This commit is contained in:
@@ -1490,6 +1490,16 @@ class ArraysTest {
|
||||
array.sortWith(comparator)
|
||||
array.iterator().assertSorted { a, b -> comparator.compare(a, b) <= 0 }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun elementAt() {
|
||||
expect(0) { byteArrayOf(0, 1, 2).elementAt(0) }
|
||||
expect(1) { shortArrayOf(0, 1, 2).elementAt(1) }
|
||||
expect(2) { intArrayOf(0, 1, 2).elementAt(2) }
|
||||
|
||||
assertFailsWith<IndexOutOfBoundsException> { arrayOf<String>().elementAt(0) }
|
||||
assertFailsWith<IndexOutOfBoundsException> { longArrayOf(0, 1, 2).elementAt(-1) }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -625,10 +625,8 @@ class UnsignedArraysTest {
|
||||
expect(1u) { ushortArrayOf(0, 1, 2).elementAt(1) }
|
||||
expect(2u) { uintArrayOf(0, 1, 2).elementAt(2) }
|
||||
|
||||
// TODO: Uncomment these tests after KT-30051 gets fixed.
|
||||
// Currently JS does not throw exception on incorrect index.
|
||||
// assertFailsWith<IndexOutOfBoundsException> { uintArrayOf().elementAt(0) }
|
||||
// assertFailsWith<IndexOutOfBoundsException> { ulongArrayOf(0, 1, 2).elementAt(-1) }
|
||||
assertFailsWith<IndexOutOfBoundsException> { uintArrayOf().elementAt(0) }
|
||||
assertFailsWith<IndexOutOfBoundsException> { ulongArrayOf(0, 1, 2).elementAt(-1) }
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -1460,4 +1460,14 @@ ${" "}
|
||||
assertEquals(" ABC\n \n 123", "ABC\n \n123".prependIndent(" "))
|
||||
assertEquals(" ", "".prependIndent(" "))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun elementAt() {
|
||||
expect('a') { "a c".elementAt(0) }
|
||||
expect(' ') { "a c".elementAt(1) }
|
||||
expect('c') { "a c".elementAt(2) }
|
||||
|
||||
assertFailsWith<IndexOutOfBoundsException> { "".elementAt(0) }
|
||||
assertFailsWith<IndexOutOfBoundsException> { "a c".elementAt(-1) }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user