Skip to content

Commit 1429cf3

Browse files
committed
[nfc][cxx-interop] Add some tests for calling C++ methods.
Finally 😅
1 parent cb27114 commit 1429cf3

File tree

4 files changed

+109
-0
lines changed

4 files changed

+109
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#ifndef TEST_INTEROP_CXX_CLASS_METHOD_METHODS_H
2+
#define TEST_INTEROP_CXX_CLASS_METHOD_METHODS_H
3+
4+
struct NonTrivialInWrapper {
5+
int value;
6+
7+
~NonTrivialInWrapper() { }
8+
};
9+
10+
struct HasMethods {
11+
void nonConstMethod() { }
12+
void constMethod() const { }
13+
14+
int nonConstPassThrough(int a) { return a; }
15+
int constPassThrough(int a) const { return a; }
16+
17+
int nonConstSum(int a, int b) { return a + b; }
18+
int constSum(int a, int b) const { return a + b; }
19+
20+
int nonConstSum(NonTrivialInWrapper a, NonTrivialInWrapper b) { return a.value + b.value; }
21+
int constSum(NonTrivialInWrapper a, NonTrivialInWrapper b) const { return a.value + b.value; }
22+
23+
NonTrivialInWrapper nonConstSumAsWrapper(NonTrivialInWrapper a, NonTrivialInWrapper b) { return {a.value + b.value}; }
24+
NonTrivialInWrapper constSumAsWrapper(NonTrivialInWrapper a, NonTrivialInWrapper b) const { return {a.value + b.value}; }
25+
26+
NonTrivialInWrapper nonConstPassThroughAsWrapper(int a) { return {a}; }
27+
NonTrivialInWrapper constPassThroughAsWrapper(int a) const { return {a}; }
28+
};
29+
30+
#endif // TEST_INTEROP_CXX_CLASS_METHOD_METHODS_H
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module Methods {
2+
header "methods.h"
3+
requires cplusplus
4+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// RUN: %target-swift-ide-test -print-module -module-to-print=Methods -I %S/Inputs -source-filename=x -enable-cxx-interop | %FileCheck %s
2+
3+
// CHECK: mutating func nonConstMethod()
4+
// CHECK: func constMethod()
5+
6+
// CHECK: mutating func nonConstPassThrough(_ a: Int32) -> Int32
7+
// CHECK: func constPassThrough(_ a: Int32) -> Int32
8+
9+
// CHECK: mutating func nonConstSum(_ a: Int32, _ b: Int32) -> Int32
10+
// CHECK: func constSum(_ a: Int32, _ b: Int32) -> Int32
11+
12+
// CHECK: mutating func nonConstSum(_ a: NonTrivialInWrapper, _ b: NonTrivialInWrapper) -> Int32
13+
// CHECK: func constSum(_ a: NonTrivialInWrapper, _ b: NonTrivialInWrapper) -> Int32
14+
15+
// CHECK: mutating func nonConstSumAsWrapper(_ a: NonTrivialInWrapper, _ b: NonTrivialInWrapper) -> NonTrivialInWrapper
16+
// CHECK: func constSumAsWrapper(_ a: NonTrivialInWrapper, _ b: NonTrivialInWrapper) -> NonTrivialInWrapper
17+
18+
// CHECK: mutating func nonConstPassThroughAsWrapper(_ a: Int32) -> NonTrivialInWrapper
19+
// CHECK: func constPassThroughAsWrapper(_ a: Int32) -> NonTrivialInWrapper
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// RUN: %target-run-simple-swift(-I %S/Inputs/ -Xfrontend -enable-cxx-interop)
2+
//
3+
// REQUIRES: executable_test
4+
//
5+
// Crash when running on windows: rdar://88391102
6+
// XFAIL: OS=windows-msvc
7+
8+
import StdlibUnittest
9+
import Methods
10+
11+
var CxxMethodTestSuite = TestSuite("CxxMethods")
12+
13+
CxxMethodTestSuite.test("() -> Void") {
14+
var instance = HasMethods()
15+
16+
instance.nonConstMethod()
17+
instance.constMethod()
18+
}
19+
20+
CxxMethodTestSuite.test("(Int) -> Int") {
21+
var instance = HasMethods()
22+
23+
expectEqual(42, instance.nonConstPassThrough(42))
24+
expectEqual(42, instance.constPassThrough(42))
25+
}
26+
27+
CxxMethodTestSuite.test("(Int, Int) -> Int") {
28+
var instance = HasMethods()
29+
30+
expectEqual(42, instance.nonConstSum(40, 2))
31+
expectEqual(42, instance.constSum(40, 2))
32+
}
33+
34+
// This causes a crash: rdar://88354445
35+
// CxxMethodTestSuite.test("(NonTrivialInWrapper, NonTrivialInWrapper) -> Int") {
36+
// var instance = HasMethods()
37+
//
38+
// expectEqual(42, instance.nonConstSum(NonTrivialInWrapper(value: 40), NonTrivialInWrapper(value: 2)))
39+
// expectEqual(42, instance.constSum(NonTrivialInWrapper(value: 40), NonTrivialInWrapper(value: 2)))
40+
// }
41+
//
42+
// CxxMethodTestSuite.test("(NonTrivialInWrapper, NonTrivialInWrapper) -> NonTrivialInWrapper") {
43+
// var instance = HasMethods()
44+
//
45+
// expectEqual(42, instance.nonConstSumAsWrapper(NonTrivialInWrapper(value: 40), NonTrivialInWrapper(value: 2)).value)
46+
// expectEqual(42, instance.constSumAsWrapper(NonTrivialInWrapper(value: 40), NonTrivialInWrapper(value: 2)).value)
47+
// }
48+
49+
CxxMethodTestSuite.test("(Int) -> NonTrivialInWrapper") {
50+
var instance = HasMethods()
51+
52+
expectEqual(42, instance.nonConstPassThroughAsWrapper(42).value)
53+
expectEqual(42, instance.constPassThroughAsWrapper(42).value)
54+
}
55+
56+
runAllTests()

0 commit comments

Comments
 (0)