• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

test/TestObjPool.cpp

00001 /*
00002  *   This file is part of the Standard Portable Library (SPL).
00003  *
00004  *   SPL is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   SPL is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with SPL.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 #include <spl/Debug.h>
00018 #include <spl/Log.h>
00019 #include <spl/collection/ObjectPool.h>
00020 
00021 #ifdef DEBUG
00022 
00023 class TestPoolObj : public IPoolable
00024 {
00025 protected:
00026         int m_count;
00027 public:
00028         TestPoolObj(int count) : m_count(count) {}
00029         ~TestPoolObj() {}
00030         virtual void OnPreRemoveFromPool()
00031         {
00032         }
00033         virtual void OnPreReturnToPool()
00034         {
00035         }
00036         int Count() { return m_count; }
00037 
00038 #if defined(DEBUG) || defined(_DEBUG)
00039         void CheckMem() const {}
00040         void ValidateMem() const {}
00041 #endif
00042 };
00043 
00044 class TestPoolFactory : public IPoolableFactory
00045 {
00046 protected:
00047         int m_count;
00048 public:
00049         TestPoolFactory() : m_count(0) {}
00050         virtual ~TestPoolFactory() {}   // ensures destructors are called
00051 
00052         virtual IPoolable *CreateInstance()
00053         {
00054                 return new TestPoolObj(m_count++);
00055         }
00056         virtual void Destroy( IPoolable *instance ) 
00057         {
00058                 delete (TestPoolObj *)instance;
00059         }
00060 
00061 #if defined(DEBUG) || defined(_DEBUG)
00062         void CheckMem() const {}
00063         void ValidateMem() const {}
00064 #endif
00065 };
00066 
00067 static void _TestObjPool1()
00068 {
00069         TestPoolFactory factory;
00070         ObjectPool pool(&factory, 0, 1, 2);
00071         Log::SWriteOkFail( "ObjectPool test 1" );
00072 }
00073 
00074 static void _TestObjPool2()
00075 {
00076         TestPoolFactory factory;
00077         ObjectPool pool(&factory, 0, 1, 2);
00078 
00079         TestPoolObj *obj1 = (TestPoolObj *)pool.GetObject();
00080         UNIT_ASSERT( "object 1", NULL != obj1 && obj1->Count() == 0 );
00081         UNIT_ASSERT( "pool count 1", 1 == pool.Count() );
00082 
00083         TestPoolObj *obj2 = (TestPoolObj *)pool.GetObject();
00084         UNIT_ASSERT( "object 2", NULL != obj2 && obj2->Count() == 1 );
00085         UNIT_ASSERT( "pool count 2", 2 == pool.Count() );
00086 
00087         TestPoolObj *obj3 = (TestPoolObj *)pool.GetObject();
00088         UNIT_ASSERT( "should be null", NULL == obj3 );
00089         UNIT_ASSERT( "no object created", 2 == pool.Count() );
00090 
00091         DEBUG_CLEAR_MEM_CHECK_POINTS();
00092         DEBUG_NOTE_MEM( obj1 );
00093         DEBUG_NOTE_MEM( obj2 );
00094         pool.CheckMem();
00095         DEBUG_DUMP_MEM_LEAKS();
00096         UNIT_ASSERT_MEM_NOTED("ObjectPool test 2.1");
00097 
00098         pool.ReleaseObject( obj1 );
00099         UNIT_ASSERT( "perfered count is one, so should delete this", 1 == pool.Count() );
00100 
00101         pool.ReleaseObject( obj2 );
00102         UNIT_ASSERT( "should keep this one", 1 == pool.Count() );
00103 
00104         DEBUG_CLEAR_MEM_CHECK_POINTS();
00105         pool.CheckMem();
00106         DEBUG_DUMP_MEM_LEAKS();
00107         UNIT_ASSERT_MEM_NOTED("ObjectPool test 2.1");
00108 
00109         Log::SWriteOkFail( "ObjectPool test 2" );
00110 }
00111 
00112 void TestObjectPool()
00113 {
00114         _TestObjPool1();
00115         DEBUG_CLEAR_MEM_CHECK_POINTS();
00116         DEBUG_DUMP_MEM_LEAKS();
00117 
00118         _TestObjPool2();
00119         DEBUG_CLEAR_MEM_CHECK_POINTS();
00120         DEBUG_DUMP_MEM_LEAKS();
00121 }
00122 
00123 #endif