libcxx

libcxx mirror with random patches
git clone https://git.neptards.moe/neptards/libcxx.git
Log | Files | Refs

weak_ptr.pass.cpp (2489B)


      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 // weak_ptr(const weak_ptr& r);
     15 // weak_ptr(weak_ptr &&r)
     16 
     17 #include <memory>
     18 #include <type_traits>
     19 #include <cassert>
     20 
     21 #include "test_macros.h"
     22 
     23 struct B
     24 {
     25     static int count;
     26 
     27     B() {++count;}
     28     B(const B&) {++count;}
     29     virtual ~B() {--count;}
     30 };
     31 
     32 int B::count = 0;
     33 
     34 struct A
     35     : public B
     36 {
     37     static int count;
     38 
     39     A() {++count;}
     40     A(const A&) {++count;}
     41     ~A() {--count;}
     42 };
     43 
     44 int A::count = 0;
     45 
     46 struct C
     47 {
     48     static int count;
     49 
     50     C() {++count;}
     51     C(const C&) {++count;}
     52     virtual ~C() {--count;}
     53 };
     54 
     55 int C::count = 0;
     56 
     57 template <class T>
     58 std::weak_ptr<T> source (std::shared_ptr<T> p) { return std::weak_ptr<T>(p); }
     59 
     60 #if TEST_STD_VER >= 11
     61 template <class T>
     62 void sink (std::weak_ptr<T> &&) {}
     63 #endif
     64 
     65 int main()
     66 {
     67     {
     68         const std::shared_ptr<A> ps(new A);
     69         const std::weak_ptr<A> pA(ps);
     70         assert(pA.use_count() == 1);
     71         assert(B::count == 1);
     72         assert(A::count == 1);
     73         {
     74             std::weak_ptr<A> pB(pA);
     75             assert(B::count == 1);
     76             assert(A::count == 1);
     77             assert(pB.use_count() == 1);
     78             assert(pA.use_count() == 1);
     79         }
     80         assert(pA.use_count() == 1);
     81         assert(B::count == 1);
     82         assert(A::count == 1);
     83     }
     84     assert(B::count == 0);
     85     assert(A::count == 0);
     86     {
     87         std::weak_ptr<A> pA;
     88         assert(pA.use_count() == 0);
     89         assert(B::count == 0);
     90         assert(A::count == 0);
     91         {
     92             std::weak_ptr<A> pB(pA);
     93             assert(B::count == 0);
     94             assert(A::count == 0);
     95             assert(pB.use_count() == 0);
     96             assert(pA.use_count() == 0);
     97         }
     98         assert(pA.use_count() == 0);
     99         assert(B::count == 0);
    100         assert(A::count == 0);
    101     }
    102     assert(B::count == 0);
    103     assert(A::count == 0);
    104 
    105 #if TEST_STD_VER >= 11
    106     {
    107         std::shared_ptr<A> ps(new A);
    108         std::weak_ptr<A> pA = source(ps);
    109         assert(pA.use_count() == 1);
    110         assert(A::count == 1);
    111         sink(std::move(pA)); // kill off the weak pointer
    112     }
    113     assert(B::count == 0);
    114     assert(A::count == 0);
    115 #endif
    116 }