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";
22 private const string TestString = "Hello World!";
27 if (!Directory.Exists (TempFolder))
28 Directory.CreateDirectory (TempFolder);
30 if (!File.Exists (_codeFileName))
31 File.Create (_codeFileName).Close ();
35 public void TearDown ()
37 if (Directory.Exists (TempFolder))
38 Directory.Delete (TempFolder, true);
43 public void TestCtor1() {
45 bool errorThrown = false;
47 new StreamReader((Stream)null);
48 } catch (ArgumentNullException) {
51 Assert.IsTrue (errorThrown, "null string error not thrown");
54 bool errorThrown = false;
55 FileStream f = new FileStream(_codeFileName, FileMode.Open, FileAccess.Write);
57 StreamReader r = new StreamReader(f);
59 } catch (ArgumentException) {
63 Assert.IsTrue (errorThrown, "no read error not thrown");
66 // this is probably incestuous, but, oh well.
67 FileStream f = new FileStream(_codeFileName,
70 StreamReader r = new StreamReader(f);
71 Assert.IsNotNull (r, "no stream reader");
78 public void TestCtor2() {
80 bool errorThrown = false;
83 } catch (ArgumentException) {
85 } catch (Exception e) {
86 Assert.Fail ("Incorrect exception thrown at 1: " + e.ToString());
88 Assert.IsTrue (errorThrown, "empty string error not thrown");
91 bool errorThrown = false;
93 new StreamReader((string)null);
94 } catch (ArgumentNullException) {
96 } catch (Exception e) {
97 Assert.Fail ("Incorrect exception thrown at 2: " + e.ToString());
99 Assert.IsTrue (errorThrown, "null string error not thrown");
102 bool errorThrown = false;
104 new StreamReader("nonexistentfile");
105 } catch (FileNotFoundException) {
107 } catch (Exception e) {
108 Assert.Fail ("Incorrect exception thrown at 3: " + e.ToString());
110 Assert.IsTrue (errorThrown, "fileNotFound error not thrown");
113 bool errorThrown = false;
115 new StreamReader("nonexistentdir/file");
116 } catch (DirectoryNotFoundException) {
118 } catch (Exception e) {
119 Assert.Fail ("Incorrect exception thrown at 4: " + e.ToString());
121 Assert.IsTrue (errorThrown, "dirNotFound error not thrown");
124 bool errorThrown = false;
126 new StreamReader("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0]);
127 } catch (IOException) {
129 } catch (ArgumentException) {
130 // FIXME - the spec says 'IOExc', but the
131 // compiler says 'ArgExc'...
133 } catch (Exception e) {
134 Assert.Fail ("Incorrect exception thrown at 5: " + e.ToString());
136 Assert.IsTrue (errorThrown, "invalid filename error not thrown");
139 // this is probably incestuous, but, oh well.
140 StreamReader r = new StreamReader(_codeFileName);
141 Assert.IsNotNull (r, "no stream reader");
147 public void TestCtor3() {
149 bool errorThrown = false;
151 new StreamReader((Stream)null, false);
152 } catch (ArgumentNullException) {
154 } catch (Exception e) {
155 Assert.Fail ("Incorrect exception thrown at 1: " + e.ToString());
157 Assert.IsTrue (errorThrown, "null stream error not thrown");
160 bool errorThrown = false;
161 FileStream f = new FileStream(_codeFileName, FileMode.Open, FileAccess.Write);
163 StreamReader r = new StreamReader(f, false);
165 } catch (ArgumentException) {
167 } catch (Exception e) {
168 Assert.Fail ("Incorrect exception thrown at 2: " + e.ToString());
171 Assert.IsTrue (errorThrown, "no read error not thrown");
174 // this is probably incestuous, but, oh well.
175 FileStream f = new FileStream(_codeFileName,
178 StreamReader r = new StreamReader(f, false);
179 Assert.IsNotNull (r, "no stream reader");
184 bool errorThrown = false;
186 StreamReader r = new StreamReader((Stream)null, true);
187 } catch (ArgumentNullException) {
189 } catch (Exception e) {
190 Assert.Fail ("Incorrect exception thrown at 3: " + e.ToString());
192 Assert.IsTrue (errorThrown, "null string error not thrown");
195 bool errorThrown = false;
196 FileStream f = new FileStream(_codeFileName, FileMode.Open, FileAccess.Write);
198 StreamReader r = new StreamReader(f, true);
200 } catch (ArgumentException) {
202 } catch (Exception e) {
203 Assert.Fail ("Incorrect exception thrown at 4: " + e.ToString());
206 Assert.IsTrue (errorThrown, "no read error not thrown");
209 // this is probably incestuous, but, oh well.
210 FileStream f = new FileStream(_codeFileName,
213 StreamReader r = new StreamReader(f, true);
214 Assert.IsNotNull (r, "no stream reader");
221 public void TestCtor4() {
223 bool errorThrown = false;
225 new StreamReader("", false);
226 } catch (ArgumentException) {
228 } catch (Exception e) {
229 Assert.Fail ("Incorrect exception thrown at 1: " + e.ToString());
231 Assert.IsTrue (errorThrown, "empty string error not thrown");
234 bool errorThrown = false;
236 new StreamReader((string)null, false);
237 } catch (ArgumentNullException) {
239 } catch (Exception e) {
240 Assert.Fail ("Incorrect exception thrown at 2: " + e.ToString());
242 Assert.IsTrue (errorThrown, "null string error not thrown");
245 bool errorThrown = false;
247 new StreamReader(TempFolder + "/nonexistentfile", false);
248 } catch (FileNotFoundException) {
250 } catch (Exception e) {
251 Assert.Fail ("Incorrect exception thrown at 3: " + e.ToString());
253 Assert.IsTrue (errorThrown, "fileNotFound error not thrown");
256 bool errorThrown = false;
258 new StreamReader(TempFolder + "/nonexistentdir/file", false);
259 } catch (DirectoryNotFoundException) {
261 } catch (Exception e) {
262 Assert.Fail ("Incorrect exception thrown at 4: " + e.ToString());
264 Assert.IsTrue (errorThrown, "dirNotFound error not thrown");
267 bool errorThrown = false;
269 new StreamReader("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0], false);
270 } catch (IOException) {
272 } catch (ArgumentException) {
273 // FIXME - the spec says 'IOExc', but the
274 // compiler says 'ArgExc'...
276 } catch (Exception e) {
277 Assert.Fail ("Incorrect exception thrown at 5: " + e.ToString());
279 Assert.IsTrue (errorThrown, "invalid filename error not thrown");
282 // this is probably incestuous, but, oh well.
283 StreamReader r = new StreamReader(_codeFileName, false);
284 Assert.IsNotNull (r, "no stream reader");
288 bool errorThrown = false;
290 new StreamReader("", true);
291 } catch (ArgumentException) {
293 } catch (Exception e) {
294 Assert.Fail ("Incorrect exception thrown at 6: " + e.ToString());
296 Assert.IsTrue (errorThrown, "empty string error not thrown");
299 bool errorThrown = false;
301 new StreamReader((string)null, true);
302 } catch (ArgumentNullException) {
304 } catch (Exception e) {
305 Assert.Fail ("Incorrect exception thrown at 7: " + e.ToString());
307 Assert.IsTrue (errorThrown, "null string error not thrown");
310 bool errorThrown = false;
312 new StreamReader(TempFolder + "/nonexistentfile", true);
313 } catch (FileNotFoundException) {
315 } catch (Exception e) {
316 Assert.Fail ("Incorrect exception thrown at 8: " + e.ToString());
318 Assert.IsTrue (errorThrown, "fileNotFound error not thrown");
321 bool errorThrown = false;
323 new StreamReader(TempFolder + "/nonexistentdir/file", true);
324 } catch (DirectoryNotFoundException) {
326 } catch (Exception e) {
327 Assert.Fail ("Incorrect exception thrown at 9: " + e.ToString());
329 Assert.IsTrue (errorThrown, "dirNotFound error not thrown");
332 bool errorThrown = false;
334 new StreamReader("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0], true);
335 } catch (IOException) {
337 } catch (ArgumentException) {
338 // FIXME - the spec says 'IOExc', but the
339 // compiler says 'ArgExc'...
341 } catch (Exception e) {
342 Assert.Fail ("Incorrect exception thrown at 10: " + e.ToString());
344 Assert.IsTrue (errorThrown, "invalid filename error not thrown");
347 // this is probably incestuous, but, oh well.
348 StreamReader r = new StreamReader(_codeFileName, true);
349 Assert.IsNotNull (r, "no stream reader");
354 // TODO - Ctor with Encoding
357 public void TestBaseStream() {
359 MemoryStream m = new MemoryStream(b);
360 StreamReader r = new StreamReader(m);
361 Assert.AreSame (m, r.BaseStream, "wrong base stream ");
366 public void TestCurrentEncoding() {
368 MemoryStream m = new MemoryStream(b);
369 StreamReader r = new StreamReader(m);
370 Assert.AreEqual (Encoding.UTF8.GetType (), r.CurrentEncoding.GetType (),
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.
378 public void TestPeek() {
379 // FIXME - how to get an IO Exception?
386 m = new MemoryStream (b);
387 r = new StreamReader(m);
389 int nothing = r.Peek();
391 } catch (ObjectDisposedException) {
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");
401 Assert.AreEqual (-1, r.Peek(), "#3");
405 public void TestRead() {
406 // FIXME - how to get an IO Exception?
408 bool errorThrown = false;
411 MemoryStream m = new MemoryStream(b);
412 StreamReader r = new StreamReader(m);
414 int nothing = r.Read();
415 } catch (ObjectDisposedException) {
417 } catch (Exception e) {
418 Assert.Fail ("Incorrect exception thrown at 1: " + e.ToString());
420 Assert.IsTrue (errorThrown, "nothing-to-read error not thrown");
423 Byte[] b = {1, 2, 3, 4, 5, 6};
424 MemoryStream m = new MemoryStream(b);
426 StreamReader r = new StreamReader(m);
427 for (int i = 1; i <= 6; i++) {
428 Assert.AreEqual (i, r.Read (), "read incorrect");
430 Assert.AreEqual (-1, r.Read (), "Should be none left");
434 bool errorThrown = false;
437 StreamReader r = new StreamReader(new MemoryStream(b));
439 } catch (ArgumentNullException) {
441 } catch (ArgumentException) {
443 } catch (Exception e) {
444 Assert.Fail ("Incorrect exception thrown at 2: " + e.ToString());
446 Assert.IsTrue (errorThrown, "null buffer error not thrown");
449 bool errorThrown = false;
452 StreamReader r = new StreamReader(new MemoryStream(b));
453 Char[] c = new Char[1];
455 } catch (ArgumentException) {
457 } catch (Exception e) {
458 Assert.Fail ("Incorrect exception thrown at 3: " + e.ToString());
460 Assert.IsTrue (errorThrown, "too-long range error not thrown");
463 bool errorThrown = false;
466 StreamReader r = new StreamReader(new MemoryStream(b));
467 Char[] c = new Char[1];
469 } catch (ArgumentOutOfRangeException) {
471 } catch (Exception e) {
472 Assert.Fail ("Incorrect exception thrown at 4: " + e.ToString());
474 Assert.IsTrue (errorThrown, "out of range error not thrown");
477 bool errorThrown = false;
480 StreamReader r = new StreamReader(new MemoryStream(b));
481 Char[] c = new Char[1];
483 } catch (ArgumentOutOfRangeException) {
485 } catch (Exception e) {
486 Assert.Fail ("Incorrect exception thrown at 5: " + e.ToString());
488 Assert.IsTrue (errorThrown, "out of range error not thrown");
493 Byte[] b = {(byte)'a', (byte)'b', (byte)'c',
494 (byte)'d', (byte)'e', (byte)'f',
496 MemoryStream m = new MemoryStream(b);
498 StreamReader r = new StreamReader(m);
501 char[] buffer = new Char[7];
503 char[] target = {'g','d','e','f','b','c','a'};
505 r.Read(buffer, 6, 1);
507 r.Read(buffer, 4, 2);
509 r.Read(buffer, 1, 3);
511 r.Read(buffer, 0, 1);
513 for (int i = 0; i < target.Length; i++) {
514 Assert.AreEqual (target[i], buffer[i], "read no work");
517 } catch (Exception e) {
518 Assert.Fail ("Caught when ii=" + ii + ". e:" + e.ToString());
524 public void TestReadLine() {
525 // TODO Out Of Memory Exc? IO Exc?
526 Byte[] b = new Byte[8];
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");
545 public void ReadLine1() {
546 Byte[] b = new Byte[10];
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");
569 public void ReadLine2() {
570 Byte[] b = new Byte[10];
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");
594 public void ReadLine3() {
595 StringBuilder sb = new StringBuilder ();
596 sb.Append (new string ('1', 32767));
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");
609 public void ReadLine4() {
610 StringBuilder sb = new StringBuilder ();
611 sb.Append (new string ('1', 32767));
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");
627 public void ReadLine5() {
628 StringBuilder sb = new StringBuilder ();
629 sb.Append (new string ('1', 32768));
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");
641 public void TestReadToEnd() {
642 // TODO Out Of Memory Exc? IO Exc?
643 Byte[] b = new Byte[8];
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");
659 public void TestBaseStreamClosed ()
662 MemoryStream m = new MemoryStream (b);
663 StreamReader r = new StreamReader (m);
668 } catch (ObjectDisposedException) {
673 [ExpectedException (typeof (ArgumentNullException))]
674 public void Contructor_Stream_NullEncoding ()
676 new StreamReader (new MemoryStream (), null);
680 [ExpectedException (typeof (ArgumentNullException))]
681 public void Contructor_Path_NullEncoding ()
683 new StreamReader (_codeFileName, null);
687 [ExpectedException (typeof (ArgumentNullException))]
688 public void Read_Null ()
690 StreamReader r = new StreamReader (new MemoryStream ());
695 [ExpectedException (typeof (ArgumentException))]
696 public void Read_IndexOverflow ()
698 char[] array = new char [16];
699 StreamReader r = new StreamReader (new MemoryStream (16));
700 r.Read (array, 1, Int32.MaxValue);
704 [ExpectedException (typeof (ArgumentException))]
705 public void Read_CountOverflow ()
707 char[] array = new char [16];
708 StreamReader r = new StreamReader (new MemoryStream (16));
709 r.Read (array, Int32.MaxValue, 1);
713 public void Read_DoesntStopAtLineEndings ()
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));
721 public void EncodingDetection()
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");
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");
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.
741 public void EncodingDetectionUnicode ()
743 byte [] bytes = new byte [3];
747 MemoryStream inStream = new MemoryStream (bytes);
748 StreamReader reader = new StreamReader (inStream, Encoding.UTF8, true);
750 // It should start with the encoding we used in the .ctor
751 Assert.AreEqual (Encoding.UTF8, reader.CurrentEncoding, "#A1");
755 Assert.AreEqual (Encoding.Unicode, reader.CurrentEncoding, "#B1");
760 private bool CheckEncodingDetected(Encoding encoding)
762 MemoryStream outStream = new MemoryStream();
763 using (StreamWriter outWriter = new StreamWriter(outStream, encoding))
765 outWriter.Write(TestString);
767 byte[] testBytes = outStream.ToArray();
769 StreamReader inReader = new StreamReader(new MemoryStream(testBytes, false));
770 string decodedString = inReader.ReadToEnd();
772 return decodedString == TestString;
776 public void EndOfBufferIsCR ()
778 using (StreamReader reader = new StreamReader ("Test/resources/Fergie.GED")) {
781 while ((line = reader.ReadLine ()) != null) {
782 Assert.IsFalse (line.Length > 1000, "#1 " + count);
785 Assert.AreEqual (16107, count, "#2");
790 public void bug75526 ()
792 StreamReader sr = new StreamReader (new Bug75526Stream ());
793 int len = sr.Read (new char [10], 0, 10);
794 Assert.AreEqual (2, len);
797 class Bug75526Stream : MemoryStream
799 public override int Read (byte [] buffer, int offset, int count)
801 buffer [offset + 0] = (byte) 'a';
802 buffer [offset + 1] = (byte) 'b';
808 public void PeekWhileBlocking ()
810 StreamReader reader = new StreamReader (new MyStream ());
811 int c = reader.Read ();
813 Assert.IsFalse (reader.EndOfStream);
815 string str = reader.ReadToEnd ();
816 Assert.AreEqual ("bc", str);
820 public void EncodingChangedAuto ()
822 int testlines = 2048; // all data should larger than stream reader default buffer size
823 string testdata = "test";
824 MemoryStream ms = new MemoryStream();
825 // write utf8 encoding data.
826 using (StreamWriter sw = new StreamWriter (ms, Encoding.UTF8)) {
827 for (int i = 0; i < testlines; i++)
828 sw.WriteLine (testdata);
831 MemoryStream readms = new MemoryStream (ms.GetBuffer());
832 using (StreamReader sr = new StreamReader(readms, Encoding.Unicode, true)) {
833 for (int i = 0; i < testlines; i++) {
834 string line = sr.ReadLine ();
835 if (line != testdata)
836 Assert.Fail ("Wrong line content");
842 class MyStream : Stream {
845 public override int Read (byte [] buffer, int offset, int size)
848 buffer [offset] = (byte) 'a';
852 buffer [offset] = (byte) 'b';
853 buffer [offset + 1] = (byte) 'c';
860 public override bool CanRead {
864 public override bool CanSeek {
865 get { return false; }
868 public override bool CanWrite {
869 get { return false; }
872 public override long Length {
876 public override long Position {
881 public override void Flush ()
885 public override long Seek (long offset, SeekOrigin origin)
890 public override void SetLength (long value)
894 public override void Write (byte[] buffer, int offset, int count)