Merge pull request #901 from Blewzman/FixAggregateExceptionGetBaseException
[mono.git] / mcs / class / corlib / Test / System.IO / StreamReaderTest.cs
1 // StreamReaderTest.cs - NUnit Test Cases for the SystemIO.StreamReader class
2 //
3 // David Brandt (bucky@keystreams.com)
4 //
5 // (C) Ximian, Inc.  http://www.ximian.com
6 // Copyright (C) 2004 Novell (http://www.novell.com)
7 // 
8
9 using System;
10 using System.IO;
11 using System.Text;
12 #if NET_4_5
13 using System.Threading.Tasks;
14 #endif
15
16 using NUnit.Framework;
17
18 namespace MonoTests.System.IO
19 {
20 [TestFixture]
21 public class StreamReaderTest
22 {
23         static string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
24         private string _codeFileName = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
25         private const string TestString = "Hello World!";
26
27         [SetUp]
28         public void SetUp ()
29         {       
30                 if (!Directory.Exists (TempFolder))
31                         Directory.CreateDirectory (TempFolder);
32                 
33                 if (!File.Exists (_codeFileName))
34                         File.Create (_codeFileName).Close ();
35         }
36
37         [TearDown]
38         public void TearDown ()
39         {
40                 if (Directory.Exists (TempFolder))
41                         Directory.Delete (TempFolder, true);
42         }
43
44
45         [Test]
46         public void TestCtor1() {
47                 {
48                         bool errorThrown = false;
49                         try {
50                                 new StreamReader((Stream)null);
51                         } catch (ArgumentNullException) {
52                                 errorThrown = true;
53                         }
54                         Assert.IsTrue (errorThrown, "null string error not thrown");
55                 }
56                 {
57                         bool errorThrown = false;
58                         FileStream f = new FileStream(_codeFileName, FileMode.Open, FileAccess.Write);
59                         try {
60                                 StreamReader r = new StreamReader(f);
61                                 r.Close();
62                         } catch (ArgumentException) {
63                                 errorThrown = true;
64                         }
65                         f.Close();
66                         Assert.IsTrue (errorThrown, "no read error not thrown");
67                 }
68                 {
69                         // this is probably incestuous, but, oh well.
70                         FileStream f = new FileStream(_codeFileName, 
71                                                       FileMode.Open, 
72                                                       FileAccess.Read);
73                         StreamReader r = new StreamReader(f);
74                         Assert.IsNotNull (r, "no stream reader");
75                         r.Close();
76                         f.Close();
77                 }
78         }
79
80         [Test]
81         public void TestCtor2() {
82                 {
83                         bool errorThrown = false;
84                         try {
85                                 new StreamReader("");
86                         } catch (ArgumentException) {
87                                 errorThrown = true;
88                         } catch (Exception e) {
89                                 Assert.Fail ("Incorrect exception thrown at 1: " + e.ToString());
90                         }
91                         Assert.IsTrue (errorThrown, "empty string error not thrown");
92                 }
93                 {
94                         bool errorThrown = false;
95                         try {
96                                 new StreamReader((string)null);
97                         } catch (ArgumentNullException) {
98                                 errorThrown = true;
99                         } catch (Exception e) {
100                                 Assert.Fail ("Incorrect exception thrown at 2: " + e.ToString());
101                         }
102                         Assert.IsTrue (errorThrown, "null string error not thrown");
103                 }
104                 {
105                         bool errorThrown = false;
106                         try {
107                                 new StreamReader("nonexistentfile");
108                         } catch (FileNotFoundException) {
109                                 errorThrown = true;
110                         } catch (Exception e) {
111                                 Assert.Fail ("Incorrect exception thrown at 3: " + e.ToString());
112                         }
113                         Assert.IsTrue (errorThrown, "fileNotFound error not thrown");
114                 }
115                 {
116                         bool errorThrown = false;
117                         try {
118                                 new StreamReader("nonexistentdir/file");
119                         } catch (DirectoryNotFoundException) {
120                                 errorThrown = true;
121                         } catch (Exception e) {
122                                 Assert.Fail ("Incorrect exception thrown at 4: " + e.ToString());
123                         }
124                         Assert.IsTrue (errorThrown, "dirNotFound error not thrown");
125                 }
126                 {
127                         bool errorThrown = false;
128                         try {
129                                 new StreamReader("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0]);
130                         } catch (IOException) {
131                                 errorThrown = true;
132                         } catch (ArgumentException) {
133                                 // FIXME - the spec says 'IOExc', but the
134                                 //   compiler says 'ArgExc'...
135                                 errorThrown = true;
136                         } catch (Exception e) {
137                                 Assert.Fail ("Incorrect exception thrown at 5: " + e.ToString());
138                         }
139                         Assert.IsTrue (errorThrown, "invalid filename error not thrown");
140                 }
141                 {
142                         // this is probably incestuous, but, oh well.
143                         StreamReader r = new StreamReader(_codeFileName);
144                         Assert.IsNotNull (r, "no stream reader");
145                         r.Close();
146                 }
147         }
148
149         [Test]
150         public void TestCtor3() {
151                 {
152                         bool errorThrown = false;
153                         try {
154                                 new StreamReader((Stream)null, false);
155                         } catch (ArgumentNullException) {
156                                 errorThrown = true;
157                         } catch (Exception e) {
158                                 Assert.Fail ("Incorrect exception thrown at 1: " + e.ToString());
159                         }
160                         Assert.IsTrue (errorThrown, "null stream error not thrown");
161                 }
162                 {
163                         bool errorThrown = false;
164                         FileStream f = new FileStream(_codeFileName, FileMode.Open, FileAccess.Write);
165                         try {
166                                 StreamReader r = new StreamReader(f, false);
167                                 r.Close();
168                         } catch (ArgumentException) {
169                                 errorThrown = true;
170                         } catch (Exception e) {
171                                 Assert.Fail ("Incorrect exception thrown at 2: " + e.ToString());
172                         }
173                         f.Close();
174                         Assert.IsTrue (errorThrown, "no read error not thrown");
175                 }
176                 {
177                         // this is probably incestuous, but, oh well.
178                         FileStream f = new FileStream(_codeFileName, 
179                                                       FileMode.Open, 
180                                                       FileAccess.Read);
181                         StreamReader r = new StreamReader(f, false);
182                         Assert.IsNotNull (r, "no stream reader");
183                         r.Close();
184                         f.Close();
185                 }
186                 {
187                         bool errorThrown = false;
188                         try {
189                                 StreamReader r = new StreamReader((Stream)null, true);
190                         } catch (ArgumentNullException) {
191                                 errorThrown = true;
192                         } catch (Exception e) {
193                                 Assert.Fail ("Incorrect exception thrown at 3: " + e.ToString());
194                         }
195                         Assert.IsTrue (errorThrown, "null string error not thrown");
196                 }
197                 {
198                         bool errorThrown = false;
199                         FileStream f = new FileStream(_codeFileName, FileMode.Open, FileAccess.Write);
200                         try {
201                                 StreamReader r = new StreamReader(f, true);
202                                 r.Close();
203                         } catch (ArgumentException) {
204                                 errorThrown = true;
205                         } catch (Exception e) {
206                                 Assert.Fail ("Incorrect exception thrown at 4: " + e.ToString());
207                         }
208                         f.Close();
209                         Assert.IsTrue (errorThrown, "no read error not thrown");
210                 }
211                 {
212                         // this is probably incestuous, but, oh well.
213                         FileStream f = new FileStream(_codeFileName, 
214                                                       FileMode.Open, 
215                                                       FileAccess.Read);
216                         StreamReader r = new StreamReader(f, true);
217                         Assert.IsNotNull (r, "no stream reader");
218                         r.Close();
219                         f.Close();
220                 }
221         }
222
223         [Test]
224         public void TestCtor4() {
225                 {
226                         bool errorThrown = false;
227                         try {
228                                 new StreamReader("", false);
229                         } catch (ArgumentException) {
230                                 errorThrown = true;
231                         } catch (Exception e) {
232                                 Assert.Fail ("Incorrect exception thrown at 1: " + e.ToString());
233                         }
234                         Assert.IsTrue (errorThrown, "empty string error not thrown");
235                 }
236                 {
237                         bool errorThrown = false;
238                         try {
239                                 new StreamReader((string)null, false);
240                         } catch (ArgumentNullException) {
241                                 errorThrown = true;
242                         } catch (Exception e) {
243                                 Assert.Fail ("Incorrect exception thrown at 2: " + e.ToString());
244                         }
245                         Assert.IsTrue (errorThrown, "null string error not thrown");
246                 }
247                 {
248                         bool errorThrown = false;
249                         try {
250                                 new StreamReader(TempFolder + "/nonexistentfile", false);
251                         } catch (FileNotFoundException) {
252                                 errorThrown = true;
253                         } catch (Exception e) {
254                                 Assert.Fail ("Incorrect exception thrown at 3: " + e.ToString());
255                         }
256                         Assert.IsTrue (errorThrown, "fileNotFound error not thrown");
257                 }
258                 {
259                         bool errorThrown = false;
260                         try {
261                                 new StreamReader(TempFolder + "/nonexistentdir/file", false);
262                         } catch (DirectoryNotFoundException) {
263                                 errorThrown = true;
264                         } catch (Exception e) {
265                                 Assert.Fail ("Incorrect exception thrown at 4: " + e.ToString());
266                         }
267                         Assert.IsTrue (errorThrown, "dirNotFound error not thrown");
268                 }
269                 {
270                         bool errorThrown = false;
271                         try {
272                                 new StreamReader("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0], false);
273                         } catch (IOException) {
274                                 errorThrown = true;
275                         } catch (ArgumentException) {
276                                 // FIXME - the spec says 'IOExc', but the
277                                 //   compiler says 'ArgExc'...
278                                 errorThrown = true;
279                         } catch (Exception e) {
280                                 Assert.Fail ("Incorrect exception thrown at 5: " + e.ToString());
281                         }
282                         Assert.IsTrue (errorThrown, "invalid filename error not thrown");
283                 }
284                 {
285                         // this is probably incestuous, but, oh well.
286                         StreamReader r = new StreamReader(_codeFileName, false);
287                         Assert.IsNotNull (r, "no stream reader");
288                         r.Close();
289                 }
290                 {
291                         bool errorThrown = false;
292                         try {
293                                 new StreamReader("", true);
294                         } catch (ArgumentException) {
295                                 errorThrown = true;
296                         } catch (Exception e) {
297                                 Assert.Fail ("Incorrect exception thrown at 6: " + e.ToString());
298                         }
299                         Assert.IsTrue (errorThrown, "empty string error not thrown");
300                 }
301                 {
302                         bool errorThrown = false;
303                         try {
304                                 new StreamReader((string)null, true);
305                         } catch (ArgumentNullException) {
306                                 errorThrown = true;
307                         } catch (Exception e) {
308                                 Assert.Fail ("Incorrect exception thrown at 7: " + e.ToString());
309                         }
310                         Assert.IsTrue (errorThrown, "null string error not thrown");
311                 }
312                 {
313                         bool errorThrown = false;
314                         try {
315                                 new StreamReader(TempFolder + "/nonexistentfile", true);
316                         } catch (FileNotFoundException) {
317                                 errorThrown = true;
318                         } catch (Exception e) {
319                                 Assert.Fail ("Incorrect exception thrown at 8: " + e.ToString());
320                         }
321                         Assert.IsTrue (errorThrown, "fileNotFound error not thrown");
322                 }
323                 {
324                         bool errorThrown = false;
325                         try {
326                                 new StreamReader(TempFolder + "/nonexistentdir/file", true);
327                         } catch (DirectoryNotFoundException) {
328                                 errorThrown = true;
329                         } catch (Exception e) {
330                                 Assert.Fail ("Incorrect exception thrown at 9: " + e.ToString());
331                         }
332                         Assert.IsTrue (errorThrown, "dirNotFound error not thrown");
333                 }
334                 {
335                         bool errorThrown = false;
336                         try {
337                                 new StreamReader("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0], true);
338                         } catch (IOException) {
339                                 errorThrown = true;
340                         } catch (ArgumentException) {
341                                 // FIXME - the spec says 'IOExc', but the
342                                 //   compiler says 'ArgExc'...
343                                 errorThrown = true;
344                         } catch (Exception e) {
345                                 Assert.Fail ("Incorrect exception thrown at 10: " + e.ToString());
346                         }
347                         Assert.IsTrue (errorThrown, "invalid filename error not thrown");
348                 }
349                 {
350                         // this is probably incestuous, but, oh well.
351                         StreamReader r = new StreamReader(_codeFileName, true);
352                         Assert.IsNotNull (r, "no stream reader");
353                         r.Close();
354                 }
355         }
356
357         // TODO - Ctor with Encoding
358         
359         [Test]
360         public void TestBaseStream() {
361                 Byte[] b = {};
362                 MemoryStream m = new MemoryStream(b);
363                 StreamReader r = new StreamReader(m);
364                 Assert.AreSame (m, r.BaseStream, "wrong base stream ");
365                 r.Close();
366                 m.Close();
367         }
368
369         public void TestCurrentEncoding()
370         {
371                 Byte[] b = {};
372                 MemoryStream m = new MemoryStream(b);
373                 StreamReader r = new StreamReader(m);
374                 Assert.AreSame (Encoding.UTF8, r.CurrentEncoding, "wrong encoding");
375         }
376
377         // TODO - Close - annoying spec - won't commit to any exceptions. How to test?
378         // TODO - DiscardBufferedData - I have no clue how to test this function.
379
380         [Test]
381         public void TestPeek() {
382                 // FIXME - how to get an IO Exception?
383                 Byte [] b;
384                 MemoryStream m;
385                 StreamReader r;
386
387                 try {
388                         b = new byte [0];
389                         m = new MemoryStream (b);
390                         r = new StreamReader(m);
391                         m.Close();
392                         int nothing = r.Peek();
393                         Assert.Fail ("#1");
394                 } catch (ObjectDisposedException) {
395                 }
396
397                 b = new byte [] {1, 2, 3, 4, 5, 6};
398                 m = new MemoryStream (b);
399                 r = new StreamReader(m);
400                 for (int i = 1; i <= 6; i++) {
401                         Assert.AreEqual (i, r.Peek(), "#2");
402                         r.Read();
403                 }
404                 Assert.AreEqual (-1, r.Peek(), "#3");
405         }
406
407         [Test]
408         public void TestRead() {
409                 // FIXME - how to get an IO Exception?
410                 {
411                         bool errorThrown = false;
412                         try {
413                                 Byte[] b = {};
414                                 MemoryStream m = new MemoryStream(b);
415                                 StreamReader r = new StreamReader(m);
416                                 m.Close();
417                                 int nothing = r.Read();
418                         } catch (ObjectDisposedException) {
419                                 errorThrown = true;
420                         } catch (Exception e) {
421                                 Assert.Fail ("Incorrect exception thrown at 1: " + e.ToString());
422                         }
423                         Assert.IsTrue (errorThrown, "nothing-to-read error not thrown");
424                 }
425                 {
426                         Byte[] b = {1, 2, 3, 4, 5, 6};
427                         MemoryStream m = new MemoryStream(b);
428                         
429                         StreamReader r = new StreamReader(m);
430                         for (int i = 1; i <= 6; i++) {
431                                 Assert.AreEqual (i, r.Read (), "read incorrect");
432                         }
433                         Assert.AreEqual (-1, r.Read (), "Should be none left");
434                 }
435
436                 {
437                         bool errorThrown = false;
438                         try {
439                                 Byte[] b = {};
440                                 StreamReader r = new StreamReader(new MemoryStream(b));
441                                 r.Read(null, 0, 0);
442                         } catch (ArgumentNullException) {
443                                 errorThrown = true;
444                         } catch (ArgumentException) {
445                                 errorThrown = true;
446                         } catch (Exception e) {
447                                 Assert.Fail ("Incorrect exception thrown at 2: " + e.ToString());
448                         }
449                         Assert.IsTrue (errorThrown, "null buffer error not thrown");
450                 }
451                 {
452                         bool errorThrown = false;
453                         try {
454                                 Byte[] b = {};
455                                 StreamReader r = new StreamReader(new MemoryStream(b));
456                                 Char[] c = new Char[1];
457                                 r.Read(c, 0, 2);
458                         } catch (ArgumentException) {
459                                 errorThrown = true;
460                         } catch (Exception e) {
461                                 Assert.Fail ("Incorrect exception thrown at 3: " + e.ToString());
462                         }
463                         Assert.IsTrue (errorThrown, "too-long range error not thrown");
464                 }
465                 {
466                         bool errorThrown = false;
467                         try {
468                                 Byte[] b = {};
469                                 StreamReader r = new StreamReader(new MemoryStream(b));
470                                 Char[] c = new Char[1];
471                                 r.Read(c, -1, 2);
472                         } catch (ArgumentOutOfRangeException) {
473                                 errorThrown = true;
474                         } catch (Exception e) {
475                                 Assert.Fail ("Incorrect exception thrown at 4: " + e.ToString());
476                         }
477                         Assert.IsTrue (errorThrown, "out of range error not thrown");
478                 }
479                 {
480                         bool errorThrown = false;
481                         try {
482                                 Byte[] b = {};
483                                 StreamReader r = new StreamReader(new MemoryStream(b));
484                                 Char[] c = new Char[1];
485                                 r.Read(c, 0, -1);
486                         } catch (ArgumentOutOfRangeException) {
487                                 errorThrown = true;
488                         } catch (Exception e) {
489                                 Assert.Fail ("Incorrect exception thrown at 5: " + e.ToString());
490                         }
491                         Assert.IsTrue (errorThrown, "out of range error not thrown");
492                 }
493                 {
494                         int ii = 1;
495                         try {
496                                 Byte[] b = {(byte)'a', (byte)'b', (byte)'c', 
497                                             (byte)'d', (byte)'e', (byte)'f', 
498                                             (byte)'g'};
499                                 MemoryStream m = new MemoryStream(b);
500                                 ii++;
501                                 StreamReader r = new StreamReader(m);
502                                 ii++;
503
504                                 char[] buffer = new Char[7];
505                                 ii++;
506                                 char[] target = {'g','d','e','f','b','c','a'};
507                                 ii++;
508                                 r.Read(buffer, 6, 1);
509                                 ii++;
510                                 r.Read(buffer, 4, 2);
511                                 ii++;
512                                 r.Read(buffer, 1, 3);
513                                 ii++;
514                                 r.Read(buffer, 0, 1);
515                                 ii++;
516                                 for (int i = 0; i < target.Length; i++) {
517                                         Assert.AreEqual (target[i], buffer[i], "read no work");
518                                 i++;
519                                 }
520                         } catch (Exception e) {
521                                 Assert.Fail ("Caught when ii=" + ii + ". e:" + e.ToString());
522                         }
523                 }
524         }
525
526         [Test]
527         public void TestReadLine() {
528                 // TODO Out Of Memory Exc? IO Exc?
529                 Byte[] b = new Byte[8];
530                 b[0] = (byte)'a';
531                 b[1] = (byte)'\n';
532                 b[2] = (byte)'b';
533                 b[3] = (byte)'\n';
534                 b[4] = (byte)'c';
535                 b[5] = (byte)'\n';
536                 b[6] = (byte)'d';
537                 b[7] = (byte)'\n';
538                 MemoryStream m = new MemoryStream(b);
539                 StreamReader r = new StreamReader(m);
540                 Assert.AreEqual ("a", r.ReadLine(), "#1");
541                 Assert.AreEqual ("b", r.ReadLine (), "#2");
542                 Assert.AreEqual ("c", r.ReadLine (), "#3");
543                 Assert.AreEqual ("d", r.ReadLine(), "#4");
544                 Assert.IsNull (r.ReadLine (), "#5");
545         }
546
547         [Test]
548         public void ReadLine1() {
549                 Byte[] b = new Byte[10];
550                 b[0] = (byte)'a';
551                 b[1] = (byte)'\r';
552                 b[2] = (byte)'b';
553                 b[3] = (byte)'\n';
554                 b[4] = (byte)'c';
555                 b[5] = (byte)'\n';
556                 b[5] = (byte)'\r';
557                 b[6] = (byte)'d';
558                 b[7] = (byte)'\n';
559                 b[8] = (byte)'\r';
560                 b[9] = (byte)'\n';
561                 MemoryStream m = new MemoryStream(b);
562                 StreamReader r = new StreamReader(m);
563                 Assert.AreEqual ("a", r.ReadLine (), "#1");
564                 Assert.AreEqual ("b", r.ReadLine (), "#2");
565                 Assert.AreEqual ("c", r.ReadLine (), "#3");
566                 Assert.AreEqual ("d", r.ReadLine (), "#4");
567                 Assert.AreEqual (string.Empty, r.ReadLine (), "#5");
568                 Assert.IsNull (r.ReadLine(), "#6");
569         }
570
571         [Test]
572         public void ReadLine2() {
573                 Byte[] b = new Byte[10];
574                 b[0] = (byte)'\r';
575                 b[1] = (byte)'\r';
576                 b[2] = (byte)'\n';
577                 b[3] = (byte)'\n';
578                 b[4] = (byte)'c';
579                 b[5] = (byte)'\n';
580                 b[5] = (byte)'\r';
581                 b[6] = (byte)'d';
582                 b[7] = (byte)'\n';
583                 b[8] = (byte)'\r';
584                 b[9] = (byte)'\n';
585                 MemoryStream m = new MemoryStream(b);
586                 StreamReader r = new StreamReader(m);
587                 Assert.AreEqual (string.Empty, r.ReadLine (), "#1");
588                 Assert.AreEqual (string.Empty, r.ReadLine (), "#2");
589                 Assert.AreEqual (string.Empty, r.ReadLine (), "#3");
590                 Assert.AreEqual ("c", r.ReadLine (), "#4");
591                 Assert.AreEqual ("d", r.ReadLine (), "#5");
592                 Assert.AreEqual (string.Empty, r.ReadLine (), "#6");
593                 Assert.IsNull (r.ReadLine (), "#7");
594         }
595
596         [Test]
597         public void ReadLine3() {
598                 StringBuilder sb = new StringBuilder ();
599                 sb.Append (new string ('1', 32767));
600                 sb.Append ('\r');
601                 sb.Append ('\n');
602                 sb.Append ("Hola\n");
603                 byte [] bytes = Encoding.Default.GetBytes (sb.ToString ());
604                 MemoryStream m = new MemoryStream(bytes);
605                 StreamReader r = new StreamReader(m);
606                 Assert.AreEqual (new string ('1', 32767), r.ReadLine(), "#1");
607                 Assert.AreEqual ("Hola", r.ReadLine (), "#2");
608                 Assert.IsNull (r.ReadLine (), "#3");
609         }
610
611         [Test]
612         public void ReadLine4() {
613                 StringBuilder sb = new StringBuilder ();
614                 sb.Append (new string ('1', 32767));
615                 sb.Append ('\r');
616                 sb.Append ('\n');
617                 sb.Append ("Hola\n");
618                 sb.Append (sb.ToString ());
619                 byte [] bytes = Encoding.Default.GetBytes (sb.ToString ());
620                 MemoryStream m = new MemoryStream(bytes);
621                 StreamReader r = new StreamReader(m);
622                 Assert.AreEqual (new string ('1', 32767), r.ReadLine (), "#1");
623                 Assert.AreEqual ("Hola", r.ReadLine (), "#2");
624                 Assert.AreEqual (new string ('1', 32767), r.ReadLine (), "#3");
625                 Assert.AreEqual ("Hola", r.ReadLine (), "#4");
626                 Assert.IsNull (r.ReadLine (), "#5");
627         }
628
629         [Test]
630         public void ReadLine5() {
631                 StringBuilder sb = new StringBuilder ();
632                 sb.Append (new string ('1', 32768));
633                 sb.Append ('\r');
634                 sb.Append ('\n');
635                 sb.Append ("Hola\n");
636                 byte [] bytes = Encoding.Default.GetBytes (sb.ToString ());
637                 MemoryStream m = new MemoryStream(bytes);
638                 StreamReader r = new StreamReader(m);
639                 Assert.AreEqual (new string ('1', 32768), r.ReadLine (), "#1");
640                 Assert.AreEqual ("Hola", r.ReadLine (), "#2");
641                 Assert.IsNull (r.ReadLine (), "#3");
642         }
643
644         public void TestReadToEnd() {
645                 // TODO Out Of Memory Exc? IO Exc?
646                 Byte[] b = new Byte[8];
647                 b[0] = (byte)'a';
648                 b[1] = (byte)'\n';
649                 b[2] = (byte)'b';
650                 b[3] = (byte)'\n';
651                 b[4] = (byte)'c';
652                 b[5] = (byte)'\n';
653                 b[6] = (byte)'d';
654                 b[7] = (byte)'\n';
655                 MemoryStream m = new MemoryStream(b);
656                 StreamReader r = new StreamReader(m);
657                 Assert.AreEqual ("a\nb\nc\nd\n", r.ReadToEnd (), "#1");
658                 Assert.AreEqual (string.Empty, r.ReadToEnd (), "#2");
659         }
660
661         [Test]
662         public void TestBaseStreamClosed ()
663         {
664                 byte [] b = {};
665                 MemoryStream m = new MemoryStream (b);
666                 StreamReader r = new StreamReader (m);
667                 m.Close ();
668                 try {
669                         r.Peek ();
670                         Assert.Fail ();
671                 } catch (ObjectDisposedException) {
672                 }
673         }
674
675         [Test]
676         [ExpectedException (typeof (ArgumentNullException))]
677         public void Contructor_Stream_NullEncoding () 
678         {
679                 new StreamReader (new MemoryStream (), null);
680         }
681
682         [Test]
683         [ExpectedException (typeof (ArgumentNullException))]
684         public void Contructor_Path_NullEncoding () 
685         {
686                 new StreamReader (_codeFileName, null);
687         }
688
689         [Test]
690         [ExpectedException (typeof (ArgumentNullException))]
691         public void Read_Null () 
692         {
693                 StreamReader r = new StreamReader (new MemoryStream ());
694                 r.Read (null, 0, 0);
695         }
696
697         [Test]
698         [ExpectedException (typeof (ArgumentException))]
699         public void Read_IndexOverflow () 
700         {
701                 char[] array = new char [16];
702                 StreamReader r = new StreamReader (new MemoryStream (16));
703                 r.Read (array, 1, Int32.MaxValue);
704         }       
705
706         [Test]
707         [ExpectedException (typeof (ArgumentException))]
708         public void Read_CountOverflow () 
709         {
710                 char[] array = new char [16];
711                 StreamReader r = new StreamReader (new MemoryStream (16));
712                 r.Read (array, Int32.MaxValue, 1);
713         }
714
715         [Test]
716         public void Read_DoesntStopAtLineEndings ()
717         {
718                 MemoryStream ms = new MemoryStream (Encoding.ASCII.GetBytes ("Line1\rLine2\r\nLine3\nLine4"));
719                 StreamReader reader = new StreamReader (ms);
720                 Assert.AreEqual (24, reader.Read (new char[24], 0, 24));
721         }
722
723         [Test]
724         public void EncodingDetection()
725         {
726                 if (!CheckEncodingDetected(Encoding.UTF8))
727                         Assert.Fail ("Failed to detect UTF8 encoded string");
728                 if (!CheckEncodingDetected(Encoding.Unicode))
729                         Assert.Fail ("Failed to detect UTF16LE encoded string");
730                 if (!CheckEncodingDetected(Encoding.BigEndianUnicode))
731                         Assert.Fail ("Failed to detect UTF16BE encoded string");
732                 if (!CheckEncodingDetected(Encoding.UTF32))
733                         Assert.Fail ("Failed to detect UTF32LE encoded string");
734                 if (!CheckEncodingDetected(new UTF32Encoding(true, true)))
735                         Assert.Fail ("Failed to detect UTF32BE encoded string");
736         }
737
738         // This is a special case, where the StreamReader has less than 4 bytes at 
739         // encoding detection time, so it tries to check for Unicode encoding, instead of
740         // waiting for more bytes to test against the UTF32 BOM.
741         [Test]
742         public void EncodingDetectionUnicode ()
743         {
744                 byte [] bytes = new byte [3];
745                 bytes [0] = 0xff;
746                 bytes [1] = 0xfe;
747                 bytes [2] = 0;
748                 MemoryStream inStream = new MemoryStream (bytes);
749                 StreamReader reader = new StreamReader (inStream, Encoding.UTF8, true);
750
751                 // It should start with the encoding we used in the .ctor
752                 Assert.AreEqual (Encoding.UTF8, reader.CurrentEncoding, "#A1");
753
754                 reader.Read ();
755                 //reader.Read ();
756                 Assert.AreEqual (Encoding.Unicode, reader.CurrentEncoding, "#B1");
757
758                 reader.Close ();
759         }
760
761         private bool CheckEncodingDetected(Encoding encoding)
762         {
763                 MemoryStream outStream = new MemoryStream();
764                 using (StreamWriter outWriter = new StreamWriter(outStream, encoding))
765                 {
766                         outWriter.Write(TestString);
767                 }
768                 byte[] testBytes = outStream.ToArray();
769
770                 StreamReader inReader = new StreamReader(new MemoryStream(testBytes, false));
771                 string decodedString = inReader.ReadToEnd();
772
773                 return decodedString == TestString;
774         }
775     
776     [Test] // Bug445326
777         [Category ("MobileNotWorking")]
778         public void EndOfBufferIsCR ()
779         {
780                 using (StreamReader reader = new StreamReader ("Test/resources/Fergie.GED")) {
781                         string line;
782                         int count = 0;
783                         while ((line = reader.ReadLine ()) != null) {
784                                 Assert.IsFalse (line.Length > 1000, "#1 " + count);
785                                 count++;
786                         }
787                         Assert.AreEqual (16107, count, "#2");
788                 }
789         }
790
791         [Test]
792         public void bug75526 ()
793         {
794                 StreamReader sr = new StreamReader (new Bug75526Stream ());
795                 int len = sr.Read (new char [10], 0, 10);
796                 Assert.AreEqual (2, len);
797         }
798
799         class Bug75526Stream : MemoryStream
800         {
801                 public override int Read (byte [] buffer, int offset, int count)
802                 {
803                         buffer [offset + 0] = (byte) 'a';
804                         buffer [offset + 1] = (byte) 'b';
805                         return 2;
806                 }
807         }
808
809         [Test]
810         public void PeekWhileBlocking ()
811         {
812                 StreamReader reader = new StreamReader (new MyStream ());
813                 int c = reader.Read ();
814                 Assert.IsFalse (reader.EndOfStream);
815                 string str = reader.ReadToEnd ();
816                 Assert.AreEqual ("bc", str);
817         }
818
819         [Test]
820         public void EncodingChangedAuto ()
821         {
822                 int testlines = 2048; // all data should larger than stream reader default buffer size
823                 string testdata = "test";
824                 MemoryStream ms = new MemoryStream();
825                 // write utf8 encoding data.
826                 using (StreamWriter sw = new StreamWriter (ms, Encoding.UTF8)) {
827                         for (int i = 0; i < testlines; i++)
828                                 sw.WriteLine (testdata);
829                 }
830
831                 MemoryStream readms = new MemoryStream (ms.GetBuffer());
832                 using (StreamReader sr = new StreamReader(readms, Encoding.Unicode, true)) {
833                         for (int i = 0; i < testlines; i++) {
834                                 string line = sr.ReadLine ();
835                                 if (line != testdata)
836                                         Assert.Fail ("Wrong line content");
837                         }
838                 }
839         }
840
841         [Test]
842         public void NullStream ()
843         {
844                 var buffer = new char[2];
845                 Assert.AreEqual (0, StreamReader.Null.ReadBlock (buffer, 0, buffer.Length));
846         }
847
848 #if NET_4_5
849         [Test]
850         public void ReadLineAsync ()
851         {
852                 MemoryStream ms = new MemoryStream ();
853                 StreamWriter sw = new StreamWriter (ms, Encoding.UTF8);
854                 sw.WriteLine ("a");
855                 sw.WriteLine ("b");
856                 sw.Flush ();
857                 ms.Seek (0, SeekOrigin.Begin);
858
859                 Func<Task<string>> res = async () => {
860                         using (StreamReader reader = new StreamReader (ms)) {
861                                 return await reader.ReadLineAsync () + await reader.ReadToEndAsync () + await reader.ReadToEndAsync ();
862                         }
863                 };
864
865                 var result = res ();
866                 Assert.IsTrue (result.Wait (3000), "#1");
867                 Assert.AreEqual ("ab" + Environment.NewLine, result.Result);
868         }
869 #endif
870 }
871
872 class MyStream : Stream {
873         int n;
874
875         public override int Read (byte [] buffer, int offset, int size)
876         {
877                 if (n == 0) {
878                         buffer [offset] = (byte) 'a';
879                         n++;
880                         return 1;
881                 } else if (n == 1) {
882                         buffer [offset] = (byte) 'b';
883                         buffer [offset + 1] = (byte) 'c';
884                         n++;
885                         return 2;
886                 }
887                 return 0;
888         }
889
890         public override bool CanRead {
891                 get { return true; }
892         }
893
894         public override bool CanSeek {
895                 get { return false; }
896         }
897
898         public override bool CanWrite {
899                 get { return false; }
900         }
901
902         public override long Length {
903                 get { return 0; }
904         }
905
906         public override long Position {
907                 get { return 0; }
908                         set { }
909         }
910
911         public override void Flush ()
912         {
913         }
914
915         public override long Seek (long offset, SeekOrigin origin)
916         {
917                 return 0;
918         }
919
920         public override void SetLength (long value)
921         {
922         }
923
924         public override void Write (byte[] buffer, int offset, int count)
925         {
926         }
927 }
928
929 }