File: 2.cc

package info (click to toggle)
gcc-arm-none-eabi 15%3A14.2.rel1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,099,328 kB
  • sloc: cpp: 3,627,108; ansic: 2,571,498; ada: 834,230; f90: 235,082; makefile: 79,231; asm: 74,984; xml: 51,692; exp: 39,736; sh: 33,298; objc: 15,629; python: 15,069; fortran: 14,429; pascal: 7,003; awk: 5,070; perl: 3,106; ml: 285; lisp: 253; lex: 204; haskell: 135
file content (273 lines) | stat: -rw-r--r-- 5,785 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// { dg-do compile { target c++23 } }

#include <memory>
#include <testsuite_hooks.h>

int counter = 0;

void
test_unique_ptr()
{
  std::unique_ptr<int> up(new int(1));
  {
    auto op = std::out_ptr(up);
    VERIFY( up == nullptr );
    int** p = op;
    VERIFY( *p == nullptr );
    *p = new int(2);

    const auto& cop = op;
    VERIFY( static_cast<int**>(cop) == static_cast<int**>(op) );
  }
  VERIFY( *up == 2 );

  {
    std::default_delete<int> d;
    auto op = std::out_ptr(up, d);
    VERIFY( up == nullptr );
    int** p = op;
    VERIFY( *p == nullptr );
    *p = new int(3);

    const auto& cop = op;
    VERIFY( static_cast<int**>(cop) == static_cast<int**>(op) );
  }
  VERIFY( *up == 3 );

  struct D
  {
    explicit D(int id) : id(id) { }
    void operator()(long* p) const { ++counter; delete p; }
    int id;
  };
  counter = 0;
  std::unique_ptr<long, D> upd(new long(1), D(11));
  {
    auto op = std::out_ptr(upd);
    VERIFY( counter == 1 );
    VERIFY( upd == nullptr );
    long** p = op;
    VERIFY( *p == nullptr );
    *p = new long(4);

    const auto& cop = op;
    VERIFY( static_cast<long**>(cop) == static_cast<long**>(op) );
  }
  VERIFY( *upd == 4 );
  VERIFY( upd.get_deleter().id == 11 );
  VERIFY( counter == 1 );

  {
    D d(33);
    auto op = std::out_ptr(upd, d);
    VERIFY( counter == 2 );
    VERIFY( upd == nullptr );
    long** p = op;
    VERIFY( *p == nullptr );

    const auto& cop = op;
    VERIFY( static_cast<long**>(cop) == static_cast<long**>(op) );
  }
  VERIFY( upd == nullptr );
  VERIFY( upd.get_deleter().id == 11 ); // Deleter not replaced if p is null.
  VERIFY( counter == 2 );

  {
    D d(33);
    auto op = std::out_ptr(upd, d);
    VERIFY( counter == 2 );
    VERIFY( upd == nullptr );
    long** p = op;
    VERIFY( *p == nullptr );
    *p = new long(5);
  }
  VERIFY( *upd == 5 );
  VERIFY( upd.get_deleter().id == 33 );
  VERIFY( counter == 2 );

  struct Base { };
  struct Derived : Base
  {
    Derived(int id) : id(id) { }
    int id;
  };
  std::unique_ptr<Derived> upbd(new Derived(1));
  {
    auto op = std::out_ptr<Base*>(upbd);
    Base** bpp = op;
    VERIFY( *bpp == nullptr );
    *bpp = new Derived(2);

    const auto& cop = op;
    VERIFY( static_cast<Base**>(cop) == static_cast<Base**>(op) );
  }
  VERIFY( upbd->id == 2 );
}

void deleter_function(float* p) { delete p; }

