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

test/testRefCountPtr.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/RefCountPtr.h>
00020 #include <spl/String.h>
00021 
00022 #ifdef DEBUG
00023 
00024 static void _TestRefCountPtr1()
00025 {
00026         RefCountPtr<int> pi(new int[1]);
00027 
00028         DEBUG_CLEAR_MEM_CHECK_POINTS();
00029         pi.CheckMem();
00030         DEBUG_DUMP_MEM_LEAKS();
00031         UNIT_ASSERT_MEM_NOTED("RefCountPtr test 1.0");
00032 
00033         UNIT_ASSERT("1", pi.ReferenceCount() == 1);
00034 
00035         {
00036                 RefCountPtr<int> pi2 = pi;
00037                 pi2.ValidateMem();
00038                 pi.ValidateMem();
00039                 UNIT_ASSERT("assign", pi2.ReferenceCount() == 2);
00040                 UNIT_ASSERT("assign", pi.ReferenceCount() == 2);
00041 
00042                 *pi2 = 6;
00043         }
00044 
00045         UNIT_ASSERT("1", pi.ReferenceCount() == 1);
00046         UNIT_ASSERT("6", *pi == 6);
00047 
00048         DEBUG_CLEAR_MEM_CHECK_POINTS();
00049         pi.CheckMem();
00050         DEBUG_DUMP_MEM_LEAKS();
00051         UNIT_ASSERT_MEM_NOTED("RefCountPtr test 1.1");
00052 
00053         Log::SWriteOkFail( "RefCountPtr test" );
00054 }
00055 
00056 static StringPtr _TestReturnStringPtrNull()
00057 {
00058         return StringPtr();
00059 }
00060 
00061 void _TestRefCountPtrRegression1()
00062 {
00063         {
00064                 StringPtr ptr = _TestReturnStringPtrNull();
00065                 UNIT_ASSERT("Should be null", ptr.IsNull());
00066                 UNIT_ASSERT("Should be not notnull", ! ptr.IsNotNull());
00067         }
00068         
00069         DEBUG_CLEAR_MEM_CHECK_POINTS();
00070         DEBUG_DUMP_MEM_LEAKS();
00071         UNIT_ASSERT_MEM_NOTED("RefCountPtr regression 1.1");
00072 
00073         Log::SWriteOkFail( "RefCountPtr StringPtr() NULL regression" );
00074 }
00075 
00076 void _TestRefCountPtr()
00077 {
00078         _TestRefCountPtr1();
00079         DEBUG_CLEAR_MEM_CHECK_POINTS();
00080         UNIT_ASSERT_MEM_NOTED("_TestRefCountPtr");
00081         
00082         _TestRefCountPtrRegression1();
00083         DEBUG_CLEAR_MEM_CHECK_POINTS();
00084         UNIT_ASSERT_MEM_NOTED("_TestRefCountPtrRegression1");
00085 }
00086 
00087 #endif