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

test/TestDebug.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 <stdlib.h>
00018 
00019 #include <spl/Debug.h>
00020 #include <spl/Log.h>
00021 
00022 #ifdef DEBUG
00023 
00024 static void simplemalloctest(  )
00025 {
00026         char *cp;
00027 
00028         DEBUG_CHECK_POINT_HEAP();
00029 
00030         cp = (char *)malloc( 2 * sizeof(char) );
00031         UNIT_ASSERT_MEM("debugmalloctest", cp, 2*sizeof(char));
00032 
00033         cp[1] = '\0';
00034         cp[0] = 'A';
00035         DEBUG_CLEAR_MEM_CHECK_POINTS();
00036 
00037         UNIT_ASSERT_PTR("debugmalloctest", cp);
00038         UNIT_ASSERT_MEM("debugmalloctest", cp, 2*sizeof(char));
00039         DEBUG_NOTE_MEM_ALLOCATION( cp );
00040         UNIT_ASSERT_MEM_NOTED("debugmalloctest");
00041 
00042         free( cp );
00043 
00044         UNIT_ASSERT_CHECK_POINT("debugmalloctest");
00045         Log::SWriteOkFail( "debug simple malloc" );
00046 }
00047 
00048 static void debugmalloctest(  )
00049 {
00050         void *allocs[1000];
00051         int x;
00052 
00053         DEBUG_CHECK_POINT_HEAP();
00054 
00055         for ( x = 0; x < 1000; x++ )
00056         {
00057                 allocs[x] = NULL;
00058         }
00059         for ( x = 0; x < 1000; x++ )
00060         {
00061                 allocs[x] = malloc( 100 );
00062         }
00063         DEBUG_CLEAR_MEM_CHECK_POINTS();
00064 
00065         for ( x = 0; x < 1000; x++ )
00066         {
00067                 UNIT_ASSERT_PTR("debugmalloctest", allocs[x]);
00068                 UNIT_ASSERT_MEM("debugmalloctest", allocs[x], 100);
00069                 DEBUG_NOTE_MEM_ALLOCATION( allocs[x] );
00070         }
00071         UNIT_ASSERT_MEM_NOTED("debugmalloctest");
00072 
00073         for ( x = 0; x < 1000; x++ )
00074         {
00075                 free(allocs[x]);
00076         }
00077         UNIT_ASSERT_CHECK_POINT("debugmalloctest");
00078         Log::SWriteOkFail( "debug malloc" );
00079 }
00080 
00081 static void pointercheck(  )
00082 {
00083         char *cp;
00084 
00085         cp = (char *)malloc(10);
00086         UNIT_ASSERT( "pointercheck", _debugCheckPtr(cp) );
00087         UNIT_ASSERT( "pointercheck", _debugCheckPtr(&cp[4]) );
00088         UNIT_ASSERT( "pointercheck", ! _debugCheckPtr(&cp[10]) );
00089         free(cp);
00090 
00091         Log::SWriteOkFail( "debug pointer check" );
00092 }
00093 
00094 static void memblockcheck(  )
00095 {
00096         char *cp;
00097         
00098         cp = (char *)malloc(10);
00099         UNIT_ASSERT( "", _debugCheckBlock(cp, 10) );
00100         free(cp);
00101 
00102         Log::SWriteOkFail( "debug mem block check" );
00103 }
00104 
00105 void _testdebug(  )
00106 {
00107         simplemalloctest(  );
00108         debugmalloctest(  );
00109         pointercheck(  );
00110         memblockcheck(  );
00111         UNIT_ASSERT_MEM_NOTED("testdebug");
00112 }
00113 
00114 #endif /* DEBUG */