Skip to content

Commit b23e91a

Browse files
committed
[out_ptr] Added tests
1 parent 4bc779e commit b23e91a

File tree

7 files changed

+256
-229
lines changed

7 files changed

+256
-229
lines changed

libcxx/test/std/utilities/smartptr/adapt/out.ptr/out.ptr.t.deref.pass.cpp renamed to libcxx/test/std/utilities/smartptr/adapt/inout.ptr/inout.ptr.compile.pass.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,25 @@
1010

1111
// <memory>
1212

13-
// [out.ptr.t], class template out_ptr_t
14-
// template<class Smart, class Pointer, class... Args>
15-
// class out_ptr_t; // since c++23
16-
17-
// operator Pointer*() const noexcept;
18-
// operator void**() const noexcept;
13+
// [inout.ptr], function template inout_ptr
14+
// template<class Pointer = void, class Smart, class... Args>
15+
// auto inout_ptr(Smart& s, Args&&... args); // since c++23
1916

2017
#include <memory>
2118

2219
int main(int, char**) {
2320
{
2421
std::unique_ptr<int> uPtr;
2522

26-
std::out_ptr_t<std::unique_ptr<int>, int*>{uPtr};
23+
auto inoutUPtr1 = std::inout_ptr(uPtr);
24+
auto inoutUPtr2 = std::inout_ptr<int*>(uPtr);
2725
}
2826
{
29-
std::unique_ptr<int, std::default_delete<int>> uPtr;
27+
auto deleter = [](auto* p) { delete p; };
28+
std::unique_ptr<int, decltype(deleter)> uPtr;
3029

31-
std::out_ptr_t<decltype(uPtr), int*, std::default_delete<int>>{uPtr, std::default_delete<int>{}};
30+
auto inoutUPtr1 = std::inout_ptr(uPtr, deleter);
31+
auto inoutUPtr2 = std::inout_ptr<int*>(uPtr, deleter);
3232
}
3333

3434
return 0;

libcxx/test/std/utilities/smartptr/adapt/inout.ptr/inout.ptr.general.pass.cpp

Lines changed: 173 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,188 @@
1414
// template<class Pointer = void, class Smart, class... Args>
1515
// auto inout_ptr(Smart& s, Args&&... args); // since c++23
1616

17+
#include <cassert>
1718
#include <memory>
1819

19-
int main(int, char**) {
20+
#include "../types.h"
21+
22+
// Test helpers.
23+
24+
void replace_int_p(int** pp) {
25+
assert(**pp == 90);
26+
delete *pp;
27+
*pp = new int{84};
28+
}
29+
30+
void replace_int_p_with_nullptr(int** pp) {
31+
assert(**pp == 90);
32+
delete *pp;
33+
*pp = nullptr;
34+
}
35+
36+
void replace_nullptr_with_int_p(int** pp) {
37+
assert(*pp == nullptr);
38+
*pp = new int{84};
39+
}
40+
41+
void replace_int_void_p(void** pp) {
42+
assert(*(static_cast<int*>(*pp)) == 90);
43+
delete static_cast<int*>(*pp);
44+
*pp = new int{84};
45+
}
46+
47+
void replace_int_void_p_with_nullptr(void** pp) {
48+
assert(*(static_cast<int*>(*pp)) == 90);
49+
delete static_cast<int*>(*pp);
50+
*pp = nullptr;
51+
}
52+
53+
void replace_nullptr_with_int_void_p(void** pp) {
54+
assert(*pp == nullptr);
55+
*pp = new int{84};
56+
}
57+
58+
void replace_SomeInt_p(SomeInt** pp) {
59+
auto si = **pp;
60+
assert(si.value == 90);
61+
delete static_cast<SomeInt*>(*pp);
62+
*pp = new SomeInt{9084};
63+
}
64+
65+
void replace_SomeInt_void_p(void** pp) {
66+
assert(reinterpret_cast<SomeInt*>(*pp)->value == 90);
67+
delete static_cast<SomeInt*>(*pp);
68+
*pp = reinterpret_cast<void*>(new SomeInt{9084});
69+
}
70+
71+
// Test `std::inout_ptr()` function.
72+
73+
void test_raw_ptr() {
74+
{
75+
auto rPtr = new int{90};
76+
77+
replace_int_p(std::inout_ptr<int*>(rPtr));
78+
assert(*rPtr == 84);
79+
80+
delete rPtr;
81+
}
82+
{
83+
auto rPtr = new int{90};
84+
85+
replace_int_p_with_nullptr(std::inout_ptr<int*>(rPtr));
86+
assert(rPtr == nullptr);
87+
}
88+
{
89+
int* rPtr = nullptr;
90+
91+
replace_nullptr_with_int_p(std::inout_ptr<int*>(rPtr));
92+
assert(*rPtr == 84);
93+
delete rPtr;
94+
}
95+
{
96+
auto rPtr = new int{90};
97+
98+
replace_int_void_p(std::inout_ptr<int*>(rPtr));
99+
assert(*rPtr == 84);
100+
delete rPtr;
101+
}
102+
{
103+
auto rPtr = new int{90};
104+
105+
replace_int_void_p_with_nullptr(std::inout_ptr<int*>(rPtr));
106+
assert(rPtr == nullptr);
107+
}
108+
{
109+
int* rPtr = nullptr;
110+
111+
replace_nullptr_with_int_void_p(std::inout_ptr<int*>(rPtr));
112+
assert(*rPtr == 84);
113+
delete rPtr;
114+
}
115+
{
116+
auto* rPtr = new SomeInt{90};
117+
118+
replace_SomeInt_p(std::inout_ptr(rPtr));
119+
assert(rPtr->value == 9084);
120+
delete rPtr;
121+
}
122+
{
123+
auto* rPtr = new SomeInt{90};
124+
125+
replace_SomeInt_void_p(std::inout_ptr<SomeInt*>(rPtr));
126+
assert(rPtr->value == 9084);
127+
delete rPtr;
128+
}
129+
}
130+
131+
void test_unique_ptr() {
132+
{
133+
auto uPtr = std::make_unique<int>(90);
134+
135+
replace_int_p(std::inout_ptr(uPtr));
136+
assert(*uPtr == 84);
137+
}
20138
{
21139
std::unique_ptr<int> uPtr;
22140

23-
auto inoutUPtr1 = std::inout_ptr(uPtr);
24-
(void)inoutUPtr1;
25-
auto inoutUPtr2 = std::inout_ptr<int*>(uPtr);
26-
(void)inoutUPtr2;
141+
replace_nullptr_with_int_p(std::inout_ptr(uPtr));
142+
assert(*uPtr == 84);
27143
}
28144
{
29-
auto deleter = [](auto* p) { delete p; };
30-
std::unique_ptr<int, decltype(deleter)> uPtr;
145+
auto uPtr = std::make_unique<int>(90);
31146

32-
auto inoutUPtr1 = std::inout_ptr(uPtr, deleter);
33-
(void)inoutUPtr1;
34-
auto inoutUPtr2 = std::inout_ptr<int*>(uPtr, deleter);
35-
(void)inoutUPtr2;
147+
replace_int_void_p(std::inout_ptr(uPtr));
148+
assert(*uPtr == 84);
36149
}
150+
{
151+
std::unique_ptr<int> uPtr;
152+
153+
replace_nullptr_with_int_void_p(std::inout_ptr(uPtr));
154+
assert(*uPtr == 84);
155+
}
156+
{
157+
auto uPtr = std::make_unique<SomeInt>(90);
158+
159+
replace_SomeInt_p(std::inout_ptr(uPtr));
160+
assert(uPtr->value == 9084);
161+
}
162+
{
163+
auto uPtr = std::make_unique<SomeInt>(90);
164+
165+
replace_SomeInt_void_p(std::inout_ptr<SomeInt*>(uPtr));
166+
assert(uPtr->value == 9084);
167+
}
168+
}
169+
170+
void test_custom_ptr() {
171+
// ConstructiblePtr
172+
{
173+
ConstructiblePtr<int> cPtr(new int{90});
174+
175+
replace_int_p(std::inout_ptr(cPtr));
176+
assert(cPtr == 84);
177+
}
178+
// ResettablePtr
179+
{
180+
ResettablePtr<int> rPtr(new int{90});
181+
182+
replace_int_p(std::inout_ptr(rPtr));
183+
assert(rPtr == 84);
184+
}
185+
// NonConstructiblePtr
186+
{
187+
NonConstructiblePtr<int> nPtr;
188+
nPtr.reset(new int{90});
189+
190+
replace_int_p(std::inout_ptr(nPtr));
191+
assert(nPtr == 84);
192+
}
193+
}
194+
195+
int main(int, char**) {
196+
test_raw_ptr();
197+
test_unique_ptr();
198+
test_custom_ptr();
37199

38200
return 0;
39201
}

0 commit comments

Comments
 (0)