void
test_shared_ptr()
{
  using DD = std::default_delete<int>;

  std::shared_ptr<int> sp(new int(1));
  {
    auto op = std::out_ptr(sp, DD{});
    VERIFY( sp == nullptr );
    int** p = op;
    VERIFY( *p == nullptr );
    *p = new int(2);

    const auto& cop = op;
    VERIFY( static_cast<int**>(cop) == static_cast<int**>(op) );
  }
  VERIFY( *sp == 2 );

  {
    auto op = std::out_ptr(sp, DD{});
    VERIFY( sp == nullptr );
    int** p = op;
    VERIFY( *p == nullptr );
    *p = new int(3);
  }
  VERIFY( *sp == 3 );

  struct D
  {
    explicit D(int id) : id(id) { }
    void operator()(long* p) const { ++counter; delete p; }
    int id;
  };
  counter = 0;
  std::shared_ptr<long> spd(new long(1), D(11));
  {
    auto op = std::out_ptr(spd, D(22));
    VERIFY( counter == 1 );
    VERIFY( spd == nullptr );
    long** p = op;
    VERIFY( *p == nullptr );
    *p = new long(5);
  }
  VERIFY( counter == 1 );
  VERIFY( *spd == 5 );
  VERIFY( std::get_deleter<D>(spd)->id == 22 );

  struct Base { };
  struct Derived : Base
  {
    Derived(int id) : id(id) { }
    int id;
  };
  std::shared_ptr<Derived> spbd(new Derived(1));
  {
    auto op = std::out_ptr<Base*>(spbd, std::default_delete<Derived>());
    Base** bpp = op;
    VERIFY( *bpp == nullptr );
    *bpp = new Derived(2);
  }
  VERIFY( spbd->id == 2 );

  std::shared_ptr<float> spf;
  {
    auto op = std::out_ptr(spf, deleter_function);
    float** fpp = op;
    *fpp = new float(0.5);
  }
  VERIFY( std::get_deleter<void(*)(float*)>(spf) != nullptr );
  VERIFY( *std::get_deleter<void(*)(float*)>(spf) == &deleter_function );
  VERIFY( *spf == 0.5 );
}

void
test_custom_ptr()
{
  struct UPtr : std::unique_ptr<int>
  {
    using std::unique_ptr<int>::unique_ptr;
  };

  UPtr up(new int(1));
  {
    auto op = std::out_ptr(up);
    VERIFY( up == nullptr );
    int** p = op;
    VERIFY( *p == nullptr );
    *p = new int(2);

    const auto& cop = op;
    VERIFY( static_cast<int**>(cop) == static_cast<int**>(op) );
  }
  VERIFY( *up == 2 );

  {
    auto op = std::out_ptr(up, std::default_delete<int>{});
    VERIFY( up == nullptr );
    int** p = op;
    VERIFY( *p == nullptr );
    *p = new int(3);
  }
  VERIFY( *up == 3 );

  struct D
  {
    explicit D(int id) : id(id) { }
    void operator()(long* p) const { ++counter; delete p; }
    int id;
  };
  counter = 0;
  struct UDPtr : std::unique_ptr<long, D>
  {
    using std::unique_ptr<long, D>::unique_ptr;
  };
  UDPtr upd(new long(1), D(11));
  {
    auto op = std::out_ptr(upd);
    VERIFY( counter == 1 );
    VERIFY( upd == nullptr );
    long** p = op;
    VERIFY( *p == nullptr );
    *p = new long(4);
  }
  VERIFY( counter == 1 );
  VERIFY( *upd == 4 );
  VERIFY( upd.get_deleter().id == 11 );

  {
    auto op = std::out_ptr(upd, D(22));
    VERIFY( upd == nullptr );
    VERIFY( counter == 2 );
    long** p = op;
    VERIFY( *p == nullptr );
    *p = new long(5);
  }
  VERIFY( counter == 2 );
  VERIFY( *upd == 5 );
  VERIFY( upd.get_deleter().id == 22 );
}

void
test_raw_ptr()
{
  long l = 5, l2 = 6;
  long* lp = &l;
  {
    auto op = std::out_ptr(lp);
    VERIFY( lp == nullptr );
    long** p = op;
    VERIFY( *p == nullptr );
    *p = &l2;

    const auto& cop = op;
    VERIFY( static_cast<long**>(cop) == static_cast<long**>(op) );
  }
  VERIFY( *lp == 6 );
}

int main()
{
  test_unique_ptr();
  test_shared_ptr();
  test_custom_ptr();
  test_raw_ptr();
}