4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
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
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.
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.
19 // The Original Code is Fraunhofer FOKUS code.
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.
26 // Stefan Bund <g0dil@berlios.de>
29 \brief intrusive_refcount unit tests */
31 //#include "intrusive_refcount.test.hh"
32 //#include "intrusive_refcount.test.ih"
35 #include "intrusive_refcount.hh"
36 #include <boost/intrusive_ptr.hpp>
38 #include <senf/Utils/auto_unit_test.hh>
39 #include <boost/test/test_tools.hpp>
42 //-/////////////////////////////////////////////////////////////////////////////////////////////////
46 : public senf::intrusive_refcount
48 typedef boost::intrusive_ptr<Tester> ptr;
50 Tester() { ++counter; }
51 ~Tester() { --counter; }
53 static unsigned counter;
56 unsigned Tester::counter = 0;
59 : public senf::intrusive_refcount_t<TesterCustom>
61 typedef boost::intrusive_ptr<TesterCustom> ptr;
62 typedef senf::intrusive_refcount_t<TesterCustom> super;
64 TesterCustom() { ++counter; }
65 ~TesterCustom() { --counter; }
67 void add_ref() { super::add_ref(); ++refs; }
68 bool release() { --refs; super::release(); return false; }
70 static unsigned counter;
74 unsigned TesterCustom::counter = 0;
75 unsigned TesterCustom::refs = 0;
78 SENF_AUTO_UNIT_TEST(intrusive_refcount)
80 BOOST_CHECK_EQUAL(Tester::counter,0u);
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);
89 BOOST_CHECK_EQUAL(Tester::counter,1u);
90 BOOST_CHECK_EQUAL(p->refcount(),2u);
91 BOOST_CHECK_EQUAL(p->is_shared(),true);
94 BOOST_CHECK_EQUAL(Tester::counter,1u);
95 BOOST_CHECK_EQUAL(p->refcount(),1u);
96 BOOST_CHECK_EQUAL(p->is_shared(),false);
99 BOOST_CHECK_EQUAL(Tester::counter,0u);
102 SENF_AUTO_UNIT_TEST(intrusive_refcount_t)
104 BOOST_CHECK_EQUAL(TesterCustom::counter,0u);
105 BOOST_CHECK_EQUAL(TesterCustom::refs,0u);
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);
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);
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);
128 TesterCustom * pp (p.get());
130 BOOST_CHECK_EQUAL(TesterCustom::counter,1u);
131 BOOST_CHECK_EQUAL(TesterCustom::refs,0u);
132 // The TesterCustom leaks ...
134 BOOST_CHECK_EQUAL(TesterCustom::counter,0u);
138 //-/////////////////////////////////////////////////////////////////////////////////////////////////
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