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 / auto_ptr.pass.cpp
blob6f641cec24bf497a96e6f5e27b737d8bab104789
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(auto_ptr<Y>&& r);
13 // REQUIRES: c++03 || c++11 || c++14
14 // ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS
16 #include <memory>
17 #include <new>
18 #include <cstdlib>
19 #include <cassert>
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 int main(int, char**)
49 globalMemCounter.reset();
51 std::auto_ptr<A> ptr(new A);
52 A* raw_ptr = ptr.get();
53 std::shared_ptr<B> p(std::move(ptr));
54 assert(A::count == 1);
55 assert(B::count == 1);
56 assert(p.use_count() == 1);
57 assert(p.get() == raw_ptr);
58 assert(ptr.get() == 0);
60 assert(A::count == 0);
61 assert(globalMemCounter.checkOutstandingNewEq(0));
63 globalMemCounter.reset();
65 std::auto_ptr<A const> ptr(new A);
66 A const* raw_ptr = ptr.get();
67 std::shared_ptr<B const> p(std::move(ptr));
68 assert(A::count == 1);
69 assert(B::count == 1);
70 assert(p.use_count() == 1);
71 assert(p.get() == raw_ptr);
72 assert(ptr.get() == 0);
74 assert(A::count == 0);
75 assert(globalMemCounter.checkOutstandingNewEq(0));
77 #if !defined(TEST_HAS_NO_EXCEPTIONS) && !defined(DISABLE_NEW_COUNT)
79 std::auto_ptr<A> ptr(new A);
80 A* raw_ptr = ptr.get();
81 globalMemCounter.throw_after = 0;
82 try
84 std::shared_ptr<B> p(std::move(ptr));
85 assert(false);
87 catch (...)
89 assert(A::count == 1);
90 assert(B::count == 1);
91 assert(ptr.get() == raw_ptr);
94 assert(A::count == 0);
95 assert(globalMemCounter.checkOutstandingNewEq(0));
96 #endif // !defined(TEST_HAS_NO_EXCEPTIONS) && !defined(DISABLE_NEW_COUNT)
98 return 0;