1 // StreamReaderTest.cs - NUnit Test Cases for the SystemIO.StreamReader class
3 // David Brandt (bucky@keystreams.com)
5 // (C) Ximian, Inc. http://www.ximian.com
6 // Copyright (C) 2004 Novell (http://www.novell.com)
13 using NUnit.Framework;
15 namespace MonoTests.System.IO
18 public class StreamReaderTest
20 static string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
21 private string _codeFileName = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
26 if (!Directory.Exists (TempFolder))
27 Directory.CreateDirectory (TempFolder);
29 if (!File.Exists (_codeFileName))
30 File.Create (_codeFileName).Close ();
34 public void TearDown ()
36 if (Directory.Exists (TempFolder))
37 Directory.Delete (TempFolder, true);
42 public void TestCtor1() {
44 bool errorThrown = false;
46 new StreamReader((Stream)null);
47 } catch (ArgumentNullException) {
50 Assert.IsTrue (errorThrown, "null string error not thrown");
53 bool errorThrown = false;
54 FileStream f = new FileStream(_codeFileName, FileMode.Open, FileAccess.Write);
56 StreamReader r = new StreamReader(f);
58 } catch (ArgumentException) {
62 Assert.IsTrue (errorThrown, "no read error not thrown");
65 // this is probably incestuous, but, oh well.
66 FileStream f = new FileStream(_codeFileName,
69 StreamReader r = new StreamReader(f);
70 Assert.IsNotNull (r, "no stream reader");
77 public void TestCtor2() {
79 bool errorThrown = false;
82 } catch (ArgumentException) {
84 } catch (Exception e) {
85 Assert.Fail ("Incorrect exception thrown at 1: " + e.ToString());
87 Assert.IsTrue (errorThrown, "empty string error not thrown");
90 bool errorThrown = false;
92 new StreamReader((string)null);
93 } catch (ArgumentNullException) {
95 } catch (Exception e) {
96 Assert.Fail ("Incorrect exception thrown at 2: " + e.ToString());
98 Assert.IsTrue (errorThrown, "null string error not thrown");
101 bool errorThrown = false;
103 new StreamReader("nonexistentfile");
104 } catch (FileNotFoundException) {
106 } catch (Exception e) {
107 Assert.Fail ("Incorrect exception thrown at 3: " + e.ToString());
109 Assert.IsTrue (errorThrown, "fileNotFound error not thrown");
112 bool errorThrown = false;
114 new StreamReader("nonexistentdir/file");
115 } catch (DirectoryNotFoundException) {
117 } catch (Exception e) {
118 Assert.Fail ("Incorrect exception thrown at 4: " + e.ToString());
120 Assert.IsTrue (errorThrown, "dirNotFound error not thrown");
123 bool errorThrown = false;
125 new StreamReader("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0]);
126 } catch (IOException) {
128 } catch (ArgumentException) {
129 // FIXME - the spec says 'IOExc', but the
130 // compiler says 'ArgExc'...
132 } catch (Exception e) {
133 Assert.Fail ("Incorrect exception thrown at 5: " + e.ToString());
135 Assert.IsTrue (errorThrown, "invalid filename error not thrown");
138 // this is probably incestuous, but, oh well.
139 StreamReader r = new StreamReader(_codeFileName);
140 Assert.IsNotNull (r, "no stream reader");
146 public void TestCtor3() {
148 bool errorThrown = false;
150 new StreamReader((Stream)null, false);
151 } catch (ArgumentNullException) {
153 } catch (Exception e) {
154 Assert.Fail ("Incorrect exception thrown at 1: " + e.ToString());
156 Assert.IsTrue (errorThrown, "null stream error not thrown");
159 bool errorThrown = false;
160 FileStream f = new FileStream(_codeFileName, FileMode.Open, FileAccess.Write);
162 StreamReader r = new StreamReader(f, false);
164 } catch (ArgumentException) {
166 } catch (Exception e) {
167 Assert.Fail ("Incorrect exception thrown at 2: " + e.ToString());
170 Assert.IsTrue (errorThrown, "no read error not thrown");
173 // this is probably incestuous, but, oh well.
174 FileStream f = new FileStream(_codeFileName,
177 StreamReader r = new StreamReader(f, false);
178 Assert.IsNotNull (r, "no stream reader");
183 bool errorThrown = false;
185 StreamReader r = new StreamReader((Stream)null, true);
186 } catch (ArgumentNullException) {
188 } catch (Exception e) {
189 Assert.Fail ("Incorrect exception thrown at 3: " + e.ToString());
191 Assert.IsTrue (errorThrown, "null string error not thrown");
194 bool errorThrown = false;
195 FileStream f = new FileStream(_codeFileName, FileMode.Open, FileAccess.Write);
197 StreamReader r = new StreamReader(f, true);
199 } catch (ArgumentException) {
201 } catch (Exception e) {
202 Assert.Fail ("Incorrect exception thrown at 4: " + e.ToString());
205 Assert.IsTrue (errorThrown, "no read error not thrown");
208 // this is probably incestuous, but, oh well.
209 FileStream f = new FileStream(_codeFileName,
212 StreamReader r = new StreamReader(f, true);
213 Assert.IsNotNull (r, "no stream reader");
220 public void TestCtor4() {
222 bool errorThrown = false;
224 new StreamReader("", false);
225 } catch (ArgumentException) {
227 } catch (Exception e) {
228 Assert.Fail ("Incorrect exception thrown at 1: " + e.ToString());
230 Assert.IsTrue (errorThrown, "empty string error not thrown");
233 bool errorThrown = false;
235 new StreamReader((string)null, false);
236 } catch (ArgumentNullException) {
238 } catch (Exception e) {
239 Assert.Fail ("Incorrect exception thrown at 2: " + e.ToString());
241 Assert.IsTrue (errorThrown, "null string error not thrown");
244 bool errorThrown = false;
246 new StreamReader(TempFolder + "/nonexistentfile", false);
247 } catch (FileNotFoundException) {
249 } catch (Exception e) {
250 Assert.Fail ("Incorrect exception thrown at 3: " + e.ToString());
252 Assert.IsTrue (errorThrown, "fileNotFound error not thrown");
255 bool errorThrown = false;
257 new StreamReader(TempFolder + "/nonexistentdir/file", false);
258 } catch (DirectoryNotFoundException) {
260 } catch (Exception e) {
261 Assert.Fail ("Incorrect exception thrown at 4: " + e.ToString());
263 Assert.IsTrue (errorThrown, "dirNotFound error not thrown");
266 bool errorThrown = false;
268 new StreamReader("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0], false);
269 } catch (IOException) {
271 } catch (ArgumentException) {
272 // FIXME - the spec says 'IOExc', but the
273 // compiler says 'ArgExc'...
275 } catch (Exception e) {
276 Assert.Fail ("Incorrect exception thrown at 5: " + e.ToString());
278 Assert.IsTrue (errorThrown, "invalid filename error not thrown");
281 // this is probably incestuous, but, oh well.
282 StreamReader r = new StreamReader(_codeFileName, false);
283 Assert.IsNotNull (r, "no stream reader");
287 bool errorThrown = false;
289 new StreamReader("", true);
290 } catch (ArgumentException) {
292 } catch (Exception e) {
293 Assert.Fail ("Incorrect exception thrown at 6: " + e.ToString());
295 Assert.IsTrue (errorThrown, "empty string error not thrown");
298 bool errorThrown = false;
300 new StreamReader((string)null, true);
301 } catch (ArgumentNullException) {
303 } catch (Exception e) {
304 Assert.Fail ("Incorrect exception thrown at 7: " + e.ToString());
306 Assert.IsTrue (errorThrown, "null string error not thrown");
309 bool errorThrown = false;
311 new StreamReader(TempFolder + "/nonexistentfile", true);
312 } catch (FileNotFoundException) {
314 } catch (Exception e) {
315 Assert.Fail ("Incorrect exception thrown at 8: " + e.ToString());
317 Assert.IsTrue (errorThrown, "fileNotFound error not thrown");
320 bool errorThrown = false;
322 new StreamReader(TempFolder + "/nonexistentdir/file", true);
323 } catch (DirectoryNotFoundException) {
325 } catch (Exception e) {
326 Assert.Fail ("Incorrect exception thrown at 9: " + e.ToString());
328 Assert.IsTrue (errorThrown, "dirNotFound error not thrown");
331 bool errorThrown = false;
333 new StreamReader("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0], true);
334 } catch (IOException) {
336 } catch (ArgumentException) {
337 // FIXME - the spec says 'IOExc', but the
338 // compiler says 'ArgExc'...
340 } catch (Exception e) {
341 Assert.Fail ("Incorrect exception thrown at 10: " + e.ToString());
343 Assert.IsTrue (errorThrown, "invalid filename error not thrown");
346 // this is probably incestuous, but, oh well.
347 StreamReader r = new StreamReader(_codeFileName, true);
348 Assert.IsNotNull (r, "no stream reader");
353 // TODO - Ctor with Encoding
356 public void TestBaseStream() {
358 MemoryStream m = new MemoryStream(b);
359 StreamReader r = new StreamReader(m);
360 Assert.AreSame (m, r.BaseStream, "wrong base stream ");
365 public void TestCurrentEncoding() {
367 MemoryStream m = new MemoryStream(b);
368 StreamReader r = new StreamReader(m);
369 Assert.AreEqual (Encoding.UTF8.GetType (), r.CurrentEncoding.GetType (),
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.
377 public void TestPeek() {
378 // FIXME - how to get an IO Exception?
385 m = new MemoryStream (b);
386 r = new StreamReader(m);
388 int nothing = r.Peek();
390 } catch (ObjectDisposedException) {
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");
400 Assert.AreEqual (-1, r.Peek(), "#3");
404 public void TestRead() {
405 // FIXME - how to get an IO Exception?
407 bool errorThrown = false;
410 MemoryStream m = new MemoryStream(b);
411 StreamReader r = new StreamReader(m);
413 int nothing = r.Read();
414 } catch (ObjectDisposedException) {
416 } catch (Exception e) {
417 Assert.Fail ("Incorrect exception thrown at 1: " + e.ToString());
419 Assert.IsTrue (errorThrown, "nothing-to-read error not thrown");
422 Byte[] b = {1, 2, 3, 4, 5, 6};
423 MemoryStream m = new MemoryStream(b);
425 StreamReader r = new StreamReader(m);
426 for (int i = 1; i <= 6; i++) {
427 Assert.AreEqual (i, r.Read (), "read incorrect");
429 Assert.AreEqual (-1, r.Read (), "Should be none left");
433 bool errorThrown = false;
436 StreamReader r = new StreamReader(new MemoryStream(b));
438 } catch (ArgumentNullException) {
440 } catch (ArgumentException) {
442 } catch (Exception e) {
443 Assert.Fail ("Incorrect exception thrown at 2: " + e.ToString());
445 Assert.IsTrue (errorThrown, "null buffer error not thrown");
448 bool errorThrown = false;
451 StreamReader r = new StreamReader(new MemoryStream(b));
452 Char[] c = new Char[1];
454 } catch (ArgumentException) {
456 } catch (Exception e) {
457 Assert.Fail ("Incorrect exception thrown at 3: " + e.ToString());
459 Assert.IsTrue (errorThrown, "too-long range error not thrown");
462 bool errorThrown = false;
465 StreamReader r = new StreamReader(new MemoryStream(b));
466 Char[] c = new Char[1];
468 } catch (ArgumentOutOfRangeException) {
470 } catch (Exception e) {
471 Assert.Fail ("Incorrect exception thrown at 4: " + e.ToString());
473 Assert.IsTrue (errorThrown, "out of range error not thrown");
476 bool errorThrown = false;
479 StreamReader r = new StreamReader(new MemoryStream(b));
480 Char[] c = new Char[1];
482 } catch (ArgumentOutOfRangeException) {
484 } catch (Exception e) {
485 Assert.Fail ("Incorrect exception thrown at 5: " + e.ToString());
487 Assert.IsTrue (errorThrown, "out of range error not thrown");
492 Byte[] b = {(byte)'a', (byte)'b', (byte)'c',
493 (byte)'d', (byte)'e', (byte)'f',
495 MemoryStream m = new MemoryStream(b);
497 StreamReader r = new StreamReader(m);
500 char[] buffer = new Char[7];
502 char[] target = {'g','d','e','f','b','c','a'};
504 r.Read(buffer, 6, 1);
506 r.Read(buffer, 4, 2);
508 r.Read(buffer, 1, 3);
510 r.Read(buffer, 0, 1);
512 for (int i = 0; i < target.Length; i++) {
513 Assert.AreEqual (target[i], buffer[i], "read no work");
516 } catch (Exception e) {
517 Assert.Fail ("Caught when ii=" + ii + ". e:" + e.ToString());
523 public void TestReadLine() {
524 // TODO Out Of Memory Exc? IO Exc?
525 Byte[] b = new Byte[8];
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");
544 public void ReadLine1() {
545 Byte[] b = new Byte[10];
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");
568 public void ReadLine2() {
569 Byte[] b = new Byte[10];
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");
593 public void ReadLine3() {
594 StringBuilder sb = new StringBuilder ();
595 sb.Append (new string ('1', 32767));
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");
608 public void ReadLine4() {
609 StringBuilder sb = new StringBuilder ();
610 sb.Append (new string ('1', 32767));
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");
626 public void ReadLine5() {
627 StringBuilder sb = new StringBuilder ();
628 sb.Append (new string ('1', 32768));
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");
640 public void TestReadToEnd() {
641 // TODO Out Of Memory Exc? IO Exc?
642 Byte[] b = new Byte[8];
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");
658 public void TestBaseStreamClosed ()
661 MemoryStream m = new MemoryStream (b);
662 StreamReader r = new StreamReader (m);
667 } catch (ObjectDisposedException) {
672 [ExpectedException (typeof (ArgumentNullException))]
673 public void Contructor_Stream_NullEncoding ()
675 new StreamReader (new MemoryStream (), null);
679 [ExpectedException (typeof (ArgumentNullException))]
680 public void Contructor_Path_NullEncoding ()
682 new StreamReader (_codeFileName, null);
686 [ExpectedException (typeof (ArgumentNullException))]
687 public void Read_Null ()
689 StreamReader r = new StreamReader (new MemoryStream ());
694 [ExpectedException (typeof (ArgumentException))]
695 public void Read_IndexOverflow ()
697 char[] array = new char [16];
698 StreamReader r = new StreamReader (new MemoryStream (16));
699 r.Read (array, 1, Int32.MaxValue);
703 [ExpectedException (typeof (ArgumentException))]
704 public void Read_CountOverflow ()
706 char[] array = new char [16];
707 StreamReader r = new StreamReader (new MemoryStream (16));
708 r.Read (array, Int32.MaxValue, 1);
712 public void Read_DoesntStopAtLineEndings ()
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));
720 public void bug75526 ()
722 StreamReader sr = new StreamReader (new Bug75526Stream ());
723 int len = sr.Read (new char [10], 0, 10);
724 Assert.AreEqual (2, len);
727 class Bug75526Stream : MemoryStream
729 public override int Read (byte [] buffer, int offset, int count)
731 buffer [offset + 0] = (byte) 'a';
732 buffer [offset + 1] = (byte) 'b';