KT-22298 Improve docs for Array.copyOf(newSize: Int)

Signed-off-by: Valeriy Zhirnov <neonailol@gmail.com>
This commit is contained in:
Valeriy Zhirnov
2018-05-16 22:15:30 +03:00
committed by Ilya Gorbunov
parent df4dcc0f8e
commit 9239de9a02
5 changed files with 283 additions and 29 deletions
@@ -17,7 +17,6 @@
package samples.collections
import samples.*
import kotlin.test.*
@RunWith(Enclosed::class)
@@ -76,4 +75,32 @@ class Arrays {
}
}
class CopyOfOperations {
@Sample
fun copyOf() {
val array = arrayOf("apples", "oranges", "limes")
val arrayCopy = array.copyOf()
assertPrints(arrayCopy.contentToString(), "[apples, oranges, limes]")
}
@Sample
fun resizingCopyOf() {
val array = arrayOf("apples", "oranges", "limes")
val arrayCopyPadded = array.copyOf(5)
assertPrints(arrayCopyPadded.contentToString(), "[apples, oranges, limes, null, null]")
val arrayCopyTruncated = array.copyOf(2)
assertPrints(arrayCopyTruncated.contentToString(), "[apples, oranges]")
}
@Sample
fun resizedPrimitiveCopyOf() {
val array = intArrayOf(1, 2, 3)
val arrayCopyPadded = array.copyOf(5)
assertPrints(arrayCopyPadded.contentToString(), "[1, 2, 3, 0, 0]")
val arrayCopyTruncated = array.copyOf(2)
assertPrints(arrayCopyTruncated.contentToString(), "[1, 2]")
}
}
}