1 // StreamReaderTest.cs - NUnit Test Cases for the SystemIO.StreamReader class
\r
3 // David Brandt (bucky@keystreams.com)
\r
5 // (C) Ximian, Inc. http://www.ximian.com
\r
6 // Copyright (C) 2004 Novell (http://www.novell.com)
\r
9 using NUnit.Framework;
\r
14 namespace MonoTests.System.IO
\r
18 public class StreamReaderTest : Assertion
\r
20 static string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
\r
21 private string _codeFileName = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
\r
24 public void SetUp ()
\r
26 if (!Directory.Exists (TempFolder))
\r
27 Directory.CreateDirectory (TempFolder);
\r
29 if (!File.Exists (_codeFileName))
\r
30 File.Create (_codeFileName).Close ();
\r
34 public void TearDown ()
\r
36 if (Directory.Exists (TempFolder))
\r
37 Directory.Delete (TempFolder, true);
\r
42 public void TestCtor1() {
\r
44 bool errorThrown = false;
\r
46 StreamReader r = new StreamReader((Stream)null);
\r
47 } catch (ArgumentNullException) {
\r
50 Assert("null string error not thrown", errorThrown);
\r
53 bool errorThrown = false;
\r
54 FileStream f = new FileStream(_codeFileName, FileMode.Open, FileAccess.Write);
\r
56 StreamReader r = new StreamReader(f);
\r
58 } catch (ArgumentException) {
\r
62 Assert("no read error not thrown", errorThrown);
\r
65 // this is probably incestuous, but, oh well.
\r
66 FileStream f = new FileStream(_codeFileName,
\r
69 StreamReader r = new StreamReader(f);
\r
70 AssertNotNull("no stream reader", r);
\r
77 public void TestCtor2() {
\r
79 bool errorThrown = false;
\r
81 StreamReader r = new StreamReader("");
\r
82 } catch (ArgumentException) {
\r
84 } catch (Exception e) {
\r
85 Fail ("Incorrect exception thrown at 1: " + e.ToString());
\r
87 Assert("empty string error not thrown", errorThrown);
\r
90 bool errorThrown = false;
\r
92 StreamReader r = new StreamReader((string)null);
\r
93 } catch (ArgumentNullException) {
\r
95 } catch (Exception e) {
\r
96 Fail ("Incorrect exception thrown at 2: " + e.ToString());
\r
98 Assert("null string error not thrown", errorThrown);
\r
101 bool errorThrown = false;
\r
103 StreamReader r = new StreamReader("nonexistentfile");
\r
104 } catch (FileNotFoundException) {
\r
105 errorThrown = true;
\r
106 } catch (Exception e) {
\r
107 Fail ("Incorrect exception thrown at 3: " + e.ToString());
\r
109 Assert("fileNotFound error not thrown", errorThrown);
\r
112 bool errorThrown = false;
\r
114 StreamReader r = new StreamReader("nonexistentdir/file");
\r
115 } catch (DirectoryNotFoundException) {
\r
116 errorThrown = true;
\r
117 } catch (Exception e) {
\r
118 Fail ("Incorrect exception thrown at 4: " + e.ToString());
\r
120 Assert("dirNotFound error not thrown", errorThrown);
\r
123 bool errorThrown = false;
\r
125 StreamReader r = new StreamReader("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0]);
\r
126 } catch (IOException) {
\r
127 errorThrown = true;
\r
128 } catch (ArgumentException) {
\r
129 // FIXME - the spec says 'IOExc', but the
\r
130 // compiler says 'ArgExc'...
\r
131 errorThrown = true;
\r
132 } catch (Exception e) {
\r
133 Fail ("Incorrect exception thrown at 5: " + e.ToString());
\r
135 Assert("invalid filename error not thrown", errorThrown);
\r
138 // this is probably incestuous, but, oh well.
\r
139 StreamReader r = new StreamReader(_codeFileName);
\r
140 AssertNotNull("no stream reader", r);
\r
146 public void TestCtor3() {
\r
148 bool errorThrown = false;
\r
150 StreamReader r = new StreamReader((Stream)null, false);
\r
151 } catch (ArgumentNullException) {
\r
152 errorThrown = true;
\r
153 } catch (Exception e) {
\r
154 Fail ("Incorrect exception thrown at 1: " + e.ToString());
\r
156 Assert("null stream error not thrown", errorThrown);
\r
159 bool errorThrown = false;
\r
160 FileStream f = new FileStream(_codeFileName, FileMode.Open, FileAccess.Write);
\r
162 StreamReader r = new StreamReader(f, false);
\r
164 } catch (ArgumentException) {
\r
165 errorThrown = true;
\r
166 } catch (Exception e) {
\r
167 Fail ("Incorrect exception thrown at 2: " + e.ToString());
\r
170 Assert("no read error not thrown", errorThrown);
\r
173 // this is probably incestuous, but, oh well.
\r
174 FileStream f = new FileStream(_codeFileName,
\r
177 StreamReader r = new StreamReader(f, false);
\r
178 AssertNotNull("no stream reader", r);
\r
183 bool errorThrown = false;
\r
185 StreamReader r = new StreamReader((Stream)null, true);
\r
186 } catch (ArgumentNullException) {
\r
187 errorThrown = true;
\r
188 } catch (Exception e) {
\r
189 Fail ("Incorrect exception thrown at 3: " + e.ToString());
\r
191 Assert("null string error not thrown", errorThrown);
\r
194 bool errorThrown = false;
\r
195 FileStream f = new FileStream(_codeFileName, FileMode.Open, FileAccess.Write);
\r
197 StreamReader r = new StreamReader(f, true);
\r
199 } catch (ArgumentException) {
\r
200 errorThrown = true;
\r
201 } catch (Exception e) {
\r
202 Fail ("Incorrect exception thrown at 4: " + e.ToString());
\r
205 Assert("no read error not thrown", errorThrown);
\r
208 // this is probably incestuous, but, oh well.
\r
209 FileStream f = new FileStream(_codeFileName,
\r
212 StreamReader r = new StreamReader(f, true);
\r
213 AssertNotNull("no stream reader", r);
\r
220 public void TestCtor4() {
\r
222 bool errorThrown = false;
\r
224 StreamReader r = new StreamReader("", false);
\r
225 } catch (ArgumentException) {
\r
226 errorThrown = true;
\r
227 } catch (Exception e) {
\r
228 Fail ("Incorrect exception thrown at 1: " + e.ToString());
\r
230 Assert("empty string error not thrown", errorThrown);
\r
233 bool errorThrown = false;
\r
235 StreamReader r = new StreamReader((string)null, false);
\r
236 } catch (ArgumentNullException) {
\r
237 errorThrown = true;
\r
238 } catch (Exception e) {
\r
239 Fail ("Incorrect exception thrown at 2: " + e.ToString());
\r
241 Assert("null string error not thrown", errorThrown);
\r
244 bool errorThrown = false;
\r
246 StreamReader r = new StreamReader(TempFolder + "/nonexistentfile", false);
\r
247 } catch (FileNotFoundException) {
\r
248 errorThrown = true;
\r
249 } catch (Exception e) {
\r
250 Fail ("Incorrect exception thrown at 3: " + e.ToString());
\r
252 Assert("fileNotFound error not thrown", errorThrown);
\r
255 bool errorThrown = false;
\r
257 StreamReader r = new StreamReader(TempFolder + "/nonexistentdir/file", false);
\r
258 } catch (DirectoryNotFoundException) {
\r
259 errorThrown = true;
\r
260 } catch (Exception e) {
\r
261 Fail ("Incorrect exception thrown at 4: " + e.ToString());
\r
263 Assert("dirNotFound error not thrown", errorThrown);
\r
266 bool errorThrown = false;
\r
268 StreamReader r = new StreamReader("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0], false);
\r
269 } catch (IOException) {
\r
270 errorThrown = true;
\r
271 } catch (ArgumentException) {
\r
272 // FIXME - the spec says 'IOExc', but the
\r
273 // compiler says 'ArgExc'...
\r
274 errorThrown = true;
\r
275 } catch (Exception e) {
\r
276 Fail ("Incorrect exception thrown at 5: " + e.ToString());
\r
278 Assert("invalid filename error not thrown", errorThrown);
\r
281 // this is probably incestuous, but, oh well.
\r
282 StreamReader r = new StreamReader(_codeFileName, false);
\r
283 AssertNotNull("no stream reader", r);
\r
287 bool errorThrown = false;
\r
289 StreamReader r = new StreamReader("", true);
\r
290 } catch (ArgumentException) {
\r
291 errorThrown = true;
\r
292 } catch (Exception e) {
\r
293 Fail ("Incorrect exception thrown at 6: " + e.ToString());
\r
295 Assert("empty string error not thrown", errorThrown);
\r
298 bool errorThrown = false;
\r
300 StreamReader r = new StreamReader((string)null, true);
\r
301 } catch (ArgumentNullException) {
\r
302 errorThrown = true;
\r
303 } catch (Exception e) {
\r
304 Fail ("Incorrect exception thrown at 7: " + e.ToString());
\r
306 Assert("null string error not thrown", errorThrown);
\r
309 bool errorThrown = false;
\r
311 StreamReader r = new StreamReader(TempFolder + "/nonexistentfile", true);
\r
312 } catch (FileNotFoundException) {
\r
313 errorThrown = true;
\r
314 } catch (Exception e) {
\r
315 Fail ("Incorrect exception thrown at 8: " + e.ToString());
\r
317 Assert("fileNotFound error not thrown", errorThrown);
\r
320 bool errorThrown = false;
\r
322 StreamReader r = new StreamReader(TempFolder + "/nonexistentdir/file", true);
\r
323 } catch (DirectoryNotFoundException) {
\r
324 errorThrown = true;
\r
325 } catch (Exception e) {
\r
326 Fail ("Incorrect exception thrown at 9: " + e.ToString());
\r
328 Assert("dirNotFound error not thrown", errorThrown);
\r
331 bool errorThrown = false;
\r
333 StreamReader r = new StreamReader("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0], true);
\r
334 } catch (IOException) {
\r
335 errorThrown = true;
\r
336 } catch (ArgumentException) {
\r
337 // FIXME - the spec says 'IOExc', but the
\r
338 // compiler says 'ArgExc'...
\r
339 errorThrown = true;
\r
340 } catch (Exception e) {
\r
341 Fail ("Incorrect exception thrown at 10: " + e.ToString());
\r
343 Assert("invalid filename error not thrown", errorThrown);
\r
346 // this is probably incestuous, but, oh well.
\r
347 StreamReader r = new StreamReader(_codeFileName, true);
\r
348 AssertNotNull("no stream reader", r);
\r
353 // TODO - Ctor with Encoding
\r
356 public void TestBaseStream() {
\r
357 string progress = "beginning";
\r
360 MemoryStream m = new MemoryStream(b);
\r
361 StreamReader r = new StreamReader(m);
\r
362 AssertEquals("wrong base stream ", m, r.BaseStream);
\r
363 progress = "Closing StreamReader";
\r
365 progress = "Closing MemoryStream";
\r
367 } catch (Exception e) {
\r
368 Fail ("At '" + progress + "' an unexpected exception was thrown: " + e.ToString());
\r
372 public void TestCurrentEncoding() {
\r
375 MemoryStream m = new MemoryStream(b);
\r
376 StreamReader r = new StreamReader(m);
\r
377 AssertEquals("wrong encoding",
\r
378 Encoding.UTF8.GetType (), r.CurrentEncoding.GetType ());
\r
379 } catch (Exception e) {
\r
380 Fail ("Unexpected exception thrown: " + e.ToString());
\r
384 // TODO - Close - annoying spec - won't commit to any exceptions. How to test?
\r
385 // TODO - DiscardBufferedData - I have no clue how to test this function.
\r
388 public void TestPeek() {
\r
389 // FIXME - how to get an IO Exception?
\r
391 bool errorThrown = false;
\r
394 MemoryStream m = new MemoryStream(b);
\r
395 StreamReader r = new StreamReader(m);
\r
397 int nothing = r.Peek();
\r
398 } catch (ObjectDisposedException) {
\r
399 errorThrown = true;
\r
401 Assert("nothing-to-peek-at error not thrown", errorThrown);
\r
404 Byte[] b = {1, 2, 3, 4, 5, 6};
\r
405 MemoryStream m = new MemoryStream(b);
\r
407 StreamReader r = new StreamReader(m);
\r
408 for (int i = 1; i <= 6; i++) {
\r
409 AssertEquals("peek incorrect", i, r.Peek());
\r
412 AssertEquals("should be none left", -1, r.Peek());
\r
417 public void TestRead() {
\r
418 // FIXME - how to get an IO Exception?
\r
420 bool errorThrown = false;
\r
423 MemoryStream m = new MemoryStream(b);
\r
424 StreamReader r = new StreamReader(m);
\r
426 int nothing = r.Read();
\r
427 } catch (ObjectDisposedException) {
\r
428 errorThrown = true;
\r
429 } catch (Exception e) {
\r
430 Fail ("Incorrect exception thrown at 1: " + e.ToString());
\r
432 Assert("nothing-to-read error not thrown", errorThrown);
\r
435 Byte[] b = {1, 2, 3, 4, 5, 6};
\r
436 MemoryStream m = new MemoryStream(b);
\r
438 StreamReader r = new StreamReader(m);
\r
439 for (int i = 1; i <= 6; i++) {
\r
440 AssertEquals("read incorrect", i, r.Read());
\r
442 AssertEquals("Should be none left", -1, r.Read());
\r
446 bool errorThrown = false;
\r
449 StreamReader r = new StreamReader(new MemoryStream(b));
\r
450 r.Read(null, 0, 0);
\r
451 } catch (ArgumentNullException) {
\r
452 errorThrown = true;
\r
453 } catch (ArgumentException) {
\r
454 errorThrown = true;
\r
455 } catch (Exception e) {
\r
456 Fail ("Incorrect exception thrown at 2: " + e.ToString());
\r
458 Assert("null buffer error not thrown", errorThrown);
\r
461 bool errorThrown = false;
\r
464 StreamReader r = new StreamReader(new MemoryStream(b));
\r
465 Char[] c = new Char[1];
\r
467 } catch (ArgumentException) {
\r
468 errorThrown = true;
\r
469 } catch (Exception e) {
\r
470 Fail ("Incorrect exception thrown at 3: " + e.ToString());
\r
472 Assert("too-long range error not thrown", errorThrown);
\r
475 bool errorThrown = false;
\r
478 StreamReader r = new StreamReader(new MemoryStream(b));
\r
479 Char[] c = new Char[1];
\r
481 } catch (ArgumentOutOfRangeException) {
\r
482 errorThrown = true;
\r
483 } catch (Exception e) {
\r
484 Fail ("Incorrect exception thrown at 4: " + e.ToString());
\r
486 Assert("out of range error not thrown", errorThrown);
\r
489 bool errorThrown = false;
\r
492 StreamReader r = new StreamReader(new MemoryStream(b));
\r
493 Char[] c = new Char[1];
\r
495 } catch (ArgumentOutOfRangeException) {
\r
496 errorThrown = true;
\r
497 } catch (Exception e) {
\r
498 Fail ("Incorrect exception thrown at 5: " + e.ToString());
\r
500 Assert("out of range error not thrown", errorThrown);
\r
505 Byte[] b = {(byte)'a', (byte)'b', (byte)'c',
\r
506 (byte)'d', (byte)'e', (byte)'f',
\r
508 MemoryStream m = new MemoryStream(b);
\r
510 StreamReader r = new StreamReader(m);
\r
513 char[] buffer = new Char[7];
\r
515 char[] target = {'g','d','e','f','b','c','a'};
\r
517 r.Read(buffer, 6, 1);
\r
519 r.Read(buffer, 4, 2);
\r
521 r.Read(buffer, 1, 3);
\r
523 r.Read(buffer, 0, 1);
\r
525 for (int i = 0; i < target.Length; i++) {
\r
526 AssertEquals("read no work",
\r
527 target[i], buffer[i]);
\r
531 } catch (Exception e) {
\r
532 Fail ("Caught when ii=" + ii + ". e:" + e.ToString());
\r
538 public void TestReadLine() {
\r
539 // TODO Out Of Memory Exc? IO Exc?
\r
540 Byte[] b = new Byte[8];
\r
549 MemoryStream m = new MemoryStream(b);
\r
550 StreamReader r = new StreamReader(m);
\r
551 AssertEquals("line doesn't match", "a", r.ReadLine());
\r
552 AssertEquals("line doesn't match", "b", r.ReadLine());
\r
553 AssertEquals("line doesn't match", "c", r.ReadLine());
\r
554 AssertEquals("line doesn't match", "d", r.ReadLine());
\r
555 AssertEquals("line doesn't match", null, r.ReadLine());
\r
558 public void TestReadToEnd() {
\r
559 // TODO Out Of Memory Exc? IO Exc?
\r
560 Byte[] b = new Byte[8];
\r
569 MemoryStream m = new MemoryStream(b);
\r
570 StreamReader r = new StreamReader(m);
\r
571 AssertEquals("line doesn't match", "a\nb\nc\nd\n", r.ReadToEnd());
\r
572 AssertEquals("line doesn't match", "", r.ReadToEnd());
\r
576 public void TestBaseStreamClosed ()
\r
579 MemoryStream m = new MemoryStream (b);
\r
580 StreamReader r = new StreamReader (m);
\r
582 bool thrown = false;
\r
585 } catch (ObjectDisposedException) {
\r
589 AssertEquals ("#01", true, thrown);
\r
593 [ExpectedException (typeof (ArgumentNullException))]
\r
594 public void Contructor_Stream_NullEncoding ()
\r
596 StreamReader r = new StreamReader (new MemoryStream (), null);
\r
600 [ExpectedException (typeof (ArgumentNullException))]
\r
601 public void Contructor_Path_NullEncoding ()
\r
603 StreamReader r = new StreamReader (_codeFileName, null);
\r
607 [ExpectedException (typeof (ArgumentNullException))]
\r
608 public void Read_Null ()
\r
610 StreamReader r = new StreamReader (new MemoryStream ());
\r
611 r.Read (null, 0, 0);
\r
615 [ExpectedException (typeof (ArgumentException))]
\r
616 public void Read_IndexOverflow ()
\r
618 char[] array = new char [16];
\r
619 StreamReader r = new StreamReader (new MemoryStream (16));
\r
620 r.Read (array, 1, Int32.MaxValue);
\r
624 [ExpectedException (typeof (ArgumentException))]
\r
625 public void Read_CountOverflow ()
\r
627 char[] array = new char [16];
\r
628 StreamReader r = new StreamReader (new MemoryStream (16));
\r
629 r.Read (array, Int32.MaxValue, 1);
\r