fixed tests
[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
23         [SetUp]
24         public void SetUp ()
25         {       
26                 if (!Directory.Exists (TempFolder))
27                         Directory.CreateDirectory (TempFolder);
28                 
29                 if (!File.Exists (_codeFileName))
30                         File.Create (_codeFileName).Close ();
31         }
32
33         [TearDown]
34         public void TearDown ()
35         {
36                 if (Directory.Exists (TempFolder))
37                         Directory.Delete (TempFolder, true);
38         }
39
40
41         [Test]
42         public void TestCtor1() {
43                 {
44                         bool errorThrown = false;
45                         try {
46                                 new StreamReader((Stream)null);
47                         } catch (ArgumentNullException) {
48                                 errorThrown = true;
49                         }
50                         Assert.IsTrue (errorThrown, "null string error not thrown");
51                 }
52                 {
53                         bool errorThrown = false;
54                         FileStream f = new FileStream(_codeFileName, FileMode.Open, FileAccess.Write);
55                         try {
56                                 StreamReader r = new StreamReader(f);
57                                 r.Close();
58                         } catch (ArgumentException) {
59                                 errorThrown = true;
60                         }
61                         f.Close();
62                         Assert.IsTrue (errorThrown, "no read error not thrown");
63                 }
64                 {
65                         // this is probably incestuous, but, oh well.
66                         FileStream f = new FileStream(_codeFileName, 
67                                                       FileMode.Open, 
68                                                       FileAccess.Read);
69                         StreamReader r = new StreamReader(f);
70                         Assert.IsNotNull (r, "no stream reader");
71                         r.Close();
72                         f.Close();
73                 }
74         }
75
76         [Test]
77         public void TestCtor2() {
78                 {
79                         bool errorThrown = false;
80                         try {
81                                 new StreamReader("");
82                         } catch (ArgumentException) {
83                                 errorThrown = true;
84                         } catch (Exception e) {
85                                 Assert.Fail ("Incorrect exception thrown at 1: " + e.ToString());
86                         }
87                         Assert.IsTrue (errorThrown, "empty string error not thrown");
88                 }
89                 {
90                         bool errorThrown = false;
91                         try {
92                                 new StreamReader((string)null);
93                         } catch (ArgumentNullException) {
94                                 errorThrown = true;
95                         } catch (Exception e) {
96                                 Assert.Fail ("Incorrect exception thrown at 2: " + e.ToString());
97                         }
98                         Assert.IsTrue (errorThrown, "null string error not thrown");
99                 }
100                 {
101                         bool errorThrown = false;
102                         try {
103                                 new StreamReader("nonexistentfile");
104                         } catch (FileNotFoundException) {
105                                 errorThrown = true;
106                         } catch (Exception e) {
107                                 Assert.Fail ("Incorrect exception thrown at 3: " + e.ToString());
108                         }
109                         Assert.IsTrue (errorThrown, "fileNotFound error not thrown");
110                 }
111                 {
112                         bool errorThrown = false;
113                         try {
114                                 new StreamReader("nonexistentdir/file");
115                         } catch (DirectoryNotFoundException) {
116                                 errorThrown = true;
117                         } catch (Exception e) {
118                                 Assert.Fail ("Incorrect exception thrown at 4: " + e.ToString());
119                         }
120                         Assert.IsTrue (errorThrown, "dirNotFound error not thrown");
121                 }
122                 {
123                         bool errorThrown = false;
124                         try {
125                                 new StreamReader("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0]);
126                         } catch (IOException) {
127                                 errorThrown = true;
128                         } catch (ArgumentException) {
129                                 // FIXME - the spec says 'IOExc', but the
130                                 //   compiler says 'ArgExc'...
131                                 errorThrown = true;
132                         } catch (Exception e) {
133                                 Assert.Fail ("Incorrect exception thrown at 5: " + e.ToString());
134                         }
135                         Assert.IsTrue (errorThrown, "invalid filename error not thrown");
136                 }
137                 {
138                         // this is probably incestuous, but, oh well.
139                         StreamReader r = new StreamReader(_codeFileName);
140                         Assert.IsNotNull (r, "no stream reader");
141                         r.Close();
142                 }
143         }
144
145         [Test]
146         public void TestCtor3() {
147                 {
148                         bool errorThrown = false;
149                         try {
150                                 new StreamReader((Stream)null, false);
151                         } catch (ArgumentNullException) {
152                                 errorThrown = true;
153                         } catch (Exception e) {
154                                 Assert.Fail ("Incorrect exception thrown at 1: " + e.ToString());
155                         }
156                         Assert.IsTrue (errorThrown, "null stream error not thrown");
157                 }
158                 {
159                         bool errorThrown = false;
160                         FileStream f = new FileStream(_codeFileName, FileMode.Open, FileAccess.Write);
161                         try {
162                                 StreamReader r = new StreamReader(f, false);
163                                 r.Close();
164                         } catch (ArgumentException) {
165                                 errorThrown = true;
166                         } catch (Exception e) {
167                                 Assert.Fail ("Incorrect exception thrown at 2: " + e.ToString());
168                         }
169                         f.Close();
170                         Assert.IsTrue (errorThrown, "no read error not thrown");
171                 }
172                 {
173                         // this is probably incestuous, but, oh well.
174                         FileStream f = new FileStream(_codeFileName, 
175                                                       FileMode.Open, 
176                                                       FileAccess.Read);
177                         StreamReader r = new StreamReader(f, false);
178                         Assert.IsNotNull (r, "no stream reader");
179                         r.Close();
180                         f.Close();
181                 }
182                 {
183                         bool errorThrown = false;
184                         try {
185                                 StreamReader r = new StreamReader((Stream)null, true);
186                         } catch (ArgumentNullException) {
187                                 errorThrown = true;
188                         } catch (Exception e) {
189                                 Assert.Fail ("Incorrect exception thrown at 3: " + e.ToString());
190                         }
191                         Assert.IsTrue (errorThrown, "null string error not thrown");
192                 }
193                 {
194                         bool errorThrown = false;
195                         FileStream f = new FileStream(_codeFileName, FileMode.Open, FileAccess.Write);
196                         try {
197                                 StreamReader r = new StreamReader(f, true);
198                                 r.Close();
199                         } catch (ArgumentException) {
200                                 errorThrown = true;
201                         } catch (Exception e) {
202                                 Assert.Fail ("Incorrect exception thrown at 4: " + e.ToString());
203                         }
204                         f.Close();
205                         Assert.IsTrue (errorThrown, "no read error not thrown");
206                 }
207                 {
208                         // this is probably incestuous, but, oh well.
209                         FileStream f = new FileStream(_codeFileName, 
210                                                       FileMode.Open, 
211                                                       FileAccess.Read);
212                         StreamReader r = new StreamReader(f, true);
213                         Assert.IsNotNull (r, "no stream reader");
214                         r.Close();
215                         f.Close();
216                 }
217         }
218
219         [Test]
220         public void TestCtor4() {
221                 {
222                         bool errorThrown = false;
223                         try {
224                                 new StreamReader("", false);
225                         } catch (ArgumentException) {
226                                 errorThrown = true;
227                         } catch (Exception e) {
228                                 Assert.Fail ("Incorrect exception thrown at 1: " + e.ToString());
229                         }
230                         Assert.IsTrue (errorThrown, "empty string error not thrown");
231                 }
232                 {
233                         bool errorThrown = false;
234                         try {
235                                 new StreamReader((string)null, false);
236                         } catch (ArgumentNullException) {
237                                 errorThrown = true;
238                         } catch (Exception e) {
239                                 Assert.Fail ("Incorrect exception thrown at 2: " + e.ToString());
240                         }
241                         Assert.IsTrue (errorThrown, "null string error not thrown");
242                 }
243                 {
244                         bool errorThrown = false;
245                         try {
246                                 new StreamReader(TempFolder + "/nonexistentfile", false);
247                         } catch (FileNotFoundException) {
248                                 errorThrown = true;
249                         } catch (Exception e) {
250                                 Assert.Fail ("Incorrect exception thrown at 3: " + e.ToString());
251                         }
252                         Assert.IsTrue (errorThrown, "fileNotFound error not thrown");
253                 }
254                 {
255                         bool errorThrown = false;
256                         try {
257                                 new StreamReader(TempFolder + "/nonexistentdir/file", false);
258                         } catch (DirectoryNotFoundException) {
259                                 errorThrown = true;
260                         } catch (Exception e) {
261                                 Assert.Fail ("Incorrect exception thrown at 4: " + e.ToString());
262                         }
263                         Assert.IsTrue (errorThrown, "dirNotFound error not thrown");
264                 }
265                 {
266                         bool errorThrown = false;
267                         try {
268                                 new StreamReader("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0], false);
269                         } catch (IOException) {
270                                 errorThrown = true;
271                         } catch (ArgumentException) {
272                                 // FIXME - the spec says 'IOExc', but the
273                                 //   compiler says 'ArgExc'...
274                                 errorThrown = true;
275                         } catch (Exception e) {
276                                 Assert.Fail ("Incorrect exception thrown at 5: " + e.ToString());
277                         }
278                         Assert.IsTrue (errorThrown, "invalid filename error not thrown");
279                 }
280                 {
281                         // this is probably incestuous, but, oh well.
282                         StreamReader r = new StreamReader(_codeFileName, false);
283                         Assert.IsNotNull (r, "no stream reader");
284                         r.Close();
285                 }
286                 {
287                         bool errorThrown = false;
288                         try {
289                                 new StreamReader("", true);
290                         } catch (ArgumentException) {
291                                 errorThrown = true;
292                         } catch (Exception e) {
293                                 Assert.Fail ("Incorrect exception thrown at 6: " + e.ToString());
294                         }
295                         Assert.IsTrue (errorThrown, "empty string error not thrown");
296                 }
297                 {
298                         bool errorThrown = false;
299                         try {
300                                 new StreamReader((string)null, true);
301                         } catch (ArgumentNullException) {
302                                 errorThrown = true;
303                         } catch (Exception e) {
304                                 Assert.Fail ("Incorrect exception thrown at 7: " + e.ToString());
305                         }
306                         Assert.IsTrue (errorThrown, "null string error not thrown");
307                 }
308                 {
309                         bool errorThrown = false;
310                         try {
311                                 new StreamReader(TempFolder + "/nonexistentfile", true);
312                         } catch (FileNotFoundException) {
313                                 errorThrown = true;
314                         } catch (Exception e) {
315                                 Assert.Fail ("Incorrect exception thrown at 8: " + e.ToString());
316                         }
317                         Assert.IsTrue (errorThrown, "fileNotFound error not thrown");
318                 }
319                 {
320                         bool errorThrown = false;
321                         try {
322                                 new StreamReader(TempFolder + "/nonexistentdir/file", true);
323                         } catch (DirectoryNotFoundException) {
324                                 errorThrown = true;
325                         } catch (Exception e) {
326                                 Assert.Fail ("Incorrect exception thrown at 9: " + e.ToString());
327                         }
328                         Assert.IsTrue (errorThrown, "dirNotFound error not thrown");
329                 }
330                 {
331                         bool errorThrown = false;
332                         try {
333                                 new StreamReader("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0], true);
334                         } catch (IOException) {
335                                 errorThrown = true;
336                         } catch (ArgumentException) {
337                                 // FIXME - the spec says 'IOExc', but the
338                                 //   compiler says 'ArgExc'...
339                                 errorThrown = true;
340                         } catch (Exception e) {
341                                 Assert.Fail ("Incorrect exception thrown at 10: " + e.ToString());
342                         }
343                         Assert.IsTrue (errorThrown, "invalid filename error not thrown");
344                 }
345                 {
346                         // this is probably incestuous, but, oh well.
347                         StreamReader r = new StreamReader(_codeFileName, true);
348                         Assert.IsNotNull (r, "no stream reader");
349                         r.Close();
350                 }
351         }
352
353         // TODO - Ctor with Encoding
354         
355         [Test]
356         public void TestBaseStream() {
357                 Byte[] b = {};
358                 MemoryStream m = new MemoryStream(b);
359                 StreamReader r = new StreamReader(m);
360                 Assert.AreSame (m, r.BaseStream, "wrong base stream ");
361                 r.Close();
362                 m.Close();
363         }
364
365         public void TestCurrentEncoding() {
366                 Byte[] b = {};
367                 MemoryStream m = new MemoryStream(b);
368                 StreamReader r = new StreamReader(m);
369                 Assert.AreEqual (Encoding.UTF8.GetType (), r.CurrentEncoding.GetType (),
370                         "wrong encoding");
371         }
372
373         // TODO - Close - annoying spec - won't commit to any exceptions. How to test?
374         // TODO - DiscardBufferedData - I have no clue how to test this function.
375
376         [Test]
377         public void TestPeek() {
378                 // FIXME - how to get an IO Exception?
379                 Byte [] b;
380                 MemoryStream m;
381                 StreamReader r;
382
383                 try {
384                         b = new byte [0];
385                         m = new MemoryStream (b);
386                         r = new StreamReader(m);
387                         m.Close();
388                         int nothing = r.Peek();
389                         Assert.Fail ("#1");
390                 } catch (ObjectDisposedException) {
391                 }
392
393                 b = new byte [] {1, 2, 3, 4, 5, 6};
394                 m = new MemoryStream (b);
395                 r = new StreamReader(m);
396                 for (int i = 1; i <= 6; i++) {
397                         Assert.AreEqual (i, r.Peek(), "#2");
398                         r.Read();
399                 }
400                 Assert.AreEqual (-1, r.Peek(), "#3");
401         }
402
403         [Test]
404         public void TestRead() {
405                 // FIXME - how to get an IO Exception?
406                 {
407                         bool errorThrown = false;
408                         try {
409                                 Byte[] b = {};
410                                 MemoryStream m = new MemoryStream(b);
411                                 StreamReader r = new StreamReader(m);
412                                 m.Close();
413                                 int nothing = r.Read();
414                         } catch (ObjectDisposedException) {
415                                 errorThrown = true;
416                         } catch (Exception e) {
417                                 Assert.Fail ("Incorrect exception thrown at 1: " + e.ToString());
418                         }
419                         Assert.IsTrue (errorThrown, "nothing-to-read error not thrown");
420                 }
421                 {
422                         Byte[] b = {1, 2, 3, 4, 5, 6};
423                         MemoryStream m = new MemoryStream(b);
424                         
425                         StreamReader r = new StreamReader(m);
426                         for (int i = 1; i <= 6; i++) {
427                                 Assert.AreEqual (i, r.Read (), "read incorrect");
428                         }
429                         Assert.AreEqual (-1, r.Read (), "Should be none left");
430                 }
431
432                 {
433                         bool errorThrown = false;
434                         try {
435                                 Byte[] b = {};
436                                 StreamReader r = new StreamReader(new MemoryStream(b));
437                                 r.Read(null, 0, 0);
438                         } catch (ArgumentNullException) {
439                                 errorThrown = true;
440                         } catch (ArgumentException) {
441                                 errorThrown = true;
442                         } catch (Exception e) {
443                                 Assert.Fail ("Incorrect exception thrown at 2: " + e.ToString());
444                         }
445                         Assert.IsTrue (errorThrown, "null buffer error not thrown");
446                 }
447                 {
448                         bool errorThrown = false;
449                         try {
450                                 Byte[] b = {};
451                                 StreamReader r = new StreamReader(new MemoryStream(b));
452                                 Char[] c = new Char[1];
453                                 r.Read(c, 0, 2);
454                         } catch (ArgumentException) {
455                                 errorThrown = true;
456                         } catch (Exception e) {
457                                 Assert.Fail ("Incorrect exception thrown at 3: " + e.ToString());
458                         }
459                         Assert.IsTrue (errorThrown, "too-long range error not thrown");
460                 }
461                 {
462                         bool errorThrown = false;
463                         try {
464                                 Byte[] b = {};
465                                 StreamReader r = new StreamReader(new MemoryStream(b));
466                                 Char[] c = new Char[1];
467                                 r.Read(c, -1, 2);
468                         } catch (ArgumentOutOfRangeException) {
469                                 errorThrown = true;
470                         } catch (Exception e) {
471                                 Assert.Fail ("Incorrect exception thrown at 4: " + e.ToString());
472                         }
473                         Assert.IsTrue (errorThrown, "out of range error not thrown");
474                 }
475                 {
476                         bool errorThrown = false;
477                         try {
478                                 Byte[] b = {};
479                                 StreamReader r = new StreamReader(new MemoryStream(b));
480                                 Char[] c = new Char[1];
481                                 r.Read(c, 0, -1);
482                         } catch (ArgumentOutOfRangeException) {
483                                 errorThrown = true;
484                         } catch (Exception e) {
485                                 Assert.Fail ("Incorrect exception thrown at 5: " + e.ToString());
486                         }
487                         Assert.IsTrue (errorThrown, "out of range error not thrown");
488                 }
489                 {
490                         int ii = 1;
491                         try {
492                                 Byte[] b = {(byte)'a', (byte)'b', (byte)'c', 
493                                             (byte)'d', (byte)'e', (byte)'f', 
494                                             (byte)'g'};
495                                 MemoryStream m = new MemoryStream(b);
496                                 ii++;
497                                 StreamReader r = new StreamReader(m);
498                                 ii++;
499
500                                 char[] buffer = new Char[7];
501                                 ii++;
502                                 char[] target = {'g','d','e','f','b','c','a'};
503                                 ii++;
504                                 r.Read(buffer, 6, 1);
505                                 ii++;
506                                 r.Read(buffer, 4, 2);
507                                 ii++;
508                                 r.Read(buffer, 1, 3);
509                                 ii++;
510                                 r.Read(buffer, 0, 1);
511                                 ii++;
512                                 for (int i = 0; i < target.Length; i++) {
513                                         Assert.AreEqual (target[i], buffer[i], "read no work");
514                                 i++;
515                                 }
516                         } catch (Exception e) {
517                                 Assert.Fail ("Caught when ii=" + ii + ". e:" + e.ToString());
518                         }
519                 }
520         }
521
522         [Test]
523         public void TestReadLine() {
524                 // TODO Out Of Memory Exc? IO Exc?
525                 Byte[] b = new Byte[8];
526                 b[0] = (byte)'a';
527                 b[1] = (byte)'\n';
528                 b[2] = (byte)'b';
529                 b[3] = (byte)'\n';
530                 b[4] = (byte)'c';
531                 b[5] = (byte)'\n';
532                 b[6] = (byte)'d';
533                 b[7] = (byte)'\n';
534                 MemoryStream m = new MemoryStream(b);
535                 StreamReader r = new StreamReader(m);
536                 Assert.AreEqual ("a", r.ReadLine(), "#1");
537                 Assert.AreEqual ("b", r.ReadLine (), "#2");
538                 Assert.AreEqual ("c", r.ReadLine (), "#3");
539                 Assert.AreEqual ("d", r.ReadLine(), "#4");
540                 Assert.IsNull (r.ReadLine (), "#5");
541         }
542
543         [Test]
544         public void ReadLine1() {
545                 Byte[] b = new Byte[10];
546                 b[0] = (byte)'a';
547                 b[1] = (byte)'\r';
548                 b[2] = (byte)'b';
549                 b[3] = (byte)'\n';
550                 b[4] = (byte)'c';
551                 b[5] = (byte)'\n';
552                 b[5] = (byte)'\r';
553                 b[6] = (byte)'d';
554                 b[7] = (byte)'\n';
555                 b[8] = (byte)'\r';
556                 b[9] = (byte)'\n';
557                 MemoryStream m = new MemoryStream(b);
558                 StreamReader r = new StreamReader(m);
559                 Assert.AreEqual ("a", r.ReadLine (), "#1");
560                 Assert.AreEqual ("b", r.ReadLine (), "#2");
561                 Assert.AreEqual ("c", r.ReadLine (), "#3");
562                 Assert.AreEqual ("d", r.ReadLine (), "#4");
563                 Assert.AreEqual (string.Empty, r.ReadLine (), "#5");
564                 Assert.IsNull (r.ReadLine(), "#6");
565         }
566
567         [Test]
568         public void ReadLine2() {
569                 Byte[] b = new Byte[10];
570                 b[0] = (byte)'\r';
571                 b[1] = (byte)'\r';
572                 b[2] = (byte)'\n';
573                 b[3] = (byte)'\n';
574                 b[4] = (byte)'c';
575                 b[5] = (byte)'\n';
576                 b[5] = (byte)'\r';
577                 b[6] = (byte)'d';
578                 b[7] = (byte)'\n';
579                 b[8] = (byte)'\r';
580                 b[9] = (byte)'\n';
581                 MemoryStream m = new MemoryStream(b);
582                 StreamReader r = new StreamReader(m);
583                 Assert.AreEqual (string.Empty, r.ReadLine (), "#1");
584                 Assert.AreEqual (string.Empty, r.ReadLine (), "#2");
585                 Assert.AreEqual (string.Empty, r.ReadLine (), "#3");
586                 Assert.AreEqual ("c", r.ReadLine (), "#4");
587                 Assert.AreEqual ("d", r.ReadLine (), "#5");
588                 Assert.AreEqual (string.Empty, r.ReadLine (), "#6");
589                 Assert.IsNull (r.ReadLine (), "#7");
590         }
591
592         [Test]
593         public void ReadLine3() {
594                 StringBuilder sb = new StringBuilder ();
595                 sb.Append (new string ('1', 32767));
596                 sb.Append ('\r');
597                 sb.Append ('\n');
598                 sb.Append ("Hola\n");
599                 byte [] bytes = Encoding.Default.GetBytes (sb.ToString ());
600                 MemoryStream m = new MemoryStream(bytes);
601                 StreamReader r = new StreamReader(m);
602                 Assert.AreEqual (new string ('1', 32767), r.ReadLine(), "#1");
603                 Assert.AreEqual ("Hola", r.ReadLine (), "#2");
604                 Assert.IsNull (r.ReadLine (), "#3");
605         }
606
607         [Test]
608         public void ReadLine4() {
609                 StringBuilder sb = new StringBuilder ();
610                 sb.Append (new string ('1', 32767));
611                 sb.Append ('\r');
612                 sb.Append ('\n');
613                 sb.Append ("Hola\n");
614                 sb.Append (sb.ToString ());
615                 byte [] bytes = Encoding.Default.GetBytes (sb.ToString ());
616                 MemoryStream m = new MemoryStream(bytes);
617                 StreamReader r = new StreamReader(m);
618                 Assert.AreEqual (new string ('1', 32767), r.ReadLine (), "#1");
619                 Assert.AreEqual ("Hola", r.ReadLine (), "#2");
620                 Assert.AreEqual (new string ('1', 32767), r.ReadLine (), "#3");
621                 Assert.AreEqual ("Hola", r.ReadLine (), "#4");
622                 Assert.IsNull (r.ReadLine (), "#5");
623         }
624
625         [Test]
626         public void ReadLine5() {
627                 StringBuilder sb = new StringBuilder ();
628                 sb.Append (new string ('1', 32768));
629                 sb.Append ('\r');
630                 sb.Append ('\n');
631                 sb.Append ("Hola\n");
632                 byte [] bytes = Encoding.Default.GetBytes (sb.ToString ());
633                 MemoryStream m = new MemoryStream(bytes);
634                 StreamReader r = new StreamReader(m);
635                 Assert.AreEqual (new string ('1', 32768), r.ReadLine (), "#1");
636                 Assert.AreEqual ("Hola", r.ReadLine (), "#2");
637                 Assert.IsNull (r.ReadLine (), "#3");
638         }
639
640         public void TestReadToEnd() {
641                 // TODO Out Of Memory Exc? IO Exc?
642                 Byte[] b = new Byte[8];
643                 b[0] = (byte)'a';
644                 b[1] = (byte)'\n';
645                 b[2] = (byte)'b';
646                 b[3] = (byte)'\n';
647                 b[4] = (byte)'c';
648                 b[5] = (byte)'\n';
649                 b[6] = (byte)'d';
650                 b[7] = (byte)'\n';
651                 MemoryStream m = new MemoryStream(b);
652                 StreamReader r = new StreamReader(m);
653                 Assert.AreEqual ("a\nb\nc\nd\n", r.ReadToEnd (), "#1");
654                 Assert.AreEqual (string.Empty, r.ReadToEnd (), "#2");
655         }
656
657         [Test]
658         public void TestBaseStreamClosed ()
659         {
660                 byte [] b = {};
661                 MemoryStream m = new MemoryStream (b);
662                 StreamReader r = new StreamReader (m);
663                 m.Close ();
664                 try {
665                         r.Peek ();
666                         Assert.Fail ();
667                 } catch (ObjectDisposedException) {
668                 }
669         }
670
671         [Test]
672         [ExpectedException (typeof (ArgumentNullException))]
673         public void Contructor_Stream_NullEncoding () 
674         {
675                 new StreamReader (new MemoryStream (), null);
676         }
677
678         [Test]
679         [ExpectedException (typeof (ArgumentNullException))]
680         public void Contructor_Path_NullEncoding () 
681         {
682                 new StreamReader (_codeFileName, null);
683         }
684
685         [Test]
686         [ExpectedException (typeof (ArgumentNullException))]
687         public void Read_Null () 
688         {
689                 StreamReader r = new StreamReader (new MemoryStream ());
690                 r.Read (null, 0, 0);
691         }
692
693         [Test]
694         [ExpectedException (typeof (ArgumentException))]
695         public void Read_IndexOverflow () 
696         {
697                 char[] array = new char [16];
698                 StreamReader r = new StreamReader (new MemoryStream (16));
699                 r.Read (array, 1, Int32.MaxValue);
700         }       
701
702         [Test]
703         [ExpectedException (typeof (ArgumentException))]
704         public void Read_CountOverflow () 
705         {
706                 char[] array = new char [16];
707                 StreamReader r = new StreamReader (new MemoryStream (16));
708                 r.Read (array, Int32.MaxValue, 1);
709         }
710
711         [Test]
712         public void Read_DoesntStopAtLineEndings ()
713         {
714                 MemoryStream ms = new MemoryStream (Encoding.ASCII.GetBytes ("Line1\rLine2\r\nLine3\nLine4"));
715                 StreamReader reader = new StreamReader (ms);
716                 Assert.AreEqual (24, reader.Read (new char[24], 0, 24));
717         }
718
719         [Test]
720         public void bug75526 ()
721         {
722                 StreamReader sr = new StreamReader (new Bug75526Stream ());
723                 int len = sr.Read (new char [10], 0, 10);
724                 Assert.AreEqual (2, len);
725         }
726
727         class Bug75526Stream : MemoryStream
728         {
729                 public override int Read (byte [] buffer, int offset, int count)
730                 {
731                         buffer [offset + 0] = (byte) 'a';
732                         buffer [offset + 1] = (byte) 'b';
733                         return 2;
734                 }
735         }
736 }
737 }