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

test/TestDelimitedFile.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 #ifdef DEBUG
00020 #include <spl/io/DelimitedFile.h>
00021 #include <spl/Log.h>
00022 #include <spl/io/MemoryStream.h>
00023 
00024 static void _TestDelimFile1()
00025 {
00026         MemoryStreamPtr ms = MemoryStreamPtr(new MemoryStream());
00027         const char *csv = "abc,123,!@#\nrfd,\"567\",$%^\n";
00028         Array<byte> csva((const byte *)csv, (int)strlen(csv));
00029         ms->Write(csva);
00030 
00031         TextReader reader( ms );
00032         DelimitedFilePtr df = DelimitedFile::Parse(reader, ',');
00033 
00034         DEBUG_CLEAR_MEM_CHECK_POINTS();
00035         reader.CheckMem();
00036         csva.CheckMem();
00037         df.CheckMem();
00038         DEBUG_DUMP_MEM_LEAKS();
00039         UNIT_ASSERT_MEM_NOTED("DelimitedFile 1.0");
00040 
00041         DEBUG_CLEAR_MEM_CHECK_POINTS();
00042         df.CheckMem();
00043         reader.CheckMem();
00044         csva.CheckMem();
00045         DEBUG_DUMP_MEM_LEAKS();
00046         UNIT_ASSERT_MEM_NOTED("DelimitedFile 1.0");
00047 
00048         UNIT_ASSERT("row count", df->RowCount() == 2);
00049         UNIT_ASSERT("col count 1", df->RowAt(0)->Count() == 3);
00050         UNIT_ASSERT("col count 2", df->RowAt(1)->Count() == 3);
00051 
00052         UNIT_ASSERT("abc", df->CellAt(0, 0)->ToString()->Equals("abc"));
00053         UNIT_ASSERT("123", df->CellAt(0, 1)->ToString()->Equals("123"));
00054         UNIT_ASSERT("!@#", df->CellAt(0, 2)->ToString()->Equals("!@#"));
00055         UNIT_ASSERT("rfd", df->CellAt(1, 0)->ToString()->Equals("rfd"));
00056         UNIT_ASSERT("567", df->CellAt(1, 1)->ToString()->Equals("567"));
00057         UNIT_ASSERT("$%^", df->CellAt(1, 2)->ToString()->Equals("$%^"));
00058 
00059         DEBUG_CLEAR_MEM_CHECK_POINTS();
00060         csva.CheckMem();
00061         df.CheckMem();
00062         reader.CheckMem();
00063         DEBUG_DUMP_MEM_LEAKS();
00064         UNIT_ASSERT_MEM_NOTED("DelimitedFile 1.2");
00065         Log::SWriteOkFail( "DelimitedFile 1" );
00066 }
00067 
00068 static void _TestDelimFile2()
00069 {
00070         MemoryStreamPtr ms = MemoryStreamPtr(new MemoryStream());
00071         const char *csv = "abc,123,!@#\nrfd,\"567\",\n,,\n";
00072         Array<byte> csva((const byte *)csv, (int)strlen(csv));
00073         ms->Write(csva);
00074         TextReader reader( ms );
00075         DelimitedFilePtr df = DelimitedFile::Parse(reader, ',');
00076 
00077         UNIT_ASSERT("row count", df->RowCount() == 3);
00078         UNIT_ASSERT("col count 1", df->RowAt(0)->Count() == 3);
00079         UNIT_ASSERT("col count 2", df->RowAt(1)->Count() == 3);
00080         UNIT_ASSERT("col count 3", df->RowAt(2)->Count() == 3);
00081 
00082         UNIT_ASSERT("abc", df->CellAt(0, 0)->ToString()->Equals("abc"));
00083         UNIT_ASSERT("123", df->CellAt(0, 1)->ToString()->Equals("123"));
00084         UNIT_ASSERT("!@#", df->CellAt(0, 2)->ToString()->Equals("!@#"));
00085         UNIT_ASSERT("rfd", df->CellAt(1, 0)->ToString()->Equals("rfd"));
00086         UNIT_ASSERT("567", df->CellAt(1, 1)->ToString()->Equals("567"));
00087         UNIT_ASSERT("$%^", df->CellAt(1, 2)->ToString()->Equals(""));
00088         UNIT_ASSERT("\\0", df->CellAt(2, 0)->ToString()->Equals(""));
00089         UNIT_ASSERT("\\0", df->CellAt(2, 1)->ToString()->Equals(""));
00090         UNIT_ASSERT("\\0", df->CellAt(2, 2)->ToString()->Equals(""));
00091 
00092         DEBUG_CLEAR_MEM_CHECK_POINTS();
00093         csva.CheckMem();
00094         reader.CheckMem();
00095         df.CheckMem();
00096         DEBUG_DUMP_MEM_LEAKS();
00097         UNIT_ASSERT_MEM_NOTED("DelimitedFile 2.2");
00098         Log::SWriteOkFail( "DelimitedFile 2" );
00099 }
00100 
00101 void _TestDelimFile()
00102 {
00103         _TestDelimFile1();
00104         DEBUG_CLEAR_MEM_CHECK_POINTS();
00105         DEBUG_DUMP_MEM_LEAKS();
00106 
00107         _TestDelimFile2();
00108         DEBUG_CLEAR_MEM_CHECK_POINTS();
00109         DEBUG_DUMP_MEM_LEAKS();
00110 }
00111 
00112 
00113 #endif