From a50a828d81b508195ce2ae624c5bc556f9422b70 Mon Sep 17 00:00:00 2001 From: Sergey Bogolepov Date: Mon, 16 Sep 2019 18:10:27 +0700 Subject: [PATCH] Add tests for objc_msgSend* family. --- backend.native/tests/build.gradle | 23 +++++++ .../tests/interop/objc/msg_send/main.kt | 47 ++++++++++++++ .../tests/interop/objc/msg_send/messaging.def | 2 + .../tests/interop/objc/msg_send/messaging.h | 56 ++++++++++++++++ .../tests/interop/objc/msg_send/messaging.m | 64 +++++++++++++++++++ 5 files changed, 192 insertions(+) create mode 100644 backend.native/tests/interop/objc/msg_send/main.kt create mode 100644 backend.native/tests/interop/objc/msg_send/messaging.def create mode 100644 backend.native/tests/interop/objc/msg_send/messaging.h create mode 100644 backend.native/tests/interop/objc/msg_send/messaging.m diff --git a/backend.native/tests/build.gradle b/backend.native/tests/build.gradle index a5f867c5735..f5d139faa51 100644 --- a/backend.native/tests/build.gradle +++ b/backend.native/tests/build.gradle @@ -3305,6 +3305,12 @@ if (PlatformInfo.isAppleTarget(project)) { it.headers "$projectDir/interop/objc_with_initializer/objc_misc.h" it.linkerOpts "-L$buildDir", "-lobjcmisc" } + + createInterop("objcMessaging") { + it.defFile 'interop/objc/msg_send/messaging.def' + it.headers "$projectDir/interop/objc/msg_send/messaging.h" + it.linkerOpts "-L$buildDir", "-lobjcmessaging" + } } createInterop("withSpaces") { @@ -3528,6 +3534,23 @@ interopTest("interop_objc_global_initializer") { } } +interopTest("interop_objc_msg_send") { + disabled = !isAppleTarget(project) + + source = "interop/objc/msg_send/main.kt" + interop = 'objcMessaging' + + doBefore { + execKonanClang(project.target) { + args "$projectDir/interop/objc/msg_send/messaging.m" + args "-lobjc", '-fobjc-arc' + args '-fPIC', '-shared', '-o', "$buildDir/libobjcmessaging.dylib" + } + if (project.target instanceof KonanTarget.IOS_X64) { + UtilsKt.codesign(project, "$buildDir/libobjcmessaging.dylib") + } + } +} standaloneTest("jsinterop_math") { doBefore { diff --git a/backend.native/tests/interop/objc/msg_send/main.kt b/backend.native/tests/interop/objc/msg_send/main.kt new file mode 100644 index 00000000000..c16c1dbb7f9 --- /dev/null +++ b/backend.native/tests/interop/objc/msg_send/main.kt @@ -0,0 +1,47 @@ +import messaging.* +import kotlinx.cinterop.* +import kotlin.test.* + +fun main(args: Array) { + autoreleasepool { + primitives() + aggregates() + } +} + +private fun primitives() { + assertEquals(3.14f, PrimitiveTestSubject.floatFn()) + assertEquals(3.14, PrimitiveTestSubject.doubleFn()) + assertEquals(42, PrimitiveTestSubject.intFn()) +} + +private fun aggregates() { + AggregateTestSubject.singleFloatFn().useContents { + assertEquals(3.14f, f) + } + AggregateTestSubject.simplePackedFn().useContents { + assertEquals('0'.toByte(), f1) + assertEquals(111, f2) + } + AggregateTestSubject.evenSmallerPackedFn().useContents { + assertEquals('x'.toByte(), x) + assertEquals(169, y) + assertEquals('z'.toByte(), z) + } + AggregateTestSubject.homogeneousBigFn().useContents { + assertEquals(1.0f, f1) + assertEquals(2.0f, f2) + assertEquals(3.0f, f3) + assertEquals(4.0f, f4) + assertEquals(5.0f, f5) + assertEquals(6.0f, f6) + assertEquals(7.0f, f7) + assertEquals(8.0f, f8) + } + AggregateTestSubject.homogeneousSmallFn().useContents { + assertEquals(1.0f, f1) + assertEquals(2.0f, f2) + assertEquals(3.0f, f3) + assertEquals(4.0f, f4) + } +} \ No newline at end of file diff --git a/backend.native/tests/interop/objc/msg_send/messaging.def b/backend.native/tests/interop/objc/msg_send/messaging.def new file mode 100644 index 00000000000..7161e3ce585 --- /dev/null +++ b/backend.native/tests/interop/objc/msg_send/messaging.def @@ -0,0 +1,2 @@ +language = Objective-C +headerFilter = **/messaging.h \ No newline at end of file diff --git a/backend.native/tests/interop/objc/msg_send/messaging.h b/backend.native/tests/interop/objc/msg_send/messaging.h new file mode 100644 index 00000000000..12433706251 --- /dev/null +++ b/backend.native/tests/interop/objc/msg_send/messaging.h @@ -0,0 +1,56 @@ +#import + +@interface PrimitiveTestSubject : NSObject + ++ (int)intFn; ++ (float)floatFn; ++ (double)doubleFn; + +@end; + +typedef struct { + float f; +} SingleFloat; + +typedef struct __attribute__((packed)) { + char f1; + short f2; + char f3; + char f4; +} SimplePacked; + +typedef struct __attribute__((packed)) { + char x; + short y; + char z; +} EvenSmallerPacked; + +typedef struct { + float f1; + float f2; + float f3; + float f4; +} HomogeneousSmall; + +typedef struct { + float f1; + float f2; + float f3; + float f4; + float f5; + float f6; + float f7; + float f8; +} HomogeneousBig; + +// TODO: Add more cases later: SIMD, bitfields. + +@interface AggregateTestSubject : NSObject + ++ (SingleFloat)singleFloatFn; ++ (SimplePacked)simplePackedFn; ++ (EvenSmallerPacked)evenSmallerPackedFn; ++ (HomogeneousSmall)homogeneousSmallFn; ++ (HomogeneousBig)homogeneousBigFn; + +@end; \ No newline at end of file diff --git a/backend.native/tests/interop/objc/msg_send/messaging.m b/backend.native/tests/interop/objc/msg_send/messaging.m new file mode 100644 index 00000000000..5b5ff5bad45 --- /dev/null +++ b/backend.native/tests/interop/objc/msg_send/messaging.m @@ -0,0 +1,64 @@ +#import "messaging.h" + +@implementation PrimitiveTestSubject + ++ (int)intFn { + return 42; +} + ++ (float)floatFn { + return 3.14f; +} + ++ (double)doubleFn { + return 3.14; +} + +@end; + +@implementation AggregateTestSubject + ++ (SingleFloat)singleFloatFn { + SingleFloat s; + s.f = 3.14f; + return s; +} + ++ (SimplePacked)simplePackedFn { + SimplePacked s; + s.f1 = '0'; + s.f2 = 111; + return s; +} + ++ (EvenSmallerPacked)evenSmallerPackedFn { + EvenSmallerPacked s; + s.x = 'x'; + s.y = 169; + s.z = 'z'; + return s; +} + ++ (HomogeneousSmall)homogeneousSmallFn { + HomogeneousSmall s; + s.f1 = 1.0f; + s.f2 = 2.0f; + s.f3 = 3.0f; + s.f4 = 4.0f; + return s; +} + ++ (HomogeneousBig)homogeneousBigFn { + HomogeneousBig s; + s.f1 = 1.0f; + s.f2 = 2.0f; + s.f3 = 3.0f; + s.f4 = 4.0f; + s.f5 = 5.0f; + s.f6 = 6.0f; + s.f7 = 7.0f; + s.f8 = 8.0f; + return s; +} + +@end; \ No newline at end of file