Skip to content

Commit 2d0863a

Browse files
committed
[cxx-interop] Initial tests for std::function usage
This makes sure that `std::function` is imported consistently on supported platforms, and that it allows basic usage: calling a function with `callAsFunction`, initializing an empty function, and passing a function retrieved from C++ back to C++ as a parameter. rdar://103979602
1 parent 9071c82 commit 2d0863a

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

test/Interop/Cxx/stdlib/Inputs/module.modulemap

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,8 @@ module StdUniquePtr {
3333
header "std-unique-ptr.h"
3434
requires cplusplus
3535
}
36+
37+
module StdFunction {
38+
header "std-function.h"
39+
requires cplusplus
40+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef TEST_INTEROP_CXX_STDLIB_INPUTS_STD_FUNCTION_H
2+
#define TEST_INTEROP_CXX_STDLIB_INPUTS_STD_FUNCTION_H
3+
4+
#include <functional>
5+
6+
using FunctionIntToInt = std::function<int(int)>;
7+
8+
FunctionIntToInt getIdentityFunction() {
9+
return [](int x) { return x; };
10+
}
11+
12+
bool isEmptyFunction(FunctionIntToInt f) { return !(bool)f; }
13+
14+
int invokeFunction(FunctionIntToInt f, int x) { return f(x); }
15+
16+
#endif // TEST_INTEROP_CXX_STDLIB_INPUTS_STD_FUNCTION_H
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-experimental-cxx-interop)
2+
// RUN: %target-run-simple-swift(-I %S/Inputs -cxx-interoperability-mode=upcoming-swift)
3+
4+
// REQUIRES: executable_test
5+
6+
import StdlibUnittest
7+
import StdFunction
8+
9+
var StdFunctionTestSuite = TestSuite("StdFunction")
10+
11+
StdFunctionTestSuite.test("init empty") {
12+
let f = FunctionIntToInt()
13+
expectTrue(isEmptyFunction(f))
14+
15+
let copied = f
16+
expectTrue(isEmptyFunction(copied))
17+
}
18+
19+
StdFunctionTestSuite.test("call") {
20+
let f = getIdentityFunction()
21+
expectEqual(123, f(123))
22+
}
23+
24+
StdFunctionTestSuite.test("retrieve and pass back as parameter") {
25+
let res = invokeFunction(getIdentityFunction(), 456)
26+
expectEqual(456, res)
27+
}
28+
29+
runAllTests()

0 commit comments

Comments
 (0)