Debian Codegraph @

libstdc++-v3/testsuite/20_util/smartptr.adapt/out_ptr/2.cc

raw
1 // { dg-do compile { target c++23 } }
2
3 #include <memory>
4 #include <testsuite_hooks.h>
5
6 int counter = 0;
7
8 void
9 test_unique_ptr()
10 {
11 std::unique_ptr<int> up(new int(1));
12 {
13 auto op = std::out_ptr(up);
14 VERIFY( up == nullptr );
15 int** p = op;
16 VERIFY( *p == nullptr );
17 *p = new int(2);
18
19 const auto& cop = op;
20 VERIFY( static_cast<int**>(cop) == static_cast<int**>(op) );
21 }
22 VERIFY( *up == 2 );
23
24 {
25 std::default_delete<int> d;
26 auto op = std::out_ptr(up, d);
27 VERIFY( up == nullptr );
28 int** p = op;
29 VERIFY( *p == nullptr );
30 *p = new int(3);
31
32 const auto& cop = op;
33 VERIFY( static_cast<int**>(cop) == static_cast<int**>(op) );
34 }
35 VERIFY( *up == 3 );
36
37 struct D
38 {
39 explicit D(int id) : id(id) { }
40 void operator()(long* p) const { ++counter; delete p; }
41 int id;
42 };
43 counter = 0;
44 std::unique_ptr<long, D> upd(new long(1), D(11));
45 {
46 auto op = std::out_ptr(upd);
47 VERIFY( counter == 1 );
48 VERIFY( upd == nullptr );
49 long** p = op;
50 VERIFY( *p == nullptr );
51 *p = new long(4);
52
53 const auto& cop = op;
54 VERIFY( static_cast<long**>(cop) == static_cast<long**>(op) );
55 }
56 VERIFY( *upd == 4 );
57 VERIFY( upd.get_deleter().id == 11 );
58 VERIFY( counter == 1 );
59
60 {
61 D d(33);
62 auto op = std::out_ptr(upd, d);
63 VERIFY( counter == 2 );
64 VERIFY( upd == nullptr );
65 long** p = op;
66 VERIFY( *p == nullptr );
67
68 const auto& cop = op;
69 VERIFY( static_cast<long**>(cop) == static_cast<long**>(op) );
70 }
71 VERIFY( upd == nullptr );
72 VERIFY( upd.get_deleter().id == 11 ); // Deleter not replaced if p is null.
73 VERIFY( counter == 2 );
74
75 {
76 D d(33);
77 auto op = std::out_ptr(upd, d);
78 VERIFY( counter == 2 );
79 VERIFY( upd == nullptr );
80 long** p = op;
81 VERIFY( *p == nullptr );
82 *p = new long(5);
83 }
84 VERIFY( *upd == 5 );
85 VERIFY( upd.get_deleter().id == 33 );
86 VERIFY( counter == 2 );
87
88 struct Base { };
89 struct Derived : Base
90 {
91 Derived(int id) : id(id) { }
92 int id;
93 };
94 std::unique_ptr<Derived> upbd(new Derived(1));
95 {
96 auto op = std::out_ptr<Base*>(upbd);
97 Base** bpp = op;
98 VERIFY( *bpp == nullptr );
99 *bpp = new Derived(2);
100
101 const auto& cop = op;
102 VERIFY( static_cast<Base**>(cop) == static_cast<Base**>(op) );
103 }
104 VERIFY( upbd->id == 2 );
105 }
106
107 void deleter_function(float* p) { delete p; }
108
109 void
110 test_shared_ptr()
111 {
112 using DD = std::default_delete<int>;
113
114 std::shared_ptr<int> sp(new int(1));
115 {
116 auto op = std::out_ptr(sp, DD{});
117 VERIFY( sp == nullptr );
118 int** p = op;
119 VERIFY( *p == nullptr );
120 *p = new int(2);
121
122 const auto& cop = op;
123 VERIFY( static_cast<int**>(cop) == static_cast<int**>(op) );
124 }
125 VERIFY( *sp == 2 );
126
127 {
128 auto op = std::out_ptr(sp, DD{});
129 VERIFY( sp == nullptr );
130 int** p = op;
131 VERIFY( *p == nullptr );
132 *p = new int(3);
133 }
134 VERIFY( *sp == 3 );
135
136 struct D
137 {
138 explicit D(int id) : id(id) { }
139 void operator()(long* p) const { ++counter; delete p; }
140 int id;
141 };
142 counter = 0;
143 std::shared_ptr<long> spd(new long(1), D(11));
144 {
145 auto op = std::out_ptr(spd, D(22));
146 VERIFY( counter == 1 );
147 VERIFY( spd == nullptr );
148 long** p = op;
149 VERIFY( *p == nullptr );
150 *p = new long(5);
151 }
152 VERIFY( counter == 1 );
153 VERIFY( *spd == 5 );
154 VERIFY( std::get_deleter<D>(spd)->id == 22 );
155
156 struct Base { };
157 struct Derived : Base
158 {
159 Derived(int id) : id(id) { }
160 int id;
161 };
162 std::shared_ptr<Derived> spbd(new Derived(1));
163 {
164 auto op = std::out_ptr<Base*>(spbd, std::default_delete<Derived>());
165 Base** bpp = op;
166 VERIFY( *bpp == nullptr );
167 *bpp = new Derived(2);
168 }
169 VERIFY( spbd->id == 2 );
170
171 std::shared_ptr<float> spf;
172 {
173 auto op = std::out_ptr(spf, deleter_function);
174 float** fpp = op;
175 *fpp = new float(0.5);
176 }
177 VERIFY( std::get_deleter<void(*)(float*)>(spf) != nullptr );
178 VERIFY( *std::get_deleter<void(*)(float*)>(spf) == &deleter_function );
179 VERIFY( *spf == 0.5 );
180 }
181
182 void
183 test_custom_ptr()
184 {
185 struct UPtr : std::unique_ptr<int>
186 {
187 using std::unique_ptr<int>::unique_ptr;
188 };
189
190 UPtr up(new int(1));
191 {
192 auto op = std::out_ptr(up);
193 VERIFY( up == nullptr );
194 int** p = op;
195 VERIFY( *p == nullptr );
196 *p = new int(2);
197
198 const auto& cop = op;
199 VERIFY( static_cast<int**>(cop) == static_cast<int**>(op) );
200 }
201 VERIFY( *up == 2 );
202
203 {
204 auto op = std::out_ptr(up, std::default_delete<int>{});
205 VERIFY( up == nullptr );
206 int** p = op;
207 VERIFY( *p == nullptr );
208 *p = new int(3);
209 }
210 VERIFY( *up == 3 );
211
212 struct D
213 {
214 explicit D(int id) : id(id) { }
215 void operator()(long* p) const { ++counter; delete p; }
216 int id;
217 };
218 counter = 0;
219 struct UDPtr : std::unique_ptr<long, D>
220 {
221 using std::unique_ptr<long, D>::unique_ptr;
222 };
223 UDPtr upd(new long(1), D(11));
224 {
225 auto op = std::out_ptr(upd);
226 VERIFY( counter == 1 );
227 VERIFY( upd == nullptr );
228 long** p = op;
229 VERIFY( *p == nullptr );
230 *p = new long(4);
231 }
232 VERIFY( counter == 1 );
233 VERIFY( *upd == 4 );
234 VERIFY( upd.get_deleter().id == 11 );
235
236 {
237 auto op = std::out_ptr(upd, D(22));
238 VERIFY( upd == nullptr );
239 VERIFY( counter == 2 );
240 long** p = op;
241 VERIFY( *p == nullptr );
242 *p = new long(5);
243 }
244 VERIFY( counter == 2 );
245 VERIFY( *upd == 5 );
246 VERIFY( upd.get_deleter().id == 22 );
247 }
248
249 void
250 test_raw_ptr()
251 {
252 long l = 5, l2 = 6;
253 long* lp = &l;
254 {
255 auto op = std::out_ptr(lp);
256 VERIFY( lp == nullptr );
257 long** p = op;
258 VERIFY( *p == nullptr );
259 *p = &l2;
260
261 const auto& cop = op;
262 VERIFY( static_cast<long**>(cop) == static_cast<long**>(op) );
263 }
264 VERIFY( *lp == 6 );
265 }
266
267 int main()
268 {
269 test_unique_ptr();
270 test_shared_ptr();
271 test_custom_ptr();
272 test_raw_ptr();
273 }