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

test/TestFile.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/io/File.h>
00020 
00021 #ifdef DEBUG
00022 
00023 static void _TestFile1()
00024 {
00025         String filename("filetest.txt");
00026         const char *line1 = "The time has come";
00027         const char *line2 = ", said the Walrus, ";
00028 
00029         if (File::Exists(filename))
00030         {
00031                 File::Delete(filename);
00032         }
00033         if (File::Exists("filetest2.txt"))
00034         {
00035                 File::Delete("filetest2.txt");
00036         }
00037         if (File::Exists("filetest3.txt"))
00038         {
00039                 File::Delete("filetest3.txt");
00040         }
00041 
00042         TextWriterPtr writer = File::CreateText(filename);
00043 
00044         DEBUG_CLEAR_MEM_CHECK_POINTS();
00045         filename.CheckMem();
00046         writer.CheckMem();
00047         DEBUG_DUMP_MEM_LEAKS();
00048         UNIT_ASSERT_MEM_NOTED("File test 1.0");
00049 
00050         writer->WriteLine(line1);
00051         writer->Close();
00052 
00053         DEBUG_CLEAR_MEM_CHECK_POINTS();
00054         filename.CheckMem();
00055         writer.CheckMem();
00056         DEBUG_DUMP_MEM_LEAKS();
00057         UNIT_ASSERT_MEM_NOTED("File test 1.1");
00058 
00059         UNIT_ASSERT("File::Exists", File::Exists(filename));
00060         UNIT_ASSERT("File::Size", File::Size(filename) > 17);
00061         UNIT_ASSERT("File::GetCreationTime", File::GetCreationTime(filename) <= DateTime::Now());
00062         UNIT_ASSERT("File::GetLastAccessTime", File::GetLastAccessTime(filename) <= DateTime::Now());
00063         UNIT_ASSERT("File::GetLastWriteTime", File::GetLastWriteTime(filename) <= DateTime::Now());
00064         UNIT_ASSERT("File::IsFile", File::IsFile(filename));
00065 
00066         DEBUG_CLEAR_MEM_CHECK_POINTS();
00067         filename.CheckMem();
00068         writer.CheckMem();
00069         DEBUG_DUMP_MEM_LEAKS();
00070         UNIT_ASSERT_MEM_NOTED("File test 1.2");
00071 
00072         TextReader *reader = new TextReader(File::OpenText(filename));
00073 
00074         DEBUG_CLEAR_MEM_CHECK_POINTS();
00075         filename.CheckMem();
00076         DEBUG_NOTE_MEM(reader);
00077         reader->CheckMem();
00078         writer.CheckMem();
00079         DEBUG_DUMP_MEM_LEAKS();
00080         UNIT_ASSERT_MEM_NOTED("File test 1.3");
00081 
00082         StringPtr line = reader->ReadLine();
00083 
00084         DEBUG_CLEAR_MEM_CHECK_POINTS();
00085         filename.CheckMem();
00086         DEBUG_NOTE_MEM(reader);
00087         reader->CheckMem();
00088         line.CheckMem();
00089         writer.CheckMem();
00090         DEBUG_DUMP_MEM_LEAKS();
00091         UNIT_ASSERT_MEM_NOTED("File test 1.4");
00092 
00093         UNIT_ASSERT("Reread line 1", line->Equals(line1));
00094 
00095         reader->Close();
00096         delete reader;
00097 
00098         writer = File::AppendText(filename);
00099         writer->WriteLine(line2);
00100         writer->Close();
00101 
00102         reader = new TextReader(File::OpenText(filename));
00103         reader->ReadLine();
00104         line = reader->ReadLine();
00105         reader->Close();
00106         delete reader;
00107 
00108         UNIT_ASSERT("Reread line 2", line->Equals(line2));
00109 
00110         File::Rename(filename, "filetest2.txt");
00111 
00112         UNIT_ASSERT("File::Rename", !File::Exists(filename));
00113         filename = "filetest2.txt";
00114         UNIT_ASSERT("File::Rename", File::Exists(filename));
00115 
00116         File::Move(filename, "filetest3.txt");
00117         UNIT_ASSERT("File::Move", !File::Exists(filename));
00118         filename = "filetest3.txt";
00119         UNIT_ASSERT("File::Move", File::Exists(filename));
00120 
00121         Permissions perms = File::GetPermissions(filename);
00122         UNIT_ASSERT("File::GetPermissions", perms.CanUserRead());
00123         UNIT_ASSERT("File::GetPermissions", perms.CanUserWrite());
00124 
00125         perms.SetOthersRead();
00126         File::SetPermissions(filename, perms);
00127 
00128         File::Delete(filename);
00129         UNIT_ASSERT("File::Delete", !File::Exists(filename));
00130 
00131         DEBUG_CLEAR_MEM_CHECK_POINTS();
00132         filename.CheckMem();
00133         writer.CheckMem();
00134         line.CheckMem();
00135         DEBUG_DUMP_MEM_LEAKS();
00136         UNIT_ASSERT_MEM_NOTED("File test 1.5");
00137 
00138         Log::SWriteOkFail( "File test 1" );
00139 }
00140 
00141 void _TestFile()
00142 {
00143         _TestFile1();
00144         DEBUG_CLEAR_MEM_CHECK_POINTS();
00145         DEBUG_DUMP_MEM_LEAKS();
00146 }
00147 
00148 #endif
00149