Wrap always_inline and noinline attributes in compiler checks and use MSVC equivalent.
[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 //  Marek Safar (marek.safar@gmail.com)
9 //
10 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
11 // Copyright (C) 2004 Novell (http://www.novell.com)
12 // Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
13 //
14
15 using System;
16 using System.IO;
17 using System.Runtime.Serialization.Formatters.Binary;
18 using System.Text;
19 using System.Threading;
20
21 using NUnit.Framework;
22
23 namespace MonoTests.System.IO
24 {
25         [TestFixture]
26         public class MemoryStreamTest
27         {
28                 class SignaledMemoryStream : MemoryStream
29                 {
30                         WaitHandle w;
31
32                         public SignaledMemoryStream (byte[] buffer, WaitHandle w)
33                                 : base (buffer)
34                         {
35                                 this.w = w;
36                         }
37
38                         public override int Read (byte[] buffer, int offset, int count)
39                         {
40                                 if (!w.WaitOne (2000))
41                                         return -1;
42
43                                 Assert.IsTrue (Thread.CurrentThread.IsThreadPoolThread, "IsThreadPoolThread");
44                                 return base.Read (buffer, offset, count);
45                         }
46                 }
47
48
49                 MemoryStream testStream;
50                 byte [] testStreamData;
51
52                 [SetUp]
53                 public void SetUp ()
54                 {
55                         testStreamData = new byte [100];
56
57                         for (int i = 0; i < 100; i++)
58                                 testStreamData[i] = (byte) (100 - i);
59
60                         testStream = new MemoryStream (testStreamData);
61                 }
62
63                 // 
64                 // Verify that the first count bytes in testBytes are the same as
65                 // the count bytes from index start in testStreamData
66                 //
67                 void VerifyTestData (string id, byte [] testBytes, int start, int count)
68                 {
69                         if (testBytes == null)
70                                 Assert.Fail (id + "+1 testBytes is null");
71
72                         if (start < 0 ||
73                             count < 0  ||
74                             start + count > testStreamData.Length ||
75                             start > testStreamData.Length)
76                                 throw new ArgumentOutOfRangeException (id + "+2");
77
78                         for (int test = 0; test < count; test++) {
79                                 if (testBytes [test] == testStreamData [start + test])
80                                         continue;
81
82                                 string failStr = "testByte {0} (testStream {1}) was <{2}>, expecting <{3}>";
83                                 failStr = String.Format (failStr,
84                                                         test,
85                                                         start + test,
86                                                         testBytes [test],
87                                                         testStreamData [start + test]);
88                                 Assert.Fail (id + "-3" + failStr);
89                         }
90                 }
91
92                 public void AssertEquals (string message, int expected, int actual)
93                 {
94                         Assert.AreEqual (expected, actual, message);
95                 }
96
97                 public void AssertEquals (string message, long expected, long actual)
98                 {
99                         Assert.AreEqual (expected, actual, message);
100                 }
101
102                 public void AssertEquals (string message, bool expected, bool actual)
103                 {
104                         Assert.AreEqual (expected, actual, message);
105                 }
106
107                 [Test]
108                 public void ConstructorsOne ()
109                 {
110                         MemoryStream ms = new MemoryStream();
111
112                         AssertEquals ("#01", 0L, ms.Length);
113                         AssertEquals ("#02", 0, ms.Capacity);
114                         AssertEquals ("#03", true, ms.CanWrite);
115                 }
116
117                 [Test]
118                 public void ConstructorsTwo ()
119                 {
120                         MemoryStream ms = new MemoryStream (10);
121
122                         AssertEquals ("#01", 0L, ms.Length);
123                         AssertEquals ("#02", 10, ms.Capacity);
124                         ms.Capacity = 0;
125                         byte [] buffer = ms.GetBuffer ();
126                         // Begin: wow!!!
127                         AssertEquals ("#03", -1, ms.ReadByte ());
128                         Assert.IsNull (buffer, "#04"); // <--
129                         ms.Read (new byte [5], 0, 5);
130                         AssertEquals ("#05", 0, ms.Position);
131                         AssertEquals ("#06", 0, ms.Length);
132                         // End
133                 }
134
135                 [Test]
136                 public void ConstructorsThree ()
137                 {
138                         MemoryStream ms = new MemoryStream (testStreamData);
139                         AssertEquals ("#01", 100, ms.Length);
140                         AssertEquals ("#02", 0, ms.Position);
141                 }
142
143                 [Test]
144                 public void ConstructorsFour ()
145                 {
146                         MemoryStream ms = new MemoryStream (testStreamData, true);
147                         AssertEquals ("#01", 100, ms.Length);
148                         AssertEquals ("#02", 0, ms.Position);
149                         ms.Position = 50;
150                         byte saved = testStreamData [50];
151                         try {
152                                 ms.WriteByte (23);
153                                 AssertEquals ("#03", testStreamData [50], 23);
154                         } finally {
155                                 testStreamData [50] = saved;
156                         }
157                         ms.Position = 100;
158                         try {
159                                 ms.WriteByte (23);
160                         } catch (Exception) {
161                                 return;
162                         }
163                         Assert.Fail ("#04");
164                 }
165
166                 [Test]
167                 public void ConstructorsFive ()
168                 {
169                         MemoryStream ms = new MemoryStream (testStreamData, 50, 50);
170                         AssertEquals ("#01", 50, ms.Length);
171                         AssertEquals ("#02", 0, ms.Position);
172                         AssertEquals ("#03", 50, ms.Capacity);
173                         ms.Position = 1;
174                         byte saved = testStreamData [51];
175                         try {
176                                 ms.WriteByte (23);
177                                 AssertEquals ("#04", testStreamData [51], 23);
178                         } finally {
179                                 testStreamData [51] = saved;
180                         }
181                         ms.Position = 100;
182
183                         try {
184                                 ms.WriteByte (23);
185                                 Assert.Fail ("#05");
186                         } catch (NotSupportedException) {
187                         }
188
189                         try {
190                                 ms.Capacity = 100;
191                                 Assert.Fail ("#06");
192                         } catch (NotSupportedException) {
193                         }
194                                            
195                         try {
196                                 ms.Capacity = 51;
197                                 Assert.Fail ("#07");
198                         } catch (NotSupportedException) {
199                         }
200
201                         AssertEquals ("#08", 50, ms.ToArray ().Length);
202                 }
203
204                 [Test]
205                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
206                 public void ConstructorsSix ()
207                 {
208                         MemoryStream ms = new MemoryStream (-2);
209                 }
210
211                 [Test]
212                 public void Read ()
213                 {
214                         byte [] readBytes = new byte [20];
215
216                         /* Test simple read */
217                         testStream.Read (readBytes, 0, 10);
218                         VerifyTestData ("R1", readBytes, 0, 10);
219
220                         /* Seek back to beginning */
221
222                         testStream.Seek (0, SeekOrigin.Begin);
223
224                         /* Read again, bit more this time */
225                         testStream.Read (readBytes, 0, 20);
226                         VerifyTestData ("R2", readBytes, 0, 20);
227
228                         /* Seek to 20 bytes from End */
229                         testStream.Seek (-20, SeekOrigin.End);
230                         testStream.Read (readBytes, 0, 20);
231                         VerifyTestData ("R3", readBytes, 80, 20);
232
233                         int readByte = testStream.ReadByte();
234                         Assert.AreEqual (-1, readByte, "R4");
235                 }
236                 
237                 [Test]
238                 public void BeginRead ()
239                 {
240                         byte [] readBytes = new byte [5];
241
242                         var res = testStream.BeginRead (readBytes, 0, 5, null, null);
243                         Assert.IsTrue (res.AsyncWaitHandle.WaitOne (1000), "#1");
244                         Assert.AreEqual (5, testStream.EndRead (res), "#2");
245                 }
246
247                 [Test]
248                 public void BeginRead_WithState ()
249                 {
250                         byte [] readBytes = new byte [5];
251                         string async_state = null;
252                         var wh = new ManualResetEvent (false);
253
254                         var res = testStream.BeginRead (readBytes, 0, 5, l => {
255                                 async_state = l.AsyncState as string;
256                                 wh.Set ();
257                         }, "state");
258
259                         Assert.IsTrue (res.AsyncWaitHandle.WaitOne (1000), "#1");
260                         Assert.AreEqual ("state", res.AsyncState, "#2");
261                         Assert.IsTrue (res.IsCompleted, "#3");
262                         Assert.AreEqual (5, testStream.EndRead (res), "#4");
263
264                         wh.WaitOne (1000);
265                         Assert.AreEqual ("state", async_state, "#5");
266                         wh.Close ();
267                 }
268                 
269                 [Test]
270                 public void BeginReadAsync ()
271                 {
272                         byte[] readBytes = new byte[5];
273                         var wh = new ManualResetEvent (false);
274                         using (var testStream = new SignaledMemoryStream (testStreamData, wh)) {
275                                 var res = testStream.BeginRead (readBytes, 0, 5, null, null);
276                                 Assert.IsFalse (res.IsCompleted, "#1");
277                                 Assert.IsFalse (res.CompletedSynchronously, "#2");
278                                 wh.Set ();
279                                 Assert.IsTrue (res.AsyncWaitHandle.WaitOne (2000), "#3");
280                                 Assert.IsTrue (res.IsCompleted, "#4");
281                                 Assert.AreEqual (5, testStream.EndRead (res), "#5");
282                         }
283
284                         wh.Close ();
285                 }
286                 
287                 [Test]
288                 public void BeginReadIsBlockingNextRead ()
289                 {
290                         byte[] readBytes = new byte[5];
291                         byte[] readBytes2 = new byte[3];
292                         var wh = new ManualResetEvent (false);
293                         var end = new ManualResetEvent (false);
294
295                         using (var testStream = new SignaledMemoryStream (testStreamData, wh)) {
296                                 var res = testStream.BeginRead (readBytes, 0, 5, null, null);
297
298                                 bool blocking = true;
299                                 ThreadPool.QueueUserWorkItem (l => {
300                                         var res2 = testStream.BeginRead (readBytes2, 0, 3, null, null);
301                                         blocking = false;
302                                         Assert.IsTrue (res2.AsyncWaitHandle.WaitOne (2000), "#10");
303                                         Assert.IsTrue (res2.IsCompleted, "#11");
304                                         Assert.AreEqual (3, testStream.EndRead (res2), "#12");
305                                         Assert.AreEqual (95, readBytes2[0], "#13");
306                                         end.Set ();
307                                 });
308
309                                 Assert.IsFalse (res.IsCompleted, "#1");
310                                 Thread.Sleep (500);     // Lame but don't know how to wait for another BeginRead which does not return
311                                 Assert.IsTrue (blocking, "#2");
312
313                                 wh.Set ();
314                                 Assert.IsTrue (res.AsyncWaitHandle.WaitOne (2000), "#3");
315                                 Assert.IsTrue (res.IsCompleted, "#4");
316                                 Assert.AreEqual (5, testStream.EndRead (res), "#5");
317                                 Assert.IsTrue (end.WaitOne (2000), "#6");
318                                 Assert.AreEqual (100, readBytes[0], "#7");
319                         }
320                 }
321
322                 [Test]
323                 public void BeginRead_Read ()
324                 {
325                         byte[] readBytes = new byte[5];
326                         var wh = new ManualResetEvent (false);
327                         using (var testStream = new SignaledMemoryStream (testStreamData, wh)) {
328                                 var res = testStream.BeginRead (readBytes, 0, 5, null, null);
329                                 Assert.AreEqual (100, testStream.ReadByte (), "#0");
330                                 Assert.IsFalse (res.IsCompleted, "#1");
331                                 Assert.IsFalse (res.CompletedSynchronously, "#2");
332                                 wh.Set ();
333                                 Assert.IsTrue (res.AsyncWaitHandle.WaitOne (2000), "#3");
334                                 Assert.IsTrue (res.IsCompleted, "#4");
335                                 Assert.AreEqual (5, testStream.EndRead (res), "#5");
336                                 Assert.AreEqual (99, readBytes [0], "#6");
337                         }
338
339                         wh.Close ();
340                 }
341
342                 [Test]
343                 public void BeginRead_BeginWrite ()
344                 {
345                         byte[] readBytes = new byte[5];
346                         byte[] readBytes2 = new byte[3] { 1, 2, 3 };
347                         var wh = new ManualResetEvent (false);
348                         var end = new ManualResetEvent (false);
349
350                         using (var testStream = new SignaledMemoryStream (testStreamData, wh)) {
351                                 var res = testStream.BeginRead (readBytes, 0, 5, null, null);
352
353                                 bool blocking = true;
354                                 ThreadPool.QueueUserWorkItem (l => {
355                                         var res2 = testStream.BeginWrite (readBytes2, 0, 3, null, null);
356                                         blocking = false;
357                                         Assert.IsTrue (res2.AsyncWaitHandle.WaitOne (2000), "#10");
358                                         Assert.IsTrue (res2.IsCompleted, "#11");
359                                         testStream.EndWrite (res2);
360                                         end.Set ();
361                                 });
362
363                                 Assert.IsFalse (res.IsCompleted, "#1");
364                                 Thread.Sleep (500);     // Lame but don't know how to wait for another BeginWrite which does not return
365                                 Assert.IsTrue (blocking, "#2");
366
367                                 wh.Set ();
368                                 Assert.IsTrue (res.AsyncWaitHandle.WaitOne (2000), "#3");
369                                 Assert.IsTrue (res.IsCompleted, "#4");
370                                 Assert.AreEqual (5, testStream.EndRead (res), "#5");
371                                 Assert.IsTrue (end.WaitOne (2000), "#6");
372                         }
373                 }
374                 
375                 [Test]
376                 public void BeginWrite ()
377                 {
378                         var writeBytes = new byte [5] { 2, 3, 4, 10, 12 };
379
380                         var res = testStream.BeginWrite (writeBytes, 0, 5, null, null);
381                         Assert.IsTrue (res.AsyncWaitHandle.WaitOne (1000), "#1");
382                         testStream.EndWrite (res);
383                 }
384
385                 [Test]
386                 public void BeginWrite_WithState ()
387                 {
388                         var writeBytes = new byte[5] { 2, 3, 4, 10, 12 };
389                         string async_state = null;
390                         var wh = new ManualResetEvent (false);
391
392                         var res = testStream.BeginWrite (writeBytes, 0, 5, l => {
393                                 async_state = l.AsyncState as string;
394                                 wh.Set ();
395                         }, "state");
396
397                         Assert.IsTrue (res.AsyncWaitHandle.WaitOne (1000), "#1");
398                         Assert.IsTrue (res.IsCompleted, "#2");
399                         Assert.AreEqual ("state", res.AsyncState, "#3");
400                         testStream.EndWrite (res);
401
402                         wh.WaitOne (1000);
403                         Assert.AreEqual ("state", async_state, "#4");
404                         wh.Close ();
405                 }
406                 
407                 [Test]
408                 public void EndRead_Twice ()
409                 {
410                         byte[] readBytes = new byte[5];
411
412                         var res = testStream.BeginRead (readBytes, 0, 5, null, null);
413                         Assert.IsTrue (res.AsyncWaitHandle.WaitOne (1000), "#1");
414                         Assert.AreEqual (5, testStream.EndRead (res), "#2");
415
416                         try {
417                                 testStream.EndRead (res);
418                                 Assert.Fail ("#3");
419                         } catch (ArgumentException) {
420                                 return;
421                         }
422                 }
423
424                 [Test]
425                 public void EndRead_Disposed ()
426                 {
427                         byte[] readBytes = new byte[5];
428
429                         var res = testStream.BeginRead (readBytes, 0, 5, null, null);
430                         Assert.IsTrue (res.AsyncWaitHandle.WaitOne (1000), "#1");
431                         testStream.Dispose ();
432                         Assert.AreEqual (5, testStream.EndRead (res), "#2");
433                 }
434                 
435                 [Test]
436                 public void EndWrite_OnBeginRead ()
437                 {
438                         byte[] readBytes = new byte[5];
439
440                         var res = testStream.BeginRead (readBytes, 0, 5, null, null);
441                         Assert.IsTrue (res.AsyncWaitHandle.WaitOne (1000), "#1");
442
443                         try {
444                                 testStream.EndWrite (res);
445                                 Assert.Fail ("#2");
446                         } catch (ArgumentException) {
447                         }
448
449                         testStream.EndRead (res);
450                 }
451
452                 [Test]
453                 public void EndWrite_Twice ()
454                 {
455                         var wBytes = new byte[5];
456
457                         var res = testStream.BeginWrite (wBytes, 0, 5, null, null);
458                         Assert.IsTrue (res.AsyncWaitHandle.WaitOne (1000), "#1");
459                         testStream.EndWrite (res);
460
461                         try {
462                                 testStream.EndWrite (res);
463                                 Assert.Fail ("#2");
464                         } catch (ArgumentException) {
465                                 return;
466                         }
467                 }
468
469                 
470                 [Test]
471                 public void WriteBytes ()
472                 {
473                         byte[] readBytes = new byte[100];
474
475                         MemoryStream ms = new MemoryStream (100);
476
477                         for (int i = 0; i < 100; i++)
478                                 ms.WriteByte (testStreamData [i]);
479
480                         ms.Seek (0, SeekOrigin.Begin); 
481                         testStream.Read (readBytes, 0, 100);
482                         VerifyTestData ("W1", readBytes, 0, 100);
483                 }
484
485                 [Test]
486                 public void WriteBlock ()
487                 {
488                         byte[] readBytes = new byte[100];
489
490                         MemoryStream ms = new MemoryStream (100);
491
492                         ms.Write (testStreamData, 0, 100);
493                         ms.Seek (0, SeekOrigin.Begin); 
494                         testStream.Read (readBytes, 0, 100);
495                         VerifyTestData ("WB1", readBytes, 0, 100);
496                         byte[] arrayBytes = testStream.ToArray();
497                         AssertEquals ("#01", 100, arrayBytes.Length);
498                         VerifyTestData ("WB2", arrayBytes, 0, 100);
499                 }
500
501                 [Test]
502                 public void PositionLength ()
503                 {
504                         MemoryStream ms = new MemoryStream ();
505                         ms.Position = 4;
506                         ms.WriteByte ((byte) 'M');
507                         ms.WriteByte ((byte) 'O');
508                         AssertEquals ("#01", 6, ms.Length);
509                         AssertEquals ("#02", 6, ms.Position);
510                         ms.Position = 0;
511                         AssertEquals ("#03", 0, ms.Position);
512                 }
513
514                 [Test]
515                 [ExpectedException (typeof (NotSupportedException))]
516                 public void MorePositionLength ()
517                 {
518                         MemoryStream ms = new MemoryStream (testStreamData);
519                         ms.Position = 101;
520                         AssertEquals ("#01", 101, ms.Position);
521                         AssertEquals ("#02", 100, ms.Length);
522                         ms.WriteByte (1); // This should throw the exception
523                 }
524
525                 [Test]
526                 public void GetBufferOne ()
527                 {
528                         MemoryStream ms = new MemoryStream ();
529                         byte [] buffer = ms.GetBuffer ();
530                         AssertEquals ("#01", 0, buffer.Length);
531                 }
532
533                 [Test]
534                 public void GetBufferTwo ()
535                 {
536                         MemoryStream ms = new MemoryStream (100);
537                         byte [] buffer = ms.GetBuffer ();
538                         AssertEquals ("#01", 100, buffer.Length);
539
540                         ms.Write (testStreamData, 0, 100);
541                         ms.Write (testStreamData, 0, 100);
542                         AssertEquals ("#02", 200, ms.Length);
543                         buffer = ms.GetBuffer ();
544                         AssertEquals ("#03", 256, buffer.Length); // Minimun size after writing
545                 }
546
547                 [Test]
548                 public void Closed ()
549                 {
550                         MemoryStream ms = new MemoryStream (100);
551                         ms.Close ();
552                         bool thrown = false;
553                         try {
554                                 int x = ms.Capacity;
555                         } catch (ObjectDisposedException) {
556                                 thrown = true;
557                         }
558
559                         if (!thrown)
560                                 Assert.Fail ("#01");
561
562                         thrown = false;
563                         try {
564                                 ms.Capacity = 1;
565                         } catch (ObjectDisposedException) {
566                                 thrown = true;
567                         }
568
569                         if (!thrown)
570                                 Assert.Fail ("#02");
571
572                         try {
573                                 ms.Read (null, 0, 1);
574                                 Assert.Fail ("#03");
575                         } catch (ArgumentNullException) {
576                         }
577
578                         try {
579                                 ms.Write (null, 0, 1);
580                                 Assert.Fail ("#04");
581                         } catch (ArgumentNullException) {
582                                 thrown = true;
583                         }
584                 }
585
586                 [Test]
587                 [ExpectedException (typeof (ObjectDisposedException))]
588                 public void Close_get_Length () 
589                 {
590                         MemoryStream ms = new MemoryStream (100);
591                         ms.Close ();
592                         long x = ms.Length;
593                 }
594
595                 [Test]
596                 [ExpectedException (typeof (ObjectDisposedException))]
597                 public void Close_get_Position () 
598                 {
599                         MemoryStream ms = new MemoryStream (100);
600                         ms.Close ();
601                         long x = ms.Position;
602                 }
603
604                 [Test]
605                 [ExpectedException (typeof (ObjectDisposedException))]
606                 public void Close_set_Position () 
607                 {
608                         MemoryStream ms = new MemoryStream (100);
609                         ms.Close ();
610                         ms.Position = 0;
611                 }
612
613                 [Test]
614                 public void Seek ()
615                 {
616                         MemoryStream ms = new MemoryStream (100);
617                         ms.Write (testStreamData, 0, 100);
618                         ms.Seek (0, SeekOrigin.Begin);
619                         ms.Position = 50;
620                         ms.Seek (-50, SeekOrigin.Current);
621                         ms.Seek (-50, SeekOrigin.End);
622
623                         bool thrown = false;
624                         ms.Position = 49;
625                         try {
626                                 ms.Seek (-50, SeekOrigin.Current);
627                         } catch (IOException) {
628                                 thrown = true;
629                         }
630                         if (!thrown)
631                                 Assert.Fail ("#01");
632                         
633                         thrown = false;
634                         try {
635                                 ms.Seek (Int64.MaxValue, SeekOrigin.Begin);
636                         } catch (ArgumentOutOfRangeException) {
637                                 thrown = true;
638                         }
639
640                         if (!thrown)
641                                 Assert.Fail ("#02");
642
643                         thrown=false;
644                         try {
645                                 // Oh, yes. They throw IOException for this one, but ArgumentOutOfRange for the previous one
646                                 ms.Seek (Int64.MinValue, SeekOrigin.Begin);
647                         } catch (IOException) {
648                                 thrown = true;
649                         }
650
651                         if (!thrown)
652                                 Assert.Fail ("#03");
653
654                         ms=new MemoryStream (256);
655
656                         ms.Write (testStreamData, 0, 100);
657                         ms.Position=0;
658                         AssertEquals ("#01", 100, ms.Length);
659                         AssertEquals ("#02", 0, ms.Position);
660
661                         ms.Position=128;
662                         AssertEquals ("#03", 100, ms.Length);
663                         AssertEquals ("#04", 128, ms.Position);
664
665                         ms.Position=768;
666                         AssertEquals ("#05", 100, ms.Length);
667                         AssertEquals ("#06", 768, ms.Position);
668
669                         ms.WriteByte (0);
670                         AssertEquals ("#07", 769, ms.Length);
671                         AssertEquals ("#08", 769, ms.Position);
672                 }
673
674                 [Test]
675                 public void Seek_Disposed () 
676                 {
677                         MemoryStream ms = new MemoryStream ();
678                         ms.Close ();
679                         try {
680                                 ms.Seek (0, SeekOrigin.Begin);
681                                 Assert.Fail ();
682                         } catch (ObjectDisposedException) {
683                         }
684                 }
685
686                 [Test]
687                 public void SetLength ()
688                 {
689                         MemoryStream ms = new MemoryStream ();
690                         ms.Write (testStreamData, 0, 100);
691                         ms.Position = 100;
692                         ms.SetLength (150);
693                         AssertEquals ("#01", 150, ms.Length);
694                         AssertEquals ("#02", 100, ms.Position);
695                         ms.SetLength (80);
696                         AssertEquals ("#03", 80, ms.Length);
697                         AssertEquals ("#04", 80, ms.Position);
698                 }
699
700                 [Test]
701                 [ExpectedException (typeof (NotSupportedException))]
702                 public void SetLength_ReadOnly ()
703                 {
704                         MemoryStream ms = new MemoryStream (testStreamData, false);
705                         ms.SetLength (10);
706                 }
707
708                 [Test]
709                 public void Capacity ()
710                 {
711                         MemoryStream ms = new MemoryStream ();
712
713                         Assert.AreEqual (0, ms.Capacity, "#A1");
714                         Assert.AreEqual (0, ms.GetBuffer ().Length, "#A2");
715
716                         ms.WriteByte ((byte)'6');
717                         Assert.AreEqual (256, ms.Capacity, "#B1");
718                         Assert.AreEqual (256, ms.GetBuffer ().Length, "#B2");
719
720                         // Shrink
721                         ms.Capacity = 100;
722                         Assert.AreEqual (100, ms.Capacity, "#C1");
723                         Assert.AreEqual (100, ms.GetBuffer ().Length, "#C2");
724
725                         // Grow
726                         ms.Capacity = 120;
727                         Assert.AreEqual (120, ms.Capacity, "#D1");
728                         Assert.AreEqual (120, ms.GetBuffer ().Length, "#D2");
729
730                         // Grow the buffer, reduce length -so we have a dirty area-
731                         // and then we assign capacity to the same. The idea is that we should
732                         // avoid creating a new internal buffer it's not needed.
733
734                         ms = new MemoryStream ();
735                         ms.Capacity = 8;
736                         byte [] buff = new byte [] { 0x01, 0x02, 0x03, 0x04, 0x05 };
737                         ms.Write (buff, 0, buff.Length);
738                         Assert.AreEqual (8, ms.Capacity, "#E1");
739                         Assert.AreEqual (8, ms.GetBuffer ().Length, "#E2");
740
741                         // Reduce *length*, not capacity
742                         byte [] buff_copy = ms.GetBuffer ();
743                         ms.SetLength (3);
744                         Assert.AreEqual (3, ms.Length, "#F1");
745                         Assert.AreEqual (true, AreBuffersEqual (buff_copy, ms.GetBuffer ()), "#F2");
746
747                         // Set Capacity to the very same value it has now
748                         ms.Capacity = ms.Capacity;
749                         Assert.AreEqual (true, AreBuffersEqual (buff_copy, ms.GetBuffer ()), "#G1"); // keep the same buffer
750
751                         // Finally, growing it discards the prev buff
752                         ms.Capacity = ms.Capacity + 1;
753                         Assert.AreEqual (false, AreBuffersEqual (buff_copy, ms.GetBuffer ()), "#H1");
754                 }
755
756                 bool AreBuffersEqual (byte [] buff1, byte [] buff2)
757                 {
758                         if ((buff1 == null) != (buff2 == null))
759                                 return false;
760
761                         if (buff1.Length != buff2.Length)
762                                 return false;
763
764                         for (int i = 0; i < buff1.Length; i++)
765                                 if (buff1 [i] != buff2 [i])
766                                         return false;
767
768                         return true;
769                 }
770
771                 [Test] // bug #327053
772                 public void ZeroingOnExpand ()
773                 {
774                         byte [] values = { 3, 2, 1 };
775                         byte [] reference = { 3, 2, 1 };
776                         byte [] cropped = { 3, 0, 0 };
777                         MemoryStream ms = new MemoryStream (values);
778                         Assert.AreEqual (values, reference, "#A1");
779                         ms.Seek (3, SeekOrigin.Begin);
780                         Assert.AreEqual (reference, values, "#A2");
781                         ms.SetLength (1);
782                         Assert.AreEqual (reference, values, "#B1");
783                         byte [] read = new byte [5];
784                         ms.Read (read, 0, 5);
785                         Assert.AreEqual (new byte [] { 0, 0, 0, 0, 0 }, read, "#B2");
786                         Assert.AreEqual (reference, values, "#B3");
787                         ms.SetLength (3);
788                         Assert.AreEqual (cropped, values, "#C1");
789                         ms.Seek (0, SeekOrigin.Begin);
790                         read = new byte [3];
791                         ms.Read (read, 0, 3);
792                         Assert.AreEqual (cropped, read, "#C2");
793                         Assert.AreEqual (cropped, values, "#C3");
794                 }
795
796                 [Test]
797                 [ExpectedException (typeof (NotSupportedException))]
798                 public void WriteNonWritable ()
799                 {
800                         MemoryStream ms = new MemoryStream (testStreamData, false);
801                         ms.Write (testStreamData, 0, 100);
802                 }
803
804                 [Test]
805                 [ExpectedException (typeof (NotSupportedException))]
806                 public void WriteExpand ()
807                 {
808                         MemoryStream ms = new MemoryStream (testStreamData);
809                         ms.Write (testStreamData, 0, 100);
810                         ms.Write (testStreamData, 0, 100); // This one throws the exception
811                 }
812
813                 [Test]
814                 public void WriteByte ()
815                 {
816                         MemoryStream ms = new MemoryStream (100);
817                         ms.Write (testStreamData, 0, 100);
818                         ms.Position = 100;
819                         ms.WriteByte (101);
820                         AssertEquals ("#01", 101, ms.Position);
821                         AssertEquals ("#02", 101, ms.Length);
822                         AssertEquals ("#03", 256, ms.Capacity);
823                         ms.Write (testStreamData, 0, 100);
824                         ms.Write (testStreamData, 0, 100);
825                         // 301
826                         AssertEquals ("#04", 301, ms.Position);
827                         AssertEquals ("#05", 301, ms.Length);
828                         AssertEquals ("#06", 512, ms.Capacity);
829                 }
830
831                 [Test]
832                 public void WriteLengths () {
833                         MemoryStream ms=new MemoryStream (256);
834                         BinaryWriter writer=new BinaryWriter (ms);
835
836                         writer.Write ((byte)'1');
837                         AssertEquals ("#01", 1, ms.Length);
838                         AssertEquals ("#02", 256, ms.Capacity);
839                         
840                         writer.Write ((ushort)0);
841                         AssertEquals ("#03", 3, ms.Length);
842                         AssertEquals ("#04", 256, ms.Capacity);
843
844                         writer.Write (testStreamData, 0, 23);
845                         AssertEquals ("#05", 26, ms.Length);
846                         AssertEquals ("#06", 256, ms.Capacity);
847
848                         writer.Write (testStreamData);
849                         writer.Write (testStreamData);
850                         writer.Write (testStreamData);
851                         AssertEquals ("#07", 326, ms.Length);
852                 }
853
854                 [Test]
855                 public void MoreWriteByte ()
856                 {
857                         byte[] buffer = new byte [44];
858                         
859                         MemoryStream ms = new MemoryStream (buffer);
860                         BinaryWriter bw = new BinaryWriter (ms);
861                         for(int i=0; i < 44; i++)
862                                 bw.Write ((byte) 1);
863                 }
864
865                 [Test]
866                 [ExpectedException (typeof (NotSupportedException))]
867                 public void MoreWriteByte2 ()
868                 {
869                         byte[] buffer = new byte [43]; // Note the 43 here
870                         
871                         MemoryStream ms = new MemoryStream (buffer);
872                         BinaryWriter bw = new BinaryWriter (ms);
873                         for(int i=0; i < 44; i++)
874                                 bw.Write ((byte) 1);
875                 }
876
877                 [Test]
878                 public void Expand () 
879                 {
880                         byte[] array = new byte [8] { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 };
881                         MemoryStream ms = new MemoryStream ();
882                         ms.Write (array, 0, array.Length);
883                         ms.SetLength (4);
884                         ms.Seek (4, SeekOrigin.End);
885                         ms.WriteByte (0xFF);
886                         Assert.AreEqual ("01-01-01-01-00-00-00-00-FF", BitConverter.ToString (ms.ToArray ()), "Result");
887                 }
888
889                 [Test]
890                 public void PubliclyVisible ()
891                 {
892                         MemoryStream ms = new MemoryStream ();
893                         Assert.IsNotNull (ms.GetBuffer (), "ctor()");
894
895                         ms = new MemoryStream (1);
896                         Assert.IsNotNull (ms.GetBuffer (), "ctor(1)");
897
898                         ms = new MemoryStream (new byte[1], 0, 1, true, true);
899                         Assert.IsNotNull (ms.GetBuffer (), "ctor(byte[],int,int,bool,bool");
900                 }
901
902                 [Test]
903                 [ExpectedException (typeof (UnauthorizedAccessException))]
904                 public void PubliclyVisible_Ctor_ByteArray ()
905                 {
906                         MemoryStream ms = new MemoryStream (new byte[0]);
907                         Assert.IsNotNull (ms.GetBuffer ());
908                 }
909
910                 [Test]
911                 [ExpectedException (typeof (UnauthorizedAccessException))]
912                 public void PubliclyVisible_Ctor_ByteArray_Boolean ()
913                 {
914                         MemoryStream ms = new MemoryStream (new byte[0], true);
915                         Assert.IsNotNull (ms.GetBuffer ());
916                 }
917
918                 [Test]
919                 [ExpectedException (typeof (UnauthorizedAccessException))]
920                 public void PubliclyVisible_Ctor_ByteArray_Int_Int ()
921                 {
922                         MemoryStream ms = new MemoryStream (new byte[1], 0, 1);
923                         Assert.IsNotNull (ms.GetBuffer ());
924                 }
925
926                 [Test]
927                 [ExpectedException (typeof (UnauthorizedAccessException))]
928                 public void PubliclyVisible_Ctor_ByteArray_Int_Int_Boolean ()
929                 {
930                         MemoryStream ms = new MemoryStream (new byte[1], 0, 1, true);
931                         Assert.IsNotNull (ms.GetBuffer ());
932                 }
933
934                 [Test]
935                 [ExpectedException (typeof (UnauthorizedAccessException))]
936                 public void PubliclyVisible_Ctor_ByteArray_Int_Int_Boolean_Boolean ()
937                 {
938                         MemoryStream ms = new MemoryStream (new byte[1], 0, 1, true, false);
939                         Assert.IsNotNull (ms.GetBuffer ());
940                 }
941
942                 [Test] // bug #350860
943                 public void ToArray_Empty ()
944                 {
945                         MemoryStream ms = new MemoryStream (1);
946                         ms.Capacity = 0;
947                         ms.ToArray ();
948                 }
949
950                 [Test] // bug #80205
951                 [Category ("NotWorking")]
952                 public void SerializeTest ()
953                 {
954                         MemoryStream input = new MemoryStream ();
955                         byte [] bufferIn = Encoding.UTF8.GetBytes ("some test");
956                         input.Write (bufferIn, 0, bufferIn.Length);
957                         input.Position = 0;
958
959                         BinaryFormatter bf = new BinaryFormatter ();
960                         MemoryStream ms = new MemoryStream ();
961                         bf.Serialize (ms, input);
962
963                         byte [] bufferOut = new byte [ms.Length];
964                         ms.Position = 0;
965                         ms.Read (bufferOut, 0, bufferOut.Length);
966
967                         Assert.AreEqual (_serialized, bufferOut);
968                 }
969
970                 [Test] // bug #676060
971                 public void ZeroCapacity ()
972                 {
973                         MemoryStream ms = new MemoryStream();
974                         ms.WriteByte(1);
975                         ms.Position = 0;
976                         ms.SetLength(0);
977                         ms.Capacity = 0;
978                         ms.WriteByte(1);
979                         byte[] bytes = ms.ToArray();
980                 }
981
982                 [Test] // bug #80205
983                 [Category ("NotWorking")]
984                 public void DeserializeTest ()
985                 {
986                         MemoryStream ms = new MemoryStream ();
987                         ms.Write (_serialized, 0, _serialized.Length);
988                         ms.Position = 0;
989
990                         BinaryFormatter bf = new BinaryFormatter ();
991                         MemoryStream output = (MemoryStream) bf.Deserialize (ms);
992                         using (StreamReader sr = new StreamReader (output)) {
993                                 Assert.AreEqual ("some test", sr.ReadToEnd ());
994                         }
995                 }
996
997                 private static byte [] _serialized = new byte [] {
998                         0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
999                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00,
1000                         0x16, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x49, 0x4f, 0x2e,
1001                         0x4d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x53, 0x74, 0x72, 0x65, 0x61,
1002                         0x6d, 0x0a, 0x00, 0x00, 0x00, 0x07, 0x5f, 0x62, 0x75, 0x66, 0x66,
1003                         0x65, 0x72, 0x07, 0x5f, 0x6f, 0x72, 0x69, 0x67, 0x69, 0x6e, 0x09,
1004                         0x5f, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x07, 0x5f,
1005                         0x6c, 0x65, 0x6e, 0x67, 0x74, 0x68, 0x09, 0x5f, 0x63, 0x61, 0x70,
1006                         0x61, 0x63, 0x69, 0x74, 0x79, 0x0b, 0x5f, 0x65, 0x78, 0x70, 0x61,
1007                         0x6e, 0x64, 0x61, 0x62, 0x6c, 0x65, 0x09, 0x5f, 0x77, 0x72, 0x69,
1008                         0x74, 0x61, 0x62, 0x6c, 0x65, 0x0a, 0x5f, 0x65, 0x78, 0x70, 0x6f,
1009                         0x73, 0x61, 0x62, 0x6c, 0x65, 0x07, 0x5f, 0x69, 0x73, 0x4f, 0x70,
1010                         0x65, 0x6e, 0x1d, 0x4d, 0x61, 0x72, 0x73, 0x68, 0x61, 0x6c, 0x42,
1011                         0x79, 0x52, 0x65, 0x66, 0x4f, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x2b,
1012                         0x5f, 0x5f, 0x69, 0x64, 0x65, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x07,
1013                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x08,
1014                         0x08, 0x08, 0x08, 0x01, 0x01, 0x01, 0x01, 0x09, 0x02, 0x00, 0x00,
1015                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x00,
1016                         0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x0a,
1017                         0x0f, 0x02, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x02, 0x73,
1018                         0x6f, 0x6d, 0x65, 0x20, 0x74, 0x65, 0x73, 0x74, 0x00, 0x00, 0x00,
1019                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1020                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1021                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1022                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1023                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1024                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1025                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1026                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1027                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1028                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1029                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1030                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1031                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1032                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1033                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1034                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1035                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1036                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1037                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1038                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1039                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1040                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1041                         0x00, 0x00, 0x0b };
1042
1043                 class MyMemoryStream : MemoryStream {
1044
1045                         public bool DisposedCalled = false;
1046
1047                         protected override void Dispose(bool disposing)
1048                         {
1049                                 DisposedCalled = true;
1050                         }
1051                 }
1052
1053                 [Test] // https://bugzilla.novell.com/show_bug.cgi?id=322672
1054                 public void BaseDisposeCalled ()
1055                 {
1056                         MyMemoryStream ms = new MyMemoryStream ();
1057                         Assert.IsFalse (ms.DisposedCalled, "Before");
1058                         ms.Close ();
1059                         Assert.IsTrue (ms.DisposedCalled, "After");
1060                 }
1061
1062 #if NET_4_5
1063                 [Test]
1064                 public void ReadAsync ()
1065                 {
1066                         var buffer = new byte[3];
1067                         var t = testStream.ReadAsync (buffer, 0, buffer.Length);
1068                         Assert.AreEqual (t.Result, 3, "#1");
1069                         Assert.AreEqual (99, buffer [1], "#2");
1070
1071                         testStream.Seek (99, SeekOrigin.Begin);
1072                         t = testStream.ReadAsync (buffer, 0, 1);
1073                         Assert.AreEqual (t.Result, 1, "#3");
1074                         Assert.AreEqual (1, buffer[0], "#4");
1075                 }
1076
1077                 [Test]
1078                 public void ReadAsync_Canceled ()
1079                 {
1080                         var buffer = new byte[3];
1081                         var t = testStream.ReadAsync (buffer, 0, buffer.Length, new CancellationToken (true));
1082                         Assert.IsTrue (t.IsCanceled);
1083
1084                         t = testStream.ReadAsync (buffer, 0, buffer.Length);
1085                         Assert.AreEqual (t.Result, 3, "#1");
1086                         Assert.AreEqual (99, buffer[1], "#2");
1087                 }
1088
1089                 [Test]
1090                 public void WriteAsync ()
1091                 {
1092                         var buffer = new byte[3] { 3, 5, 9 };
1093
1094                         var ms = new MemoryStream ();
1095                         var t = ms.WriteAsync (buffer, 0, buffer.Length);
1096                         Assert.IsTrue (t.IsCompleted, "#1");
1097
1098                         ms.Seek (0, SeekOrigin.Begin);
1099                         Assert.AreEqual (3, ms.ReadByte (), "#2");
1100                 }
1101
1102                 [Test]
1103                 public void WriteAsync_Canceled ()
1104                 {
1105                         var buffer = new byte[3] { 1, 2, 3 };
1106                         var t = testStream.WriteAsync (buffer, 0, buffer.Length, new CancellationToken (true));
1107                         Assert.IsTrue (t.IsCanceled);
1108
1109                         t = testStream.WriteAsync (buffer, 0, buffer.Length);
1110                         Assert.IsTrue (t.IsCompleted, "#1");
1111                 }
1112 #endif
1113         }
1114 }