forked from JetBrains/kotlin-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Interop][Test] Add tests for unions
- Loading branch information
1 parent
9f9609a
commit 47e5058
Showing
3 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
--- | ||
typedef union { | ||
short s; | ||
long long ll; | ||
} BasicUnion; | ||
|
||
typedef struct { | ||
union { | ||
int i; | ||
float f; | ||
} as; | ||
} StructWithUnion; | ||
|
||
typedef union { | ||
unsigned int i : 31; | ||
unsigned char b : 1; | ||
} Packed; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import cunion.* | ||
import kotlinx.cinterop.* | ||
import kotlin.test.* | ||
|
||
fun main() { | ||
memScoped { | ||
val basicUnion = alloc<BasicUnion>() | ||
for (value in Short.MIN_VALUE..Short.MAX_VALUE) { | ||
basicUnion.ll = value.toLong() | ||
assertEquals(value.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 | ||
assertEquals(1u, union.i) | ||
union.i = 0u | ||
assertEquals(0u, union.b) | ||
} | ||
} |