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 T> void swap(weak_ptr<T>& a, weak_ptr<T>& b)
15 
16 #include <memory>
17 #include <cassert>
18 
19 #include "test_macros.h"
20 
21 struct A
22 {
23     static int count;
24 
25     A() {++count;}
26     A(const A&) {++count;}
27     ~A() {--count;}
28 };
29 
30 int A::count = 0;
31 
32 int main(int, char**)
33 {
34     {
35         A* ptr1 = new A;
36         A* ptr2 = new A;
37         std::shared_ptr<A> p1(ptr1);
38         std::weak_ptr<A> w1(p1);
39         {
40             std::shared_ptr<A> p2(ptr2);
41             std::weak_ptr<A> w2(p2);
42             swap(w1, w2);
43             assert(w1.use_count() == 1);
44             assert(w1.lock().get() == ptr2);
45             assert(w2.use_count() == 1);
46             assert(w2.lock().get() == ptr1);
47             assert(A::count == 2);
48         }
49     }
50     assert(A::count == 0);
51 
52   return 0;
53 }
54