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.mod / reset_pointer.pass.cpp
blobb9d91979b02cb12639a14693700ddfc9b2da5f7f
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 // shared_ptr
13 // template<class Y> void reset(Y* p);
15 #include <memory>
16 #include <cassert>
17 #include <utility>
19 #include "reset_helper.h"
20 #include "test_macros.h"
22 struct B
24 static int count;
26 B() {++count;}
27 B(const B&) {++count;}
28 virtual ~B() {--count;}
31 int B::count = 0;
33 struct A
34 : public B
36 static int count;
38 A() {++count;}
39 A(const A& other) : B(other) {++count;}
40 ~A() {--count;}
43 int A::count = 0;
45 struct Derived : A {};
47 static_assert( HasReset<std::shared_ptr<int>, int*>::value, "");
48 static_assert( HasReset<std::shared_ptr<A>, Derived*>::value, "");
49 static_assert(!HasReset<std::shared_ptr<A>, int*>::value, "");
51 #if TEST_STD_VER >= 17
52 static_assert( HasReset<std::shared_ptr<int[]>, int*>::value, "");
53 static_assert(!HasReset<std::shared_ptr<int[]>, int(*)[]>::value, "");
54 static_assert( HasReset<std::shared_ptr<int[5]>, int*>::value, "");
55 static_assert(!HasReset<std::shared_ptr<int[5]>, int(*)[5]>::value, "");
56 #endif
58 int main(int, char**)
61 std::shared_ptr<B> p(new B);
62 A* ptr = new A;
63 p.reset(ptr);
64 assert(A::count == 1);
65 assert(B::count == 1);
66 assert(p.use_count() == 1);
67 assert(p.get() == ptr);
69 assert(A::count == 0);
71 std::shared_ptr<B> p;
72 A* ptr = new A;
73 p.reset(ptr);
74 assert(A::count == 1);
75 assert(B::count == 1);
76 assert(p.use_count() == 1);
77 assert(p.get() == ptr);
79 assert(A::count == 0);
81 #if TEST_STD_VER > 14
83 std::shared_ptr<const A[]> p;
84 A* ptr = new A[8];
85 p.reset(ptr);
86 assert(A::count == 8);
87 assert(p.use_count() == 1);
88 assert(p.get() == ptr);
90 assert(A::count == 0);
91 #endif
93 return 0;