183564056SLogan Smith //===----------------------------------------------------------------------===//
283564056SLogan Smith //
383564056SLogan Smith // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
483564056SLogan Smith // See https://llvm.org/LICENSE.txt for license information.
583564056SLogan Smith // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
683564056SLogan Smith //
783564056SLogan Smith //===----------------------------------------------------------------------===//
883564056SLogan Smith 
983564056SLogan Smith // <memory>
1031cbe0f2SLouis Dionne // UNSUPPORTED: c++03, c++11, c++14
1183564056SLogan Smith 
1283564056SLogan Smith // template<class T> class shared_ptr
1383564056SLogan Smith 
1483564056SLogan Smith // shared_ptr(weak_ptr<T>) -> shared_ptr<T>
1583564056SLogan Smith // shared_ptr(unique_ptr<T>) -> shared_ptr<T>
1683564056SLogan Smith 
1783564056SLogan Smith #include <memory>
1883564056SLogan Smith #include <cassert>
19*d5e26775SNikolas Klauser #include <utility>
2083564056SLogan Smith 
2183564056SLogan Smith #include "test_macros.h"
2283564056SLogan Smith 
2383564056SLogan Smith struct A {};
2483564056SLogan Smith 
2583564056SLogan Smith struct D {
operator ()D266b9e0af8SLouis Dionne   void operator()(A const* ptr) const
27afc8b497Szoecarver   {
28afc8b497Szoecarver     delete ptr;
29afc8b497Szoecarver   }
3083564056SLogan Smith };
3183564056SLogan Smith 
main(int,char **)3283564056SLogan Smith int main(int, char**)
3383564056SLogan Smith {
3483564056SLogan Smith   {
3583564056SLogan Smith     std::shared_ptr<A> s0(new A);
3683564056SLogan Smith     std::weak_ptr<A> w = s0;
3783564056SLogan Smith     auto s = std::shared_ptr(w);
3883564056SLogan Smith     ASSERT_SAME_TYPE(decltype(s), std::shared_ptr<A>);
3983564056SLogan Smith     assert(s0.use_count() == 2);
4083564056SLogan Smith     assert(s.use_count() == 2);
4183564056SLogan Smith     assert(s0.get() == s.get());
4283564056SLogan Smith   }
4383564056SLogan Smith   {
446b9e0af8SLouis Dionne     std::shared_ptr<A const> s0(new A);
456b9e0af8SLouis Dionne     std::weak_ptr<A const> w = s0;
466b9e0af8SLouis Dionne     auto s = std::shared_ptr(w);
476b9e0af8SLouis Dionne     ASSERT_SAME_TYPE(decltype(s), std::shared_ptr<A const>);
486b9e0af8SLouis Dionne     assert(s0.use_count() == 2);
496b9e0af8SLouis Dionne     assert(s.use_count() == 2);
506b9e0af8SLouis Dionne     assert(s0.get() == s.get());
516b9e0af8SLouis Dionne   }
526b9e0af8SLouis Dionne 
536b9e0af8SLouis Dionne   {
5483564056SLogan Smith     std::unique_ptr<A> u(new A);
556b9e0af8SLouis Dionne     A* uPointee = u.get();
5683564056SLogan Smith     std::shared_ptr s = std::move(u);
5783564056SLogan Smith     ASSERT_SAME_TYPE(decltype(s), std::shared_ptr<A>);
5883564056SLogan Smith     assert(u == nullptr);
5983564056SLogan Smith     assert(s.get() == uPointee);
6083564056SLogan Smith   }
6183564056SLogan Smith   {
626b9e0af8SLouis Dionne     std::unique_ptr<A const> u(new A);
636b9e0af8SLouis Dionne     A const* uPointee = u.get();
646b9e0af8SLouis Dionne     std::shared_ptr s = std::move(u);
656b9e0af8SLouis Dionne     ASSERT_SAME_TYPE(decltype(s), std::shared_ptr<A const>);
666b9e0af8SLouis Dionne     assert(u == nullptr);
676b9e0af8SLouis Dionne     assert(s.get() == uPointee);
686b9e0af8SLouis Dionne   }
696b9e0af8SLouis Dionne 
706b9e0af8SLouis Dionne   {
7183564056SLogan Smith     std::unique_ptr<A, D> u(new A, D{});
726b9e0af8SLouis Dionne     A* uPointee = u.get();
7383564056SLogan Smith     std::shared_ptr s(std::move(u));
7483564056SLogan Smith     ASSERT_SAME_TYPE(decltype(s), std::shared_ptr<A>);
7583564056SLogan Smith     assert(u == nullptr);
7683564056SLogan Smith     assert(s.get() == uPointee);
7783564056SLogan Smith   }
786b9e0af8SLouis Dionne   {
796b9e0af8SLouis Dionne     std::unique_ptr<A const, D> u(new A, D{});
806b9e0af8SLouis Dionne     A const* uPointee = u.get();
816b9e0af8SLouis Dionne     std::shared_ptr s(std::move(u));
826b9e0af8SLouis Dionne     ASSERT_SAME_TYPE(decltype(s), std::shared_ptr<A const>);
836b9e0af8SLouis Dionne     assert(u == nullptr);
846b9e0af8SLouis Dionne     assert(s.get() == uPointee);
856b9e0af8SLouis Dionne   }
8683564056SLogan Smith 
8783564056SLogan Smith   return 0;
8883564056SLogan Smith }
89