]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/shared_ptr/casts/rval.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 20_util / shared_ptr / casts / rval.cc
1 // { dg-options "-std=gnu++2a" }
2 // { dg-do run { target c++2a } }
3
4 // Copyright (C) 2019-2020 Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
10 // any later version.
11
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING3. If not see
19 // <http://www.gnu.org/licenses/>.
20
21 // shared_ptr casts [util.smartptr.shared.cast]
22
23 #include <memory>
24 #include <testsuite_hooks.h>
25 #include <testsuite_tr1.h>
26
27 struct MyP { virtual ~MyP() { }; };
28 struct MyDP : MyP { };
29
30 void test01()
31 {
32 using __gnu_test::check_ret_type;
33 using std::shared_ptr;
34 using std::static_pointer_cast;
35 using std::const_pointer_cast;
36 using std::dynamic_pointer_cast;
37 using std::reinterpret_pointer_cast;
38
39 shared_ptr<double> spd;
40 shared_ptr<const int> spci;
41 shared_ptr<MyP> spa;
42
43 check_ret_type<shared_ptr<void>>(static_pointer_cast<void>(std::move(spd)));
44 check_ret_type<shared_ptr<int>>(const_pointer_cast<int>(std::move(spci)));
45 check_ret_type<shared_ptr<MyDP>>(dynamic_pointer_cast<MyDP>(std::move(spa)));
46 check_ret_type<shared_ptr<void>>(reinterpret_pointer_cast<void>(std::move(spd)));
47 check_ret_type<shared_ptr<const short>>(reinterpret_pointer_cast<const short>(std::move(spci)));
48 check_ret_type<shared_ptr<MyDP>>(reinterpret_pointer_cast<MyDP>(std::move(spa)));
49 }
50
51 void
52 test02()
53 {
54 using std::shared_ptr;
55 using std::static_pointer_cast;
56 using std::const_pointer_cast;
57 using std::dynamic_pointer_cast;
58 using std::reinterpret_pointer_cast;
59
60 int* ptr = new int(1);
61 shared_ptr<const void> pcv(ptr);
62 auto pci = static_pointer_cast<const int>(std::move(pcv));
63 VERIFY(pci.use_count() == 1);
64 VERIFY(pcv.use_count() == 0);
65 VERIFY(pci.get() == ptr);
66 VERIFY(pcv.get() == nullptr);
67 auto pi = const_pointer_cast<int>(std::move(pci));
68 VERIFY(pi.use_count() == 1);
69 VERIFY(pci.use_count() == 0);
70 VERIFY(pi.get() == ptr);
71 VERIFY(pci.get() == nullptr);
72
73 MyP* pptr = new MyP;
74 shared_ptr<MyP> pp(pptr);
75 auto pdp = dynamic_pointer_cast<MyDP>(std::move(pp));
76 VERIFY(pdp.use_count() == 0);
77 VERIFY(pp.use_count() == 1);
78 VERIFY(pdp.get() == nullptr);
79 VERIFY(pp.get() == pptr);
80 pptr = new MyDP;
81 pp.reset(pptr);
82 pdp = dynamic_pointer_cast<MyDP>(std::move(pp));
83 VERIFY(pdp.use_count() == 1);
84 VERIFY(pp.use_count() == 0);
85 VERIFY(pdp.get() == pptr);
86 VERIFY(pp.get() == nullptr);
87
88 ptr = new int(2);
89 pi.reset(ptr);
90 auto pl = reinterpret_pointer_cast<long>(std::move(pi));
91 VERIFY(pl.use_count() == 1);
92 VERIFY(pi.use_count() == 0);
93 VERIFY(reinterpret_cast<int*>(pl.get()) == ptr);
94 VERIFY(pi.get() == nullptr);
95 }
96
97 int main()
98 {
99 test01();
100 test02();
101 }