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 | //===----------------------------------------------------------------------===// |
8 | |
9 | // <memory> |
10 | |
11 | // template<class Y> explicit shared_ptr(auto_ptr<Y>&& r); |
12 | |
13 | // REQUIRES: c++03 || c++11 || c++14 |
14 | // ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS |
15 | |
16 | #include <memory> |
17 | #include <new> |
18 | #include <cstdlib> |
19 | #include <cassert> |
20 | |
21 | #include "test_macros.h" |
22 | #include "count_new.h" |
23 | |
24 | struct B |
25 | { |
26 | static int count; |
27 | |
28 | B() {++count;} |
29 | B(const B&) {++count;} |
30 | virtual ~B() {--count;} |
31 | }; |
32 | |
33 | int B::count = 0; |
34 | |
35 | struct A |
36 | : public B |
37 | { |
38 | static int count; |
39 | |
40 | A() {++count;} |
41 | A(const A& other) : B(other) {++count;} |
42 | ~A() {--count;} |
43 | }; |
44 | |
45 | int A::count = 0; |
46 | |
47 | int main(int, char**) |
48 | { |
49 | globalMemCounter.reset(); |
50 | { |
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); |
59 | } |
60 | assert(A::count == 0); |
61 | assert(globalMemCounter.checkOutstandingNewEq(0)); |
62 | |
63 | globalMemCounter.reset(); |
64 | { |
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); |
73 | } |
74 | assert(A::count == 0); |
75 | assert(globalMemCounter.checkOutstandingNewEq(0)); |
76 | |
77 | #if !defined(TEST_HAS_NO_EXCEPTIONS) && !defined(DISABLE_NEW_COUNT) |
78 | { |
79 | std::auto_ptr<A> ptr(new A); |
80 | A* raw_ptr = ptr.get(); |
81 | globalMemCounter.throw_after = 0; |
82 | try |
83 | { |
84 | std::shared_ptr<B> p(std::move(ptr)); |
85 | assert(false); |
86 | } |
87 | catch (...) |
88 | { |
89 | assert(A::count == 1); |
90 | assert(B::count == 1); |
91 | assert(ptr.get() == raw_ptr); |
92 | } |
93 | } |
94 | assert(A::count == 0); |
95 | assert(globalMemCounter.checkOutstandingNewEq(0)); |
96 | #endif // !defined(TEST_HAS_NO_EXCEPTIONS) && !defined(DISABLE_NEW_COUNT) |
97 | |
98 | return 0; |
99 | } |
100 | |