]>
Commit | Line | Data |
---|---|---|
b2a485f2 | 1 | // { dg-options "-std=gnu++14" } |
930d5602 | 2 | |
818ab71a | 3 | // Copyright (C) 2015-2016 Free Software Foundation, Inc. |
930d5602 FY |
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 B : A | |
36 | { | |
37 | B() { ++ctor_count; } | |
38 | virtual ~B() { ++dtor_count; } | |
39 | static long ctor_count; | |
40 | static long dtor_count; | |
41 | }; | |
42 | long B::ctor_count = 0; | |
43 | long B::dtor_count = 0; | |
44 | ||
45 | void deleter(A* p) { delete [] p; } | |
46 | ||
47 | struct reset_count_struct | |
48 | { | |
49 | ~reset_count_struct() | |
50 | { | |
51 | A::ctor_count = 0; | |
52 | A::dtor_count = 0; | |
53 | B::ctor_count = 0; | |
54 | B::dtor_count = 0; | |
55 | } | |
56 | }; | |
57 | ||
58 | // 8.2.1.1 shared_ptr constructors [memory.smartptr.shared.const] | |
59 | ||
60 | // Copy construction | |
61 | ||
62 | int | |
63 | test01() | |
64 | { | |
65 | reset_count_struct __attribute__((unused)) reset; | |
66 | bool test __attribute__((unused)) = true; | |
67 | ||
68 | std::experimental::shared_ptr<A[5]> a1; | |
69 | std::experimental::shared_ptr<A[5]> a2(a1); | |
70 | VERIFY( a2.use_count() == 0 ); | |
71 | VERIFY( A::ctor_count == 0 ); | |
72 | VERIFY( A::dtor_count == 0 ); | |
73 | VERIFY( B::ctor_count == 0 ); | |
74 | VERIFY( B::dtor_count == 0 ); | |
75 | ||
76 | return 0; | |
77 | } | |
78 | ||
79 | int | |
80 | test02() | |
81 | { | |
82 | reset_count_struct __attribute__((unused)) reset; | |
83 | bool test __attribute__((unused)) = true; | |
84 | ||
85 | std::experimental::shared_ptr<A[5]> a1(new A[5]); | |
86 | std::experimental::shared_ptr<A[5]> a2(a1); | |
87 | VERIFY( a2.use_count() == 2 ); | |
88 | VERIFY( A::ctor_count == 5 ); | |
89 | VERIFY( A::dtor_count == 0 ); | |
90 | VERIFY( B::ctor_count == 0 ); | |
91 | VERIFY( B::dtor_count == 0 ); | |
92 | ||
93 | return 0; | |
94 | } | |
95 | ||
96 | int | |
97 | test03() | |
98 | { | |
99 | reset_count_struct __attribute__((unused)) reset; | |
100 | bool test __attribute__((unused)) = true; | |
101 | ||
102 | std::experimental::shared_ptr<A[5]> a1(new A[5], &deleter); | |
103 | std::experimental::shared_ptr<A[5]> a2(a1); | |
104 | VERIFY( a2.use_count() == 2 ); | |
105 | VERIFY( A::ctor_count == 5 ); | |
106 | VERIFY( A::dtor_count == 0 ); | |
107 | VERIFY( B::ctor_count == 0 ); | |
108 | VERIFY( B::dtor_count == 0 ); | |
109 | ||
110 | return 0; | |
111 | } | |
112 | ||
113 | int | |
114 | test04() | |
115 | { | |
116 | reset_count_struct __attribute__((unused)) reset; | |
117 | bool test __attribute__((unused)) = true; | |
118 | ||
119 | std::experimental::shared_ptr<A[5]> a1(std::experimental::shared_ptr<A[5]> | |
120 | (new A[5])); | |
121 | VERIFY( a1.use_count() == 1 ); | |
122 | VERIFY( A::ctor_count == 5 ); | |
123 | VERIFY( A::dtor_count == 0 ); | |
124 | VERIFY( B::ctor_count == 0 ); | |
125 | VERIFY( B::dtor_count == 0 ); | |
126 | ||
127 | return 0; | |
128 | } | |
129 | ||
130 | int | |
131 | test05() | |
132 | { | |
133 | reset_count_struct __attribute__((unused)) reset; | |
134 | bool test __attribute__((unused)) = true; | |
135 | ||
136 | std::experimental::shared_ptr<A[5]> a1(new A[5]); | |
137 | std::experimental::shared_ptr<A[]> a2(a1); | |
138 | ||
139 | VERIFY( a2.use_count() == 2 ); | |
140 | VERIFY( a2.get() == a1.get() ); | |
141 | VERIFY( A::ctor_count == 5 ); | |
142 | VERIFY( A::dtor_count == 0 ); | |
143 | VERIFY( B::ctor_count == 0 ); | |
144 | VERIFY( B::dtor_count == 0 ); | |
145 | ||
146 | return 0; | |
147 | } | |
148 | ||
149 | int | |
150 | test06() | |
151 | { | |
152 | reset_count_struct __attribute__((unused)) reset; | |
153 | bool test __attribute__((unused)) = true; | |
154 | ||
155 | std::experimental::shared_ptr<B> a1(new B); | |
156 | std::experimental::shared_ptr<A> a2(a1); | |
157 | ||
158 | VERIFY( a2.use_count() == 2 ); | |
159 | VERIFY( a2.get() == a1.get() ); | |
160 | VERIFY( A::ctor_count == 1 ); | |
161 | VERIFY( A::dtor_count == 0 ); | |
162 | VERIFY( B::ctor_count == 1 ); | |
163 | VERIFY( B::dtor_count == 0 ); | |
164 | ||
165 | return 0; | |
166 | } | |
167 | ||
168 | int | |
169 | main() | |
170 | { | |
171 | test01(); | |
172 | test02(); | |
173 | test03(); | |
174 | test04(); | |
175 | test05(); | |
176 | test06(); | |
177 | return 0; | |
178 | } |