lock.pass.cpp revision f5256e16dfc425c1d466f6308d4026d529ce9e0b
1//===----------------------------------------------------------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// <memory>
11
12// weak_ptr
13
14// shared_ptr<T> lock() const;
15
16#include <memory>
17#include <cassert>
18
19struct A
20{
21    static int count;
22
23    A() {++count;}
24    A(const A&) {++count;}
25    ~A() {--count;}
26};
27
28int A::count = 0;
29
30int main()
31{
32    {
33        std::weak_ptr<A> wp;
34        std::shared_ptr<A> sp = wp.lock();
35        assert(sp.use_count() == 0);
36        assert(sp.get() == 0);
37        assert(A::count == 0);
38    }
39    {
40        std::shared_ptr<A> sp0(new A);
41        std::weak_ptr<A> wp(sp0);
42        std::shared_ptr<A> sp = wp.lock();
43        assert(sp.use_count() == 2);
44        assert(sp.get() == sp0.get());
45        assert(A::count == 1);
46    }
47    assert(A::count == 0);
48    {
49        std::shared_ptr<A> sp0(new A);
50        std::weak_ptr<A> wp(sp0);
51        sp0.reset();
52        std::shared_ptr<A> sp = wp.lock();
53        assert(sp.use_count() == 0);
54        assert(sp.get() == 0);
55        assert(A::count == 0);
56    }
57    assert(A::count == 0);
58}
59