removed some useless spaces; not very important, I know :)
[senf.git] / Utils / intrusive_refcount.test.cc
index 8a55096..212073b 100644 (file)
@@ -1,9 +1,9 @@
 // $Id$
 //
 // Copyright (C) 2006
-// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
-// Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
-//     Stefan Bund <stefan.bund@fokus.fraunhofer.de>
+// Fraunhofer Institute for Open Communication Systems (FOKUS)
+// Competence Center NETwork research (NET), St. Augustin, GERMANY
+//     Stefan Bund <g0dil@berlios.de>
 //
 // This program is free software; you can redistribute it and/or modify
 // it under the terms of the GNU General Public License as published by
@@ -29,7 +29,7 @@
 #include "intrusive_refcount.hh"
 #include <boost/intrusive_ptr.hpp>
 
-#include <boost/test/auto_unit_test.hpp>
+#include "../Utils/auto_unit_test.hh"
 #include <boost/test/test_tools.hpp>
 
 #define prefix_
@@ -48,6 +48,25 @@ namespace {
     };
 
     unsigned Tester::counter = 0;
+
+    struct TesterCustom
+        : public senf::intrusive_refcount_t<TesterCustom>
+    {
+        typedef boost::intrusive_ptr<TesterCustom> ptr;
+        typedef senf::intrusive_refcount_t<TesterCustom> super;
+
+        TesterCustom() { ++counter; }
+        ~TesterCustom() { --counter; }
+
+        void add_ref() { super::add_ref(); ++refs; }
+        bool release() { --refs; super::release(); return false; }
+
+        static unsigned counter;
+        static unsigned refs;
+    };
+
+    unsigned TesterCustom::counter = 0;
+    unsigned TesterCustom::refs = 0;
 }
 
 BOOST_AUTO_UNIT_TEST(intrusive_refcount)
@@ -74,6 +93,42 @@ BOOST_AUTO_UNIT_TEST(intrusive_refcount)
     BOOST_CHECK_EQUAL(Tester::counter,0u);
 }
 
+BOOST_AUTO_UNIT_TEST(intrusive_refcount_t)
+{
+    BOOST_CHECK_EQUAL(TesterCustom::counter,0u);
+    BOOST_CHECK_EQUAL(TesterCustom::refs,0u);
+
+    TesterCustom::ptr p (new TesterCustom);
+    BOOST_CHECK_EQUAL(TesterCustom::counter,1u);
+    BOOST_CHECK_EQUAL(p->refcount(),1u);
+    BOOST_CHECK_EQUAL(p->is_shared(),false);
+    BOOST_CHECK_EQUAL(TesterCustom::refs,1u);
+    
+
+    {
+        TesterCustom::ptr pp (p);
+        BOOST_CHECK_EQUAL(TesterCustom::counter,1u);
+        BOOST_CHECK_EQUAL(p->refcount(),2u);
+        BOOST_CHECK_EQUAL(p->is_shared(),true);
+        BOOST_CHECK_EQUAL(TesterCustom::refs,2u);
+    }
+
+    BOOST_CHECK_EQUAL(TesterCustom::counter,1u);
+    BOOST_CHECK_EQUAL(p->refcount(),1u);
+    BOOST_CHECK_EQUAL(p->is_shared(),false);
+    BOOST_CHECK_EQUAL(TesterCustom::refs,1u);
+
+    {
+        TesterCustom * pp (p.get());
+        p = 0;
+        BOOST_CHECK_EQUAL(TesterCustom::counter,1u);
+        BOOST_CHECK_EQUAL(TesterCustom::refs,0u);
+        // The TesterCustom leaks ...
+        delete pp;
+        BOOST_CHECK_EQUAL(TesterCustom::counter,0u);
+    }
+}
+
 ///////////////////////////////cc.e////////////////////////////////////////
 #undef prefix_