Files
kotlin-fork/compiler/testData/codegen/box/toArray/toArrayShouldBePublic.kt
T
Ivan Kylchik c7435ba760 Replace all occurrences of WITH_RUNTIME with WITH_STDLIB
We are going to deprecate `WITH_RUNTIME` directive. The main reason
behind this change is that `WITH_STDLIB` directive better describes
its meaning, specifically it will add kotlin stdlib to test's classpath.
2021-11-17 15:26:38 +03:00

50 lines
1.2 KiB
Kotlin
Vendored

// TARGET_BACKEND: JVM
// WITH_STDLIB
// FILE: SingletonCollection.kt
package test
open class SingletonCollection<T>(val value: T) : AbstractCollection<T>() {
override val size = 1
override fun iterator(): Iterator<T> = listOf(value).iterator()
protected override final fun toArray(): Array<Any?> =
arrayOf<Any?>(value)
protected override final fun <E> toArray(a: Array<E>): Array<E> {
a[0] = value as E
return a
}
}
// FILE: DerivedSingletonCollection.kt
package test2
import test.*
class DerivedSingletonCollection<T>(value: T) : SingletonCollection<T>(value)
// FILE: box.kt
import test.*
import test2.*
fun box(): String {
val sc = SingletonCollection(42)
val test1 = (sc as java.util.Collection<Int>).toArray()
if (test1[0] != 42) return "Failed #1"
val test2 = arrayOf<Any?>(0)
(sc as java.util.Collection<Int>).toArray(test2)
if (test2[0] != 42) return "Failed #2"
val dsc = DerivedSingletonCollection(42)
val test3 = (dsc as java.util.Collection<Int>).toArray()
if (test3[0] != 42) return "Failed #3"
val test4 = arrayOf<Any?>(0)
(dsc as java.util.Collection<Int>).toArray(test4)
if (test4[0] != 42) return "Failed #4"
return "OK"
}