1//Copyright (c) 2006-2009 Emil Dotchevski and Reverge Studios, Inc.
2
3//Distributed under the Boost Software License, Version 1.0. (See accompanying
4//file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5
6#include <boost/exception/exception.hpp>
7#include <boost/detail/lightweight_test.hpp>
8
9struct
10test_type
11 {
12 test_type( int & count ):
13 count_(count)
14 {
15 BOOST_TEST(count_==42);
16 count_=0;
17 }
18
19 ~test_type()
20 {
21 BOOST_TEST(!count_);
22 count_=42;
23 }
24
25 void
26 add_ref()
27 {
28 ++count_;
29 }
30
31 bool
32 release()
33 {
34 if( --count_ )
35 return false;
36 else
37 {
38 delete this;
39 return true;
40 }
41 }
42
43 private:
44
45 test_type( test_type const & );
46 test_type & operator=( test_type const & );
47
48 int & count_;
49 };
50
51int
52main()
53 {
54 using boost::exception_detail::refcount_ptr;
55
56 {
57 refcount_ptr<test_type> x;
58 BOOST_TEST(!x.get());
59 }
60
61 {
62 int count=42;
63 test_type * a=new test_type(count);
64 BOOST_TEST(!count);
65 {
66 refcount_ptr<test_type> p;
67 BOOST_TEST(0==count);
68 p.adopt(px: a);
69 BOOST_TEST(p.get()==a);
70 BOOST_TEST(1==count);
71 {
72 refcount_ptr<test_type> q;
73 q.adopt(px: p.get());
74 BOOST_TEST(q.get()==a);
75 BOOST_TEST(2==count);
76 {
77 refcount_ptr<test_type> t(p);
78 BOOST_TEST(t.get()==a);
79 BOOST_TEST(3==count);
80 {
81 refcount_ptr<test_type> n;
82 n=t;
83 BOOST_TEST(n.get()==a);
84 BOOST_TEST(4==count);
85 int cb=42;
86 test_type * b=new test_type(cb);
87 BOOST_TEST(0==cb);
88 n.adopt(px: b);
89 BOOST_TEST(1==cb);
90 BOOST_TEST(n.get()==b);
91 BOOST_TEST(3==count);
92 n.adopt(px: 0);
93 BOOST_TEST(42==cb);
94 }
95 BOOST_TEST(t.get()==a);
96 BOOST_TEST(3==count);
97 }
98 BOOST_TEST(q.get()==a);
99 BOOST_TEST(2==count);
100 }
101 BOOST_TEST(p.get()==a);
102 BOOST_TEST(1==count);
103 }
104 BOOST_TEST(42==count);
105 }
106
107 return boost::report_errors();
108 }
109

source code of boost/libs/exception/test/refcount_ptr_test.cpp