Skip to content

Commit c8ccd4f

Browse files
committed
[cxx-interop] add some initial move only tests
1 parent 4ad8fed commit c8ccd4f

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module MoveOnlyCxxValueType {
2+
header "move-only-cxx-value-type.h"
3+
requires cplusplus
4+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#ifndef TEST_INTEROP_CXX_CLASS_MOVE_ONLY_VT_H
2+
#define TEST_INTEROP_CXX_CLASS_MOVE_ONLY_VT_H
3+
4+
#include <memory>
5+
6+
struct Copyable {
7+
int x;
8+
};
9+
10+
struct NonCopyable {
11+
inline NonCopyable(int x) : x(x) {}
12+
inline NonCopyable(const NonCopyable &) = delete;
13+
inline NonCopyable(NonCopyable &&other) : x(other.x) {}
14+
15+
inline int method(int y) const { return x * y; }
16+
inline int mutMethod(int y) {
17+
x = y;
18+
return y;
19+
}
20+
private:
21+
int x;
22+
};
23+
24+
struct NonCopyableHolder {
25+
inline NonCopyableHolder(int x) : x(x) {}
26+
inline NonCopyableHolder(const NonCopyableHolder &) = delete;
27+
inline NonCopyableHolder(NonCopyableHolder &&other) : x(std::move(other.x)) {}
28+
29+
inline NonCopyable &returnMutNonCopyableRef() { return x; }
30+
31+
inline const NonCopyable &returnNonCopyableRef() const { return x; }
32+
private:
33+
NonCopyable x;
34+
};
35+
36+
#endif // TEST_INTEROP_CXX_CLASS_MOVE_ONLY_VT_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 -enable-experimental-feature NoncopyableGenerics)
2+
//
3+
// REQUIRES: executable_test
4+
5+
import MoveOnlyCxxValueType
6+
import StdlibUnittest
7+
8+
var MoveOnlyCxxValueType = TestSuite("Move Only Value Types + Generics")
9+
10+
func borrowNC(_ x: borrowing NonCopyable) -> CInt {
11+
return x.method(3)
12+
}
13+
14+
func inoutNC(_ x: inout NonCopyable, _ y: CInt) -> CInt {
15+
return x.mutMethod(y)
16+
}
17+
18+
MoveOnlyCxxValueType.test("Test move only type ref return pointee borrow") {
19+
var holder = NonCopyableHolder(11)
20+
var k = borrowNC(holder.__returnNonCopyableRefUnsafe().pointee)
21+
expectEqual(k, 33)
22+
k = inoutNC(&holder.__returnMutNonCopyableRefUnsafe().pointee, 2)
23+
expectEqual(k, 2)
24+
k = borrowNC(holder.__returnMutNonCopyableRefUnsafe().pointee)
25+
expectEqual(k, 6)
26+
}
27+
28+
runAllTests()
29+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// RUN: %target-run-simple-swift(-I %S/Inputs/ -Xfrontend -enable-experimental-cxx-interop)
2+
//
3+
// REQUIRES: executable_test
4+
5+
import MoveOnlyCxxValueType
6+
import StdlibUnittest
7+
8+
var MoveOnlyCxxValueType = TestSuite("Move Only Value Types")
9+
10+
MoveOnlyCxxValueType.test("Test move only type member access") {
11+
var c = NonCopyable(2)
12+
let k = c.method(-2)
13+
expectEqual(k, -4)
14+
expectEqual(c.method(1), 2)
15+
}
16+
17+
runAllTests()
18+

0 commit comments

Comments
 (0)