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 / pointer.pass.cpp
blob37ce087ca7ba74a4b48bc5670f5f2a21eb01f308
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 // <memory>
11 // template<class Y> explicit shared_ptr(Y* p);
13 #include <cassert>
14 #include <memory>
15 #include <type_traits>
17 #include "test_macros.h"
19 struct A
21 static int count;
23 A() {++count;}
24 A(const A&) {++count;}
25 ~A() {--count;}
28 int A::count = 0;
30 struct derived : A {};
32 // https://llvm.org/PR60258
33 // Invalid constructor SFINAE for std::shared_ptr's array ctors
34 static_assert( std::is_constructible<std::shared_ptr<int>, int*>::value, "");
35 static_assert( std::is_constructible<std::shared_ptr<A>, derived*>::value, "");
36 static_assert(!std::is_constructible<std::shared_ptr<A>, int*>::value, "");
38 #if TEST_STD_VER >= 17
39 static_assert( std::is_constructible<std::shared_ptr<int[]>, int*>::value, "");
40 static_assert(!std::is_constructible<std::shared_ptr<int[]>, int(*)[]>::value, "");
41 static_assert( std::is_constructible<std::shared_ptr<int[5]>, int*>::value, "");
42 static_assert(!std::is_constructible<std::shared_ptr<int[5]>, int(*)[5]>::value, "");
43 #endif
45 // Test explicit
46 static_assert(std::is_constructible<std::shared_ptr<int>, int*>::value, "");
47 static_assert(!std::is_convertible<int*, std::shared_ptr<int> >::value, "");
49 int main(int, char**)
52 assert(A::count == 0);
53 A* ptr = new A;
54 std::shared_ptr<A> p(ptr);
55 assert(A::count == 1);
56 assert(p.use_count() == 1);
57 assert(p.get() == ptr);
61 assert(A::count == 0);
62 A const* ptr = new A;
63 std::shared_ptr<A const> p(ptr);
64 assert(A::count == 1);
65 assert(p.use_count() == 1);
66 assert(p.get() == ptr);
70 assert(A::count == 0);
71 A* ptr = new A;
72 std::shared_ptr<void> p(ptr);
73 assert(A::count == 1);
74 assert(p.use_count() == 1);
75 assert(p.get() == ptr);
78 #if TEST_STD_VER > 14
80 assert(A::count == 0);
81 std::shared_ptr<A[8]> pA(new A[8]);
82 assert(pA.use_count() == 1);
83 assert(A::count == 8);
87 assert(A::count == 0);
88 std::shared_ptr<A[]> pA(new A[8]);
89 assert(pA.use_count() == 1);
90 assert(A::count == 8);
94 assert(A::count == 0);
95 std::shared_ptr<const A[]> pA(new A[8]);
96 assert(pA.use_count() == 1);
97 assert(A::count == 8);
101 assert(A::count == 0);
102 std::shared_ptr<A> pA(new derived);
103 assert(pA.use_count() == 1);
104 assert(A::count == 1);
106 #endif
108 return 0;