[K/N][Tests] Migrate some cinterop tests to new infra

^KT-61259
This commit is contained in:
Vladimir Sukharev
2024-01-04 19:01:46 +01:00
committed by Space Team
parent 422128f3dc
commit bf01cb16b4
63 changed files with 1776 additions and 1025 deletions
@@ -6272,6 +6272,26 @@ public class FirLightTreeBlackBoxCodegenTestGenerated extends AbstractFirLightTr
public void testAllFilesPresentInCinterop() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
@Nested
@TestMetadata("compiler/testData/codegen/box/cinterop/basics")
@TestDataPath("$PROJECT_ROOT")
public class Basics {
@Test
public void testAllFilesPresentInBasics() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop/basics"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
}
@Nested
@TestMetadata("compiler/testData/codegen/box/cinterop/exceptions")
@TestDataPath("$PROJECT_ROOT")
public class Exceptions {
@Test
public void testAllFilesPresentInExceptions() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop/exceptions"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
}
}
@Nested
@@ -6272,6 +6272,26 @@ public class FirLightTreeBlackBoxCodegenWithIrFakeOverrideGeneratorTestGenerated
public void testAllFilesPresentInCinterop() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
@Nested
@TestMetadata("compiler/testData/codegen/box/cinterop/basics")
@TestDataPath("$PROJECT_ROOT")
public class Basics {
@Test
public void testAllFilesPresentInBasics() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop/basics"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
}
@Nested
@TestMetadata("compiler/testData/codegen/box/cinterop/exceptions")
@TestDataPath("$PROJECT_ROOT")
public class Exceptions {
@Test
public void testAllFilesPresentInExceptions() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop/exceptions"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
}
}
@Nested
@@ -6272,6 +6272,26 @@ public class FirPsiBlackBoxCodegenTestGenerated extends AbstractFirPsiBlackBoxCo
public void testAllFilesPresentInCinterop() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
@Nested
@TestMetadata("compiler/testData/codegen/box/cinterop/basics")
@TestDataPath("$PROJECT_ROOT")
public class Basics {
@Test
public void testAllFilesPresentInBasics() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop/basics"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
}
@Nested
@TestMetadata("compiler/testData/codegen/box/cinterop/exceptions")
@TestDataPath("$PROJECT_ROOT")
public class Exceptions {
@Test
public void testAllFilesPresentInExceptions() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop/exceptions"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
}
}
@Nested
@@ -2,17 +2,26 @@
* Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
// TARGET_BACKEND: NATIVE
// MODULE: cinterop
// FILE: cstdlib.def
headers = stdlib.h
// MODULE: main(cinterop)
// FILE: main.kt
@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class)
import cstdlib.*
import kotlinx.cinterop.*
import kotlin.test.*
fun main(args: Array<String>) {
println(atoi("257"))
fun box(): String {
assertEquals(257, atoi("257"))
val divResult = div(-5, 3)
val (quot, rem) = divResult.useContents { Pair(quot, rem) }
println(quot)
println(rem)
assertEquals(-1, quot)
assertEquals(-2, rem)
return "OK"
}
+36
View File
@@ -0,0 +1,36 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
// TARGET_BACKEND: NATIVE
// MODULE: cinterop
// FILE: cstdlib_3.def
headers = stdlib.h
// MODULE: main(cinterop)
// FILE: main.kt
@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class)
import cstdlib_3.*
import kotlinx.cinterop.*
import kotlin.test.*
fun box(): String {
val values = intArrayOf(14, 12, 9, 13, 8)
val count = values.size
qsort(values.refTo(0), count.convert(), IntVar.size.convert(), staticCFunction { a, b ->
val aValue = a!!.reinterpret<IntVar>()[0]
val bValue = b!!.reinterpret<IntVar>()[0]
(aValue - bValue)
})
assertEquals(8, values[0])
assertEquals(9, values[1])
assertEquals(12, values[2])
assertEquals(13, values[3])
assertEquals(14, values[4])
return "OK"
}
@@ -2,26 +2,49 @@
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
// TARGET_BACKEND: NATIVE
// MODULE: cinterop
// FILE: cstdlib_setenv.def
headers = stdlib.h
compilerOpts.mingw = -D ADD_WINDOWS_ENV_FUNCTIONS
---
#ifdef ADD_WINDOWS_ENV_FUNCTIONS
static inline int setenv(const char *name, const char *value, int overwrite)
{
int errcode = 0;
if (!overwrite) {
size_t envsize = 0;
errcode = getenv_s(&envsize, NULL, 0, name);
if(errcode || envsize) return errcode;
}
return _putenv_s(name, value);
}
static inline int unsetenv(const char *name)
{
return _putenv_s(name, "");
}
#endif
// MODULE: main(cinterop)
// FILE: main.kt
@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class, kotlin.experimental.ExperimentalNativeApi::class)
import kotlin.native.*
import kotlin.test.*
import cstdlib.*
import cstdlib_setenv.*
import kotlinx.cinterop.*
@OptIn(kotlin.ExperimentalStdlibApi::class)
fun main(args: Array<String>) {
require(args.size == 1) {
"An expected anount of processors should be specified as program argument"
}
val x: Int = Platform.getAvailableProcessors()
println("Got available processors: $x")
assertTrue(x > 0)
if (!(Platform.osFamily == OsFamily.LINUX && (Platform.cpuArchitecture == CpuArchitecture.ARM32 ||
Platform.cpuArchitecture == CpuArchitecture.ARM64))) {
assertEquals(args[0].trim().toInt(), x)
}
fun box(): String {
val platformProcessors: Int = Platform.getAvailableProcessors()
val jvmProcessors: Int = platformProcessors // Ideally, must be value of Runtime.getRuntime().availableProcessors() from testsystem
assertTrue(jvmProcessors > 0)
assertTrue(platformProcessors > 0)
assertEquals(jvmProcessors, platformProcessors)
setenv("KOTLIN_NATIVE_AVAILABLE_PROCESSORS", "12345", 1)
assertEquals(Platform.getAvailableProcessors(), 12345)
@@ -39,5 +62,7 @@ fun main(args: Array<String>) {
setenv("KOTLIN_NATIVE_AVAILABLE_PROCESSORS", "123aaaa", 1)
assertFailsWith<IllegalStateException> { Platform.getAvailableProcessors() }
unsetenv("KOTLIN_NATIVE_AVAILABLE_PROCESSORS")
assertEquals(Platform.getAvailableProcessors(), x)
assertEquals(Platform.getAvailableProcessors(), platformProcessors)
return "OK"
}
+151
View File
@@ -0,0 +1,151 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
// TARGET_BACKEND: NATIVE
// MODULE: cinterop
// FILE: cmacros.def
excludedMacros = EXCLUDED
---
int* ptr_call() {
return (int*) 1;
}
int int_call() {
return 42;
}
void void_call() {}
int arg_call(int x) {
return x;
}
typedef struct {
int value
} struct_t;
struct_t getStruct() {
return (struct_t){ 1 };
}
int global_var = 5;
#define TOO_MANY_ERRORS x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x
#define LEFT_BRACE {
#define ZERO 0
#define ONE 1
#define RIGHT_BRACE }
#define MAX_LONG 9223372036854775807
#define FOO_STRING "foo"
// This one should be ignored:
#define WIDE_FOO_STRING L"foo"
#define FOURTY_TWO 42
#define SEVENTEEN ((long long) 17)
#define ONE_POINT_ZERO 1.0
#define ONE_POINT_FIVE 1.5f
#define LEFT_PAREN (
#define NULL_PTR ((void*)0)
#define VOID_PTR ((void*)1)
#define INT_PTR ((int*)1)
#define PTR_SUM (INT_PTR + 1)
#define PTR_SUM_EXPECTED (sizeof(int) + 1)
#define RIGHT_PAREN )
enum {
INT_CALL = 1 // Should be replaced by macro below.
};
#define PTR_CALL ptr_call()
#define INT_CALL int_call()
#define CALL_SUM (int_call() + int_call())
#define GLOBAL_VAR (global_var)
// This one should be excluded:
#define EXCLUDED 42
// These ones should be ignored:
#define VOID_CALL (void_call())
#define ARG_CALL(x) (arg_call(x))
#define GET_STRUCT (getStruct())
#define UNDECLARED (undeclared())
#define BAD1 bar
#define BAD2 5;
#define BAD3 { foo(); }
void increment(int* counter);
#define increment(counter) { (*(counter))++; }
#define DEFAULT_DOUBLE_NAN __builtin_nan("")
#define DEFAULT_FLOAT_NAN __builtin_nanf("")
#define OTHER_DOUBLE_NAN __builtin_nan("0x123456789ab")
#define OTHER_FLOAT_NAN __builtin_nanf("0x12345")
// MODULE: main(cinterop)
// FILE: main.kt
@file:OptIn(kotlin.experimental.ExperimentalNativeApi::class, kotlinx.cinterop.ExperimentalForeignApi::class)
import kotlin.test.*
import cmacros.*
import kotlinx.cinterop.*
fun box(): String {
assertEquals("foo", FOO_STRING)
assertEquals(0, ZERO)
assertEquals(1, ONE)
assertEquals(Long.MAX_VALUE, MAX_LONG)
assertEquals(42, FOURTY_TWO)
val seventeen: Long = SEVENTEEN
assertEquals(17L, seventeen)
val onePointFive: Float = ONE_POINT_FIVE
val onePointZero: Double = ONE_POINT_ZERO
assertEquals(1.5f, onePointFive)
assertEquals(1.0, onePointZero)
val nullPtr: COpaquePointer? = NULL_PTR
val voidPtr: COpaquePointer? = VOID_PTR
val intPtr: CPointer<IntVar>? = INT_PTR
val ptrSum: CPointer<IntVar>? = PTR_SUM
val ptrCall: CPointer<IntVar>? = PTR_CALL
assertEquals(null, nullPtr)
assertEquals(1L, voidPtr.rawValue.toLong())
assertEquals(1L, intPtr.rawValue.toLong())
assertEquals(PTR_SUM_EXPECTED.toLong(), ptrSum.rawValue.toLong())
assertEquals(1L, ptrCall.rawValue.toLong())
assertEquals(42, INT_CALL)
assertEquals(84, CALL_SUM)
assertEquals(5, GLOBAL_VAR)
memScoped {
val counter = alloc<IntVar>()
counter.value = 42
increment(counter.ptr)
assertEquals(43, counter.value)
}
val floatNanBase = Float.NaN.toRawBits()
assertEquals(floatNanBase, 0x7fc00000)
val doubleNanBase = Double.NaN.toRawBits()
assertEquals(doubleNanBase, 0x7ff8000000000000L)
assertEquals(floatNanBase, DEFAULT_FLOAT_NAN.toRawBits())
assertEquals(doubleNanBase, DEFAULT_DOUBLE_NAN.toRawBits())
assertEquals(floatNanBase, OTHER_FLOAT_NAN.toRawBits())
assertEquals(doubleNanBase, OTHER_DOUBLE_NAN.toRawBits())
return "OK"
}
@@ -0,0 +1,24 @@
// TARGET_BACKEND: NATIVE
// MODULE: cinterop
// FILE: mangling.def
---
// test mangling of special names
enum _Companion {Companion, Any};
enum _Companion companion = Companion;
// MODULE: main(cinterop)
// FILE: main.kt
@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class)
import kotlinx.cinterop.*
import kotlin.test.*
import mangling.*
fun box(): String {
companion = _Companion.`Companion$`
assertEquals(_Companion.`Companion$`, companion)
return "OK"
}
@@ -0,0 +1,23 @@
// TARGET_BACKEND: NATIVE
// MODULE: cinterop
// FILE: mangling2.def
---
// test mangling of special names
enum Companion {One, Two};
// MODULE: main(cinterop)
// FILE: main.kt
@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class)
import kotlinx.cinterop.*
import kotlin.test.*
import mangling2.*
fun box(): String {
val mangled = `Companion$`.Two
assertEquals(`Companion$`.Two, mangled)
return "OK"
}
@@ -1,7 +1,34 @@
// TARGET_BACKEND: NATIVE
// MODULE: cinterop
// FILE: mangling_keywords.def
---
#define as "as"
#define class "class"
#define dynamic "dynamic"
#define false "false"
#define fun "fun"
#define in "in"
#define interface "interface"
#define is "is"
#define null "null"
#define object "object"
#define package "package"
#define super "super"
#define this "this"
#define throw "throw"
#define true "true"
#define try "try"
#define typealias "typealias"
#define val "val"
#define var "var"
#define when "when"
// MODULE: main(cinterop)
// FILE: main.kt
@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class)
import kotlin.test.*
import mangling_keywords.*
fun main() {
fun box(): String {
// Check that all Kotlin keywords are imported and mangled.
assertEquals("as", `as`)
assertEquals("class", `class`)
@@ -23,5 +50,6 @@ fun main() {
assertEquals("val", `val`)
assertEquals("var", `var`)
assertEquals("when", `when`)
return "OK"
}
@@ -1,8 +1,87 @@
// TARGET_BACKEND: NATIVE
// MODULE: cinterop
// FILE: mangling_keywords2.def
---
enum KotlinKeywordsEnum {
as,
class,
dynamic,
false,
fun,
in,
interface,
is,
null,
object,
package,
super,
this,
throw,
true,
try,
typealias,
val,
var,
when,
};
struct KotlinKeywordsStruct {
int as;
int class;
int dynamic;
int false;
int fun;
int in;
int interface;
int is;
int null;
int object;
int package;
int super;
int this;
int throw;
int true;
int try;
int typealias;
int val;
int var;
int when;
};
struct KotlinKeywordsStruct createKotlinKeywordsStruct() {
struct KotlinKeywordsStruct s = {
.as = 0,
.class = 0,
.dynamic = 0,
.false = 0,
.fun = 0,
.in = 0,
.interface = 0,
.is = 0,
.null = 0,
.object = 0,
.package = 0,
.super = 0,
.this = 0,
.throw = 0,
.true = 0,
.try = 0,
.typealias = 0,
.val = 0,
.var = 0,
.when = 0,
};
return s;
}
// MODULE: main(cinterop)
// FILE: main.kt
@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class)
import kotlin.test.*
import mangling_keywords2.*
import kotlinx.cinterop.useContents
fun main() {
fun box(): String {
// Check that all Kotlin keywords are imported and mangled.
createKotlinKeywordsStruct().useContents {
assertEquals(0, `as`)
@@ -47,4 +126,6 @@ fun main() {
assertEquals(KotlinKeywordsEnum.`val`, KotlinKeywordsEnum.`val`)
assertEquals(KotlinKeywordsEnum.`var`, KotlinKeywordsEnum.`var`)
assertEquals(KotlinKeywordsEnum.`when`, KotlinKeywordsEnum.`when`)
return "OK"
}
@@ -0,0 +1,319 @@
// TARGET_BACKEND: NATIVE
// MODULE: cinterop
// FILE: structAnonym.def
---
/*
* Test of return/send-by-value for aggregate type (struct or union) with anonymous inner struct or union member.
* Specific issues: alignment, packed, nested named and anon struct/union, other anon types (named field of anon struct type; anon bitfield)
*/
#include <inttypes.h>
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Winitializer-overrides"
union _GLKVector3
{
struct { float x, y, z; };
struct { float r, g, b; };
struct { float s, t, p; };
float v[3];
};
static union _GLKVector3 get_GLKVector3() {
union _GLKVector3 ret = {{1, 2, 3}};
return ret;
}
static float hash_GLKVector3(union _GLKVector3 x) {
union _GLKVector3 ret = {{1, 2, 3}};
return x.x + 2.0f * x.y + 4.0f * x.z;
}
// trivial alignment: member is already aligned, but this implies implicit larger alignment of the root struct
struct StructAnonRecordMember_ImplicitAlignment {
int32_t a[4];
struct {
int b __attribute__((aligned(16)));
};
};
static struct StructAnonRecordMember_ImplicitAlignment retByValue_StructAnonRecordMember_ImplicitAlignment() {
struct StructAnonRecordMember_ImplicitAlignment t = {
.a = {1,2,3,4},
.b = 42
};
return t;
}
struct StructAnonRecordMember_ExplicitAlignment {
char a;
struct {
__attribute__((aligned(4)))
char x;
};
};
static struct StructAnonRecordMember_ExplicitAlignment retByValue_StructAnonRecordMember_ExplicitAlignment() {
struct StructAnonRecordMember_ExplicitAlignment t = {
.a = 'a',
.x = 'x'
};
return t;
}
// Deep nesting
struct StructAnonRecordMember_Nested {
int x;
union { // implicitly aligned to 8 bytes due to int64, or 4 bytes at 32-bit arch
int a[2];
struct {
int64_t b;
};
};
char z;
double y;
};
static struct StructAnonRecordMember_Nested retByValue_StructAnonRecordMember_Nested() {
struct StructAnonRecordMember_Nested c = {
.x = 37,
.b = 42,
.z = 'z',
.y = 3.14
};
return c;
}
static int sendByValue_StructAnonRecordMember_Nested(struct StructAnonRecordMember_Nested c) {
return c.a[0] + 2 * c.a[1];
}
// Basic, 2 levels
struct StructAnonRecordMember_Complicate {
char first; // __attribute__((aligned(16)));
union {
int a[2];
union { char c1; int c2; };
struct { char b1; int64_t b2; }; // implicit 64-bits alignment
};
char second __attribute__((aligned(16)));
struct {
char x;
struct { int64_t b11, b12; } Y2;
int32_t f __attribute__((aligned(16)));
}; // __attribute__((aligned(16)));
char last;
};
#define INIT(T, x) struct T x = \
{ \
.first = 'a', \
.b1 = 'b', \
.b2 = 42, \
.second = 's', \
.last = 'z', \
.f = 314, \
.Y2 = {11, 12} \
}
static struct StructAnonRecordMember_Complicate retByValue_StructAnonRecordMember_Complicate() {
INIT(StructAnonRecordMember_Complicate, c);
return c;
}
struct StructAnonRecordMember_Packed {
char first;
union {
int a[2];
union { char c1; int c2; };
struct { char b1; int64_t b2; };
};
char second;
struct {
char x;
struct { int64_t b11, b12; } Y2;
int32_t f;
} __attribute__((aligned(16)));
char last;
} __attribute__ ((packed));
static struct StructAnonRecordMember_Packed retByValue_StructAnonRecordMember_Packed() {
INIT(StructAnonRecordMember_Packed, c);
return c;
}
// Nested struct may be packed too
#pragma pack(1)
struct StructAnonRecordMember_PragmaPacked {
char first;
union {
int a[2];
union { char c1; int c2; };
struct { char b1; int64_t b2; };
};
char second;
struct {
char x;
struct { int64_t b11, b12; } Y2;
int32_t f __attribute__((aligned(16))); // another kind of alignment
};
char last;
} __attribute__ ((packed));
#pragma pack()
static struct StructAnonRecordMember_PragmaPacked retByValue_StructAnonRecordMember_PragmaPacked() {
INIT(StructAnonRecordMember_PragmaPacked, c);
return c;
}
#pragma pack(2)
struct StructAnonRecordMember_Packed2 {
char first;
union {
int a[2];
union { char c1; int c2; };
struct { char b1; int64_t b2; };
};
char second;
struct {
char x;
struct { int64_t b11, b12; } Y2;
int32_t f;
} __attribute__((aligned(16)));
char last;
} __attribute__ ((packed));
#pragma pack()
static struct StructAnonRecordMember_Packed2 retByValue_StructAnonRecordMember_Packed2() {
INIT(StructAnonRecordMember_Packed2, c);
return c;
}
#pragma clang diagnostic pop
// MODULE: main(cinterop)
// FILE: main.kt
@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class)
import kotlinx.cinterop.*
import kotlin.test.*
import structAnonym.*
fun test_GLKVector3() {
get_GLKVector3().useContents {
assertEquals(1.0f, x)
assertEquals(2.0f, g)
assertEquals(3.0f, p)
r = 0.1f
g = 0.2f
b = 0.3f
assertEquals(v[0], r)
assertEquals(v[1], g)
assertEquals(v[2], b)
val ret = hash_GLKVector3(this.readValue())
assertEquals(s + 2f * t + 4f * p , ret)
}
}
fun test_StructAnonRecordMember_ImplicitAlignment() {
retByValue_StructAnonRecordMember_ImplicitAlignment()
.useContents {
assertEquals(1, a[0])
assertEquals(4, a[3])
assertEquals(42, b)
}
}
fun test_StructAnonRecordMember_ExplicitAlignment() {
retByValue_StructAnonRecordMember_ExplicitAlignment()
.useContents {
assertEquals('a', a.toInt().toChar())
assertEquals('x', x.toInt().toChar())
}
}
fun test_StructAnonRecordMember_Nested() {
retByValue_StructAnonRecordMember_Nested()
.useContents {
assertEquals(37, x)
assertEquals(42, b)
assertEquals('z', z.toInt().toChar())
assertEquals(3.14, y)
a[0] = 3
a[1] = 5
assertEquals(3 + 2*5, sendByValue_StructAnonRecordMember_Nested(this.readValue()))
}
}
fun test_StructAnonym_Complicate() {
retByValue_StructAnonRecordMember_Complicate()
.useContents{
assertEquals('a', first.toInt().toChar())
assertEquals('s', second.toInt().toChar())
assertEquals('z', last.toInt().toChar())
assertEquals('b', b1.toInt().toChar())
assertEquals(42L, b2)
assertEquals(314, f)
assertEquals(11L, Y2.b11)
}
}
fun test_StructAnonym_Packed() {
retByValue_StructAnonRecordMember_Packed2()
.useContents{
assertEquals('a', first.toInt().toChar())
assertEquals('s', second.toInt().toChar())
assertEquals('z', last.toInt().toChar())
assertEquals('b', b1.toInt().toChar())
assertEquals(42L, b2)
assertEquals(314, f)
assertEquals(11L, Y2.b11)
}
}
fun test_StructAnonym_PragmaPacked() {
retByValue_StructAnonRecordMember_PragmaPacked()
.useContents{
assertEquals('a', first.toInt().toChar())
assertEquals('s', second.toInt().toChar())
assertEquals('z', last.toInt().toChar())
assertEquals('b', b1.toInt().toChar())
assertEquals(42L, b2)
assertEquals(314, f)
assertEquals(11L, Y2.b11)
}
}
fun test_StructAnonym_Packed2() {
retByValue_StructAnonRecordMember_Packed2()
.useContents{
assertEquals('a', first.toInt().toChar())
assertEquals('s', second.toInt().toChar())
assertEquals('z', last.toInt().toChar())
assertEquals('b', b1.toInt().toChar())
assertEquals(42L, b2)
assertEquals(314, f)
assertEquals(11L, Y2.b11)
}
}
fun box(): String {
test_GLKVector3()
test_StructAnonRecordMember_ImplicitAlignment()
test_StructAnonRecordMember_ExplicitAlignment()
test_StructAnonRecordMember_Nested()
test_StructAnonym_Complicate()
test_StructAnonym_Packed()
test_StructAnonym_PragmaPacked()
test_StructAnonym_Packed2()
return "OK"
}
@@ -1,10 +1,82 @@
// TARGET_BACKEND: NATIVE
// MODULE: cinterop
// FILE: cstructs.def
nonStrictEnums = NonStrict
---
typedef struct {
int i;
} Trivial;
enum E {
R, G, B
};
enum NonStrict {
N, S, K
};
struct Complex {
unsigned int ui;
Trivial t;
struct Complex* next;
enum E e;
enum NonStrict nonStrict;
int arr[2];
_Bool b;
};
struct __attribute__((packed)) Packed {
int i : 1;
enum E e : 2;
};
struct Complex produceComplex() {
struct Complex complex = {
.ui = 128,
.t = {1},
.next = 0,
.e = R,
.nonStrict = K,
.arr = {-51, -19},
.b = 1
};
return complex;
};
struct WithFlexibleArray {
int size;
int data[];
};
struct WithFlexibleArrayWithPadding {
int size;
char c;
long long data[];
};
void fillArray(struct WithFlexibleArrayWithPadding *flex, int count) {
flex->size = count;
flex->c = '!';
for (int i = 0; i < count; i++) {
flex->data[i] = (((long long)i) << 32) | (i * 100);
}
}
struct WithZeroSizedArray {
int size;
int data[0];
};
// MODULE: main(cinterop)
// FILE: main.kt
@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class)
import cstructs.*
import kotlinx.cinterop.*
import kotlin.test.*
fun main() {
fun box(): String {
produceComplex().useContents {
assertEquals(ui, 128u)
ui = 333u
@@ -107,7 +179,7 @@ fun main() {
assertEquals((i.toLong() shl 32) or (i.toLong() * 100), flex.data[i])
}
}
return "OK"
}
fun <T : E> checkEnumSubTyping(e: T) = memScoped {
@@ -1,3 +1,6 @@
// TARGET_BACKEND: NATIVE
// MODULE: cinterop
// FILE: ctypes.def
---
// KT-28065
@@ -38,8 +41,8 @@ static int vlaSum2DBothDimensions(int rows, int columns, int array[rows][columns
int result = 0;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < columns; ++j) {
result += array[i][j];
}
result += array[i][j];
}
}
return result;
}
@@ -80,11 +83,50 @@ enum EnumCharBase : Char {
};
static int sendEnum(enum EnumCharBase x) {
return (int)x + 2;
return (int)x + 2;
}
enum EnumExplicitChar : char {
EnumExplicitCharA = 'a',
EnumExplicitCharB = 'b',
EnumExplicitCharDup = 'a'
EnumExplicitCharA = 'a',
EnumExplicitCharB = 'b',
EnumExplicitCharDup = 'a'
};
// MODULE: main(cinterop)
// FILE: main.kt
@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class)
import kotlinx.cinterop.*
import kotlin.native.*
import kotlin.test.*
import ctypes.*
fun box(): String {
getStructWithConstFields().useContents {
assertEquals(111, x)
assertEquals(222, y)
}
assertEquals(1u, ForwardDeclaredEnum.ONE.value)
assertEquals(6, vlaSum(3, cValuesOf(1, 2, 3)))
assertEquals(10, vlaSum2D(2, cValuesOf(1, 2, 3, 4)))
assertEquals(21, vlaSum2DBothDimensions(2, 3, cValuesOf(1, 2, 3, 4, 5, 6)))
// Not supported by clang:
// assertEquals(10, vlaSum2DForward(cValuesOf(1, 2, 3, 4), 2))
assertEquals(0u, StrictEnum1.StrictEnum1A.value)
assertEquals(1u, StrictEnum2.StrictEnum2B.value)
assertEquals(0u, NonStrictEnum1A)
assertEquals(1u, NonStrictEnum2B)
assertEquals(1, EnumCharBase.EnumCharBaseB.value)
assertEquals(3, sendEnum(EnumCharBase.EnumCharBaseB))
assertEquals('a'.toByte(), EnumExplicitCharA)
assertEquals('b'.toByte(), EnumExplicitCharB)
assertEquals(EnumExplicitCharA, EnumExplicitCharDup)
return "OK"
}
@@ -1,3 +1,27 @@
// TARGET_BACKEND: NATIVE
// MODULE: cinterop
// FILE: cunion.def
---
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;
// MODULE: main(cinterop)
// FILE: main.kt
@file:OptIn(kotlin.experimental.ExperimentalNativeApi::class, kotlinx.cinterop.ExperimentalForeignApi::class)
import cunion.*
@@ -5,7 +29,7 @@ import kotlinx.cinterop.*
import kotlin.native.*
import kotlin.test.*
fun main() {
fun box(): String {
memScoped {
val basicUnion = alloc<BasicUnion>()
for (value in Short.MIN_VALUE..Short.MAX_VALUE) {
@@ -35,4 +59,5 @@ fun main() {
union.i = 0u
assertEquals(0u, union.b)
}
return "OK"
}
@@ -1,3 +1,10 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
// TARGET_BACKEND: NATIVE
// MODULE: cinterop
// FILE: cunsupported.def
compilerOpts = -mno-xsave
---
@@ -25,3 +32,16 @@ ALL_ATTRS1 void macroAttr5() {}
#define ALL_ATTRS2 __attribute__((always_inline, __target__("xsave")))
ALL_ATTRS2 void macroAttr6() {}
// MODULE: main(cinterop)
// FILE: main.kt
@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class)
import kotlin.test.*
import cunsupported.*
fun box(): String {
noAttr()
noTargetAttr()
return "OK"
}
+30
View File
@@ -0,0 +1,30 @@
// TARGET_BACKEND: NATIVE
// MODULE: cinterop
// FILE: cvalues.def
---
_Bool isNullString(const char* str) {
return str == (const char*)0;
}
typedef const short* LPCWSTR;
_Bool isNullWString(LPCWSTR str) {
return str == (LPCWSTR)0;
}
// MODULE: main(cinterop)
// FILE: main.kt
@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class)
import kotlinx.cinterop.*
import kotlin.test.*
import cvalues.*
fun box(): String {
assertTrue(isNullString(null))
assertTrue(isNullWString(null))
assertFalse(isNullString("a"))
assertFalse(isNullWString("b"))
return "OK"
}
@@ -1,3 +1,37 @@
// TARGET_BACKEND: NATIVE
// MODULE: cinterop
// FILE: cvectors.def
---
typedef float __attribute__ ((__vector_size__ (16))) KVector4f;
typedef int __attribute__ ((__vector_size__ (16))) KVector4i32;
struct Complex {
unsigned int ui;
KVector4f vec4f;
struct Complex* next;
int arr[2];
};
struct Complex produceComplexVector() {
struct Complex complex = {
.ui = 128,
.vec4f = {1.0, 1.0, 1.0, 1.0},
.next = 0,
.arr = {-51, -19}
};
return complex;
};
static float sendV4F(KVector4f v) {
return v[0] + 2 * v[1] + 4 * v[2] + 8 * v[3];
}
static int sendV4I(KVector4i32 v) {
return v[0] + 2 * v[1] + 4 * v[2] + 8 * v[3];
}
// MODULE: main(cinterop)
// FILE: main.kt
@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class, kotlin.experimental.ExperimentalNativeApi::class)
import kotlinx.cinterop.*
@@ -6,8 +40,8 @@ import kotlin.native.*
import kotlin.test.*
import cvectors.*
fun main() {
produceComplex().useContents {
fun box(): String {
produceComplexVector().useContents {
assertEquals(vec4f, vectorOf(1.0f, 1.0f, 1.0f, 1.0f))
vec4f = vectorOf(0.0f, 0.0f, 0.0f, 0.0f)
assertEquals(vec4f, vectorOf(0.0f, 0.0f, 0.0f, 0.0f))
@@ -25,4 +59,6 @@ fun main() {
}
assertEquals(vector.value, vectorOf(1, 2, 3, 4))
}
return "OK"
}
@@ -0,0 +1,37 @@
// TARGET_BACKEND: NATIVE
// MODULE: cinterop
// FILE: cCallback.def
language = C
---
extern char* sb;
void runAndCatch(void(*f)(void));
// FILE: cCallback.cpp
#include <stdio.h>
char* sb = nullptr;
extern "C" void runAndCatch(void(*f)(void)) {
try {
f();
} catch (...) {
sb = "OK";
}
}
// MODULE: main(cinterop)
// FILE: main.kt
@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class)
import kotlinx.cinterop.staticCFunction
import kotlinx.cinterop.toKString
import cCallback.runAndCatch
import cCallback.sb
fun throwingCallback() {
throw IllegalStateException("Kotlin Exception!")
}
fun box(): String {
runAndCatch(staticCFunction(::throwingCallback))
return sb?.toKString() ?: "FAIL"
}
+2 -2
View File
@@ -1,5 +1,5 @@
// TARGET_BACKEND: NATIVE
// MODULE: cinterop
// MODULE: cinterop_kt63048
// FILE: kt63048.def
language = Objective-C
headers = kt63048.h
@@ -23,7 +23,7 @@ headers = kt63048.h
@end
// MODULE: main(cinterop)
// MODULE: main(cinterop_kt63048)
// FILE: main.kt
@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class, kotlin.experimental.ExperimentalObjCName::class)
+12 -4
View File
@@ -3,8 +3,16 @@
// FILE: kt63049.def
depends = Foundation
language = Objective-C
---
@interface WithClassProperty
headers = kt63049.h
// FILE: kt63049.h
#import "Foundation/NSObject.h"
@interface KT63049 : NSObject
@end
// FILE: kt63049.m
#import "kt63049.h"
@implementation KT63049 : NSObject
@end
// MODULE: main(cinterop)
@@ -14,8 +22,8 @@ language = Objective-C
import kt63049.*
import kotlin.test.assertEquals
class Impl : WithClassProperty() {
companion object : WithClassPropertyMeta() {
class Impl : KT63049() {
companion object : KT63049Meta() {
fun stringProperty(): String? = "OK"
}
}
@@ -6272,6 +6272,26 @@ public class JvmAbiConsistencyTestBoxGenerated extends AbstractJvmAbiConsistency
public void testAllFilesPresentInCinterop() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
@Nested
@TestMetadata("compiler/testData/codegen/box/cinterop/basics")
@TestDataPath("$PROJECT_ROOT")
public class Basics {
@Test
public void testAllFilesPresentInBasics() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop/basics"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
}
@Nested
@TestMetadata("compiler/testData/codegen/box/cinterop/exceptions")
@TestDataPath("$PROJECT_ROOT")
public class Exceptions {
@Test
public void testAllFilesPresentInExceptions() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop/exceptions"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
}
}
@Nested
@@ -6038,6 +6038,26 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
public void testAllFilesPresentInCinterop() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM, true);
}
@Nested
@TestMetadata("compiler/testData/codegen/box/cinterop/basics")
@TestDataPath("$PROJECT_ROOT")
public class Basics {
@Test
public void testAllFilesPresentInBasics() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop/basics"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM, true);
}
}
@Nested
@TestMetadata("compiler/testData/codegen/box/cinterop/exceptions")
@TestDataPath("$PROJECT_ROOT")
public class Exceptions {
@Test
public void testAllFilesPresentInExceptions() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop/exceptions"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM, true);
}
}
}
@Nested
@@ -6272,6 +6272,26 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
public void testAllFilesPresentInCinterop() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
@Nested
@TestMetadata("compiler/testData/codegen/box/cinterop/basics")
@TestDataPath("$PROJECT_ROOT")
public class Basics {
@Test
public void testAllFilesPresentInBasics() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop/basics"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
}
@Nested
@TestMetadata("compiler/testData/codegen/box/cinterop/exceptions")
@TestDataPath("$PROJECT_ROOT")
public class Exceptions {
@Test
public void testAllFilesPresentInExceptions() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop/exceptions"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
}
}
@Nested
@@ -6272,6 +6272,26 @@ public class IrBlackBoxCodegenWithIrInlinerTestGenerated extends AbstractIrBlack
public void testAllFilesPresentInCinterop() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
@Nested
@TestMetadata("compiler/testData/codegen/box/cinterop/basics")
@TestDataPath("$PROJECT_ROOT")
public class Basics {
@Test
public void testAllFilesPresentInBasics() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop/basics"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
}
@Nested
@TestMetadata("compiler/testData/codegen/box/cinterop/exceptions")
@TestDataPath("$PROJECT_ROOT")
public class Exceptions {
@Test
public void testAllFilesPresentInExceptions() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop/exceptions"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
}
}
@Nested
@@ -5483,6 +5483,32 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
public void testAllFilesPresentInCinterop() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
@TestMetadata("compiler/testData/codegen/box/cinterop/basics")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Basics extends AbstractLightAnalysisModeTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.JVM_IR, testDataFilePath);
}
public void testAllFilesPresentInBasics() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop/basics"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
}
@TestMetadata("compiler/testData/codegen/box/cinterop/exceptions")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Exceptions extends AbstractLightAnalysisModeTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.JVM_IR, testDataFilePath);
}
public void testAllFilesPresentInExceptions() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop/exceptions"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
}
}
@TestMetadata("compiler/testData/codegen/box/classLiteral")
@@ -4610,6 +4610,26 @@ public class FirJsCodegenBoxTestGenerated extends AbstractFirJsCodegenBoxTest {
public void testAllFilesPresentInCinterop() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR, true);
}
@Nested
@TestMetadata("compiler/testData/codegen/box/cinterop/basics")
@TestDataPath("$PROJECT_ROOT")
public class Basics {
@Test
public void testAllFilesPresentInBasics() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop/basics"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR, true);
}
}
@Nested
@TestMetadata("compiler/testData/codegen/box/cinterop/exceptions")
@TestDataPath("$PROJECT_ROOT")
public class Exceptions {
@Test
public void testAllFilesPresentInExceptions() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop/exceptions"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR, true);
}
}
}
@Nested
@@ -4610,6 +4610,26 @@ public class FirJsES6CodegenBoxTestGenerated extends AbstractFirJsES6CodegenBoxT
public void testAllFilesPresentInCinterop() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR_ES6, true);
}
@Nested
@TestMetadata("compiler/testData/codegen/box/cinterop/basics")
@TestDataPath("$PROJECT_ROOT")
public class Basics {
@Test
public void testAllFilesPresentInBasics() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop/basics"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR_ES6, true);
}
}
@Nested
@TestMetadata("compiler/testData/codegen/box/cinterop/exceptions")
@TestDataPath("$PROJECT_ROOT")
public class Exceptions {
@Test
public void testAllFilesPresentInExceptions() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop/exceptions"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR_ES6, true);
}
}
}
@Nested
@@ -4610,6 +4610,26 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
public void testAllFilesPresentInCinterop() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR, true);
}
@Nested
@TestMetadata("compiler/testData/codegen/box/cinterop/basics")
@TestDataPath("$PROJECT_ROOT")
public class Basics {
@Test
public void testAllFilesPresentInBasics() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop/basics"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR, true);
}
}
@Nested
@TestMetadata("compiler/testData/codegen/box/cinterop/exceptions")
@TestDataPath("$PROJECT_ROOT")
public class Exceptions {
@Test
public void testAllFilesPresentInExceptions() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop/exceptions"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR, true);
}
}
}
@Nested
@@ -4610,6 +4610,26 @@ public class IrJsES6CodegenBoxTestGenerated extends AbstractIrJsES6CodegenBoxTes
public void testAllFilesPresentInCinterop() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR_ES6, true);
}
@Nested
@TestMetadata("compiler/testData/codegen/box/cinterop/basics")
@TestDataPath("$PROJECT_ROOT")
public class Basics {
@Test
public void testAllFilesPresentInBasics() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop/basics"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR_ES6, true);
}
}
@Nested
@TestMetadata("compiler/testData/codegen/box/cinterop/exceptions")
@TestDataPath("$PROJECT_ROOT")
public class Exceptions {
@Test
public void testAllFilesPresentInExceptions() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop/exceptions"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR_ES6, true);
}
}
}
@Nested
@@ -1069,10 +1069,6 @@ createInterop("sysstat") {
it.defFile f
}
createInterop("cstdlib") {
it.defFile 'interop/basics/cstdlib.def'
}
createInterop("cstdio") {
it.defFile 'interop/basics/cstdio.def'
}
@@ -1081,54 +1077,6 @@ createInterop("sockets") {
it.defFile 'interop/basics/sockets.def'
}
createInterop("cmacros") {
it.defFile 'interop/basics/cmacros.def'
}
createInterop("cunsupported") {
it.defFile 'interop/basics/cunsupported.def'
}
createInterop("ctypes") {
it.defFile 'interop/basics/ctypes.def'
}
createInterop("structAnonym") {
it.defFile 'interop/basics/structAnonym.def'
}
createInterop("cvalues") {
it.defFile 'interop/basics/cvalues.def'
}
createInterop("cvectors") {
it.defFile 'interop/basics/cvectors.def'
}
createInterop("cstructs") {
it.defFile 'interop/basics/cstructs.def'
}
createInterop("cmangling") {
it.defFile 'interop/basics/mangling.def'
}
createInterop("cmangling2") {
it.defFile 'interop/basics/mangling2.def'
}
createInterop("cmangling_keywords") {
it.defFile 'interop/basics/mangling_keywords.def'
}
createInterop("cmangling_keywords2") {
it.defFile 'interop/basics/mangling_keywords2.def'
}
createInterop("cunion") {
it.defFile 'interop/basics/cunion.def'
}
createInterop("concurrentTerminate") {
it.defFile 'interop/concurrentTerminate/concurrentTerminate.def'
it.headers "$projectDir/interop/concurrentTerminate/async.h"
@@ -1368,11 +1316,6 @@ createInterop("exceptions_throwThroughBridge") {
it.extraOpts "-Xcompile-source", "$projectDir/interop/exceptions/throwThroughBridgeInterop.cpp"
}
createInterop("exceptions_cCallback") {
it.defFile "interop/exceptions/cCallback.def"
it.extraOpts "-Xcompile-source", "$projectDir/interop/exceptions/cCallback.cpp"
}
/**
* Creates a task for the interop test. Configures runner and adds library and test build tasks.
*/
@@ -1439,12 +1382,6 @@ standaloneTest("interop_objc_allocException") {
UtilsKt.dependsOnPlatformLibs(it)
}
interopTest("interop_available_processors") {
source = 'interop/basics/available_processors.kt'
arguments = [Runtime.getRuntime().availableProcessors().toString()]
interop = 'cstdlib'
}
interopTest("interop0") {
disabled = (project.testTarget == 'linux_arm64')
useGoldenData = true
@@ -1452,24 +1389,12 @@ interopTest("interop0") {
interop = 'sysstat'
}
interopTest("interop1") {
useGoldenData = true
source = "interop/basics/1.kt"
interop = 'cstdlib'
}
interopTest("interop2") {
useGoldenData = true
source = "interop/basics/2.kt"
interop = 'cstdio'
}
interopTest("interop3") {
useGoldenData = true
source = "interop/basics/3.kt"
interop = 'cstdlib'
}
interopTest("interop4") {
useGoldenData = true
source = "interop/basics/4.kt"
@@ -1482,51 +1407,6 @@ standaloneTest("interop5") {
UtilsKt.dependsOnPlatformLibs(it)
}
interopTest("interop_macros") {
source = "interop/basics/macros.kt"
interop = 'cmacros'
}
interopTest("interop_unsupported") {
source = "interop/basics/unsupported.kt"
interop = 'cunsupported'
}
interopTest("interop_types") {
source = "interop/basics/types.kt"
interop = 'ctypes'
}
interopTest("interop_structAnonym") {
source = "interop/basics/structAnonym.kt"
interop = 'structAnonym'
}
interopTest("interop_vectors") {
source = "interop/basics/vectors.kt"
interop = 'cvectors'
}
interopTest("interop_mangling") {
source = "interop/basics/mangling.kt"
interop = 'cmangling'
}
interopTest("interop_mangling2") {
source = "interop/basics/mangling2.kt"
interop = 'cmangling2'
}
interopTest("interop_mangling_keywords") {
source = "interop/basics/mangling_keywords.kt"
interop = 'cmangling_keywords'
}
interopTest("interop_mangling_keywords2") {
source = "interop/basics/mangling_keywords2.kt"
interop = 'cmangling_keywords2'
}
interopTest("interop_concurrentTerminate") {
source = "interop/concurrentTerminate/main.kt"
interop = 'concurrentTerminate'
@@ -1539,16 +1419,6 @@ interopTest("interop_embedStaticLibraries") {
interop = 'embedStaticLibraries'
}
interopTest("interop_values") {
source = "interop/basics/values.kt"
interop = 'cvalues'
}
interopTest("interop_structs") {
source = "interop/basics/structs.kt"
interop = 'cstructs'
}
interopTest("interop_threadStates") {
source = "interop/threadStates/threadStates.kt"
flags = ['-opt-in=kotlin.native.internal.InternalForKotlinNative']
@@ -1586,11 +1456,6 @@ interopTest("interop_withSpaces") {
}
}
interopTest("interop_union") {
interop = 'cunion'
source = "interop/basics/union.kt"
}
// TODO: This test should be run with caches on.
interopTest("interop_kt51925") {
interop = 'kt51925'
@@ -2310,12 +2175,6 @@ dynamicTest("interop_kotlin_exception_hook") {
outputChecker = { s -> s.contains("OK. Kotlin unhandled exception hook") }
}
interopTest("interop_exceptions_cCallback") {
source = "interop/exceptions/cCallback.kt"
outputChecker = { s -> s.contains("CATCH IN C++") }
interop = "exceptions_cCallback"
}
standaloneTest("library_ir_provider_mismatch") {
enabled = !isAggressiveGC // No need to test with different GC schedulers
@@ -1,3 +0,0 @@
257
-1
-2
@@ -1,25 +0,0 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class)
import kotlinx.cinterop.*
fun main(args: Array<String>) {
val values = intArrayOf(14, 12, 9, 13, 8)
val count = values.size
cstdlib.qsort(values.refTo(0), count.convert(), IntVar.size.convert(), staticCFunction { a, b ->
val aValue = a!!.reinterpret<IntVar>()[0]
val bValue = b!!.reinterpret<IntVar>()[0]
(aValue - bValue)
})
for (i in 0..count - 1) {
print(values[i])
print(" ")
}
println()
}
@@ -1 +0,0 @@
8 9 12 13 14
@@ -1,83 +0,0 @@
excludedMacros = EXCLUDED
---
int* ptr_call() {
return (int*) 1;
}
int int_call() {
return 42;
}
void void_call() {}
int arg_call(int x) {
return x;
}
typedef struct {
int value
} struct_t;
struct_t getStruct() {
return (struct_t){ 1 };
}
int global_var = 5;
#define TOO_MANY_ERRORS x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x
#define LEFT_BRACE {
#define ZERO 0
#define ONE 1
#define RIGHT_BRACE }
#define MAX_LONG 9223372036854775807
#define FOO_STRING "foo"
// This one should be ignored:
#define WIDE_FOO_STRING L"foo"
#define FOURTY_TWO 42
#define SEVENTEEN ((long long) 17)
#define ONE_POINT_ZERO 1.0
#define ONE_POINT_FIVE 1.5f
#define LEFT_PAREN (
#define NULL_PTR ((void*)0)
#define VOID_PTR ((void*)1)
#define INT_PTR ((int*)1)
#define PTR_SUM (INT_PTR + 1)
#define PTR_SUM_EXPECTED (sizeof(int) + 1)
#define RIGHT_PAREN )
enum {
INT_CALL = 1 // Should be replaced by macro below.
};
#define PTR_CALL ptr_call()
#define INT_CALL int_call()
#define CALL_SUM (int_call() + int_call())
#define GLOBAL_VAR (global_var)
// This one should be excluded:
#define EXCLUDED 42
// These ones should be ignored:
#define VOID_CALL (void_call())
#define ARG_CALL(x) (arg_call(x))
#define GET_STRUCT (getStruct())
#define UNDECLARED (undeclared())
#define BAD1 bar
#define BAD2 5;
#define BAD3 { foo(); }
void increment(int* counter);
#define increment(counter) { (*(counter))++; }
#define DEFAULT_DOUBLE_NAN __builtin_nan("")
#define DEFAULT_FLOAT_NAN __builtin_nanf("")
#define OTHER_DOUBLE_NAN __builtin_nan("0x123456789ab")
#define OTHER_FLOAT_NAN __builtin_nanf("0x12345")
@@ -1,21 +0,0 @@
headers = stdlib.h
compilerOpts.mingw = -D ADD_WINDOWS_ENV_FUNCTIONS
---
#ifdef ADD_WINDOWS_ENV_FUNCTIONS
static inline int setenv(const char *name, const char *value, int overwrite)
{
int errcode = 0;
if (!overwrite) {
size_t envsize = 0;
errcode = getenv_s(&envsize, NULL, 0, name);
if(errcode || envsize) return errcode;
}
return _putenv_s(name, value);
}
static inline int unsetenv(const char *name)
{
return _putenv_s(name, "");
}
#endif
@@ -1,65 +0,0 @@
nonStrictEnums = NonStrict
---
typedef struct {
int i;
} Trivial;
enum E {
R, G, B
};
enum NonStrict {
N, S, K
};
struct Complex {
unsigned int ui;
Trivial t;
struct Complex* next;
enum E e;
enum NonStrict nonStrict;
int arr[2];
_Bool b;
};
struct __attribute__((packed)) Packed {
int i : 1;
enum E e : 2;
};
struct Complex produceComplex() {
struct Complex complex = {
.ui = 128,
.t = {1},
.next = 0,
.e = R,
.nonStrict = K,
.arr = {-51, -19},
.b = 1
};
return complex;
};
struct WithFlexibleArray {
int size;
int data[];
};
struct WithFlexibleArrayWithPadding {
int size;
char c;
long long data[];
};
void fillArray(struct WithFlexibleArrayWithPadding *flex, int count) {
flex->size = count;
flex->c = '!';
for (int i = 0; i < count; i++) {
flex->data[i] = (((long long)i) << 32) | (i * 100);
}
}
struct WithZeroSizedArray {
int size;
int data[0];
};
@@ -1,17 +0,0 @@
---
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;
@@ -1,10 +0,0 @@
---
_Bool isNullString(const char* str) {
return str == (const char*)0;
}
typedef const short* LPCWSTR;
_Bool isNullWString(LPCWSTR str) {
return str == (LPCWSTR)0;
}
@@ -1,28 +0,0 @@
---
typedef float __attribute__ ((__vector_size__ (16))) KVector4f;
typedef int __attribute__ ((__vector_size__ (16))) KVector4i32;
struct Complex {
unsigned int ui;
KVector4f vec4f;
struct Complex* next;
int arr[2];
};
struct Complex produceComplex() {
struct Complex complex = {
.ui = 128,
.vec4f = {1.0, 1.0, 1.0, 1.0},
.next = 0,
.arr = {-51, -19}
};
return complex;
};
static float sendV4F(KVector4f v) {
return v[0] + 2 * v[1] + 4 * v[2] + 8 * v[3];
}
static int sendV4I(KVector4i32 v) {
return v[0] + 2 * v[1] + 4 * v[2] + 8 * v[3];
}
@@ -1,66 +0,0 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
@file:OptIn(kotlin.experimental.ExperimentalNativeApi::class, kotlinx.cinterop.ExperimentalForeignApi::class)
import kotlin.test.*
import cmacros.*
import kotlinx.cinterop.*
fun main(args: Array<String>) {
assertEquals("foo", FOO_STRING)
assertEquals(0, ZERO)
assertEquals(1, ONE)
assertEquals(Long.MAX_VALUE, MAX_LONG)
assertEquals(42, FOURTY_TWO)
val seventeen: Long = SEVENTEEN
assertEquals(17L, seventeen)
val onePointFive: Float = ONE_POINT_FIVE
val onePointZero: Double = ONE_POINT_ZERO
assertEquals(1.5f, onePointFive)
assertEquals(1.0, onePointZero)
val nullPtr: COpaquePointer? = NULL_PTR
val voidPtr: COpaquePointer? = VOID_PTR
val intPtr: CPointer<IntVar>? = INT_PTR
val ptrSum: CPointer<IntVar>? = PTR_SUM
val ptrCall: CPointer<IntVar>? = PTR_CALL
assertEquals(null, nullPtr)
assertEquals(1L, voidPtr.rawValue.toLong())
assertEquals(1L, intPtr.rawValue.toLong())
assertEquals(PTR_SUM_EXPECTED.toLong(), ptrSum.rawValue.toLong())
assertEquals(1L, ptrCall.rawValue.toLong())
assertEquals(42, INT_CALL)
assertEquals(84, CALL_SUM)
assertEquals(5, GLOBAL_VAR)
memScoped {
val counter = alloc<IntVar>()
counter.value = 42
increment(counter.ptr)
assertEquals(43, counter.value)
}
/**
* Mips processors are using different notation for quite/signaling nans.
* In particular, same clang intrinsic __builtin_nan() return other bitpatterns on mips,
* to avoid values, which would be singaling on MIPS. So, tested values are incorrect in that case.
*/
if (Platform.cpuArchitecture != CpuArchitecture.MIPS32 && Platform.cpuArchitecture != CpuArchitecture.MIPSEL32) {
val floatNanBase = Float.NaN.toRawBits()
assertEquals(floatNanBase, 0x7fc00000)
val doubleNanBase = Double.NaN.toRawBits()
assertEquals(doubleNanBase, 0x7ff8000000000000L)
assertEquals(floatNanBase, DEFAULT_FLOAT_NAN.toRawBits())
assertEquals(doubleNanBase, DEFAULT_DOUBLE_NAN.toRawBits())
assertEquals(floatNanBase, OTHER_FLOAT_NAN.toRawBits())
assertEquals(doubleNanBase, OTHER_DOUBLE_NAN.toRawBits())
}
}
@@ -1,6 +0,0 @@
---
// test mangling of special names
enum _Companion {Companion, Any};
enum _Companion companion = Companion;
@@ -1,9 +0,0 @@
import kotlinx.cinterop.*
import kotlin.test.*
import mangling.*
fun main() {
companion = _Companion.`Companion$`
assertEquals(_Companion.`Companion$`, companion)
}
@@ -1,6 +0,0 @@
---
// test mangling of special names
enum Companion {One, Two};
@@ -1,9 +0,0 @@
import kotlinx.cinterop.*
import kotlin.test.*
import mangling2.*
fun main() {
val mangled = `Companion$`.Two
assertEquals(`Companion$`.Two, mangled)
}
@@ -1,21 +0,0 @@
---
#define as "as"
#define class "class"
#define dynamic "dynamic"
#define false "false"
#define fun "fun"
#define in "in"
#define interface "interface"
#define is "is"
#define null "null"
#define object "object"
#define package "package"
#define super "super"
#define this "this"
#define throw "throw"
#define true "true"
#define try "try"
#define typealias "typealias"
#define val "val"
#define var "var"
#define when "when"
@@ -1,72 +0,0 @@
---
enum KotlinKeywordsEnum {
as,
class,
dynamic,
false,
fun,
in,
interface,
is,
null,
object,
package,
super,
this,
throw,
true,
try,
typealias,
val,
var,
when,
};
struct KotlinKeywordsStruct {
int as;
int class;
int dynamic;
int false;
int fun;
int in;
int interface;
int is;
int null;
int object;
int package;
int super;
int this;
int throw;
int true;
int try;
int typealias;
int val;
int var;
int when;
};
struct KotlinKeywordsStruct createKotlinKeywordsStruct() {
struct KotlinKeywordsStruct s = {
.as = 0,
.class = 0,
.dynamic = 0,
.false = 0,
.fun = 0,
.in = 0,
.interface = 0,
.is = 0,
.null = 0,
.object = 0,
.package = 0,
.super = 0,
.this = 0,
.throw = 0,
.true = 0,
.try = 0,
.typealias = 0,
.val = 0,
.var = 0,
.when = 0,
};
return s;
}
@@ -1,192 +0,0 @@
---
/*
* Test of return/send-by-value for aggregate type (struct or union) with anonymous inner struct or union member.
* Specific issues: alignment, packed, nested named and anon struct/union, other anon types (named field of anon struct type; anon bitfield)
*/
#include <inttypes.h>
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Winitializer-overrides"
union _GLKVector3
{
struct { float x, y, z; };
struct { float r, g, b; };
struct { float s, t, p; };
float v[3];
};
static union _GLKVector3 get_GLKVector3() {
union _GLKVector3 ret = {{1, 2, 3}};
return ret;
}
static float hash_GLKVector3(union _GLKVector3 x) {
union _GLKVector3 ret = {{1, 2, 3}};
return x.x + 2.0f * x.y + 4.0f * x.z;
}
// trivial alignment: member is already aligned, but this implies implicit larger alignment of the root struct
struct StructAnonRecordMember_ImplicitAlignment {
int32_t a[4];
struct {
int b __attribute__((aligned(16)));
};
};
static struct StructAnonRecordMember_ImplicitAlignment retByValue_StructAnonRecordMember_ImplicitAlignment() {
struct StructAnonRecordMember_ImplicitAlignment t = {
.a = {1,2,3,4},
.b = 42
};
return t;
}
struct StructAnonRecordMember_ExplicitAlignment {
char a;
struct {
__attribute__((aligned(4)))
char x;
};
};
static struct StructAnonRecordMember_ExplicitAlignment retByValue_StructAnonRecordMember_ExplicitAlignment() {
struct StructAnonRecordMember_ExplicitAlignment t = {
.a = 'a',
.x = 'x'
};
return t;
}
// Deep nesting
struct StructAnonRecordMember_Nested {
int x;
union { // implicitly aligned to 8 bytes due to int64, or 4 bytes at 32-bit arch
int a[2];
struct {
int64_t b;
};
};
char z;
double y;
};
static struct StructAnonRecordMember_Nested retByValue_StructAnonRecordMember_Nested() {
struct StructAnonRecordMember_Nested c = {
.x = 37,
.b = 42,
.z = 'z',
.y = 3.14
};
return c;
}
static int sendByValue_StructAnonRecordMember_Nested(struct StructAnonRecordMember_Nested c) {
return c.a[0] + 2 * c.a[1];
}
// Basic, 2 levels
struct StructAnonRecordMember_Complicate {
char first; // __attribute__((aligned(16)));
union {
int a[2];
union { char c1; int c2; };
struct { char b1; int64_t b2; }; // implicit 64-bits alignment
};
char second __attribute__((aligned(16)));
struct {
char x;
struct { int64_t b11, b12; } Y2;
int32_t f __attribute__((aligned(16)));
}; // __attribute__((aligned(16)));
char last;
};
#define INIT(T, x) struct T x = \
{ \
.first = 'a', \
.b1 = 'b', \
.b2 = 42, \
.second = 's', \
.last = 'z', \
.f = 314, \
.Y2 = {11, 12} \
}
static struct StructAnonRecordMember_Complicate retByValue_StructAnonRecordMember_Complicate() {
INIT(StructAnonRecordMember_Complicate, c);
return c;
}
struct StructAnonRecordMember_Packed {
char first;
union {
int a[2];
union { char c1; int c2; };
struct { char b1; int64_t b2; };
};
char second;
struct {
char x;
struct { int64_t b11, b12; } Y2;
int32_t f;
} __attribute__((aligned(16)));
char last;
} __attribute__ ((packed));
static struct StructAnonRecordMember_Packed retByValue_StructAnonRecordMember_Packed() {
INIT(StructAnonRecordMember_Packed, c);
return c;
}
// Nested struct may be packed too
#pragma pack(1)
struct StructAnonRecordMember_PragmaPacked {
char first;
union {
int a[2];
union { char c1; int c2; };
struct { char b1; int64_t b2; };
};
char second;
struct {
char x;
struct { int64_t b11, b12; } Y2;
int32_t f __attribute__((aligned(16))); // another kind of alignment
};
char last;
} __attribute__ ((packed));
#pragma pack()
static struct StructAnonRecordMember_PragmaPacked retByValue_StructAnonRecordMember_PragmaPacked() {
INIT(StructAnonRecordMember_PragmaPacked, c);
return c;
}
#pragma pack(2)
struct StructAnonRecordMember_Packed2 {
char first;
union {
int a[2];
union { char c1; int c2; };
struct { char b1; int64_t b2; };
};
char second;
struct {
char x;
struct { int64_t b11, b12; } Y2;
int32_t f;
} __attribute__((aligned(16)));
char last;
} __attribute__ ((packed));
#pragma pack()
static struct StructAnonRecordMember_Packed2 retByValue_StructAnonRecordMember_Packed2() {
INIT(StructAnonRecordMember_Packed2, c);
return c;
}
#pragma clang diagnostic pop
@@ -1,118 +0,0 @@
@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class)
import kotlinx.cinterop.*
import kotlin.test.*
import structAnonym.*
fun test_GLKVector3() {
get_GLKVector3().useContents {
assertEquals(1.0f, x)
assertEquals(2.0f, g)
assertEquals(3.0f, p)
r = 0.1f
g = 0.2f
b = 0.3f
assertEquals(v[0], r)
assertEquals(v[1], g)
assertEquals(v[2], b)
val ret = hash_GLKVector3(this.readValue())
assertEquals(s + 2f * t + 4f * p , ret)
}
}
fun test_StructAnonRecordMember_ImplicitAlignment() {
retByValue_StructAnonRecordMember_ImplicitAlignment()
.useContents {
assertEquals(1, a[0])
assertEquals(4, a[3])
assertEquals(42, b)
}
}
fun test_StructAnonRecordMember_ExplicitAlignment() {
retByValue_StructAnonRecordMember_ExplicitAlignment()
.useContents {
assertEquals('a', a.toInt().toChar())
assertEquals('x', x.toInt().toChar())
}
}
fun test_StructAnonRecordMember_Nested() {
retByValue_StructAnonRecordMember_Nested()
.useContents {
assertEquals(37, x)
assertEquals(42, b)
assertEquals('z', z.toInt().toChar())
assertEquals(3.14, y)
a[0] = 3
a[1] = 5
assertEquals(3 + 2*5, sendByValue_StructAnonRecordMember_Nested(this.readValue()))
}
}
fun test_StructAnonym_Complicate() {
retByValue_StructAnonRecordMember_Complicate()
.useContents{
assertEquals('a', first.toInt().toChar())
assertEquals('s', second.toInt().toChar())
assertEquals('z', last.toInt().toChar())
assertEquals('b', b1.toInt().toChar())
assertEquals(42L, b2)
assertEquals(314, f)
assertEquals(11L, Y2.b11)
}
}
fun test_StructAnonym_Packed() {
retByValue_StructAnonRecordMember_Packed2()
.useContents{
assertEquals('a', first.toInt().toChar())
assertEquals('s', second.toInt().toChar())
assertEquals('z', last.toInt().toChar())
assertEquals('b', b1.toInt().toChar())
assertEquals(42L, b2)
assertEquals(314, f)
assertEquals(11L, Y2.b11)
}
}
fun test_StructAnonym_PragmaPacked() {
retByValue_StructAnonRecordMember_PragmaPacked()
.useContents{
assertEquals('a', first.toInt().toChar())
assertEquals('s', second.toInt().toChar())
assertEquals('z', last.toInt().toChar())
assertEquals('b', b1.toInt().toChar())
assertEquals(42L, b2)
assertEquals(314, f)
assertEquals(11L, Y2.b11)
}
}
fun test_StructAnonym_Packed2() {
retByValue_StructAnonRecordMember_Packed2()
.useContents{
assertEquals('a', first.toInt().toChar())
assertEquals('s', second.toInt().toChar())
assertEquals('z', last.toInt().toChar())
assertEquals('b', b1.toInt().toChar())
assertEquals(42L, b2)
assertEquals(314, f)
assertEquals(11L, Y2.b11)
}
}
fun main() {
test_GLKVector3()
test_StructAnonRecordMember_ImplicitAlignment()
test_StructAnonRecordMember_ExplicitAlignment()
test_StructAnonRecordMember_Nested()
test_StructAnonym_Complicate()
test_StructAnonym_Packed()
test_StructAnonym_PragmaPacked()
test_StructAnonym_Packed2()
}
@@ -1,2 +0,0 @@
---
typedef int my_int;
@@ -1,33 +0,0 @@
@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class)
import kotlinx.cinterop.*
import kotlin.native.*
import kotlin.test.*
import ctypes.*
fun main() {
getStructWithConstFields().useContents {
assertEquals(111, x)
assertEquals(222, y)
}
assertEquals(1u, ForwardDeclaredEnum.ONE.value)
assertEquals(6, vlaSum(3, cValuesOf(1, 2, 3)))
assertEquals(10, vlaSum2D(2, cValuesOf(1, 2, 3, 4)))
assertEquals(21, vlaSum2DBothDimensions(2, 3, cValuesOf(1, 2, 3, 4, 5, 6)))
// Not supported by clang:
// assertEquals(10, vlaSum2DForward(cValuesOf(1, 2, 3, 4), 2))
assertEquals(0u, StrictEnum1.StrictEnum1A.value)
assertEquals(1u, StrictEnum2.StrictEnum2B.value)
assertEquals(0u, NonStrictEnum1A)
assertEquals(1u, NonStrictEnum2B)
assertEquals(1, EnumCharBase.EnumCharBaseB.value)
assertEquals(3, sendEnum(EnumCharBase.EnumCharBaseB))
assertEquals('a'.toByte(), EnumExplicitCharA)
assertEquals('b'.toByte(), EnumExplicitCharB)
assertEquals(EnumExplicitCharA, EnumExplicitCharDup)
}
@@ -1,10 +0,0 @@
---
int global;
#define global bad macro
void foo(void);
#define foo <
// Setter for x should not be generated.
typedef const int cint;
cint x;
@@ -1,12 +0,0 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
import kotlin.test.*
import cunsupported.*
fun main(args: Array<String>) {
noAttr()
noTargetAttr()
}
@@ -1,12 +0,0 @@
@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class)
import kotlinx.cinterop.*
import kotlin.test.*
import cvalues.*
fun main() {
assertTrue(isNullString(null))
assertTrue(isNullWString(null))
assertFalse(isNullString("a"))
assertFalse(isNullWString("b"))
}
@@ -1,9 +0,0 @@
#include <stdio.h>
extern "C" void runAndCatch(void(*f)(void)) {
try {
f();
} catch (...) {
printf("CATCH IN C++!\n");
}
}
@@ -1,5 +0,0 @@
language = C
---
void runAndCatch(void(*f)(void));
@@ -1,13 +0,0 @@
@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class)
import kotlinx.cinterop.staticCFunction
import cCallback.runAndCatch
fun throwingCallback() {
throw IllegalStateException("Kotlin Exception!")
}
fun main() {
runAndCatch(staticCFunction(::throwingCallback))
}
@@ -4867,6 +4867,128 @@ public class FirNativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTe
public void testToKString() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/toKString.kt");
}
@Nested
@TestMetadata("compiler/testData/codegen/box/cinterop/basics")
@TestDataPath("$PROJECT_ROOT")
@Tag("frontend-fir")
@FirPipeline()
@UseExtTestCaseGroupProvider()
public class Basics {
@Test
@TestMetadata("1.kt")
public void test1() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/1.kt");
}
@Test
@TestMetadata("3.kt")
public void test3() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/3.kt");
}
@Test
public void testAllFilesPresentInBasics() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop/basics"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.NATIVE, true);
}
@Test
@TestMetadata("available_processors.kt")
public void testAvailable_processors() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/available_processors.kt");
}
@Test
@TestMetadata("macros.kt")
public void testMacros() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/macros.kt");
}
@Test
@TestMetadata("mangling.kt")
public void testMangling() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/mangling.kt");
}
@Test
@TestMetadata("mangling2.kt")
public void testMangling2() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/mangling2.kt");
}
@Test
@TestMetadata("mangling_keywords.kt")
public void testMangling_keywords() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/mangling_keywords.kt");
}
@Test
@TestMetadata("mangling_keywords2.kt")
public void testMangling_keywords2() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/mangling_keywords2.kt");
}
@Test
@TestMetadata("structAnonym.kt")
public void testStructAnonym() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/structAnonym.kt");
}
@Test
@TestMetadata("structs.kt")
public void testStructs() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/structs.kt");
}
@Test
@TestMetadata("types.kt")
public void testTypes() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/types.kt");
}
@Test
@TestMetadata("union.kt")
public void testUnion() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/union.kt");
}
@Test
@TestMetadata("unsupported.kt")
public void testUnsupported() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/unsupported.kt");
}
@Test
@TestMetadata("values.kt")
public void testValues() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/values.kt");
}
@Test
@TestMetadata("vectors.kt")
public void testVectors() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/vectors.kt");
}
}
@Nested
@TestMetadata("compiler/testData/codegen/box/cinterop/exceptions")
@TestDataPath("$PROJECT_ROOT")
@Tag("frontend-fir")
@FirPipeline()
@UseExtTestCaseGroupProvider()
public class Exceptions {
@Test
public void testAllFilesPresentInExceptions() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop/exceptions"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.NATIVE, true);
}
@Test
@TestMetadata("cCallback.kt")
public void testCCallback() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/exceptions/cCallback.kt");
}
}
}
@Nested
@@ -4977,6 +4977,132 @@ public class FirNativeCodegenBoxTestNoPLGenerated extends AbstractNativeCodegenB
public void testToKString() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/toKString.kt");
}
@Nested
@TestMetadata("compiler/testData/codegen/box/cinterop/basics")
@TestDataPath("$PROJECT_ROOT")
@Tag("frontend-fir")
@FirPipeline()
@UseExtTestCaseGroupProvider()
@UsePartialLinkage(mode = Mode.DISABLED)
@Tag("no-partial-linkage-may-be-skipped")
public class Basics {
@Test
@TestMetadata("1.kt")
public void test1() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/1.kt");
}
@Test
@TestMetadata("3.kt")
public void test3() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/3.kt");
}
@Test
public void testAllFilesPresentInBasics() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop/basics"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.NATIVE, true);
}
@Test
@TestMetadata("available_processors.kt")
public void testAvailable_processors() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/available_processors.kt");
}
@Test
@TestMetadata("macros.kt")
public void testMacros() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/macros.kt");
}
@Test
@TestMetadata("mangling.kt")
public void testMangling() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/mangling.kt");
}
@Test
@TestMetadata("mangling2.kt")
public void testMangling2() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/mangling2.kt");
}
@Test
@TestMetadata("mangling_keywords.kt")
public void testMangling_keywords() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/mangling_keywords.kt");
}
@Test
@TestMetadata("mangling_keywords2.kt")
public void testMangling_keywords2() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/mangling_keywords2.kt");
}
@Test
@TestMetadata("structAnonym.kt")
public void testStructAnonym() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/structAnonym.kt");
}
@Test
@TestMetadata("structs.kt")
public void testStructs() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/structs.kt");
}
@Test
@TestMetadata("types.kt")
public void testTypes() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/types.kt");
}
@Test
@TestMetadata("union.kt")
public void testUnion() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/union.kt");
}
@Test
@TestMetadata("unsupported.kt")
public void testUnsupported() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/unsupported.kt");
}
@Test
@TestMetadata("values.kt")
public void testValues() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/values.kt");
}
@Test
@TestMetadata("vectors.kt")
public void testVectors() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/vectors.kt");
}
}
@Nested
@TestMetadata("compiler/testData/codegen/box/cinterop/exceptions")
@TestDataPath("$PROJECT_ROOT")
@Tag("frontend-fir")
@FirPipeline()
@UseExtTestCaseGroupProvider()
@UsePartialLinkage(mode = Mode.DISABLED)
@Tag("no-partial-linkage-may-be-skipped")
public class Exceptions {
@Test
public void testAllFilesPresentInExceptions() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop/exceptions"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.NATIVE, true);
}
@Test
@TestMetadata("cCallback.kt")
public void testCCallback() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/exceptions/cCallback.kt");
}
}
}
@Nested
@@ -4757,6 +4757,124 @@ public class NativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTest
public void testToKString() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/toKString.kt");
}
@Nested
@TestMetadata("compiler/testData/codegen/box/cinterop/basics")
@TestDataPath("$PROJECT_ROOT")
@UseExtTestCaseGroupProvider()
public class Basics {
@Test
@TestMetadata("1.kt")
public void test1() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/1.kt");
}
@Test
@TestMetadata("3.kt")
public void test3() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/3.kt");
}
@Test
public void testAllFilesPresentInBasics() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop/basics"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.NATIVE, true);
}
@Test
@TestMetadata("available_processors.kt")
public void testAvailable_processors() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/available_processors.kt");
}
@Test
@TestMetadata("macros.kt")
public void testMacros() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/macros.kt");
}
@Test
@TestMetadata("mangling.kt")
public void testMangling() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/mangling.kt");
}
@Test
@TestMetadata("mangling2.kt")
public void testMangling2() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/mangling2.kt");
}
@Test
@TestMetadata("mangling_keywords.kt")
public void testMangling_keywords() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/mangling_keywords.kt");
}
@Test
@TestMetadata("mangling_keywords2.kt")
public void testMangling_keywords2() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/mangling_keywords2.kt");
}
@Test
@TestMetadata("structAnonym.kt")
public void testStructAnonym() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/structAnonym.kt");
}
@Test
@TestMetadata("structs.kt")
public void testStructs() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/structs.kt");
}
@Test
@TestMetadata("types.kt")
public void testTypes() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/types.kt");
}
@Test
@TestMetadata("union.kt")
public void testUnion() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/union.kt");
}
@Test
@TestMetadata("unsupported.kt")
public void testUnsupported() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/unsupported.kt");
}
@Test
@TestMetadata("values.kt")
public void testValues() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/values.kt");
}
@Test
@TestMetadata("vectors.kt")
public void testVectors() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/vectors.kt");
}
}
@Nested
@TestMetadata("compiler/testData/codegen/box/cinterop/exceptions")
@TestDataPath("$PROJECT_ROOT")
@UseExtTestCaseGroupProvider()
public class Exceptions {
@Test
public void testAllFilesPresentInExceptions() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop/exceptions"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.NATIVE, true);
}
@Test
@TestMetadata("cCallback.kt")
public void testCCallback() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/exceptions/cCallback.kt");
}
}
}
@Nested
@@ -4868,6 +4868,128 @@ public class NativeCodegenBoxTestNoPLGenerated extends AbstractNativeCodegenBoxT
public void testToKString() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/toKString.kt");
}
@Nested
@TestMetadata("compiler/testData/codegen/box/cinterop/basics")
@TestDataPath("$PROJECT_ROOT")
@UseExtTestCaseGroupProvider()
@UsePartialLinkage(mode = Mode.DISABLED)
@Tag("no-partial-linkage-may-be-skipped")
public class Basics {
@Test
@TestMetadata("1.kt")
public void test1() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/1.kt");
}
@Test
@TestMetadata("3.kt")
public void test3() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/3.kt");
}
@Test
public void testAllFilesPresentInBasics() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop/basics"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.NATIVE, true);
}
@Test
@TestMetadata("available_processors.kt")
public void testAvailable_processors() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/available_processors.kt");
}
@Test
@TestMetadata("macros.kt")
public void testMacros() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/macros.kt");
}
@Test
@TestMetadata("mangling.kt")
public void testMangling() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/mangling.kt");
}
@Test
@TestMetadata("mangling2.kt")
public void testMangling2() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/mangling2.kt");
}
@Test
@TestMetadata("mangling_keywords.kt")
public void testMangling_keywords() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/mangling_keywords.kt");
}
@Test
@TestMetadata("mangling_keywords2.kt")
public void testMangling_keywords2() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/mangling_keywords2.kt");
}
@Test
@TestMetadata("structAnonym.kt")
public void testStructAnonym() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/structAnonym.kt");
}
@Test
@TestMetadata("structs.kt")
public void testStructs() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/structs.kt");
}
@Test
@TestMetadata("types.kt")
public void testTypes() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/types.kt");
}
@Test
@TestMetadata("union.kt")
public void testUnion() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/union.kt");
}
@Test
@TestMetadata("unsupported.kt")
public void testUnsupported() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/unsupported.kt");
}
@Test
@TestMetadata("values.kt")
public void testValues() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/values.kt");
}
@Test
@TestMetadata("vectors.kt")
public void testVectors() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/basics/vectors.kt");
}
}
@Nested
@TestMetadata("compiler/testData/codegen/box/cinterop/exceptions")
@TestDataPath("$PROJECT_ROOT")
@UseExtTestCaseGroupProvider()
@UsePartialLinkage(mode = Mode.DISABLED)
@Tag("no-partial-linkage-may-be-skipped")
public class Exceptions {
@Test
public void testAllFilesPresentInExceptions() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop/exceptions"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.NATIVE, true);
}
@Test
@TestMetadata("cCallback.kt")
public void testCCallback() throws Exception {
runTest("compiler/testData/codegen/box/cinterop/exceptions/cCallback.kt");
}
}
}
@Nested
@@ -4592,6 +4592,26 @@ public class FirWasmCodegenBoxTestGenerated extends AbstractFirWasmCodegenBoxTes
public void testAllFilesPresentInCinterop() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop"), Pattern.compile("^([^_](.+))\\.kt$"), null, TargetBackend.WASM, true);
}
@Nested
@TestMetadata("compiler/testData/codegen/box/cinterop/basics")
@TestDataPath("$PROJECT_ROOT")
public class Basics {
@Test
public void testAllFilesPresentInBasics() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop/basics"), Pattern.compile("^([^_](.+))\\.kt$"), null, TargetBackend.WASM, true);
}
}
@Nested
@TestMetadata("compiler/testData/codegen/box/cinterop/exceptions")
@TestDataPath("$PROJECT_ROOT")
public class Exceptions {
@Test
public void testAllFilesPresentInExceptions() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop/exceptions"), Pattern.compile("^([^_](.+))\\.kt$"), null, TargetBackend.WASM, true);
}
}
}
@Nested
@@ -4592,6 +4592,26 @@ public class K1WasmCodegenBoxTestGenerated extends AbstractK1WasmCodegenBoxTest
public void testAllFilesPresentInCinterop() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop"), Pattern.compile("^([^_](.+))\\.kt$"), null, TargetBackend.WASM, true);
}
@Nested
@TestMetadata("compiler/testData/codegen/box/cinterop/basics")
@TestDataPath("$PROJECT_ROOT")
public class Basics {
@Test
public void testAllFilesPresentInBasics() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop/basics"), Pattern.compile("^([^_](.+))\\.kt$"), null, TargetBackend.WASM, true);
}
}
@Nested
@TestMetadata("compiler/testData/codegen/box/cinterop/exceptions")
@TestDataPath("$PROJECT_ROOT")
public class Exceptions {
@Test
public void testAllFilesPresentInExceptions() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop/exceptions"), Pattern.compile("^([^_](.+))\\.kt$"), null, TargetBackend.WASM, true);
}
}
}
@Nested