]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/experimental/memory/shared_ptr/cons/move_ctor.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / experimental / memory / shared_ptr / cons / move_ctor.cc
1 // { dg-do run { target c++14 } }
2
3 // Copyright (C) 2015-2020 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
19
20 // 8.2.1 Class template shared_ptr [memory.smartptr.shared]
21
22 #include <experimental/memory>
23 #include <testsuite_hooks.h>
24
25 struct A
26 {
27 A() { ++ctor_count; }
28 virtual ~A() { ++dtor_count; }
29 static long ctor_count;
30 static long dtor_count;
31 };
32 long A::ctor_count = 0;
33 long A::dtor_count = 0;
34
35 struct D
36 {
37 void operator()(A* p) const { delete [] p; ++delete_count; }
38 static long delete_count;
39 };
40 long D::delete_count = 0;
41
42 struct reset_count_struct
43 {
44 ~reset_count_struct()
45 {
46 A::ctor_count = 0;
47 A::dtor_count = 0;
48 D::delete_count = 0;
49 }
50 };
51
52 // 8.2.1.1 shared_ptr constructors [memory.smartptr.shared.const]
53
54 // Rvalue construction
55 int test01()
56 {
57 reset_count_struct __attribute__((unused)) reset;
58
59 std::experimental::shared_ptr<A[5]> a1;
60 std::experimental::shared_ptr<A[5]> a2(std::move(a1));
61 VERIFY( a1.use_count() == 0 );
62 VERIFY( a2.use_count() == 0 );
63 VERIFY( A::ctor_count == 0 );
64 VERIFY( A::dtor_count == 0 );
65
66 return 0;
67 }
68
69 int
70 test02()
71 {
72 reset_count_struct __attribute__((unused)) reset;
73
74 std::experimental::shared_ptr<A[5]> a1(new A[5]);
75 std::experimental::shared_ptr<A[5]> a2(std::move(a1));
76 VERIFY( a2.use_count() == 1 );
77 VERIFY( A::ctor_count == 5 );
78 VERIFY( A::dtor_count == 0 );
79
80 return 0;
81 }
82
83 int
84 test03()
85 {
86 reset_count_struct __attribute__((unused)) reset;
87
88 std::experimental::shared_ptr<A[5]> b(new A[5], D());
89 std::experimental::shared_ptr<A[5]> b1(std::move(b));
90 VERIFY( b.use_count() == 0 );
91 VERIFY( b1.use_count() == 1 );
92 VERIFY( A::ctor_count == 5 );
93 VERIFY( A::dtor_count == 0 );
94
95 b1 = std::move(std::experimental::shared_ptr<A[5]> ());
96
97 VERIFY( A::ctor_count == 5 );
98 VERIFY( A::dtor_count == 5 );
99 VERIFY( D::delete_count == 1 );
100
101 return 0;
102 }
103
104 void
105 test04()
106 {
107 reset_count_struct __attribute__((unused)) reset;
108
109 std::experimental::shared_ptr<A[5]> a(std::move(std::experimental
110 ::shared_ptr<A[5]>
111 (new A[5])));
112
113 VERIFY( a.use_count() == 1 );
114 VERIFY( A::ctor_count == 5 );
115 VERIFY( A::dtor_count == 0 );
116 }
117
118 void
119 test05()
120 {
121 reset_count_struct __attribute__((unused)) reset;
122
123 std::experimental::shared_ptr<A[]> a(std::move(std::experimental
124 ::shared_ptr<A[5]>
125 (new A[5])));
126
127 VERIFY( a.use_count() == 1 );
128 VERIFY( A::ctor_count == 5 );
129 VERIFY( A::dtor_count == 0 );
130 }
131
132 int
133 main()
134 {
135 test01();
136 test02();
137 test03();
138 test04();
139 test05();
140 return 0;
141 }