[stdlib] Test that modification of the array returned by toCharArray() doesn't affect the original string
This commit is contained in:
committed by
Space
parent
f7d3cb269f
commit
5b10396f48
@@ -123,6 +123,19 @@ class StringTest {
|
|||||||
assertFailsWith<IndexOutOfBoundsException> { s.toCharArray(-1) }
|
assertFailsWith<IndexOutOfBoundsException> { s.toCharArray(-1) }
|
||||||
assertFailsWith<IndexOutOfBoundsException> { s.toCharArray(0, 6) }
|
assertFailsWith<IndexOutOfBoundsException> { s.toCharArray(0, 6) }
|
||||||
assertFailsWith<IllegalArgumentException> { s.toCharArray(3, 1) }
|
assertFailsWith<IllegalArgumentException> { s.toCharArray(3, 1) }
|
||||||
|
|
||||||
|
// Array modifications must not affect original string
|
||||||
|
val a = s.toCharArray()
|
||||||
|
for (i in a.indices) {
|
||||||
|
a[i] = ' '
|
||||||
|
}
|
||||||
|
assertContentEquals(charArrayOf(' ', ' ', ' ', ' ', ' '), a)
|
||||||
|
val a13 = s.toCharArray(1, 3)
|
||||||
|
for (i in a13.indices) {
|
||||||
|
a13[i] = ' '
|
||||||
|
}
|
||||||
|
assertContentEquals(charArrayOf(' ', ' '), a13)
|
||||||
|
assertEquals("hello", s)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test fun isEmptyAndBlank() = withOneCharSequenceArg { arg1 ->
|
@Test fun isEmptyAndBlank() = withOneCharSequenceArg { arg1 ->
|
||||||
|
|||||||
Reference in New Issue
Block a user