Skip to content

Commit 4ba62e1

Browse files
zoecarverhyp
authored andcommitted
[cxx-interop] Add tests for std::unique_ptr.
1 parent e738bdf commit 4ba62e1

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,8 @@ module MsvcUseVecIt {
2828
requires cplusplus
2929
export *
3030
}
31+
32+
module StdUniquePtr {
33+
header "std-unique-ptr.h"
34+
requires cplusplus
35+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#ifndef TEST_INTEROP_CXX_STDLIB_INPUTS_STD_UNIQUE_PTR_H
2+
#define TEST_INTEROP_CXX_STDLIB_INPUTS_STD_UNIQUE_PTR_H
3+
4+
#include <memory>
5+
6+
std::unique_ptr<int> makeInt() {
7+
return std::make_unique<int>(42);
8+
}
9+
10+
std::unique_ptr<int[]> makeArray() {
11+
int *array = new int[3];
12+
array[0] = 1;
13+
array[1] = 2;
14+
array[2] = 3;
15+
return std::unique_ptr<int[]>(array);
16+
}
17+
18+
static bool dtorCalled = false;
19+
struct HasDtor {
20+
~HasDtor() {
21+
dtorCalled = true;
22+
}
23+
};
24+
25+
std::unique_ptr<HasDtor> makeDtor() {
26+
return std::make_unique<HasDtor>();
27+
}
28+
29+
#endif // TEST_INTEROP_CXX_STDLIB_INPUTS_STD_UNIQUE_PTR_H
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-experimental-cxx-interop)
2+
//
3+
// REQUIRES: executable_test
4+
5+
import StdlibUnittest
6+
import StdUniquePtr
7+
#if os(Linux)
8+
import CxxStdlib
9+
// FIXME: import CxxStdlib.string once libstdc++ is split into submodules.
10+
#else
11+
import CxxStdlib.memory
12+
#endif
13+
14+
var StdUniquePtrTestSuite = TestSuite("StdUniquePtr")
15+
16+
StdUniquePtrTestSuite.test("int") {
17+
let u = makeInt()
18+
expectEqual(u.pointee, 42)
19+
}
20+
21+
StdUniquePtrTestSuite.test("array") {
22+
var u = makeArray()
23+
expectEqual(u[0], 1)
24+
// Over consume:
25+
// expectEqual(u[1], 2)
26+
// expectEqual(u[2], 3)
27+
// Crash:
28+
// u[0] = 10
29+
// expectEqual(u[0], 10)
30+
}
31+
32+
StdUniquePtrTestSuite.test("custom dtor") {
33+
expectEqual(dtorCalled, false)
34+
let c = {
35+
_ = makeDtor()
36+
}
37+
c()
38+
expectEqual(dtorCalled, true)
39+
}
40+
41+
runAllTests()
42+

0 commit comments

Comments
 (0)