StringReaderTest.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //
  2. // System.IO.StringWriter
  3. //
  4. // Author: Marcin Szczepanski ([email protected])
  5. //
  6. // TODO: Add some testing for exceptions
  7. //
  8. // TODO: Some of the tests could be a bit more thorough
  9. //
  10. using NUnit.Framework;
  11. using System.IO;
  12. using System;
  13. namespace MonoTests.System.IO {
  14. [TestFixture]
  15. public class StringReaderTest : Assertion {
  16. public void TestReadLine() {
  17. string testme = "a\nb\nc\n";
  18. StringReader sr = new StringReader (testme);
  19. string inputLine;
  20. int lines = 0;
  21. while ((inputLine = sr.ReadLine ()) != null)
  22. lines++;
  23. AssertEquals ("Incorrect number of lines", 3, lines);
  24. }
  25. public void TestPeekRead() {
  26. StringReader reader = new StringReader( "Test String" );
  27. char c = (char)reader.Peek();
  28. AssertEquals("A1", 'T', c );
  29. char read = (char)reader.Read();
  30. AssertEquals("A2", 'T', read );
  31. c = (char)reader.Peek();
  32. AssertEquals("A3", 'e', c );
  33. }
  34. public void TestPeekAndReadAtEndOfString() {
  35. StringReader reader = new StringReader("x");
  36. char c = (char)reader.Peek();
  37. AssertEquals("A1", 'x', c );
  38. c = (char)reader.Read();
  39. AssertEquals("A2", 'x', c);
  40. int i = reader.Peek();
  41. AssertEquals("A3", -1, i);
  42. i = reader.Read();
  43. AssertEquals("A4", -1, i);
  44. i = reader.Peek();
  45. AssertEquals("A5", -1, i);
  46. }
  47. public void TestPeekAndReadEmptyString() {
  48. StringReader reader = new StringReader("");
  49. int i = reader.Peek();
  50. AssertEquals("A1", -1, i);
  51. i = reader.Read();
  52. AssertEquals("A2", -1, i);
  53. }
  54. public void TestRead() {
  55. StringReader reader = new StringReader( "Test String" );
  56. /* Read from start of string */
  57. char[] test = new char[5];
  58. int charsRead = reader.Read( test, 0, 5 );
  59. AssertEquals( 5, charsRead );
  60. AssertEquals( "Test ", new String(test) );
  61. /* Read to end of string */
  62. //reader = new StringReader( "Test String" );
  63. test = new char[6];
  64. charsRead = reader.Read( test, 0, 6 );
  65. AssertEquals( 6, charsRead);
  66. AssertEquals( "String", new String( test ) );
  67. /* Read past end of string */
  68. test = new char[6];
  69. reader = new StringReader( "Foo" );
  70. charsRead = reader.Read( test, 0, 6 );
  71. AssertEquals( 3, charsRead );
  72. AssertEquals( "Foo\0\0\0", new String( test ) );
  73. /* Check that a new invocation on the empty reader will return 0 */
  74. charsRead = reader.Read (test, 0, 6);
  75. AssertEquals (0, charsRead);
  76. }
  77. public void TestReadEOL() {
  78. StringReader reader = new StringReader( "Line1\rLine2\r\nLine3\nLine4" );
  79. string test = reader.ReadLine();
  80. AssertEquals( "Line1", test );
  81. test = reader.ReadLine();
  82. AssertEquals( "Line2", test );
  83. test = reader.ReadLine();
  84. AssertEquals( "Line3", test );
  85. test = reader.ReadLine();
  86. AssertEquals( "Line4", test );
  87. }
  88. public void TestClose() {
  89. StringReader reader = new StringReader("reader");
  90. reader.Close ();
  91. try {
  92. reader.Read ();
  93. Fail();
  94. } catch (Exception e) {
  95. AssertEquals ("Close 1", typeof (ObjectDisposedException), e.GetType ());
  96. }
  97. try {
  98. reader.Peek ();
  99. Fail ();
  100. } catch (Exception e) {
  101. AssertEquals ("Close 2", typeof (ObjectDisposedException), e.GetType ());
  102. }
  103. }
  104. public void TestExceptions() {
  105. StringReader reader;
  106. try {
  107. reader = new StringReader(null);
  108. Fail ();
  109. } catch (Exception e) {
  110. AssertEquals ("Exception 1", typeof (ArgumentNullException), e.GetType ());
  111. }
  112. reader = new StringReader ("this is a test\nAnd nothing else");
  113. try {
  114. reader.Read (null, 0, 12);
  115. Fail ();
  116. } catch (Exception e) {
  117. AssertEquals ("Exception 2", typeof (ArgumentNullException), e.GetType ());
  118. }
  119. }
  120. [Test]
  121. public void MoreEOL ()
  122. {
  123. TextReader tr = new StringReader ("There she was just a walking\n" +
  124. "Down the street singin'\r" +
  125. "Do wah diddy diddy dum diddy do");
  126. int i = 0;
  127. while (tr.ReadLine () != null)
  128. i++;
  129. AssertEquals ("#01", 3, i);
  130. }
  131. [Test]
  132. [ExpectedException (typeof (ArgumentException))]
  133. public void Read_IndexOverflow ()
  134. {
  135. StringReader sr = new StringReader ("Mono");
  136. sr.Read (new char [4], Int32.MaxValue, 1);
  137. }
  138. [Test]
  139. [ExpectedException (typeof (ArgumentException))]
  140. public void Read_CountOverflow ()
  141. {
  142. StringReader sr = new StringReader ("Mono");
  143. sr.Read (new char [4], 1, Int32.MaxValue);
  144. }
  145. [Test]
  146. public void Read_DoesntStopAtLineEndings ()
  147. {
  148. StringReader reader = new StringReader ("Line1\rLine2\r\nLine3\nLine4");
  149. AssertEquals (24, reader.Read (new char[24], 0, 24));
  150. }
  151. }
  152. }