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