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)
14 namespace MonoTests.System.IO
18 public class StreamReaderTest : Assertion
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 StreamReader r = new StreamReader((Stream)null);
47 } catch (ArgumentNullException) {
50 Assert("null string error not thrown", errorThrown);
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("no read error not thrown", errorThrown);
65 // this is probably incestuous, but, oh well.
66 FileStream f = new FileStream(_codeFileName,
69 StreamReader r = new StreamReader(f);
70 AssertNotNull("no stream reader", r);
77 public void TestCtor2() {
79 bool errorThrown = false;
81 StreamReader r = new StreamReader("");
82 } catch (ArgumentException) {
84 } catch (Exception e) {
85 Fail ("Incorrect exception thrown at 1: " + e.ToString());
87 Assert("empty string error not thrown", errorThrown);
90 bool errorThrown = false;
92 StreamReader r = new StreamReader((string)null);
93 } catch (ArgumentNullException) {
95 } catch (Exception e) {
96 Fail ("Incorrect exception thrown at 2: " + e.ToString());
98 Assert("null string error not thrown", errorThrown);
101 bool errorThrown = false;
103 StreamReader r = new StreamReader("nonexistentfile");
104 } catch (FileNotFoundException) {
106 } catch (Exception e) {
107 Fail ("Incorrect exception thrown at 3: " + e.ToString());
109 Assert("fileNotFound error not thrown", errorThrown);
112 bool errorThrown = false;
114 StreamReader r = new StreamReader("nonexistentdir/file");
115 } catch (DirectoryNotFoundException) {
117 } catch (Exception e) {
118 Fail ("Incorrect exception thrown at 4: " + e.ToString());
120 Assert("dirNotFound error not thrown", errorThrown);
123 bool errorThrown = false;
125 StreamReader r = 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 Fail ("Incorrect exception thrown at 5: " + e.ToString());
135 Assert("invalid filename error not thrown", errorThrown);
138 // this is probably incestuous, but, oh well.
139 StreamReader r = new StreamReader(_codeFileName);
140 AssertNotNull("no stream reader", r);
146 public void TestCtor3() {
148 bool errorThrown = false;
150 StreamReader r = new StreamReader((Stream)null, false);
151 } catch (ArgumentNullException) {
153 } catch (Exception e) {
154 Fail ("Incorrect exception thrown at 1: " + e.ToString());
156 Assert("null stream error not thrown", errorThrown);
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 Fail ("Incorrect exception thrown at 2: " + e.ToString());
170 Assert("no read error not thrown", errorThrown);
173 // this is probably incestuous, but, oh well.
174 FileStream f = new FileStream(_codeFileName,
177 StreamReader r = new StreamReader(f, false);
178 AssertNotNull("no stream reader", r);
183 bool errorThrown = false;
185 StreamReader r = new StreamReader((Stream)null, true);
186 } catch (ArgumentNullException) {
188 } catch (Exception e) {
189 Fail ("Incorrect exception thrown at 3: " + e.ToString());
191 Assert("null string error not thrown", errorThrown);
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 Fail ("Incorrect exception thrown at 4: " + e.ToString());
205 Assert("no read error not thrown", errorThrown);
208 // this is probably incestuous, but, oh well.
209 FileStream f = new FileStream(_codeFileName,
212 StreamReader r = new StreamReader(f, true);
213 AssertNotNull("no stream reader", r);
220 public void TestCtor4() {
222 bool errorThrown = false;
224 StreamReader r = new StreamReader("", false);
225 } catch (ArgumentException) {
227 } catch (Exception e) {
228 Fail ("Incorrect exception thrown at 1: " + e.ToString());
230 Assert("empty string error not thrown", errorThrown);
233 bool errorThrown = false;
235 StreamReader r = new StreamReader((string)null, false);
236 } catch (ArgumentNullException) {
238 } catch (Exception e) {
239 Fail ("Incorrect exception thrown at 2: " + e.ToString());
241 Assert("null string error not thrown", errorThrown);
244 bool errorThrown = false;
246 StreamReader r = new StreamReader(TempFolder + "/nonexistentfile", false);
247 } catch (FileNotFoundException) {
249 } catch (Exception e) {
250 Fail ("Incorrect exception thrown at 3: " + e.ToString());
252 Assert("fileNotFound error not thrown", errorThrown);
255 bool errorThrown = false;
257 StreamReader r = new StreamReader(TempFolder + "/nonexistentdir/file", false);
258 } catch (DirectoryNotFoundException) {
260 } catch (Exception e) {
261 Fail ("Incorrect exception thrown at 4: " + e.ToString());
263 Assert("dirNotFound error not thrown", errorThrown);
266 bool errorThrown = false;
268 StreamReader r = 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 Fail ("Incorrect exception thrown at 5: " + e.ToString());
278 Assert("invalid filename error not thrown", errorThrown);
281 // this is probably incestuous, but, oh well.
282 StreamReader r = new StreamReader(_codeFileName, false);
283 AssertNotNull("no stream reader", r);
287 bool errorThrown = false;
289 StreamReader r = new StreamReader("", true);
290 } catch (ArgumentException) {
292 } catch (Exception e) {
293 Fail ("Incorrect exception thrown at 6: " + e.ToString());
295 Assert("empty string error not thrown", errorThrown);
298 bool errorThrown = false;
300 StreamReader r = new StreamReader((string)null, true);
301 } catch (ArgumentNullException) {
303 } catch (Exception e) {
304 Fail ("Incorrect exception thrown at 7: " + e.ToString());
306 Assert("null string error not thrown", errorThrown);
309 bool errorThrown = false;
311 StreamReader r = new StreamReader(TempFolder + "/nonexistentfile", true);
312 } catch (FileNotFoundException) {
314 } catch (Exception e) {
315 Fail ("Incorrect exception thrown at 8: " + e.ToString());
317 Assert("fileNotFound error not thrown", errorThrown);
320 bool errorThrown = false;
322 StreamReader r = new StreamReader(TempFolder + "/nonexistentdir/file", true);
323 } catch (DirectoryNotFoundException) {
325 } catch (Exception e) {
326 Fail ("Incorrect exception thrown at 9: " + e.ToString());
328 Assert("dirNotFound error not thrown", errorThrown);
331 bool errorThrown = false;
333 StreamReader r = 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 Fail ("Incorrect exception thrown at 10: " + e.ToString());
343 Assert("invalid filename error not thrown", errorThrown);
346 // this is probably incestuous, but, oh well.
347 StreamReader r = new StreamReader(_codeFileName, true);
348 AssertNotNull("no stream reader", r);
353 // TODO - Ctor with Encoding
356 public void TestBaseStream() {
357 string progress = "beginning";
360 MemoryStream m = new MemoryStream(b);
361 StreamReader r = new StreamReader(m);
362 AssertEquals("wrong base stream ", m, r.BaseStream);
363 progress = "Closing StreamReader";
365 progress = "Closing MemoryStream";
367 } catch (Exception e) {
368 Fail ("At '" + progress + "' an unexpected exception was thrown: " + e.ToString());
372 public void TestCurrentEncoding() {
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());
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.
388 public void TestPeek() {
389 // FIXME - how to get an IO Exception?
391 bool errorThrown = false;
394 MemoryStream m = new MemoryStream(b);
395 StreamReader r = new StreamReader(m);
397 int nothing = r.Peek();
398 } catch (ObjectDisposedException) {
401 Assert("nothing-to-peek-at error not thrown", errorThrown);
404 Byte[] b = {1, 2, 3, 4, 5, 6};
405 MemoryStream m = new MemoryStream(b);
407 StreamReader r = new StreamReader(m);
408 for (int i = 1; i <= 6; i++) {
409 AssertEquals("peek incorrect", i, r.Peek());
412 AssertEquals("should be none left", -1, r.Peek());
417 public void TestRead() {
418 // FIXME - how to get an IO Exception?
420 bool errorThrown = false;
423 MemoryStream m = new MemoryStream(b);
424 StreamReader r = new StreamReader(m);
426 int nothing = r.Read();
427 } catch (ObjectDisposedException) {
429 } catch (Exception e) {
430 Fail ("Incorrect exception thrown at 1: " + e.ToString());
432 Assert("nothing-to-read error not thrown", errorThrown);
435 Byte[] b = {1, 2, 3, 4, 5, 6};
436 MemoryStream m = new MemoryStream(b);
438 StreamReader r = new StreamReader(m);
439 for (int i = 1; i <= 6; i++) {
440 AssertEquals("read incorrect", i, r.Read());
442 AssertEquals("Should be none left", -1, r.Read());
446 bool errorThrown = false;
449 StreamReader r = new StreamReader(new MemoryStream(b));
451 } catch (ArgumentNullException) {
453 } catch (ArgumentException) {
455 } catch (Exception e) {
456 Fail ("Incorrect exception thrown at 2: " + e.ToString());
458 Assert("null buffer error not thrown", errorThrown);
461 bool errorThrown = false;
464 StreamReader r = new StreamReader(new MemoryStream(b));
465 Char[] c = new Char[1];
467 } catch (ArgumentException) {
469 } catch (Exception e) {
470 Fail ("Incorrect exception thrown at 3: " + e.ToString());
472 Assert("too-long range error not thrown", errorThrown);
475 bool errorThrown = false;
478 StreamReader r = new StreamReader(new MemoryStream(b));
479 Char[] c = new Char[1];
481 } catch (ArgumentOutOfRangeException) {
483 } catch (Exception e) {
484 Fail ("Incorrect exception thrown at 4: " + e.ToString());
486 Assert("out of range error not thrown", errorThrown);
489 bool errorThrown = false;
492 StreamReader r = new StreamReader(new MemoryStream(b));
493 Char[] c = new Char[1];
495 } catch (ArgumentOutOfRangeException) {
497 } catch (Exception e) {
498 Fail ("Incorrect exception thrown at 5: " + e.ToString());
500 Assert("out of range error not thrown", errorThrown);
505 Byte[] b = {(byte)'a', (byte)'b', (byte)'c',
506 (byte)'d', (byte)'e', (byte)'f',
508 MemoryStream m = new MemoryStream(b);
510 StreamReader r = new StreamReader(m);
513 char[] buffer = new Char[7];
515 char[] target = {'g','d','e','f','b','c','a'};
517 r.Read(buffer, 6, 1);
519 r.Read(buffer, 4, 2);
521 r.Read(buffer, 1, 3);
523 r.Read(buffer, 0, 1);
525 for (int i = 0; i < target.Length; i++) {
526 AssertEquals("read no work",
527 target[i], buffer[i]);
531 } catch (Exception e) {
532 Fail ("Caught when ii=" + ii + ". e:" + e.ToString());
538 public void TestReadLine() {
539 // TODO Out Of Memory Exc? IO Exc?
540 Byte[] b = new Byte[8];
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());
559 public void ReadLine1() {
560 Byte[] b = new Byte[10];
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());
583 public void ReadLine2() {
584 Byte[] b = new Byte[10];
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());
608 public void ReadLine3() {
609 StringBuilder sb = new StringBuilder ();
610 sb.Append (new string ('1', 32767));
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());
623 public void ReadLine4() {
624 StringBuilder sb = new StringBuilder ();
625 sb.Append (new string ('1', 32767));
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());
641 public void ReadLine5() {
642 StringBuilder sb = new StringBuilder ();
643 sb.Append (new string ('1', 32768));
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());
655 public void TestReadToEnd() {
656 // TODO Out Of Memory Exc? IO Exc?
657 Byte[] b = new Byte[8];
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());
673 public void TestBaseStreamClosed ()
676 MemoryStream m = new MemoryStream (b);
677 StreamReader r = new StreamReader (m);
682 } catch (ObjectDisposedException) {
686 AssertEquals ("#01", true, thrown);
690 [ExpectedException (typeof (ArgumentNullException))]
691 public void Contructor_Stream_NullEncoding ()
693 StreamReader r = new StreamReader (new MemoryStream (), null);
697 [ExpectedException (typeof (ArgumentNullException))]
698 public void Contructor_Path_NullEncoding ()
700 StreamReader r = new StreamReader (_codeFileName, null);
704 [ExpectedException (typeof (ArgumentNullException))]
705 public void Read_Null ()
707 StreamReader r = new StreamReader (new MemoryStream ());
712 [ExpectedException (typeof (ArgumentException))]
713 public void Read_IndexOverflow ()
715 char[] array = new char [16];
716 StreamReader r = new StreamReader (new MemoryStream (16));
717 r.Read (array, 1, Int32.MaxValue);
721 [ExpectedException (typeof (ArgumentException))]
722 public void Read_CountOverflow ()
724 char[] array = new char [16];
725 StreamReader r = new StreamReader (new MemoryStream (16));
726 r.Read (array, Int32.MaxValue, 1);
730 public void Read_DoesntStopAtLineEndings ()
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));