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

test/TestStringBuffer.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/Int32.h>
00019 #include <spl/text/StringBuffer.h>
00020 
00021 #ifdef DEBUG
00022 
00023 #include <spl/Log.h>
00024 
00025 static void _TestStrBuf1()
00026 {
00027         StringBuffer *sb = new StringBuffer();
00028         
00029         DEBUG_CLEAR_MEM_CHECK_POINTS();
00030         DEBUG_NOTE_MEM(sb);
00031         sb->CheckMem();
00032         DEBUG_DUMP_MEM_LEAKS();
00033         UNIT_ASSERT_MEM_NOTED("StringBuffer test 1.0");
00034 
00035         sb->Append("hi");
00036         sb->Append(Int32::ToString(2));
00037         sb->Append('z');
00038         DEBUG_CLEAR_MEM_CHECK_POINTS();
00039         DEBUG_NOTE_MEM(sb);
00040         sb->CheckMem();
00041         DEBUG_DUMP_MEM_LEAKS();
00042         UNIT_ASSERT_MEM_NOTED("StringBuffer test 1.0");
00043 
00044         UNIT_ASSERT("Contains hi2z", sb->Equals("hi2z"));
00045         UNIT_ASSERT("len==4", sb->Length() == 4);
00046         UNIT_ASSERT("sb[2]", (*sb)[2] == '2');
00047         sb->SetCharAt(2, '3');
00048         UNIT_ASSERT("Contains hi3z", sb->Equals("hi3z"));
00049         sb->Insert(2, "there");
00050         sb->ValidateMem();
00051         UNIT_ASSERT("Contains hithere3z", sb->Equals("hithere3z"));
00052         UNIT_ASSERT("not numeric", !sb->IsNumeric());
00053         UNIT_ASSERT("indexof he", sb->IndexOf("he", 0) == 3);
00054         UNIT_ASSERT("indexof HE", sb->IndexOf("HE", 0) == -1);
00055         UNIT_ASSERT("indexofIgnoreCase", sb->IndexOfIgnoreCase("HE", 0) == 3);
00056         sb->Append("     ");
00057         UNIT_ASSERT("check append 5 spaces", sb->Equals("hithere3z     "));
00058         sb->Insert(0, " ");
00059         UNIT_ASSERT("appended 6 spaces", sb->Length() == 15);
00060         UNIT_ASSERT(sb->GetChars(), sb->Equals(" hithere3z     "));
00061         sb->Trim();
00062         UNIT_ASSERT("trim", sb->Length() == 9); 
00063         UNIT_ASSERT("trim cont", sb->Equals("hithere3z"));
00064 
00065         DEBUG_CLEAR_MEM_CHECK_POINTS();
00066         DEBUG_NOTE_MEM(sb);
00067         sb->CheckMem();
00068         DEBUG_DUMP_MEM_LEAKS();
00069         UNIT_ASSERT_MEM_NOTED("StringBuffer test 1.2");
00070 
00071         delete sb;
00072         DEBUG_CLEAR_MEM_CHECK_POINTS();
00073         DEBUG_DUMP_MEM_LEAKS();
00074         UNIT_ASSERT_MEM_NOTED("StringBuffer test 1.3");
00075         Log::SWriteOkFail( "StringBuffer test 1" );
00076 }
00077 
00078 static void _TestStrBuf2()
00079 {
00080         StringBuffer sb;
00081         sb.Append("1230");
00082         UNIT_ASSERT("1234 IsNumeric", sb.IsNumeric());
00083         sb.SetLength(0);
00084         sb.Append("22.2");
00085         UNIT_ASSERT("22.2 IsNumeric", sb.IsNumeric());
00086         Log::SWriteOkFail( "StringBuffer test 2" );
00087 }
00088 
00089 static void _TestStrBuf3()
00090 {
00091         StringBuffer sb;
00092         sb.Append("1");
00093         sb.Fill('x', 2);
00094         UNIT_ASSERT("1xx", sb.Equals("1xx"));
00095         Log::SWriteOkFail( "StringBuffer Fill test" );
00096 }
00097 
00098 
00099 static void _TestStringBufferRemoveChar()
00100 {
00101         StringBuffer *str = new StringBuffer("bob");
00102         str->RemoveCharAt(0);
00103         UNIT_ASSERT( "_TestStringBufferRemoveChar 1", strcmp(str->GetChars(), "ob") == 0 );
00104         UNIT_ASSERT( "_TestStringBufferRemoveChar 2", str->Length() == 2 );
00105         DEBUG_CLEAR_MEM_CHECK_POINTS();
00106         DEBUG_NOTE_MEM( str );
00107         str->CheckMem();
00108         UNIT_ASSERT_MEM_NOTED("_TestStringBufferRemoveChar 1.0");
00109 
00110         str->RemoveCharAt(1);
00111         UNIT_ASSERT( "_TestTStringRemoveChar 3", strcmp(str->GetChars(), "o") == 0 );
00112         UNIT_ASSERT( "_TestTStringRemoveChar 4", str->Length() == 1 );
00113         DEBUG_CLEAR_MEM_CHECK_POINTS();
00114         DEBUG_NOTE_MEM( str );
00115         str->CheckMem();
00116         UNIT_ASSERT_MEM_NOTED("_TestStringBufferRemoveChar 1.1");
00117 
00118         str->RemoveCharAt(0);
00119         UNIT_ASSERT( "_TestStringBufferRemoveChar 5", strcmp(str->GetChars(), "") == 0 );
00120         UNIT_ASSERT( "_TestStringBufferRemoveChar 6", str->Length() == 0 );
00121         DEBUG_CLEAR_MEM_CHECK_POINTS();
00122         DEBUG_NOTE_MEM( str );
00123         str->CheckMem();
00124         UNIT_ASSERT_MEM_NOTED("_TestStringBufferRemoveChar 1.2");
00125 
00126         delete str;
00127         DEBUG_CLEAR_MEM_CHECK_POINTS();
00128         UNIT_ASSERT_MEM_NOTED("_TestStringBufferRemoveChar 1.3");
00129 
00130         Log::SWriteOkFail( "StringBuffer test RemoveChar" );
00131 }
00132 
00133 static void _TestStringBufferInsertChar()
00134 {
00135         StringBuffer *str = new StringBuffer("abc");
00136         str->InsertCharAt('1', 0);
00137         str->ValidateMem();
00138         UNIT_ASSERT( "_TestStringBufferInsertChar 1", strcmp(str->GetChars(), "1abc") == 0 );
00139         UNIT_ASSERT( "_TestStringBufferInsertChar 2", str->Length() == 4 );
00140         DEBUG_CLEAR_MEM_CHECK_POINTS();
00141         DEBUG_NOTE_MEM( str );
00142         str->CheckMem();
00143         UNIT_ASSERT_MEM_NOTED("_TestStringBufferInsertChar 1.0");
00144         
00145         delete str;
00146         DEBUG_CLEAR_MEM_CHECK_POINTS();
00147         UNIT_ASSERT_MEM_NOTED("_TestStringBufferInsertChar 1.1");
00148 
00149         str = new StringBuffer("abc");
00150         str->InsertCharAt('1', 3);
00151         str->ValidateMem();
00152         str->InsertCharAt('2', 4);
00153         str->ValidateMem();
00154         str->InsertCharAt('3', 5);
00155         str->ValidateMem();
00156         UNIT_ASSERT( "_TestStringBufferInsertChar 1", strcmp(str->GetChars(), "abc123") == 0 );
00157         UNIT_ASSERT( "_TestStringBufferInsertChar 2", str->Length() == 6 );
00158         DEBUG_CLEAR_MEM_CHECK_POINTS();
00159         DEBUG_NOTE_MEM( str );
00160         str->CheckMem();
00161         UNIT_ASSERT_MEM_NOTED("_TestStringBufferInsertChar 1.2");
00162 
00163         delete str;
00164         DEBUG_CLEAR_MEM_CHECK_POINTS();
00165         UNIT_ASSERT_MEM_NOTED("_TestStringBufferInsertChar 1.3");
00166         
00167         Log::SWriteOkFail( "StringBuffer test InsertChar" );
00168 }
00169 
00170 void TestStringBuffer()
00171 {
00172         _TestStrBuf1();
00173         DEBUG_CLEAR_MEM_CHECK_POINTS();
00174         DEBUG_DUMP_MEM_LEAKS();
00175 
00176         _TestStrBuf2();
00177         DEBUG_CLEAR_MEM_CHECK_POINTS();
00178         DEBUG_DUMP_MEM_LEAKS();
00179 
00180         _TestStrBuf3();
00181         DEBUG_CLEAR_MEM_CHECK_POINTS();
00182         DEBUG_DUMP_MEM_LEAKS();
00183 
00184         _TestStringBufferRemoveChar();
00185         DEBUG_CLEAR_MEM_CHECK_POINTS();
00186         DEBUG_DUMP_MEM_LEAKS();
00187 
00188         _TestStringBufferInsertChar();
00189         DEBUG_CLEAR_MEM_CHECK_POINTS();
00190         DEBUG_DUMP_MEM_LEAKS();
00191 }
00192 
00193 #endif