Skip to content

Commit 3d80541

Browse files
tangalbert919jpakkane
authored andcommitted
test cases: Test Swift interoperability with C++
Skip if Swift 5.9 or above is not detected.
1 parent 6925011 commit 3d80541

File tree

8 files changed

+113
-0
lines changed

8 files changed

+113
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
testCallFromSwift()
2+
testCallWithParam("Calling C++ function from Swift with param is working")
3+
4+
var test = Test()
5+
var testtwo = Test(1)
6+
test.testCallFromClass()
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
project('mixed cpp', 'cpp', 'swift')
2+
3+
swiftc = meson.get_compiler('swift')
4+
5+
# Testing C++ and Swift interoperability requires Swift 5.9
6+
if not swiftc.version().version_compare('>= 5.9')
7+
error('MESON_SKIP_TEST Test requires Swift 5.9')
8+
endif
9+
10+
lib = static_library('mylib', 'mylib.cpp')
11+
exe = executable('prog', 'main.swift', 'mylib.h', link_with: lib)
12+
test('cpp interface', exe)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "mylib.h"
2+
#include <iostream>
3+
4+
Test::Test() {
5+
std::cout << "Test initialized" << std::endl;
6+
}
7+
8+
Test::Test(int param) {
9+
std::cout << "Test initialized with param " << param << std::endl;
10+
}
11+
12+
void Test::testCallFromClass() {
13+
std::cout << "Calling C++ class function from Swift is working" << std::endl;
14+
}
15+
16+
void testCallFromSwift() {
17+
std::cout << "Calling this C++ function from Swift is working" << std::endl;
18+
}
19+
20+
void testCallWithParam(const std::string &param) {
21+
std::cout << param << std::endl;
22+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
#include <string>
3+
4+
class Test {
5+
public:
6+
Test();
7+
Test(int param);
8+
9+
void testCallFromClass();
10+
};
11+
12+
void testCallFromSwift();
13+
void testCallWithParam(const std::string &param);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
var test: ObjCPPTest = ObjCPPTest()
2+
test.testCallToObjCPP()
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
project('mixed objcpp', 'objcpp', 'swift')
2+
3+
swiftc = meson.get_compiler('swift')
4+
5+
# Testing Objective-C++ and Swift interoperability requires Swift 5.9
6+
if not swiftc.version().version_compare('>= 5.9')
7+
error('MESON_SKIP_TEST Test requires Swift 5.9')
8+
endif
9+
10+
lib = static_library('mylib', 'mylib.mm')
11+
exe = executable('prog', 'main.swift', 'mylib.h', link_with: lib)
12+
test('objcpp interface', exe)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
#import <Foundation/Foundation.h>
3+
4+
class Test {
5+
public:
6+
Test();
7+
8+
void testCallFromClass();
9+
};
10+
11+
@interface ObjCPPTest: NSObject {
12+
@private Test *test;
13+
}
14+
- (id)init;
15+
- (void)dealloc;
16+
- (void)testCallToObjCPP;
17+
@end
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "mylib.h"
2+
#include <iostream>
3+
4+
Test::Test() {
5+
std::cout << "Test initialized" << std::endl;
6+
}
7+
8+
void Test::testCallFromClass() {
9+
std::cout << "Calling Objective-C++ class function from Swift is working" << std::endl;
10+
}
11+
12+
@implementation ObjCPPTest
13+
- (id)init {
14+
self = [super init];
15+
if (self) {
16+
test = new Test();
17+
}
18+
return self;
19+
}
20+
21+
- (void)dealloc {
22+
delete test;
23+
[super dealloc];
24+
}
25+
26+
- (void)testCallToObjCPP {
27+
test->testCallFromClass();
28+
}
29+
@end

0 commit comments

Comments
 (0)