Files
kotlin-fork/kotlin-native/backend.native/tests/interop/basics/union.kt
T
Vsevolod Tolstopyatov f4e8ae5191 Explicitly declare stability levels of declarations in kotlinx.cinterop package
* @ExperimentalForeignApi for all declarations that operate on
    unmanaged memory (i.e. pointers and references)
* @BetaInteropApi for the rest of the interoperability declarations,
    namely Swift/CInterop-specific interfaces and convenience-functions

### Implementation details

* Some typealiases are not marked explicitly because it crashes the compiler,
    yet their experimentality is properly propagated
* License header is adjusted where it previously had been existing
* Deprecated with ERROR interop declarations that are deprecated for more than
    two years are removed
* WASM target interop declarations are deprecated
* Deliberately make Boolean.toByte and Byte.toBoolean foreign-experimental to scare
    people away

^KT-57728 fixed

Merge-request: KT-MR-9788
Merged-by: Vsevolod Tolstopyatov <qwwdfsad@gmail.com>
2023-05-04 13:52:21 +00:00

39 lines
1.0 KiB
Kotlin

@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class)
import cunion.*
import kotlinx.cinterop.*
import kotlin.native.*
import kotlin.test.*
fun main() {
memScoped {
val basicUnion = alloc<BasicUnion>()
for (value in Short.MIN_VALUE..Short.MAX_VALUE) {
basicUnion.ll = value.toLong()
val expected = if (Platform.isLittleEndian) {
value
} else {
value.toLong() ushr (Long.SIZE_BITS - Short.SIZE_BITS)
}
assertEquals(expected.toShort(), basicUnion.s)
}
}
memScoped {
val struct = alloc<StructWithUnion>()
struct.`as`.i = Float.NaN.toRawBits()
assertEquals(Float.NaN, struct.`as`.f)
}
memScoped {
val union = alloc<Packed>()
union.b = 1u
var expected = if (Platform.isLittleEndian) {
1u
} else {
1u shl (Int.SIZE_BITS - Byte.SIZE_BITS)
}
assertEquals(expected, union.i)
union.i = 0u
assertEquals(0u, union.b)
}
}