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

test/TestTVector.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 
00019 #if defined(DEBUG) || defined(_DEBUG)
00020 
00021 #include <spl/Int32.h>
00022 #include <spl/Log.h>
00023 #include <spl/collection/Vector.h>
00024 #include <spl/String.h>
00025 
00026 static void _TVecTest1()
00027 {
00028         Vector<int> vec;
00029         vec.Add(1);
00030         DEBUG_CLEAR_MEM_CHECK_POINTS();
00031         vec.CheckMem();
00032         UNIT_ASSERT_MEM_NOTED("TVector 1.0");
00033 
00034         Log::SWriteOkFail( "Vector test 1" );
00035 }
00036 
00037 static void _TVecTest2()
00038 {
00039         int x;
00040         Vector<int> vec;
00041 
00042         for ( x = 0; x < 100; x++ )
00043         {
00044                 vec.Add(x);
00045         }
00046         UNIT_ASSERT( "Vector count", vec.Count() == 100 );
00047         DEBUG_CLEAR_MEM_CHECK_POINTS();
00048         vec.CheckMem();
00049         UNIT_ASSERT_MEM_NOTED("Vector 2.0");
00050 
00051         for ( x = 0; x < 100; x++ )
00052         {
00053                 UNIT_ASSERT("Vec elementAt", vec.ElementAt(x) == x);
00054         }
00055         for ( x = 0; x < 100; x++ )
00056         {
00057                 vec.RemoveAt(0);
00058         }
00059         UNIT_ASSERT( "Vector count", vec.Count() == 0 );
00060         DEBUG_CLEAR_MEM_CHECK_POINTS();
00061         vec.CheckMem();
00062         UNIT_ASSERT_MEM_NOTED("Vector 2.1");
00063 
00064         Log::SWriteOkFail( "Vector test 2" );
00065 }
00066 
00067 static void _TVecTest3()
00068 {
00069         int x;
00070         Vector<int> vec;
00071 
00072         for ( x = 0; x < 100; x++ )
00073         {
00074                 vec.Insert(x, x);
00075         }
00076         for ( x = 0; x < 100; x++ )
00077         {
00078                 vec.Insert(x, 50);
00079         }
00080         UNIT_ASSERT( "Vector count", vec.Count() == 200 );
00081         DEBUG_CLEAR_MEM_CHECK_POINTS();
00082         vec.CheckMem();
00083         UNIT_ASSERT_MEM_NOTED("Vector 3.0");
00084 
00085         for ( x = 0; x < 100; x++ )
00086         {
00087                 vec.RemoveAt(50);
00088         }
00089         for ( x = 0; x < 100; x++ )
00090         {
00091                 vec.Pop();
00092         }
00093         UNIT_ASSERT( "Vector count", vec.Count() == 0 );
00094         DEBUG_CLEAR_MEM_CHECK_POINTS();
00095         vec.CheckMem();
00096         UNIT_ASSERT_MEM_NOTED("Vector 3.1");
00097 
00098         Log::SWriteOkFail( "Vector test 3" );
00099 }
00100 
00101 static void _TVecTest4()
00102 {
00103         int x;
00104         Vector<int> vec;
00105 
00106         for ( x = 0; x < 100; x++ )
00107         {
00108                 vec.Add(x);
00109         }
00110         UNIT_ASSERT( "Vector count", vec.Count() == 100 );
00111         DEBUG_CLEAR_MEM_CHECK_POINTS();
00112         vec.CheckMem();
00113         UNIT_ASSERT_MEM_NOTED("Vector 4.0");
00114 
00115         for ( x = 0; x < 100; x++ )
00116         {
00117                 vec.Remove(x);
00118         }
00119         UNIT_ASSERT( "Vector count", vec.Count() == 0 );
00120         DEBUG_CLEAR_MEM_CHECK_POINTS();
00121         vec.CheckMem();
00122         UNIT_ASSERT_MEM_NOTED("Vector 4.1");
00123 
00124         Log::SWriteOkFail( "Vector test 4" );
00125 }
00126 
00127 static void _TVecTest5()
00128 {
00129         Vector<String> vec;
00130         vec.ValidateMem();
00131 
00132         DEBUG_CLEAR_MEM_CHECK_POINTS();
00133         vec.CheckMem();
00134         UNIT_ASSERT_MEM_NOTED("Vector 5.0");
00135 
00136         vec.Add(String("hi"));
00137         vec.ValidateMem();
00138         UNIT_ASSERT( "Vector count", vec.Count() == 1 );
00139 
00140         DEBUG_CLEAR_MEM_CHECK_POINTS();
00141         vec.CheckMem();
00142         UNIT_ASSERT_MEM_NOTED("Vector 5.1");
00143 
00144         Log::SWriteOkFail( "Vector test 5" );
00145 }
00146 
00147 static void _TVecTest6()
00148 {
00149         int x;
00150         Vector<String> vec;
00151         vec.ValidateMem();
00152 
00153         DEBUG_CLEAR_MEM_CHECK_POINTS();
00154         vec.CheckMem();
00155         UNIT_ASSERT_MEM_NOTED("Vector 6.0");
00156 
00157         for ( x = 0; x < 100; x++ )
00158         {
00159                 vec.Add(String(Int32::ToString(x)));
00160         }
00161         vec.ValidateMem();
00162         UNIT_ASSERT( "Vector count", vec.Count() == 100 );
00163 
00164         int fecount = 0;
00165         foreach(s, vec)
00166         {
00167                 fecount++;
00168                 UNIT_ASSERT("String val", Int32::IsInt(s.CurrentRef()));
00169         }
00170         UNIT_ASSERT("foreach count", fecount == 100);
00171 
00172         DEBUG_CLEAR_MEM_CHECK_POINTS();
00173         vec.CheckMem();
00174         UNIT_ASSERT_MEM_NOTED("Vector 6.1");
00175 
00176         Log::SWriteOkFail( "Vector test 6" );
00177 }
00178 
00179 static void _VecRemoveAtTest()
00180 {
00181         int x;
00182         Vector<char> *vec = new Vector<char>();
00183 
00184         for ( x = 0; x < 101; x++ )
00185         {
00186                 vec->Add( (char) (x+1) );
00187         }
00188         for ( x = 0; x < 101; x++ )
00189         {
00190                 vec->RemoveAt( 0 );
00191         }
00192         ASSERT_MEM( vec, sizeof(Vector<char>) );
00193         vec->ValidateMem();
00194 
00195         DEBUG_CLEAR_MEM_CHECK_POINTS();
00196         DEBUG_NOTE_MEM( vec );
00197         vec->CheckMem();
00198         UNIT_ASSERT_MEM_NOTED("Vector va1");
00199 
00200         for ( x = 0; x < 101; x++ )
00201         {
00202                 vec->Add( (char) (x+1) );
00203         }
00204         for ( x = 0; x < 50; x++ )
00205         {
00206                 vec->RemoveAt( 0 );
00207         }
00208         for ( x = 0; x < 1011; x++ )
00209         {
00210                 vec->Add( (char) (x+1) );
00211         }
00212         while ( vec->Count() > 0 )
00213         {
00214                 vec->RemoveAt( 0 );
00215         }
00216         ASSERT_MEM( vec, sizeof(Vector<char>) );
00217         vec->ValidateMem();
00218 
00219         DEBUG_CLEAR_MEM_CHECK_POINTS();
00220         DEBUG_NOTE_MEM( vec );
00221         vec->CheckMem();
00222         UNIT_ASSERT_MEM_NOTED("Vector va1");
00223 
00224         delete vec;
00225 
00226         DEBUG_CLEAR_MEM_CHECK_POINTS();
00227         UNIT_ASSERT_MEM_NOTED("Vector va1");
00228 
00229         Log::SWriteOkFail( "Vector RemoveAt stress test" );
00230 }
00231 
00232 void TVectorTest(  )
00233 {
00234         _TVecTest1();
00235         DEBUG_CLEAR_MEM_CHECK_POINTS();
00236         UNIT_ASSERT_MEM_NOTED("Vector A");
00237 
00238         _TVecTest2();
00239         DEBUG_CLEAR_MEM_CHECK_POINTS();
00240         UNIT_ASSERT_MEM_NOTED("Vector B");
00241 
00242         _TVecTest3();
00243         DEBUG_CLEAR_MEM_CHECK_POINTS();
00244         UNIT_ASSERT_MEM_NOTED("Vector C");
00245 
00246         _TVecTest4();
00247         DEBUG_CLEAR_MEM_CHECK_POINTS();
00248         UNIT_ASSERT_MEM_NOTED("Vector D");
00249 
00250         _TVecTest5();
00251         DEBUG_CLEAR_MEM_CHECK_POINTS();
00252         UNIT_ASSERT_MEM_NOTED("Vector E");
00253 
00254         _TVecTest6();
00255         DEBUG_CLEAR_MEM_CHECK_POINTS();
00256         UNIT_ASSERT_MEM_NOTED("Vector F");
00257 
00258         _VecRemoveAtTest();
00259         DEBUG_CLEAR_MEM_CHECK_POINTS();
00260         UNIT_ASSERT_MEM_NOTED("Vector F");
00261 }
00262 
00263 #endif