2003-03-05 Dick Porter <dick@ximian.com>
[mono.git] / mcs / class / corlib / Test / System.IO / MemoryStreamTest.cs
1 //
2 // System.IO.StringWriter
3 //
4 // Authors:
5 //      Marcin Szczepanski (marcins@zipworld.com.au)
6 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //
8 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
9 //
10
11 using NUnit.Framework;
12 using System.IO;
13 using System;
14 using System.Text;
15
16 namespace MonoTests.System.IO
17 {
18         [TestFixture]
19         public class MemoryStreamTest
20         {
21                 MemoryStream testStream;
22                 byte [] testStreamData;
23
24                 [SetUp]
25                 void SetUp ()
26                 {
27                         testStreamData = new byte [100];
28
29                         for (int i = 0; i < 100; i++)
30                                 testStreamData[i] = (byte) (100 - i);
31
32                         testStream = new MemoryStream (testStreamData);
33                 }
34
35                 // 
36                 // Verify that the first count bytes in testBytes are the same as
37                 // the count bytes from index start in testStreamData
38                 //
39                 void VerifyTestData (string id, byte [] testBytes, int start, int count)
40                 {
41                         if (testBytes == null)
42                                 Assertion.Fail (id + "+1 testBytes is null");
43
44                         if (start < 0 ||
45                             count < 0  ||
46                             start + count > testStreamData.Length ||
47                             start > testStreamData.Length)
48                                 throw new ArgumentOutOfRangeException (id + "+2");
49
50                         for (int test = 0; test < count; test++) {
51                                 if (testBytes [test] == testStreamData [start + test])
52                                         continue;
53
54                                 string failStr = "testByte {0} (testStream {1}) was <{2}>, expecting <{3}>";
55                                 failStr = String.Format (failStr,
56                                                         test,
57                                                         start + test,
58                                                         testBytes [test],
59                                                         testStreamData [start + test]);
60                                 Assertion.Fail (id + "-3" + failStr);
61                         }
62                 }
63
64                 [Test]
65                 public void ConstructorsOne ()
66                 {
67                         MemoryStream ms = new MemoryStream();
68
69                         Assertion.AssertEquals ("#01", 0L, ms.Length);
70                         Assertion.AssertEquals ("#02", 0, ms.Capacity);
71                         Assertion.AssertEquals ("#03", true, ms.CanWrite);
72                 }
73
74                 [Test]
75                 public void ConstructorsTwo ()
76                 {
77                         MemoryStream ms = new MemoryStream (10);
78
79                         Assertion.AssertEquals ("#01", 0L, ms.Length);
80                         Assertion.AssertEquals ("#02", 10, ms.Capacity);
81                         ms.Capacity = 0;
82                         byte [] buffer = ms.GetBuffer ();
83                         // Begin: wow!!!
84                         Assertion.AssertEquals ("#03", -1, ms.ReadByte ());
85                         Assertion.AssertEquals ("#04", null, buffer); // <--
86                         ms.Read (new byte [5], 0, 5);
87                         Assertion.AssertEquals ("#05", 0, ms.Position);
88                         Assertion.AssertEquals ("#06", 0, ms.Length);
89                         // End
90                 }
91
92                 [Test]
93                 public void ConstructorsThree ()
94                 {
95                         MemoryStream ms = new MemoryStream (testStreamData);
96                         Assertion.AssertEquals ("#01", 100, ms.Length);
97                         Assertion.AssertEquals ("#02", 0, ms.Position);
98                 }
99
100                 [Test]
101                 public void ConstructorsFour ()
102                 {
103                         MemoryStream ms = new MemoryStream (testStreamData, true);
104                         Assertion.AssertEquals ("#01", 100, ms.Length);
105                         Assertion.AssertEquals ("#02", 0, ms.Position);
106                         ms.Position = 50;
107                         byte saved = testStreamData [50];
108                         try {
109                                 ms.WriteByte (23);
110                                 Assertion.AssertEquals ("#03", testStreamData [50], 23);
111                         } finally {
112                                 testStreamData [50] = saved;
113                         }
114                         ms.Position = 100;
115                         try {
116                                 ms.WriteByte (23);
117                         } catch (Exception) {
118                                 return;
119                         }
120                         Assertion.Fail ("#04");
121                 }
122
123                 [Test]
124                 public void ConstructorsFive ()
125                 {
126                         MemoryStream ms = new MemoryStream (testStreamData, 50, 50);
127                         Assertion.AssertEquals ("#01", 50, ms.Length);
128                         Assertion.AssertEquals ("#02", 0, ms.Position);
129                         Assertion.AssertEquals ("#03", 50, ms.Capacity);
130                         ms.Position = 1;
131                         byte saved = testStreamData [51];
132                         try {
133                                 ms.WriteByte (23);
134                                 Assertion.AssertEquals ("#04", testStreamData [51], 23);
135                         } finally {
136                                 testStreamData [51] = saved;
137                         }
138                         ms.Position = 100;
139                         bool gotException = false;
140                         try {
141                                 ms.WriteByte (23);
142                         } catch (NotSupportedException) {
143                                 gotException = true;
144                         }
145
146                         if (!gotException)
147                                 Assertion.Fail ("#05");
148
149                         gotException = false;
150                         try {
151                                 ms.GetBuffer ();
152                         } catch (UnauthorizedAccessException) {
153                                 gotException = true;
154                         }
155
156                         if (!gotException)
157                                 Assertion.Fail ("#06");
158
159                         ms.Capacity = 100; // Allowed. It's the same as the one in the ms.
160                                            // This is lame, as the length is 50!!!
161                                            
162                         gotException = false;
163                         try {
164                                 ms.Capacity = 51;
165                         } catch (NotSupportedException) {
166                                 gotException = true;
167                         }
168
169                         if (!gotException)
170                                 Assertion.Fail ("#07");
171
172                         Assertion.AssertEquals ("#08", 50, ms.ToArray ().Length);
173                 }
174
175                 [Test]
176                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
177                 public void ConstructorsSix ()
178                 {
179                         MemoryStream ms = new MemoryStream (-2);
180                 }
181
182                 [Test]
183                 public void Read ()
184                 {
185                         byte [] readBytes = new byte [20];
186
187                         /* Test simple read */
188                         testStream.Read (readBytes, 0, 10);
189                         VerifyTestData ("R1", readBytes, 0, 10);
190
191                         /* Seek back to beginning */
192
193                         testStream.Seek (0, SeekOrigin.Begin);
194
195                         /* Read again, bit more this time */
196                         testStream.Read (readBytes, 0, 20);
197                         VerifyTestData ("R2", readBytes, 0, 20);
198
199                         /* Seek to 20 bytes from End */
200                         testStream.Seek (-20, SeekOrigin.End);
201                         testStream.Read (readBytes, 0, 20);
202                         VerifyTestData ("R3", readBytes, 80, 20);
203
204                         int readByte = testStream.ReadByte();
205                         Assertion.AssertEquals (-1, readByte);
206                 }
207
208                 [Test]
209                 public void WriteBytes ()
210                 {
211                         byte[] readBytes = new byte[100];
212
213                         MemoryStream ms = new MemoryStream (100);
214
215                         for (int i = 0; i < 100; i++)
216                                 ms.WriteByte (testStreamData [i]);
217
218                         ms.Seek (0, SeekOrigin.Begin); 
219                         testStream.Read (readBytes, 0, 100);
220                         VerifyTestData ("W1", readBytes, 0, 100);
221                 }               
222
223                 [Test]
224                 public void WriteBlock ()
225                 {
226                         byte[] readBytes = new byte[100];
227
228                         MemoryStream ms = new MemoryStream (100);
229
230                         ms.Write (testStreamData, 0, 100);
231                         ms.Seek (0, SeekOrigin.Begin); 
232                         testStream.Read (readBytes, 0, 100);
233                         VerifyTestData ("WB1", readBytes, 0, 100);
234                         byte[] arrayBytes = testStream.ToArray();
235                         Assertion.AssertEquals ("#01", 100, arrayBytes.Length);
236                         VerifyTestData ("WB2", arrayBytes, 0, 100);
237                 }
238
239                 [Test]
240                 public void PositionLength ()
241                 {
242                         MemoryStream ms = new MemoryStream ();
243                         ms.Position = 4;
244                         ms.WriteByte ((byte) 'M');
245                         ms.WriteByte ((byte) 'O');
246                         Assertion.AssertEquals ("#01", 6, ms.Length);
247                         Assertion.AssertEquals ("#02", 6, ms.Position);
248                         ms.Position = 0;
249                         Assertion.AssertEquals ("#03", 0, ms.Position);
250                 }
251
252                 [Test]
253                 [ExpectedException (typeof (NotSupportedException))]
254                 public void MorePositionLength ()
255                 {
256                         MemoryStream ms = new MemoryStream (testStreamData);
257                         ms.Position = 101;
258                         Assertion.AssertEquals ("#01", 101, ms.Position);
259                         Assertion.AssertEquals ("#02", 100, ms.Length);
260                         ms.WriteByte (1); // This should throw the exception
261                 }
262
263                 [Test]
264                 public void GetBufferOne ()
265                 {
266                         MemoryStream ms = new MemoryStream ();
267                         byte [] buffer = ms.GetBuffer ();
268                         Assertion.AssertEquals ("#01", 0, buffer.Length);
269                 }
270
271                 [Test]
272                 public void GetBufferTwo ()
273                 {
274                         MemoryStream ms = new MemoryStream (100);
275                         byte [] buffer = ms.GetBuffer ();
276                         Assertion.AssertEquals ("#01", 100, buffer.Length);
277
278                         ms.Write (testStreamData, 0, 100);
279                         ms.Write (testStreamData, 0, 100);
280                         Assertion.AssertEquals ("#02", 200, ms.Length);
281                         buffer = ms.GetBuffer ();
282                         Assertion.AssertEquals ("#03", 256, buffer.Length); // Minimun size after writing
283                 }
284
285                 [Test]
286                 public void Closed ()
287                 {
288                         MemoryStream ms = new MemoryStream (100);
289                         ms.Close ();
290                         bool thrown = false;
291                         try {
292                                 int x = ms.Capacity;
293                         } catch (ObjectDisposedException) {
294                                 thrown = true;
295                         }
296
297                         if (!thrown)
298                                 Assertion.Fail ("#01");
299
300                         thrown = false;
301                         try {
302                                 ms.Capacity = 1;
303                         } catch (ObjectDisposedException) {
304                                 thrown = true;
305                         }
306
307                         if (!thrown)
308                                 Assertion.Fail ("#02");
309
310                         // The first exception thrown is ObjectDisposed, not ArgumentNull
311                         thrown = false;
312                         try {
313                                 ms.Read (null, 0, 1);
314                         } catch (ObjectDisposedException) {
315                                 thrown = true;
316                         }
317
318                         if (!thrown)
319                                 Assertion.Fail ("#03");
320
321                         thrown = false;
322                         try {
323                                 ms.Write (null, 0, 1);
324                         } catch (ObjectDisposedException) {
325                                 thrown = true;
326                         }
327
328                         if (!thrown)
329                                 Assertion.Fail ("#03");
330                 }
331
332                 [Test]
333                 public void Seek ()
334                 {
335                         MemoryStream ms = new MemoryStream (100);
336                         ms.Write (testStreamData, 0, 100);
337                         ms.Seek (0, SeekOrigin.Begin);
338                         ms.Position = 50;
339                         ms.Seek (-50, SeekOrigin.Current);
340                         ms.Seek (-50, SeekOrigin.End);
341
342                         bool thrown = false;
343                         ms.Position = 49;
344                         try {
345                                 ms.Seek (-50, SeekOrigin.Current);
346                         } catch (IOException) {
347                                 thrown = true;
348                         }
349                         if (!thrown)
350                                 Assertion.Fail ("#01");
351                         
352                         thrown = false;
353                         try {
354                                 ms.Seek (Int64.MaxValue, SeekOrigin.Begin);
355                         } catch (ArgumentOutOfRangeException) {
356                                 thrown = true;
357                         }
358
359                         if (!thrown)
360                                 Assertion.Fail ("#02");
361
362                         thrown=false;
363                         try {
364                                 // Oh, yes. They throw IOException for this one, but ArgumentOutOfRange for the previous one
365                                 ms.Seek (Int64.MinValue, SeekOrigin.Begin);
366                         } catch (IOException) {
367                                 thrown = true;
368                         }
369
370                         if (!thrown)
371                                 Assertion.Fail ("#03");
372
373                         ms=new MemoryStream (256);
374
375                         ms.Write (testStreamData, 0, 100);
376                         ms.Position=0;
377                         Assertion.AssertEquals ("#01", 100, ms.Length);
378                         Assertion.AssertEquals ("#02", 0, ms.Position);
379
380                         ms.Position=128;
381                         Assertion.AssertEquals ("#03", 100, ms.Length);
382                         Assertion.AssertEquals ("#04", 128, ms.Position);
383
384                         ms.Position=768;
385                         Assertion.AssertEquals ("#05", 100, ms.Length);
386                         Assertion.AssertEquals ("#06", 768, ms.Position);
387
388                         ms.WriteByte (0);
389                         Assertion.AssertEquals ("#07", 769, ms.Length);
390                         Assertion.AssertEquals ("#08", 769, ms.Position);
391                 }
392
393                 [Test]
394                 public void SetLength ()
395                 {
396                         MemoryStream ms = new MemoryStream ();
397                         ms.Write (testStreamData, 0, 100);
398                         ms.Position = 100;
399                         ms.SetLength (150);
400                         Assertion.AssertEquals ("#01", 150, ms.Length);
401                         Assertion.AssertEquals ("#02", 100, ms.Position);
402                         ms.SetLength (80);
403                         Assertion.AssertEquals ("#03", 80, ms.Length);
404                         Assertion.AssertEquals ("#04", 80, ms.Position);
405                 }
406
407                 [Test]
408                 [ExpectedException (typeof (NotSupportedException))]
409                 public void WriteNonWritable ()
410                 {
411                         MemoryStream ms = new MemoryStream (testStreamData, false);
412                         ms.Write (testStreamData, 0, 100);
413                 }
414
415                 [Test]
416                 [ExpectedException (typeof (NotSupportedException))]
417                 public void WriteExpand ()
418                 {
419                         MemoryStream ms = new MemoryStream (testStreamData);
420                         ms.Write (testStreamData, 0, 100);
421                         ms.Write (testStreamData, 0, 100); // This one throws the exception
422                 }
423
424                 [Test]
425                 public void WriteByte ()
426                 {
427                         MemoryStream ms = new MemoryStream (100);
428                         ms.Write (testStreamData, 0, 100);
429                         ms.Position = 100;
430                         ms.WriteByte (101);
431                         Assertion.AssertEquals ("#01", 101, ms.Position);
432                         Assertion.AssertEquals ("#02", 101, ms.Length);
433                         Assertion.AssertEquals ("#03", 256, ms.Capacity);
434                         ms.Write (testStreamData, 0, 100);
435                         ms.Write (testStreamData, 0, 100);
436                         // 301
437                         Assertion.AssertEquals ("#04", 301, ms.Position);
438                         Assertion.AssertEquals ("#05", 301, ms.Length);
439                         Assertion.AssertEquals ("#06", 512, ms.Capacity);
440                 }
441
442                 [Test]
443                 public void WriteLengths () {
444                         MemoryStream ms=new MemoryStream (256);
445                         BinaryWriter writer=new BinaryWriter (ms);
446
447                         writer.Write ((byte)'1');
448                         Assertion.AssertEquals ("#01", 1, ms.Length);
449                         Assertion.AssertEquals ("#02", 256, ms.Capacity);
450                         
451                         writer.Write ((ushort)0);
452                         Assertion.AssertEquals ("#03", 3, ms.Length);
453                         Assertion.AssertEquals ("#04", 256, ms.Capacity);
454
455                         writer.Write (testStreamData, 0, 23);
456                         Assertion.AssertEquals ("#05", 26, ms.Length);
457                         Assertion.AssertEquals ("#06", 256, ms.Capacity);
458
459                         writer.Write (testStreamData);
460                         writer.Write (testStreamData);
461                         writer.Write (testStreamData);
462                         Assertion.AssertEquals ("#07", 326, ms.Length);
463                 }
464         }
465 }
466