New test.
[mono.git] / mcs / class / corlib / Test / System.IO / MemoryStreamTest.cs
1 //
2 // System.IO.MemoryStreamTest
3 //
4 // Authors:
5 //      Marcin Szczepanski (marcins@zipworld.com.au)
6 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //      Sebastien Pouliot  <sebastien@ximian.com>
8 //
9 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
10 // Copyright (C) 2004 Novell (http://www.novell.com)
11 //
12
13 using NUnit.Framework;
14 using System.IO;
15 using System;
16 using System.Text;
17
18 namespace MonoTests.System.IO
19 {
20         [TestFixture]
21         public class MemoryStreamTest
22         {
23                 MemoryStream testStream;
24                 byte [] testStreamData;
25
26                 [SetUp]
27                 public void SetUp ()
28                 {
29                         testStreamData = new byte [100];
30
31                         for (int i = 0; i < 100; i++)
32                                 testStreamData[i] = (byte) (100 - i);
33
34                         testStream = new MemoryStream (testStreamData);
35                 }
36
37                 // 
38                 // Verify that the first count bytes in testBytes are the same as
39                 // the count bytes from index start in testStreamData
40                 //
41                 void VerifyTestData (string id, byte [] testBytes, int start, int count)
42                 {
43                         if (testBytes == null)
44                                 Assert.Fail (id + "+1 testBytes is null");
45
46                         if (start < 0 ||
47                             count < 0  ||
48                             start + count > testStreamData.Length ||
49                             start > testStreamData.Length)
50                                 throw new ArgumentOutOfRangeException (id + "+2");
51
52                         for (int test = 0; test < count; test++) {
53                                 if (testBytes [test] == testStreamData [start + test])
54                                         continue;
55
56                                 string failStr = "testByte {0} (testStream {1}) was <{2}>, expecting <{3}>";
57                                 failStr = String.Format (failStr,
58                                                         test,
59                                                         start + test,
60                                                         testBytes [test],
61                                                         testStreamData [start + test]);
62                                 Assert.Fail (id + "-3" + failStr);
63                         }
64                 }
65
66                 public void AssertEquals (string message, int expected, int actual)
67                 {
68                         Assert.AreEqual (expected, actual, message);
69                 }
70
71                 public void AssertEquals (string message, long expected, long actual)
72                 {
73                         Assert.AreEqual (expected, actual, message);
74                 }
75
76                 public void AssertEquals (string message, bool expected, bool actual)
77                 {
78                         Assert.AreEqual (expected, actual, message);
79                 }
80
81                 [Test]
82                 public void ConstructorsOne ()
83                 {
84                         MemoryStream ms = new MemoryStream();
85
86                         AssertEquals ("#01", 0L, ms.Length);
87                         AssertEquals ("#02", 0, ms.Capacity);
88                         AssertEquals ("#03", true, ms.CanWrite);
89                 }
90
91                 [Test]
92                 public void ConstructorsTwo ()
93                 {
94                         MemoryStream ms = new MemoryStream (10);
95
96                         AssertEquals ("#01", 0L, ms.Length);
97                         AssertEquals ("#02", 10, ms.Capacity);
98                         ms.Capacity = 0;
99                         byte [] buffer = ms.GetBuffer ();
100                         // Begin: wow!!!
101                         AssertEquals ("#03", -1, ms.ReadByte ());
102                         Assert.IsNull (buffer, "#04"); // <--
103                         ms.Read (new byte [5], 0, 5);
104                         AssertEquals ("#05", 0, ms.Position);
105                         AssertEquals ("#06", 0, ms.Length);
106                         // End
107                 }
108
109                 [Test]
110                 public void ConstructorsThree ()
111                 {
112                         MemoryStream ms = new MemoryStream (testStreamData);
113                         AssertEquals ("#01", 100, ms.Length);
114                         AssertEquals ("#02", 0, ms.Position);
115                 }
116
117                 [Test]
118                 public void ConstructorsFour ()
119                 {
120                         MemoryStream ms = new MemoryStream (testStreamData, true);
121                         AssertEquals ("#01", 100, ms.Length);
122                         AssertEquals ("#02", 0, ms.Position);
123                         ms.Position = 50;
124                         byte saved = testStreamData [50];
125                         try {
126                                 ms.WriteByte (23);
127                                 AssertEquals ("#03", testStreamData [50], 23);
128                         } finally {
129                                 testStreamData [50] = saved;
130                         }
131                         ms.Position = 100;
132                         try {
133                                 ms.WriteByte (23);
134                         } catch (Exception) {
135                                 return;
136                         }
137                         Assert.Fail ("#04");
138                 }
139
140                 [Test]
141                 public void ConstructorsFive ()
142                 {
143                         MemoryStream ms = new MemoryStream (testStreamData, 50, 50);
144                         AssertEquals ("#01", 50, ms.Length);
145                         AssertEquals ("#02", 0, ms.Position);
146                         AssertEquals ("#03", 50, ms.Capacity);
147                         ms.Position = 1;
148                         byte saved = testStreamData [51];
149                         try {
150                                 ms.WriteByte (23);
151                                 AssertEquals ("#04", testStreamData [51], 23);
152                         } finally {
153                                 testStreamData [51] = saved;
154                         }
155                         ms.Position = 100;
156                         bool gotException = false;
157                         try {
158                                 ms.WriteByte (23);
159                         } catch (NotSupportedException) {
160                                 gotException = true;
161                         }
162
163                         if (!gotException)
164                                 Assert.Fail ("#05");
165
166                         ms.Capacity = 100; // Allowed. It's the same as the one in the ms.
167                                            // This is lame, as the length is 50!!!
168                                            
169                         gotException = false;
170                         try {
171                                 ms.Capacity = 51;
172                         } catch (NotSupportedException) {
173                                 gotException = true;
174                         }
175
176                         if (!gotException)
177                                 Assert.Fail ("#07");
178
179                         AssertEquals ("#08", 50, ms.ToArray ().Length);
180                 }
181
182                 [Test]
183                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
184                 public void ConstructorsSix ()
185                 {
186                         MemoryStream ms = new MemoryStream (-2);
187                 }
188
189                 [Test]
190                 public void Read ()
191                 {
192                         byte [] readBytes = new byte [20];
193
194                         /* Test simple read */
195                         testStream.Read (readBytes, 0, 10);
196                         VerifyTestData ("R1", readBytes, 0, 10);
197
198                         /* Seek back to beginning */
199
200                         testStream.Seek (0, SeekOrigin.Begin);
201
202                         /* Read again, bit more this time */
203                         testStream.Read (readBytes, 0, 20);
204                         VerifyTestData ("R2", readBytes, 0, 20);
205
206                         /* Seek to 20 bytes from End */
207                         testStream.Seek (-20, SeekOrigin.End);
208                         testStream.Read (readBytes, 0, 20);
209                         VerifyTestData ("R3", readBytes, 80, 20);
210
211                         int readByte = testStream.ReadByte();
212                         Assert.AreEqual (-1, readByte, "R4");
213                 }
214
215                 [Test]
216                 public void WriteBytes ()
217                 {
218                         byte[] readBytes = new byte[100];
219
220                         MemoryStream ms = new MemoryStream (100);
221
222                         for (int i = 0; i < 100; i++)
223                                 ms.WriteByte (testStreamData [i]);
224
225                         ms.Seek (0, SeekOrigin.Begin); 
226                         testStream.Read (readBytes, 0, 100);
227                         VerifyTestData ("W1", readBytes, 0, 100);
228                 }               
229
230                 [Test]
231                 public void WriteBlock ()
232                 {
233                         byte[] readBytes = new byte[100];
234
235                         MemoryStream ms = new MemoryStream (100);
236
237                         ms.Write (testStreamData, 0, 100);
238                         ms.Seek (0, SeekOrigin.Begin); 
239                         testStream.Read (readBytes, 0, 100);
240                         VerifyTestData ("WB1", readBytes, 0, 100);
241                         byte[] arrayBytes = testStream.ToArray();
242                         AssertEquals ("#01", 100, arrayBytes.Length);
243                         VerifyTestData ("WB2", arrayBytes, 0, 100);
244                 }
245
246                 [Test]
247                 public void PositionLength ()
248                 {
249                         MemoryStream ms = new MemoryStream ();
250                         ms.Position = 4;
251                         ms.WriteByte ((byte) 'M');
252                         ms.WriteByte ((byte) 'O');
253                         AssertEquals ("#01", 6, ms.Length);
254                         AssertEquals ("#02", 6, ms.Position);
255                         ms.Position = 0;
256                         AssertEquals ("#03", 0, ms.Position);
257                 }
258
259                 [Test]
260                 [ExpectedException (typeof (NotSupportedException))]
261                 public void MorePositionLength ()
262                 {
263                         MemoryStream ms = new MemoryStream (testStreamData);
264                         ms.Position = 101;
265                         AssertEquals ("#01", 101, ms.Position);
266                         AssertEquals ("#02", 100, ms.Length);
267                         ms.WriteByte (1); // This should throw the exception
268                 }
269
270                 [Test]
271                 public void GetBufferOne ()
272                 {
273                         MemoryStream ms = new MemoryStream ();
274                         byte [] buffer = ms.GetBuffer ();
275                         AssertEquals ("#01", 0, buffer.Length);
276                 }
277
278                 [Test]
279                 public void GetBufferTwo ()
280                 {
281                         MemoryStream ms = new MemoryStream (100);
282                         byte [] buffer = ms.GetBuffer ();
283                         AssertEquals ("#01", 100, buffer.Length);
284
285                         ms.Write (testStreamData, 0, 100);
286                         ms.Write (testStreamData, 0, 100);
287                         AssertEquals ("#02", 200, ms.Length);
288                         buffer = ms.GetBuffer ();
289                         AssertEquals ("#03", 256, buffer.Length); // Minimun size after writing
290                 }
291
292                 [Test]
293                 public void Closed ()
294                 {
295                         MemoryStream ms = new MemoryStream (100);
296                         ms.Close ();
297                         bool thrown = false;
298                         try {
299                                 int x = ms.Capacity;
300                         } catch (ObjectDisposedException) {
301                                 thrown = true;
302                         }
303
304                         if (!thrown)
305                                 Assert.Fail ("#01");
306
307                         thrown = false;
308                         try {
309                                 ms.Capacity = 1;
310                         } catch (ObjectDisposedException) {
311                                 thrown = true;
312                         }
313
314                         if (!thrown)
315                                 Assert.Fail ("#02");
316
317                         // The first exception thrown is ObjectDisposed, not ArgumentNull
318                         thrown = false;
319                         try {
320                                 ms.Read (null, 0, 1);
321                         } catch (ObjectDisposedException) {
322                                 thrown = true;
323                         }
324
325                         if (!thrown)
326                                 Assert.Fail ("#03");
327
328                         thrown = false;
329                         try {
330                                 ms.Write (null, 0, 1);
331                         } catch (ObjectDisposedException) {
332                                 thrown = true;
333                         }
334
335                         if (!thrown)
336                                 Assert.Fail ("#03");
337                 }
338
339                 [Test]
340                 [ExpectedException (typeof (ObjectDisposedException))]
341                 public void Close_get_Length () 
342                 {
343                         MemoryStream ms = new MemoryStream (100);
344                         ms.Close ();
345                         long x = ms.Length;
346                 }
347
348                 [Test]
349                 [ExpectedException (typeof (ObjectDisposedException))]
350                 public void Close_get_Position () 
351                 {
352                         MemoryStream ms = new MemoryStream (100);
353                         ms.Close ();
354                         long x = ms.Position;
355                 }
356
357                 [Test]
358                 [ExpectedException (typeof (ObjectDisposedException))]
359                 public void Close_set_Position () 
360                 {
361                         MemoryStream ms = new MemoryStream (100);
362                         ms.Close ();
363                         ms.Position = 0;
364                 }
365
366                 [Test]
367                 public void Seek ()
368                 {
369                         MemoryStream ms = new MemoryStream (100);
370                         ms.Write (testStreamData, 0, 100);
371                         ms.Seek (0, SeekOrigin.Begin);
372                         ms.Position = 50;
373                         ms.Seek (-50, SeekOrigin.Current);
374                         ms.Seek (-50, SeekOrigin.End);
375
376                         bool thrown = false;
377                         ms.Position = 49;
378                         try {
379                                 ms.Seek (-50, SeekOrigin.Current);
380                         } catch (IOException) {
381                                 thrown = true;
382                         }
383                         if (!thrown)
384                                 Assert.Fail ("#01");
385                         
386                         thrown = false;
387                         try {
388                                 ms.Seek (Int64.MaxValue, SeekOrigin.Begin);
389                         } catch (ArgumentOutOfRangeException) {
390                                 thrown = true;
391                         }
392
393                         if (!thrown)
394                                 Assert.Fail ("#02");
395
396                         thrown=false;
397                         try {
398                                 // Oh, yes. They throw IOException for this one, but ArgumentOutOfRange for the previous one
399                                 ms.Seek (Int64.MinValue, SeekOrigin.Begin);
400                         } catch (IOException) {
401                                 thrown = true;
402                         }
403
404                         if (!thrown)
405                                 Assert.Fail ("#03");
406
407                         ms=new MemoryStream (256);
408
409                         ms.Write (testStreamData, 0, 100);
410                         ms.Position=0;
411                         AssertEquals ("#01", 100, ms.Length);
412                         AssertEquals ("#02", 0, ms.Position);
413
414                         ms.Position=128;
415                         AssertEquals ("#03", 100, ms.Length);
416                         AssertEquals ("#04", 128, ms.Position);
417
418                         ms.Position=768;
419                         AssertEquals ("#05", 100, ms.Length);
420                         AssertEquals ("#06", 768, ms.Position);
421
422                         ms.WriteByte (0);
423                         AssertEquals ("#07", 769, ms.Length);
424                         AssertEquals ("#08", 769, ms.Position);
425                 }
426
427                 [Test]
428                 [ExpectedException (typeof (ObjectDisposedException))]
429                 public void Seek_Disposed () 
430                 {
431                         MemoryStream ms = new MemoryStream ();
432                         ms.Close ();
433                         ms.Seek (0, SeekOrigin.Begin);
434                 }
435
436                 [Test]
437                 public void SetLength ()
438                 {
439                         MemoryStream ms = new MemoryStream ();
440                         ms.Write (testStreamData, 0, 100);
441                         ms.Position = 100;
442                         ms.SetLength (150);
443                         AssertEquals ("#01", 150, ms.Length);
444                         AssertEquals ("#02", 100, ms.Position);
445                         ms.SetLength (80);
446                         AssertEquals ("#03", 80, ms.Length);
447                         AssertEquals ("#04", 80, ms.Position);
448                 }
449
450                 [Test]
451                 [ExpectedException (typeof (NotSupportedException))]
452                 public void SetLength_ReadOnly ()
453                 {
454                         MemoryStream ms = new MemoryStream (testStreamData, false);
455                         ms.SetLength (10);
456                 }
457
458                 [Test]
459                 [ExpectedException (typeof (NotSupportedException))]
460                 public void WriteNonWritable ()
461                 {
462                         MemoryStream ms = new MemoryStream (testStreamData, false);
463                         ms.Write (testStreamData, 0, 100);
464                 }
465
466                 [Test]
467                 [ExpectedException (typeof (NotSupportedException))]
468                 public void WriteExpand ()
469                 {
470                         MemoryStream ms = new MemoryStream (testStreamData);
471                         ms.Write (testStreamData, 0, 100);
472                         ms.Write (testStreamData, 0, 100); // This one throws the exception
473                 }
474
475                 [Test]
476                 public void WriteByte ()
477                 {
478                         MemoryStream ms = new MemoryStream (100);
479                         ms.Write (testStreamData, 0, 100);
480                         ms.Position = 100;
481                         ms.WriteByte (101);
482                         AssertEquals ("#01", 101, ms.Position);
483                         AssertEquals ("#02", 101, ms.Length);
484                         AssertEquals ("#03", 256, ms.Capacity);
485                         ms.Write (testStreamData, 0, 100);
486                         ms.Write (testStreamData, 0, 100);
487                         // 301
488                         AssertEquals ("#04", 301, ms.Position);
489                         AssertEquals ("#05", 301, ms.Length);
490                         AssertEquals ("#06", 512, ms.Capacity);
491                 }
492
493                 [Test]
494                 public void WriteLengths () {
495                         MemoryStream ms=new MemoryStream (256);
496                         BinaryWriter writer=new BinaryWriter (ms);
497
498                         writer.Write ((byte)'1');
499                         AssertEquals ("#01", 1, ms.Length);
500                         AssertEquals ("#02", 256, ms.Capacity);
501                         
502                         writer.Write ((ushort)0);
503                         AssertEquals ("#03", 3, ms.Length);
504                         AssertEquals ("#04", 256, ms.Capacity);
505
506                         writer.Write (testStreamData, 0, 23);
507                         AssertEquals ("#05", 26, ms.Length);
508                         AssertEquals ("#06", 256, ms.Capacity);
509
510                         writer.Write (testStreamData);
511                         writer.Write (testStreamData);
512                         writer.Write (testStreamData);
513                         AssertEquals ("#07", 326, ms.Length);
514                 }
515
516                 [Test]
517                 public void MoreWriteByte ()
518                 {
519                         byte[] buffer = new byte [44];
520                         
521                         MemoryStream ms = new MemoryStream (buffer);
522                         BinaryWriter bw = new BinaryWriter (ms);
523                         for(int i=0; i < 44; i++)
524                                 bw.Write ((byte) 1);
525                 }
526
527                 [Test]
528                 [ExpectedException (typeof (NotSupportedException))]
529                 public void MoreWriteByte2 ()
530                 {
531                         byte[] buffer = new byte [43]; // Note the 43 here
532                         
533                         MemoryStream ms = new MemoryStream (buffer);
534                         BinaryWriter bw = new BinaryWriter (ms);
535                         for(int i=0; i < 44; i++)
536                                 bw.Write ((byte) 1);
537                 }
538
539                 [Test]
540                 public void Expand () 
541                 {
542                         byte[] array = new byte [8] { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 };
543                         MemoryStream ms = new MemoryStream ();
544                         ms.Write (array, 0, array.Length);
545                         ms.SetLength (4);
546                         ms.Seek (4, SeekOrigin.End);
547                         ms.WriteByte (0xFF);
548                         Assert.AreEqual ("01-01-01-01-00-00-00-00-FF", BitConverter.ToString (ms.ToArray ()), "Result");
549                 }
550
551                 [Test]
552                 public void PubliclyVisible ()
553                 {
554                         MemoryStream ms = new MemoryStream ();
555                         Assert.IsNotNull (ms.GetBuffer (), "ctor()");
556
557                         ms = new MemoryStream (1);
558                         Assert.IsNotNull (ms.GetBuffer (), "ctor(1)");
559
560                         ms = new MemoryStream (new byte[1], 0, 1, true, true);
561                         Assert.IsNotNull (ms.GetBuffer (), "ctor(byte[],int,int,bool,bool");
562                 }
563
564                 [Test]
565                 [ExpectedException (typeof (UnauthorizedAccessException))]
566                 public void PubliclyVisible_Ctor_ByteArray ()
567                 {
568                         MemoryStream ms = new MemoryStream (new byte[0]);
569                         Assert.IsNotNull (ms.GetBuffer ());
570                 }
571
572                 [Test]
573                 [ExpectedException (typeof (UnauthorizedAccessException))]
574                 public void PubliclyVisible_Ctor_ByteArray_Boolean ()
575                 {
576                         MemoryStream ms = new MemoryStream (new byte[0], true);
577                         Assert.IsNotNull (ms.GetBuffer ());
578                 }
579
580                 [Test]
581                 [ExpectedException (typeof (UnauthorizedAccessException))]
582                 public void PubliclyVisible_Ctor_ByteArray_Int_Int ()
583                 {
584                         MemoryStream ms = new MemoryStream (new byte[1], 0, 1);
585                         Assert.IsNotNull (ms.GetBuffer ());
586                 }
587
588                 [Test]
589                 [ExpectedException (typeof (UnauthorizedAccessException))]
590                 public void PubliclyVisible_Ctor_ByteArray_Int_Int_Boolean ()
591                 {
592                         MemoryStream ms = new MemoryStream (new byte[1], 0, 1, true);
593                         Assert.IsNotNull (ms.GetBuffer ());
594                 }
595
596                 [Test]
597                 [ExpectedException (typeof (UnauthorizedAccessException))]
598                 public void PubliclyVisible_Ctor_ByteArray_Int_Int_Boolean_Boolean ()
599                 {
600                         MemoryStream ms = new MemoryStream (new byte[1], 0, 1, true, false);
601                         Assert.IsNotNull (ms.GetBuffer ());
602                 }
603         }
604 }
605