Revert "[libc] Breakup freelist_malloc into separate files" (#119749)
[llvm-project.git] / libcxx / test / std / utilities / memory / util.smartptr / util.smartptr.shared / util.smartptr.shared.const / unique_ptr.pass.cpp
blob9308bb3858c65cd303efe956dd291f7c4a3cacbc
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: sanitizer-new-delete
11 // <memory>
13 // template <class Y, class D> shared_ptr(unique_ptr<Y, D>&&r);
15 #include <memory>
16 #include <new>
17 #include <cstdlib>
18 #include <cassert>
19 #include <utility>
21 #include "test_macros.h"
22 #include "count_new.h"
24 struct B
26 static int count;
28 B() {++count;}
29 B(const B&) {++count;}
30 virtual ~B() {--count;}
33 int B::count = 0;
35 struct A
36 : public B
38 static int count;
40 A() {++count;}
41 A(const A& other) : B(other) {++count;}
42 ~A() {--count;}
45 int A::count = 0;
47 void fn ( const std::shared_ptr<int> &) {}
48 void fn ( const std::shared_ptr<B> &) { assert (false); }
50 template <typename T>
51 void assert_deleter ( T * ) { assert(false); }
53 namespace adl {
54 struct D {
55 void operator()(int *) const {}
57 void ref(D);
60 template <class T>
61 struct StatefulDeleter {
62 int state = 0;
64 StatefulDeleter(int val = 0) : state(val) {}
65 StatefulDeleter(StatefulDeleter const&) { assert(false); }
67 void operator()(T* ptr) {
68 assert(state == 42);
69 delete ptr;
73 template <class T>
74 struct StatefulArrayDeleter {
75 int state = 0;
77 StatefulArrayDeleter(int val = 0) : state(val) {}
78 StatefulArrayDeleter(StatefulArrayDeleter const&) { assert(false); }
80 void operator()(T* ptr) {
81 assert(state == 42);
82 delete []ptr;
86 struct MovingDeleter {
87 explicit MovingDeleter(int *moves) : moves_(moves) {}
88 MovingDeleter(MovingDeleter&& rhs) : moves_(rhs.moves_) { *moves_ += 1; }
89 void operator()(int*) const {}
90 int *moves_;
93 // https://llvm.org/PR53368
94 // Bogus unique_ptr-to-shared_ptr conversions should be forbidden
95 #if TEST_STD_VER >= 17
96 static_assert( std::is_constructible<std::shared_ptr<A>, std::unique_ptr<A>&&>::value, "");
97 static_assert( std::is_constructible<std::shared_ptr<A[]>, std::unique_ptr<A[]>&&>::value, "");
98 static_assert(!std::is_constructible<std::shared_ptr<A>, std::unique_ptr<A[]>&&>::value, "");
99 static_assert(!std::is_constructible<std::shared_ptr<B[]>, std::unique_ptr<A[]>&&>::value, "");
100 static_assert(!std::is_constructible<std::shared_ptr<B>, std::unique_ptr<A[]>&&>::value, "");
101 #endif
103 int main(int, char**)
106 std::unique_ptr<A> ptr(new A);
107 A* raw_ptr = ptr.get();
108 std::shared_ptr<B> p(std::move(ptr));
109 assert(A::count == 1);
110 assert(B::count == 1);
111 assert(p.use_count() == 1);
112 assert(p.get() == raw_ptr);
113 assert(ptr.get() == 0);
117 std::unique_ptr<A const> ptr(new A);
118 A const* raw_ptr = ptr.get();
119 std::shared_ptr<B const> p(std::move(ptr));
120 assert(A::count == 1);
121 assert(B::count == 1);
122 assert(p.use_count() == 1);
123 assert(p.get() == raw_ptr);
124 assert(ptr.get() == 0);
127 #ifndef TEST_HAS_NO_EXCEPTIONS
128 assert(A::count == 0);
130 std::unique_ptr<A> ptr(new A);
131 A* raw_ptr = ptr.get();
132 globalMemCounter.throw_after = 0;
135 std::shared_ptr<B> p(std::move(ptr));
136 assert(false);
138 catch (...)
140 assert(A::count == 1);
141 assert(B::count == 1);
142 assert(ptr.get() == raw_ptr);
145 #endif
147 #if TEST_STD_VER > 14
149 std::unique_ptr<int> ptr;
150 std::shared_ptr<int> p(std::move(ptr));
151 assert(p.get() == 0);
152 assert(p.use_count() == 0);
154 #endif
157 StatefulDeleter<A> d;
158 std::unique_ptr<A, StatefulDeleter<A>&> u(new A, d);
159 std::shared_ptr<A> p(std::move(u));
160 d.state = 42;
161 assert(A::count == 1);
163 assert(A::count == 0);
165 { // LWG 2399
166 fn(std::unique_ptr<int>(new int));
168 #if TEST_STD_VER >= 14
169 { // LWG 2415
170 std::unique_ptr<int, void (*)(int*)> p(nullptr, assert_deleter<int>);
171 std::shared_ptr<int> p2(std::move(p)); // should not call deleter when going out of scope
173 #endif
176 adl::D d;
177 std::unique_ptr<int, adl::D&> u(nullptr, d);
178 std::shared_ptr<int> s = std::move(u);
181 assert(A::count == 0);
183 #if TEST_STD_VER > 14
185 StatefulArrayDeleter<A> d;
186 std::unique_ptr<A[], StatefulArrayDeleter<A>&> u(new A[4], d);
187 std::shared_ptr<A[]> p(std::move(u));
188 d.state = 42;
189 assert(A::count == 4);
191 assert(A::count == 0);
192 assert(B::count == 0);
195 std::unique_ptr<A[]> ptr(new A[8]);
196 A* raw_ptr = ptr.get();
197 std::shared_ptr<A[]> p(std::move(ptr));
198 assert(A::count == 8);
199 assert(p.use_count() == 1);
200 assert(p.get() == raw_ptr);
201 assert(ptr.get() == 0);
203 assert(A::count == 0);
206 int *p = new int[8];
207 std::unique_ptr<int[]> u(p);
208 std::shared_ptr<int[]> s(std::move(u));
209 assert(u == nullptr);
210 assert(s.get() == p);
212 #endif // TEST_STD_VER > 14
214 { // LWG 3548
216 int moves = 0;
217 int i = 42;
218 std::unique_ptr<int, MovingDeleter> u(&i, MovingDeleter(&moves));
219 assert(moves == 1);
220 std::shared_ptr<int> s(std::move(u));
221 assert(moves >= 2);
222 assert(u == nullptr);
223 assert(s.get() == &i);
226 #if TEST_STD_VER > 14
228 int moves = 0;
229 int a[8];
230 std::unique_ptr<int[], MovingDeleter> u(a, MovingDeleter(&moves));
231 assert(moves == 1);
232 std::shared_ptr<int[]> s = std::move(u);
233 assert(moves >= 2);
234 assert(u == nullptr);
235 assert(s.get() == a);
237 #endif // TEST_STD_VER > 14
240 return 0;