Files
kotlin-fork/plugins/atomicfu/atomicfu-compiler/testData/box/atomics_basic/UncheckedCastTest.kt
T
mvicsokolova 5c5367d377 [atomicfu-JVM] Preparation for commonization of JVM and K/N transformers
The following updates in the JVM/IR plugin were made:
* Lots of refactoring with preparation for K/N support: commonization of transformations.
* Improved error handling (checks for visibility constraints, appending message about usage constraints in case of an error).
* Explicit requirements for the visibility of atomic properties: to prevent leaking they should be private/internal or be members of private/internal classes.
* Fixed visibility of generated properties: volatile properties are always private and atomic updaters have the same visibility as the original atomic property.
* Volatile fields are generated from scratch and original atomic properties are removed.
* Delegated properties support is fixed (only declaration in the same scope is allowed).
* Non-inline atomic extensions are forbidden.
* For top-level atomics: only one wrapper class per file (with corresponding visibility) is generated.
* Bug fixes.

The corresponding tickets: 
https://github.com/Kotlin/kotlinx-atomicfu/issues/322
KT-60528



Merge-request: KT-MR-10579
Merged-by: Maria Sokolova <maria.sokolova@jetbrains.com>
2023-07-20 13:59:23 +00:00

54 lines
1.5 KiB
Kotlin
Vendored

import kotlinx.atomicfu.*
import kotlin.test.*
private val topLevelS = atomic<Any>(arrayOf("A", "B"))
class UncheckedCastTest {
private val s = atomic<Any>("AAA")
private val bs = atomic<Any?>(null)
@Suppress("UNCHECKED_CAST")
fun testAtomicValUncheckedCast() {
assertEquals((s as AtomicRef<String>).value, "AAA")
bs.lazySet(arrayOf(arrayOf(Box(1), Box(2))))
assertEquals((bs as AtomicRef<Array<Array<Box>>>).value[0]!![0].b * 10, 10)
}
@Suppress("UNCHECKED_CAST")
fun testTopLevelValUnchekedCast() {
assertEquals((topLevelS as AtomicRef<Array<String>>).value[1], "B")
}
private data class Box(val b: Int)
@Suppress("NOTHING_TO_INLINE", "UNCHECKED_CAST")
private inline fun <T> AtomicRef<T>.getString(): String =
(this as AtomicRef<String>).value
fun testInlineFunc() {
assertEquals("AAA", s.getString())
}
private val a = atomicArrayOfNulls<Any?>(10)
fun testArrayValueUncheckedCast() {
a[0].value = "OK"
@Suppress("UNCHECKED_CAST")
assertEquals("OK", (a[0] as AtomicRef<String>).value)
}
fun testArrayValueUncheckedCastInlineFunc() {
a[0].value = "OK"
assertEquals("OK", a[0].getString())
}
}
fun box(): String {
val testClass = UncheckedCastTest()
testClass.testAtomicValUncheckedCast()
testClass.testTopLevelValUnchekedCast()
testClass.testArrayValueUncheckedCast()
testClass.testArrayValueUncheckedCastInlineFunc()
testClass.testInlineFunc()
return "OK"
}