• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 //  sp_recursive_assign_test.cpp
3 //
4 //  Copyright 2009 Peter Dimov
5 //
6 //  Distributed under the Boost Software License, Version 1.0.
7 //  See accompanying file LICENSE_1_0.txt or copy at
8 //  http://www.boost.org/LICENSE_1_0.txt
9 //
10 
11 
12 #include <boost/shared_ptr.hpp>
13 #include <boost/core/lightweight_test.hpp>
14 
15 //
16 
17 class X
18 {
19 public:
20 
21     static int instances;
22 
X()23     X()
24     {
25         ++instances;
26     }
27 
~X()28     ~X()
29     {
30         --instances;
31     }
32 
33 private:
34 
35     X( X const& );
36 };
37 
38 int X::instances = 0;
39 
40 class Y
41 {
42 public:
43 
44     static int instances;
45 
Y()46     Y()
47     {
48         ++instances;
49     }
50 
~Y()51     ~Y()
52     {
53         --instances;
54     }
55 
56 private:
57 
58     Y( Y const& );
59 };
60 
61 int Y::instances = 0;
62 
63 static boost::shared_ptr<void> s_pv;
64 
65 class Z
66 {
67 public:
68 
69     static int instances;
70 
Z()71     Z()
72     {
73         ++instances;
74     }
75 
~Z()76     ~Z()
77     {
78         --instances;
79 
80         boost::shared_ptr<void> pv( new Y );
81         s_pv = pv;
82     }
83 
84 private:
85 
86     Z( Z const& );
87 };
88 
89 int Z::instances = 0;
90 
main()91 int main()
92 {
93     BOOST_TEST( X::instances == 0 );
94     BOOST_TEST( Y::instances == 0 );
95     BOOST_TEST( Z::instances == 0 );
96 
97     {
98         boost::shared_ptr<void> pv( new Z );
99         s_pv = pv;
100     }
101 
102     BOOST_TEST( X::instances == 0 );
103     BOOST_TEST( Y::instances == 0 );
104     BOOST_TEST( Z::instances == 1 );
105 
106     {
107         boost::shared_ptr<void> pv( new X );
108         s_pv = pv;
109     }
110 
111     BOOST_TEST( X::instances == 0 );
112     BOOST_TEST( Y::instances == 1 );
113     BOOST_TEST( Z::instances == 0 );
114 
115     s_pv.reset();
116 
117     BOOST_TEST( X::instances == 0 );
118     BOOST_TEST( Y::instances == 0 );
119     BOOST_TEST( Z::instances == 0 );
120 
121     return boost::report_errors();
122 }
123