]>
Commit | Line | Data |
---|---|---|
930d5602 FY |
1 | // { dg-options "-std=gnu++1y" } |
2 | ||
3 | // Copyright (C) 2015 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 | bool test __attribute__((unused)) = true; | |
59 | ||
60 | std::experimental::shared_ptr<A[5]> a1; | |
61 | std::experimental::shared_ptr<A[5]> a2(std::move(a1)); | |
62 | VERIFY( a1.use_count() == 0 ); | |
63 | VERIFY( a2.use_count() == 0 ); | |
64 | VERIFY( A::ctor_count == 0 ); | |
65 | VERIFY( A::dtor_count == 0 ); | |
66 | ||
67 | return 0; | |
68 | } | |
69 | ||
70 | int | |
71 | test02() | |
72 | { | |
73 | reset_count_struct __attribute__((unused)) reset; | |
74 | bool test __attribute__((unused)) = true; | |
75 | ||
76 | std::experimental::shared_ptr<A[5]> a1(new A[5]); | |
77 | std::experimental::shared_ptr<A[5]> a2(std::move(a1)); | |
78 | VERIFY( a2.use_count() == 1 ); | |
79 | VERIFY( A::ctor_count == 5 ); | |
80 | VERIFY( A::dtor_count == 0 ); | |
81 | ||
82 | return 0; | |
83 | } | |
84 | ||
85 | int | |
86 | test03() | |
87 | { | |
88 | reset_count_struct __attribute__((unused)) reset; | |
89 | bool test __attribute__((unused)) = true; | |
90 | ||
91 | std::experimental::shared_ptr<A[5]> b(new A[5], D()); | |
92 | std::experimental::shared_ptr<A[5]> b1(std::move(b)); | |
93 | VERIFY( b.use_count() == 0 ); | |
94 | VERIFY( b1.use_count() == 1 ); | |
95 | VERIFY( A::ctor_count == 5 ); | |
96 | VERIFY( A::dtor_count == 0 ); | |
97 | ||
98 | b1 = std::move(std::experimental::shared_ptr<A[5]> ()); | |
99 | ||
100 | VERIFY( A::ctor_count == 5 ); | |
101 | VERIFY( A::dtor_count == 5 ); | |
102 | VERIFY( D::delete_count == 1 ); | |
103 | ||
104 | return 0; | |
105 | } | |
106 | ||
107 | void | |
108 | test04() | |
109 | { | |
110 | reset_count_struct __attribute__((unused)) reset; | |
111 | bool test __attribute__((unused)) = true; | |
112 | ||
113 | std::experimental::shared_ptr<A[5]> a(std::move(std::experimental | |
114 | ::shared_ptr<A[5]> | |
115 | (new A[5]))); | |
116 | ||
117 | VERIFY( a.use_count() == 1 ); | |
118 | VERIFY( A::ctor_count == 5 ); | |
119 | VERIFY( A::dtor_count == 0 ); | |
120 | } | |
121 | ||
122 | void | |
123 | test05() | |
124 | { | |
125 | reset_count_struct __attribute__((unused)) reset; | |
126 | bool test __attribute__((unused)) = true; | |
127 | ||
128 | std::experimental::shared_ptr<A[]> a(std::move(std::experimental | |
129 | ::shared_ptr<A[5]> | |
130 | (new A[5]))); | |
131 | ||
132 | VERIFY( a.use_count() == 1 ); | |
133 | VERIFY( A::ctor_count == 5 ); | |
134 | VERIFY( A::dtor_count == 0 ); | |
135 | } | |
136 | ||
137 | int | |
138 | main() | |
139 | { | |
140 | test01(); | |
141 | test02(); | |
142 | test03(); | |
143 | test04(); | |
144 | test05(); | |
145 | return 0; | |
146 | } |