Debian Codegraph @

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

raw
1 // { dg-do compile { target c++23 } }
2
3 #include <memory>
4 #include <testsuite_hooks.h>
5
6 std::unique_ptr<int> intptr;
7
8 void
9 val_out(void** p)
10 {
11 // The smart pointer should be reset by the out_ptr_t constructor.
12 VERIFY( intptr == nullptr );
13 // The initial value of the pointer should be null.
14 VERIFY( *p == nullptr );
15 // Although the implementation is allowed to access the unique_ptr's
16 // pointer directly, it can't do that here because the unique_ptr stores
17 // an int* and we are writing to a void*.
18 VERIFY( intptr.get() != *p );
19
20 // Return a heap-allocated int in *p.
21 *p = (void*) new int(1234);
22 }
23
24 void
25 test_out_ptr_void()
26 {
27 intptr.reset(new int);
28 val_out(std::out_ptr<void*>(intptr));
29 VERIFY( *intptr == 1234 );
30 }
31
32 void
33 val_inout(void** p)
34 {
35 // The smart pointer should have been released by the inout_ptr_t constructor
36 VERIFY( intptr == nullptr );
37 // The initial value of the pointer should be non-null.
38 VERIFY( *(int*)*p == 5678 );
39 // Although the implementation is allowed to access the unique_ptr's
40 // pointer directly, it can't do that here because the unique_ptr stores
41 // an int* and we are reading from and writing to a void*.
42 VERIFY( intptr.get() != *p );
43
44 // Return a heap-allocated int in *p.
45 *p = (void*) new int(999);
46 }
47
48 void
49 test_inout_ptr_void()
50 {
51 intptr.reset(new int(5678));
52 val_inout(std::inout_ptr<void*>(intptr));
53 VERIFY( *intptr == 999 );
54 }
55
56 int main()
57 {
58 test_out_ptr_void();
59 test_inout_ptr_void();
60 }