Introduce Common String.toCharArray(destination) #KT-60657
Merge-request: KT-MR-11340 Merged-by: Abduqodiri Qurbonzoda <abduqodiri.qurbonzoda@jetbrains.com>
This commit is contained in:
committed by
Space Team
parent
aa0eca6d6d
commit
84c6333b23
@@ -115,24 +115,25 @@ class StringTest {
|
||||
@Test
|
||||
fun toCharArray() {
|
||||
val s = "hello"
|
||||
val destination = CharArray(5) { '.' }
|
||||
assertArrayContentEquals(charArrayOf('h', 'e', 'l', 'l', 'o'), s.toCharArray())
|
||||
assertArrayContentEquals(charArrayOf('e', 'l'), s.toCharArray(1, 3))
|
||||
assertSame(destination, s.toCharArray(destination, 2, 1, 3))
|
||||
assertArrayContentEquals(charArrayOf('.', '.', 'e', 'l', '.'), destination)
|
||||
|
||||
assertFailsWith<IndexOutOfBoundsException> { s.toCharArray(-1) }
|
||||
assertFailsWith<IndexOutOfBoundsException> { s.toCharArray(0, 6) }
|
||||
assertFailsWith<IllegalArgumentException> { s.toCharArray(3, 1) }
|
||||
assertFailsWith<IndexOutOfBoundsException> { s.toCharArray(destination, -1, 1, 3) }
|
||||
assertFailsWith<IndexOutOfBoundsException> { s.toCharArray(destination, 4, 1, 3) }
|
||||
|
||||
// Array modifications must not affect original string
|
||||
val a = s.toCharArray()
|
||||
for (i in a.indices) {
|
||||
a[i] = ' '
|
||||
}
|
||||
assertContentEquals(charArrayOf(' ', ' ', ' ', ' ', ' '), a)
|
||||
a.fill(' ')
|
||||
val a13 = s.toCharArray(1, 3)
|
||||
for (i in a13.indices) {
|
||||
a13[i] = ' '
|
||||
}
|
||||
assertContentEquals(charArrayOf(' ', ' '), a13)
|
||||
a13.fill(' ')
|
||||
assertSame(destination, s.toCharArray(destination))
|
||||
destination.fill(' ')
|
||||
assertEquals("hello", s)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user