Upstream version 8.36.169.0
[platform/framework/web/crosswalk.git] / src / third_party / libc++ / trunk / test / utilities / memory / util.smartptr / util.smartptr.weak / util.smartptr.weak.assign / weak_ptr_Y.pass.cpp
1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // <memory>
11
12 // weak_ptr
13
14 // template<class Y> weak_ptr& operator=(const weak_ptr<Y>& r);
15
16 #include <memory>
17 #include <type_traits>
18 #include <cassert>
19
20 struct B
21 {
22     static int count;
23
24     B() {++count;}
25     B(const B&) {++count;}
26     virtual ~B() {--count;}
27 };
28
29 int B::count = 0;
30
31 struct A
32     : public B
33 {
34     static int count;
35
36     A() {++count;}
37     A(const A&) {++count;}
38     ~A() {--count;}
39 };
40
41 int A::count = 0;
42
43 int main()
44 {
45     {
46         const std::shared_ptr<A> ps(new A);
47         const std::weak_ptr<A> pA(ps);
48         {
49             std::weak_ptr<B> pB;
50             pB = pA;
51             assert(B::count == 1);
52             assert(A::count == 1);
53             assert(pB.use_count() == 1);
54             assert(pA.use_count() == 1);
55         }
56         assert(pA.use_count() == 1);
57         assert(B::count == 1);
58         assert(A::count == 1);
59     }
60     assert(B::count == 0);
61     assert(A::count == 0);
62
63     {
64         const std::shared_ptr<A> ps(new A);
65         std::weak_ptr<A> pA(ps);
66         {
67             std::weak_ptr<B> pB;
68             pB = std::move(pA);
69             assert(B::count == 1);
70             assert(A::count == 1);
71             assert(pB.use_count() == 1);
72         }
73         assert(B::count == 1);
74         assert(A::count == 1);
75     }
76     assert(B::count == 0);
77     assert(A::count == 0);
78 }