* roottypes.cs: Rename from tree.cs.
[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 NUnit.Framework;
10 using System;
11 using System.IO;
12 using System.Text;
13
14 namespace MonoTests.System.IO
15 {
16
17 [TestFixture]
18 public class StreamReaderTest : Assertion
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                                 StreamReader r = new StreamReader((Stream)null);
47                         } catch (ArgumentNullException) {
48                                 errorThrown = true;
49                         }
50                         Assert("null string error not thrown", errorThrown);
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("no read error not thrown", errorThrown);
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                         AssertNotNull("no stream reader", r);
71                         r.Close();
72                         f.Close();
73                 }
74         }
75
76         [Test]
77         public void TestCtor2() {
78                 {
79                         bool errorThrown = false;
80                         try {
81                                 StreamReader r = new StreamReader("");
82                         } catch (ArgumentException) {
83                                 errorThrown = true;
84                         } catch (Exception e) {
85                                 Fail ("Incorrect exception thrown at 1: " + e.ToString());
86                         }
87                         Assert("empty string error not thrown", errorThrown);
88                 }
89                 {
90                         bool errorThrown = false;
91                         try {
92                                 StreamReader r = new StreamReader((string)null);
93                         } catch (ArgumentNullException) {
94                                 errorThrown = true;
95                         } catch (Exception e) {
96                                 Fail ("Incorrect exception thrown at 2: " + e.ToString());
97                         }
98                         Assert("null string error not thrown", errorThrown);
99                 }
100                 {
101                         bool errorThrown = false;
102                         try {
103                                 StreamReader r = new StreamReader("nonexistentfile");
104                         } catch (FileNotFoundException) {
105                                 errorThrown = true;
106                         } catch (Exception e) {
107                                 Fail ("Incorrect exception thrown at 3: " + e.ToString());
108                         }
109                         Assert("fileNotFound error not thrown", errorThrown);
110                 }
111                 {
112                         bool errorThrown = false;
113                         try {
114                                 StreamReader r = new StreamReader("nonexistentdir/file");
115                         } catch (DirectoryNotFoundException) {
116                                 errorThrown = true;
117                         } catch (Exception e) {
118                                 Fail ("Incorrect exception thrown at 4: " + e.ToString());
119                         }
120                         Assert("dirNotFound error not thrown", errorThrown);
121                 }
122                 {
123                         bool errorThrown = false;
124                         try {
125                                 StreamReader r = 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                                 Fail ("Incorrect exception thrown at 5: " + e.ToString());
134                         }
135                         Assert("invalid filename error not thrown", errorThrown);
136                 }
137                 {
138                         // this is probably incestuous, but, oh well.
139                         StreamReader r = new StreamReader(_codeFileName);
140                         AssertNotNull("no stream reader", r);
141                         r.Close();
142                 }
143         }
144
145         [Test]
146         public void TestCtor3() {
147                 {
148                         bool errorThrown = false;
149                         try {
150                                 StreamReader r = new StreamReader((Stream)null, false);
151                         } catch (ArgumentNullException) {
152                                 errorThrown = true;
153                         } catch (Exception e) {
154                                 Fail ("Incorrect exception thrown at 1: " + e.ToString());
155                         }
156                         Assert("null stream error not thrown", errorThrown);
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                                 Fail ("Incorrect exception thrown at 2: " + e.ToString());
168                         }
169                         f.Close();
170                         Assert("no read error not thrown", errorThrown);
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                         AssertNotNull("no stream reader", r);
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                                 Fail ("Incorrect exception thrown at 3: " + e.ToString());
190                         }
191                         Assert("null string error not thrown", errorThrown);
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                                 Fail ("Incorrect exception thrown at 4: " + e.ToString());
203                         }
204                         f.Close();
205                         Assert("no read error not thrown", errorThrown);
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                         AssertNotNull("no stream reader", r);
214                         r.Close();
215                         f.Close();
216                 }
217         }
218
219         [Test]
220         public void TestCtor4() {
221                 {
222                         bool errorThrown = false;
223                         try {
224                                 StreamReader r = new StreamReader("", false);
225                         } catch (ArgumentException) {
226                                 errorThrown = true;
227                         } catch (Exception e) {
228                                 Fail ("Incorrect exception thrown at 1: " + e.ToString());
229                         }
230                         Assert("empty string error not thrown", errorThrown);
231                 }
232                 {
233                         bool errorThrown = false;
234                         try {
235                                 StreamReader r = new StreamReader((string)null, false);
236                         } catch (ArgumentNullException) {
237                                 errorThrown = true;
238                         } catch (Exception e) {
239                                 Fail ("Incorrect exception thrown at 2: " + e.ToString());
240                         }
241                         Assert("null string error not thrown", errorThrown);
242                 }
243                 {
244                         bool errorThrown = false;
245                         try {
246                                 StreamReader r = new StreamReader(TempFolder + "/nonexistentfile", false);
247                         } catch (FileNotFoundException) {
248                                 errorThrown = true;
249                         } catch (Exception e) {
250                                 Fail ("Incorrect exception thrown at 3: " + e.ToString());
251                         }
252                         Assert("fileNotFound error not thrown", errorThrown);
253                 }
254                 {
255                         bool errorThrown = false;
256                         try {
257                                 StreamReader r = new StreamReader(TempFolder + "/nonexistentdir/file", false);
258                         } catch (DirectoryNotFoundException) {
259                                 errorThrown = true;
260                         } catch (Exception e) {
261                                 Fail ("Incorrect exception thrown at 4: " + e.ToString());
262                         }
263                         Assert("dirNotFound error not thrown", errorThrown);
264                 }
265                 {
266                         bool errorThrown = false;
267                         try {
268                                 StreamReader r = 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                                 Fail ("Incorrect exception thrown at 5: " + e.ToString());
277                         }
278                         Assert("invalid filename error not thrown", errorThrown);
279                 }
280                 {
281                         // this is probably incestuous, but, oh well.
282                         StreamReader r = new StreamReader(_codeFileName, false);
283                         AssertNotNull("no stream reader", r);
284                         r.Close();
285                 }
286                 {
287                         bool errorThrown = false;
288                         try {
289                                 StreamReader r = new StreamReader("", true);
290                         } catch (ArgumentException) {
291                                 errorThrown = true;
292                         } catch (Exception e) {
293                                 Fail ("Incorrect exception thrown at 6: " + e.ToString());
294                         }
295                         Assert("empty string error not thrown", errorThrown);
296                 }
297                 {
298                         bool errorThrown = false;
299                         try {
300                                 StreamReader r = new StreamReader((string)null, true);
301                         } catch (ArgumentNullException) {
302                                 errorThrown = true;
303                         } catch (Exception e) {
304                                 Fail ("Incorrect exception thrown at 7: " + e.ToString());
305                         }
306                         Assert("null string error not thrown", errorThrown);
307                 }
308                 {
309                         bool errorThrown = false;
310                         try {
311                                 StreamReader r = new StreamReader(TempFolder + "/nonexistentfile", true);
312                         } catch (FileNotFoundException) {
313                                 errorThrown = true;
314                         } catch (Exception e) {
315                                 Fail ("Incorrect exception thrown at 8: " + e.ToString());
316                         }
317                         Assert("fileNotFound error not thrown", errorThrown);
318                 }
319                 {
320                         bool errorThrown = false;
321                         try {
322                                 StreamReader r = new StreamReader(TempFolder + "/nonexistentdir/file", true);
323                         } catch (DirectoryNotFoundException) {
324                                 errorThrown = true;
325                         } catch (Exception e) {
326                                 Fail ("Incorrect exception thrown at 9: " + e.ToString());
327                         }
328                         Assert("dirNotFound error not thrown", errorThrown);
329                 }
330                 {
331                         bool errorThrown = false;
332                         try {
333                                 StreamReader r = 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                                 Fail ("Incorrect exception thrown at 10: " + e.ToString());
342                         }
343                         Assert("invalid filename error not thrown", errorThrown);
344                 }
345                 {
346                         // this is probably incestuous, but, oh well.
347                         StreamReader r = new StreamReader(_codeFileName, true);
348                         AssertNotNull("no stream reader", r);
349                         r.Close();
350                 }
351         }
352
353         // TODO - Ctor with Encoding
354         
355         [Test]
356         public void TestBaseStream() {
357                 string progress = "beginning";
358                 try {
359                         Byte[] b = {};
360                         MemoryStream m = new MemoryStream(b);
361                         StreamReader r = new StreamReader(m);
362                         AssertEquals("wrong base stream ", m, r.BaseStream);
363                         progress = "Closing StreamReader";
364                         r.Close();
365                         progress = "Closing MemoryStream";
366                         m.Close();
367                 } catch (Exception e) {
368                         Fail ("At '" + progress + "' an unexpected exception was thrown: " + e.ToString());
369                 }
370         }
371
372         public void TestCurrentEncoding() {
373                 try {
374                         Byte[] b = {};
375                         MemoryStream m = new MemoryStream(b);
376                         StreamReader r = new StreamReader(m);
377                         AssertEquals("wrong encoding", 
378                                      Encoding.UTF8.GetType (), r.CurrentEncoding.GetType ());
379                 } catch (Exception e) {
380                         Fail ("Unexpected exception thrown: " + e.ToString());
381                 }
382         }
383
384         // TODO - Close - annoying spec - won't commit to any exceptions. How to test?
385         // TODO - DiscardBufferedData - I have no clue how to test this function.
386
387         [Test]
388         public void TestPeek() {
389                 // FIXME - how to get an IO Exception?
390                 {
391                         bool errorThrown = false;
392                         try {
393                                 Byte[] b = {};
394                                 MemoryStream m = new MemoryStream(b);
395                                 StreamReader r = new StreamReader(m);
396                                 m.Close();
397                                 int nothing = r.Peek();
398                         } catch (ObjectDisposedException) {
399                                 errorThrown = true;
400                         }
401                         Assert("nothing-to-peek-at error not thrown", errorThrown);
402                 }
403                 {
404                         Byte[] b = {1, 2, 3, 4, 5, 6};
405                         MemoryStream m = new MemoryStream(b);
406                         
407                         StreamReader r = new StreamReader(m);
408                         for (int i = 1; i <= 6; i++) {
409                                 AssertEquals("peek incorrect", i, r.Peek());
410                                 r.Read();
411                         }
412                         AssertEquals("should be none left", -1, r.Peek());
413                 }
414         }
415
416         [Test]
417         public void TestRead() {
418                 // FIXME - how to get an IO Exception?
419                 {
420                         bool errorThrown = false;
421                         try {
422                                 Byte[] b = {};
423                                 MemoryStream m = new MemoryStream(b);
424                                 StreamReader r = new StreamReader(m);
425                                 m.Close();
426                                 int nothing = r.Read();
427                         } catch (ObjectDisposedException) {
428                                 errorThrown = true;
429                         } catch (Exception e) {
430                                 Fail ("Incorrect exception thrown at 1: " + e.ToString());
431                         }
432                         Assert("nothing-to-read error not thrown", errorThrown);
433                 }
434                 {
435                         Byte[] b = {1, 2, 3, 4, 5, 6};
436                         MemoryStream m = new MemoryStream(b);
437                         
438                         StreamReader r = new StreamReader(m);
439                         for (int i = 1; i <= 6; i++) {
440                                 AssertEquals("read incorrect", i, r.Read());
441                         }
442                         AssertEquals("Should be none left", -1, r.Read());
443                 }
444
445                 {
446                         bool errorThrown = false;
447                         try {
448                                 Byte[] b = {};
449                                 StreamReader r = new StreamReader(new MemoryStream(b));
450                                 r.Read(null, 0, 0);
451                         } catch (ArgumentNullException) {
452                                 errorThrown = true;
453                         } catch (ArgumentException) {
454                                 errorThrown = true;
455                         } catch (Exception e) {
456                                 Fail ("Incorrect exception thrown at 2: " + e.ToString());
457                         }
458                         Assert("null buffer error not thrown", errorThrown);
459                 }
460                 {
461                         bool errorThrown = false;
462                         try {
463                                 Byte[] b = {};
464                                 StreamReader r = new StreamReader(new MemoryStream(b));
465                                 Char[] c = new Char[1];
466                                 r.Read(c, 0, 2);
467                         } catch (ArgumentException) {
468                                 errorThrown = true;
469                         } catch (Exception e) {
470                                 Fail ("Incorrect exception thrown at 3: " + e.ToString());
471                         }
472                         Assert("too-long range error not thrown", errorThrown);
473                 }
474                 {
475                         bool errorThrown = false;
476                         try {
477                                 Byte[] b = {};
478                                 StreamReader r = new StreamReader(new MemoryStream(b));
479                                 Char[] c = new Char[1];
480                                 r.Read(c, -1, 2);
481                         } catch (ArgumentOutOfRangeException) {
482                                 errorThrown = true;
483                         } catch (Exception e) {
484                                 Fail ("Incorrect exception thrown at 4: " + e.ToString());
485                         }
486                         Assert("out of range error not thrown", errorThrown);
487                 }
488                 {
489                         bool errorThrown = false;
490                         try {
491                                 Byte[] b = {};
492                                 StreamReader r = new StreamReader(new MemoryStream(b));
493                                 Char[] c = new Char[1];
494                                 r.Read(c, 0, -1);
495                         } catch (ArgumentOutOfRangeException) {
496                                 errorThrown = true;
497                         } catch (Exception e) {
498                                 Fail ("Incorrect exception thrown at 5: " + e.ToString());
499                         }
500                         Assert("out of range error not thrown", errorThrown);
501                 }
502                 {
503                         int ii = 1;
504                         try {
505                                 Byte[] b = {(byte)'a', (byte)'b', (byte)'c', 
506                                             (byte)'d', (byte)'e', (byte)'f', 
507                                             (byte)'g'};
508                                 MemoryStream m = new MemoryStream(b);
509                                 ii++;
510                                 StreamReader r = new StreamReader(m);
511                                 ii++;
512
513                                 char[] buffer = new Char[7];
514                                 ii++;
515                                 char[] target = {'g','d','e','f','b','c','a'};
516                                 ii++;
517                                 r.Read(buffer, 6, 1);
518                                 ii++;
519                                 r.Read(buffer, 4, 2);
520                                 ii++;
521                                 r.Read(buffer, 1, 3);
522                                 ii++;
523                                 r.Read(buffer, 0, 1);
524                                 ii++;
525                                 for (int i = 0; i < target.Length; i++) {
526                                         AssertEquals("read no work", 
527                                                      target[i], buffer[i]);
528                                 i++;
529                                 }
530                                                     
531                         } catch (Exception e) {
532                                 Fail ("Caught when ii=" + ii + ". e:" + e.ToString());
533                         }
534                 }
535         }
536
537         [Test]
538         public void TestReadLine() {
539                 // TODO Out Of Memory Exc? IO Exc?
540                 Byte[] b = new Byte[8];
541                 b[0] = (byte)'a';
542                 b[1] = (byte)'\n';
543                 b[2] = (byte)'b';
544                 b[3] = (byte)'\n';
545                 b[4] = (byte)'c';
546                 b[5] = (byte)'\n';
547                 b[6] = (byte)'d';
548                 b[7] = (byte)'\n';
549                 MemoryStream m = new MemoryStream(b);
550                 StreamReader r = new StreamReader(m);
551                 AssertEquals("line doesn't match", "a", r.ReadLine());
552                 AssertEquals("line doesn't match", "b", r.ReadLine());
553                 AssertEquals("line doesn't match", "c", r.ReadLine());
554                 AssertEquals("line doesn't match", "d", r.ReadLine());
555                 AssertEquals("line doesn't match", null, r.ReadLine());
556         }
557
558         [Test]
559         public void ReadLine1() {
560                 Byte[] b = new Byte[10];
561                 b[0] = (byte)'a';
562                 b[1] = (byte)'\r';
563                 b[2] = (byte)'b';
564                 b[3] = (byte)'\n';
565                 b[4] = (byte)'c';
566                 b[5] = (byte)'\n';
567                 b[5] = (byte)'\r';
568                 b[6] = (byte)'d';
569                 b[7] = (byte)'\n';
570                 b[8] = (byte)'\r';
571                 b[9] = (byte)'\n';
572                 MemoryStream m = new MemoryStream(b);
573                 StreamReader r = new StreamReader(m);
574                 AssertEquals("line doesn't match", "a", r.ReadLine());
575                 AssertEquals("line doesn't match", "b", r.ReadLine());
576                 AssertEquals("line doesn't match", "c", r.ReadLine());
577                 AssertEquals("line doesn't match", "d", r.ReadLine());
578                 AssertEquals("line doesn't match", "", r.ReadLine());
579                 AssertEquals("line doesn't match", null, r.ReadLine());
580         }
581
582         [Test]
583         public void ReadLine2() {
584                 Byte[] b = new Byte[10];
585                 b[0] = (byte)'\r';
586                 b[1] = (byte)'\r';
587                 b[2] = (byte)'\n';
588                 b[3] = (byte)'\n';
589                 b[4] = (byte)'c';
590                 b[5] = (byte)'\n';
591                 b[5] = (byte)'\r';
592                 b[6] = (byte)'d';
593                 b[7] = (byte)'\n';
594                 b[8] = (byte)'\r';
595                 b[9] = (byte)'\n';
596                 MemoryStream m = new MemoryStream(b);
597                 StreamReader r = new StreamReader(m);
598                 AssertEquals("line doesn't match", "", r.ReadLine());
599                 AssertEquals("line doesn't match", "", r.ReadLine());
600                 AssertEquals("line doesn't match", "", r.ReadLine());
601                 AssertEquals("line doesn't match", "c", r.ReadLine());
602                 AssertEquals("line doesn't match", "d", r.ReadLine());
603                 AssertEquals("line doesn't match", "", r.ReadLine());
604                 AssertEquals("line doesn't match", null, r.ReadLine());
605         }
606
607         [Test]
608         public void ReadLine3() {
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                 byte [] bytes = Encoding.Default.GetBytes (sb.ToString ());
615                 MemoryStream m = new MemoryStream(bytes);
616                 StreamReader r = new StreamReader(m);
617                 AssertEquals("line doesn't match", new string ('1', 32767), r.ReadLine());
618                 AssertEquals("line doesn't match", "Hola", r.ReadLine());
619                 AssertEquals("line doesn't match", null, r.ReadLine());
620         }
621
622         [Test]
623         public void ReadLine4() {
624                 StringBuilder sb = new StringBuilder ();
625                 sb.Append (new string ('1', 32767));
626                 sb.Append ('\r');
627                 sb.Append ('\n');
628                 sb.Append ("Hola\n");
629                 sb.Append (sb.ToString ());
630                 byte [] bytes = Encoding.Default.GetBytes (sb.ToString ());
631                 MemoryStream m = new MemoryStream(bytes);
632                 StreamReader r = new StreamReader(m);
633                 AssertEquals("line doesn't match", new string ('1', 32767), r.ReadLine());
634                 AssertEquals("line doesn't match", "Hola", r.ReadLine());
635                 AssertEquals("line doesn't match", new string ('1', 32767), r.ReadLine());
636                 AssertEquals("line doesn't match", "Hola", r.ReadLine());
637                 AssertEquals("line doesn't match", null, r.ReadLine());
638         }
639
640         [Test]
641         public void ReadLine5() {
642                 StringBuilder sb = new StringBuilder ();
643                 sb.Append (new string ('1', 32768));
644                 sb.Append ('\r');
645                 sb.Append ('\n');
646                 sb.Append ("Hola\n");
647                 byte [] bytes = Encoding.Default.GetBytes (sb.ToString ());
648                 MemoryStream m = new MemoryStream(bytes);
649                 StreamReader r = new StreamReader(m);
650                 AssertEquals("line doesn't match", new string ('1', 32768), r.ReadLine());
651                 AssertEquals("line doesn't match", "Hola", r.ReadLine());
652                 AssertEquals("line doesn't match", null, r.ReadLine());
653         }
654
655         public void TestReadToEnd() {
656                 // TODO Out Of Memory Exc? IO Exc?
657                 Byte[] b = new Byte[8];
658                 b[0] = (byte)'a';
659                 b[1] = (byte)'\n';
660                 b[2] = (byte)'b';
661                 b[3] = (byte)'\n';
662                 b[4] = (byte)'c';
663                 b[5] = (byte)'\n';
664                 b[6] = (byte)'d';
665                 b[7] = (byte)'\n';
666                 MemoryStream m = new MemoryStream(b);
667                 StreamReader r = new StreamReader(m);
668                 AssertEquals("line doesn't match", "a\nb\nc\nd\n", r.ReadToEnd());
669                 AssertEquals("line doesn't match", "", r.ReadToEnd());
670         }
671
672         [Test]
673         public void TestBaseStreamClosed ()
674         {
675                 byte [] b = {};
676                 MemoryStream m = new MemoryStream (b);
677                 StreamReader r = new StreamReader (m);
678                 m.Close ();
679                 bool thrown = false;
680                 try {
681                         r.Peek ();
682                 } catch (ObjectDisposedException) {
683                         thrown = true;
684                 }
685
686                 AssertEquals ("#01", true, thrown);
687         }
688
689         [Test]
690         [ExpectedException (typeof (ArgumentNullException))]
691         public void Contructor_Stream_NullEncoding () 
692         {
693                 StreamReader r = new StreamReader (new MemoryStream (), null);
694         }
695
696         [Test]
697         [ExpectedException (typeof (ArgumentNullException))]
698         public void Contructor_Path_NullEncoding () 
699         {
700                 StreamReader r = new StreamReader (_codeFileName, null);
701         }
702
703         [Test]
704         [ExpectedException (typeof (ArgumentNullException))]
705         public void Read_Null () 
706         {
707                 StreamReader r = new StreamReader (new MemoryStream ());
708                 r.Read (null, 0, 0);
709         }
710
711         [Test]
712         [ExpectedException (typeof (ArgumentException))]
713         public void Read_IndexOverflow () 
714         {
715                 char[] array = new char [16];
716                 StreamReader r = new StreamReader (new MemoryStream (16));
717                 r.Read (array, 1, Int32.MaxValue);
718         }       
719
720         [Test]
721         [ExpectedException (typeof (ArgumentException))]
722         public void Read_CountOverflow () 
723         {
724                 char[] array = new char [16];
725                 StreamReader r = new StreamReader (new MemoryStream (16));
726                 r.Read (array, Int32.MaxValue, 1);
727         }
728
729         [Test]
730         public void Read_DoesntStopAtLineEndings ()
731         {
732                 MemoryStream ms = new MemoryStream (Encoding.ASCII.GetBytes ("Line1\rLine2\r\nLine3\nLine4"));
733                 StreamReader reader = new StreamReader (ms);
734                 AssertEquals (24, reader.Read (new char[24], 0, 24));
735         }       
736 }
737 }