00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include <spl/Debug.h>
00018 #include <spl/io/File.h>
00019 #include <spl/io/FileStream.h>
00020 #include <spl/Log.h>
00021 #include <spl/io/MemoryStream.h>
00022
00023 using namespace spl;
00024
00025 #ifdef DEBUG
00026
00027 static void _TestStream1()
00028 {
00029 String str1("hi there this is some text that");
00030 MemoryStream ms;
00031 ms.Write(str1.ToByteArray());
00032
00033 DEBUG_CLEAR_MEM_CHECK_POINTS();
00034 str1.CheckMem();
00035 ms.CheckMem();
00036 DEBUG_DUMP_MEM_LEAKS();
00037 UNIT_ASSERT_MEM_NOTED("Stream test 1.0");
00038
00039 const char *filename = "t_testfile.txt";
00040 if ( File::Exists (filename) )
00041 {
00042 File::Delete (filename);
00043 }
00044
00045 DEBUG_CLEAR_MEM_CHECK_POINTS();
00046 ms.CheckMem();
00047 str1.CheckMem();
00048 DEBUG_DUMP_MEM_LEAKS();
00049 UNIT_ASSERT_MEM_NOTED("Stream test 1.0a");
00050
00051 TextWriterPtr tw = File::CreateText(filename);
00052
00053 DEBUG_CLEAR_MEM_CHECK_POINTS();
00054 ms.CheckMem();
00055 str1.CheckMem();
00056 tw.CheckMem();
00057 DEBUG_DUMP_MEM_LEAKS();
00058 UNIT_ASSERT_MEM_NOTED("Stream test 1.0ab");
00059
00060 tw->WriteLine("The time has come");
00061 tw->WriteLine("said the walrus");
00062
00063 DEBUG_CLEAR_MEM_CHECK_POINTS();
00064 ms.CheckMem();
00065 str1.CheckMem();
00066 tw.CheckMem();
00067 DEBUG_DUMP_MEM_LEAKS();
00068 UNIT_ASSERT_MEM_NOTED("Stream test 1.0b");
00069
00070 tw->Close();
00071
00072 DEBUG_CLEAR_MEM_CHECK_POINTS();
00073 ms.CheckMem();
00074 str1.CheckMem();
00075 tw.CheckMem();
00076 DEBUG_DUMP_MEM_LEAKS();
00077 UNIT_ASSERT_MEM_NOTED("Stream test 1.0c");
00078
00079 FileStreamPtr fs(new FileStream(filename, File::FILEMODE_Open, File::FILEACC_Read));
00080 UNIT_ASSERT("Can not read file", fs->CanRead());
00081
00082 DEBUG_CLEAR_MEM_CHECK_POINTS();
00083 str1.CheckMem();
00084 ms.CheckMem();
00085 fs.CheckMem();
00086 tw.CheckMem();
00087 DEBUG_DUMP_MEM_LEAKS();
00088 UNIT_ASSERT_MEM_NOTED("Stream test 1.1");
00089
00090 TextReader tr(fs);
00091 StringPtr line = tr.ReadLine();
00092 UNIT_ASSERT("Line 1", line->Equals("The time has come"));
00093 line = tr.ReadLine();
00094 UNIT_ASSERT("Line 2", line->Equals("said the walrus"));
00095 UNIT_ASSERT("EOF", tr.Read() == -1);
00096
00097 tr.Close();
00098 File::Delete(filename);
00099
00100 Log::SWriteOkFail( "Stream test 1" );
00101 }
00102
00103 static void _TestMemoryStream()
00104 {
00105 int x;
00106 MemoryStream ms;
00107
00108 for ( x = 0; x < 1000; x++ )
00109 {
00110 ms.WriteByte((byte)'a');
00111 ms.WriteByte((byte)'b');
00112 ms.WriteByte((byte)'c');
00113 }
00114 UNIT_ASSERT("Should be 3000", ms.Length() == 3000);
00115
00116 ms.Seek(0, IStream::SEEK_Begin);
00117 for ( x = 0; x < 1000; x++ )
00118 {
00119 UNIT_ASSERT("a", ms.ReadByte() == 'a');
00120 UNIT_ASSERT("b", ms.ReadByte() == 'b');
00121 UNIT_ASSERT("c", ms.ReadByte() == 'c');
00122 }
00123
00124 UNIT_ASSERT("Should be empty", ms.Length() == 0);
00125
00126 Log::SWriteOkFail( "MemoryStream test 1" );
00127 }
00128
00129 static void _TestMemoryStream2()
00130 {
00131 MemoryStream ms;
00132 int x;
00133
00134 for ( x = 0; x < 1000; x++ )
00135 {
00136 ms.WriteByte((byte)'a');
00137 ms.WriteByte((byte)'b');
00138 ms.WriteByte((byte)'c');
00139 }
00140 UNIT_ASSERT("Should be 3000", ms.Length() == 3000);
00141 UNIT_ASSERT("Position", ms.Position() == 0);
00142
00143 ms.Seek(0, IStream::SEEK_Begin);
00144 Array<byte> buf(1001);
00145
00146 ms.Read(buf, 0, 900);
00147 UNIT_ASSERT("Should be 2100", ms.Length() == 2100);
00148 UNIT_ASSERT("Position", ms.Position() == 0);
00149 for ( x = 0; x < 900; x+=3 )
00150 {
00151 UNIT_ASSERT("a", buf[x] == 'a');
00152 UNIT_ASSERT("b", buf[x+1] == 'b');
00153 UNIT_ASSERT("c", buf[x+2] == 'c');
00154 }
00155
00156 DEBUG_CLEAR_MEM_CHECK_POINTS();
00157 ms.CheckMem();
00158 buf.CheckMem();
00159 DEBUG_DUMP_MEM_LEAKS();
00160 UNIT_ASSERT_MEM_NOTED("MemoryStream 2.1");
00161
00162 for ( x = 0; x < 700; x++ )
00163 {
00164 UNIT_ASSERT("a", ms.ReadByte() == 'a');
00165 UNIT_ASSERT("b", ms.ReadByte() == 'b');
00166 UNIT_ASSERT("c", ms.ReadByte() == 'c');
00167 }
00168 UNIT_ASSERT("Should be 0", ms.Length() == 0);
00169 UNIT_ASSERT("EOF", ms.ReadByte() == -1);
00170
00171 ms.WriteByte('x');
00172 UNIT_ASSERT("Should be 1", ms.Length() == 1);
00173 int readlen = ms.Read(buf, 0, 2);
00174 UNIT_ASSERT("Should be one", readlen == 1);
00175 UNIT_ASSERT("x", buf[0] == 'x');
00176 UNIT_ASSERT("Should be 0", ms.Length() == 0);
00177
00178 DEBUG_CLEAR_MEM_CHECK_POINTS();
00179 ms.CheckMem();
00180 buf.CheckMem();
00181 DEBUG_DUMP_MEM_LEAKS();
00182 UNIT_ASSERT_MEM_NOTED("MemoryStream 2.2");
00183
00184 Log::SWriteOkFail( "MemoryStream test 2" );
00185 }
00186
00187 void TestStreams()
00188 {
00189 _TestStream1();
00190 _TestMemoryStream();
00191 _TestMemoryStream2();
00192
00193 DEBUG_CLEAR_MEM_CHECK_POINTS();
00194 DEBUG_DUMP_MEM_LEAKS();
00195 }
00196
00197 #endif