switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Utils / intrusive_refcount.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief intrusive_refcount unit tests */
30
31 //#include "intrusive_refcount.test.hh"
32 //#include "intrusive_refcount.test.ih"
33
34 // Custom includes
35 #include "intrusive_refcount.hh"
36 #include <boost/intrusive_ptr.hpp>
37
38 #include <senf/Utils/auto_unit_test.hh>
39 #include <boost/test/test_tools.hpp>
40
41 #define prefix_
42 //-/////////////////////////////////////////////////////////////////////////////////////////////////
43
44 namespace {
45     struct Tester
46         : public senf::intrusive_refcount
47     {
48         typedef boost::intrusive_ptr<Tester> ptr;
49
50         Tester() { ++counter; }
51         ~Tester() { --counter; }
52
53         static unsigned counter;
54     };
55
56     unsigned Tester::counter = 0;
57
58     struct TesterCustom
59         : public senf::intrusive_refcount_t<TesterCustom>
60     {
61         typedef boost::intrusive_ptr<TesterCustom> ptr;
62         typedef senf::intrusive_refcount_t<TesterCustom> super;
63
64         TesterCustom() { ++counter; }
65         ~TesterCustom() { --counter; }
66
67         void add_ref() { super::add_ref(); ++refs; }
68         bool release() { --refs; super::release(); return false; }
69
70         static unsigned counter;
71         static unsigned refs;
72     };
73
74     unsigned TesterCustom::counter = 0;
75     unsigned TesterCustom::refs = 0;
76 }
77
78 SENF_AUTO_UNIT_TEST(intrusive_refcount)
79 {
80     BOOST_CHECK_EQUAL(Tester::counter,0u);
81
82     Tester::ptr p (new Tester);
83     BOOST_CHECK_EQUAL(Tester::counter,1u);
84     BOOST_CHECK_EQUAL(p->refcount(),1u);
85     BOOST_CHECK_EQUAL(p->is_shared(),false);
86
87     {
88         Tester::ptr pp (p);
89         BOOST_CHECK_EQUAL(Tester::counter,1u);
90         BOOST_CHECK_EQUAL(p->refcount(),2u);
91         BOOST_CHECK_EQUAL(p->is_shared(),true);
92     }
93
94     BOOST_CHECK_EQUAL(Tester::counter,1u);
95     BOOST_CHECK_EQUAL(p->refcount(),1u);
96     BOOST_CHECK_EQUAL(p->is_shared(),false);
97
98     p = 0;
99     BOOST_CHECK_EQUAL(Tester::counter,0u);
100 }
101
102 SENF_AUTO_UNIT_TEST(intrusive_refcount_t)
103 {
104     BOOST_CHECK_EQUAL(TesterCustom::counter,0u);
105     BOOST_CHECK_EQUAL(TesterCustom::refs,0u);
106
107     TesterCustom::ptr p (new TesterCustom);
108     BOOST_CHECK_EQUAL(TesterCustom::counter,1u);
109     BOOST_CHECK_EQUAL(p->refcount(),1u);
110     BOOST_CHECK_EQUAL(p->is_shared(),false);
111     BOOST_CHECK_EQUAL(TesterCustom::refs,1u);
112
113
114     {
115         TesterCustom::ptr pp (p);
116         BOOST_CHECK_EQUAL(TesterCustom::counter,1u);
117         BOOST_CHECK_EQUAL(p->refcount(),2u);
118         BOOST_CHECK_EQUAL(p->is_shared(),true);
119         BOOST_CHECK_EQUAL(TesterCustom::refs,2u);
120     }
121
122     BOOST_CHECK_EQUAL(TesterCustom::counter,1u);
123     BOOST_CHECK_EQUAL(p->refcount(),1u);
124     BOOST_CHECK_EQUAL(p->is_shared(),false);
125     BOOST_CHECK_EQUAL(TesterCustom::refs,1u);
126
127     {
128         TesterCustom * pp (p.get());
129         p = 0;
130         BOOST_CHECK_EQUAL(TesterCustom::counter,1u);
131         BOOST_CHECK_EQUAL(TesterCustom::refs,0u);
132         // The TesterCustom leaks ...
133         delete pp;
134         BOOST_CHECK_EQUAL(TesterCustom::counter,0u);
135     }
136 }
137
138 //-/////////////////////////////////////////////////////////////////////////////////////////////////
139 #undef prefix_
140
141 \f
142 // Local Variables:
143 // mode: c++
144 // fill-column: 100
145 // c-file-style: "senf"
146 // indent-tabs-mode: nil
147 // ispell-local-dictionary: "american"
148 // compile-command: "scons -u test"
149 // comment-column: 40
150 // End: