2009-06-12 Bill Holmes <billholmes54@gmail.com>
[mono.git] / mcs / class / corlib / Test / System.IO / StringReaderTest.cs
1 //
2 // System.IO.StringWriter
3 //
4 // Author: Marcin Szczepanski (marcins@zipworld.com.au)
5 //
6 // TODO: Add some testing for exceptions
7 //
8 // TODO: Some of the tests could be a bit more thorough
9 //
10
11 using NUnit.Framework;
12 using System.IO;
13 using System;
14
15 namespace MonoTests.System.IO {
16
17 [TestFixture]
18 public class StringReaderTest : Assertion {
19         public  void TestReadLine() {
20                 string testme = "a\nb\nc\n";
21                 StringReader sr = new StringReader (testme);
22                 string inputLine;
23                 int lines = 0;
24                 while ((inputLine = sr.ReadLine ()) != null)
25                         lines++;
26                 
27                 AssertEquals ("Incorrect number of lines", 3, lines);
28         }
29
30         public void TestPeekRead() {
31                 StringReader reader = new StringReader( "Test String" );
32
33                 char c = (char)reader.Peek();
34                 AssertEquals("A1", 'T', c );
35
36                 char read = (char)reader.Read();
37
38                 AssertEquals("A2", 'T', read );
39
40                 c = (char)reader.Peek();
41
42                 AssertEquals("A3", 'e', c );
43         }
44
45         public void TestPeekAndReadAtEndOfString() {
46                 StringReader reader = new StringReader("x");
47
48                 char c = (char)reader.Peek();
49                 AssertEquals("A1", 'x', c );
50
51                 c = (char)reader.Read();
52                 AssertEquals("A2", 'x', c);
53
54                 int i = reader.Peek();
55                 AssertEquals("A3", -1, i);
56
57                 i = reader.Read();
58                 AssertEquals("A4", -1, i);
59
60                 i = reader.Peek();
61                 AssertEquals("A5", -1, i);
62         }
63
64         public void TestPeekAndReadEmptyString() {
65                 StringReader reader = new StringReader("");
66
67                 int i = reader.Peek();
68                 AssertEquals("A1", -1, i);
69
70                 i = reader.Read();
71                 AssertEquals("A2", -1, i);
72         }
73
74         public void TestRead() {
75                 StringReader reader = new StringReader( "Test String" );
76
77                 /* Read from start of string */
78                 char[] test = new char[5];
79
80                 int charsRead = reader.Read( test, 0, 5 );
81
82                 AssertEquals( 5, charsRead );
83                 AssertEquals( "Test ", new String(test)  );
84
85                 /* Read to end of string */
86                 //reader = new StringReader( "Test String" );
87
88                 test = new char[6];
89                 charsRead = reader.Read( test, 0, 6 );
90                 AssertEquals( 6, charsRead);
91                 AssertEquals( "String", new String( test )  );
92
93                 /* Read past end of string */
94
95                 test = new char[6];
96                 reader = new StringReader( "Foo" );
97                 charsRead = reader.Read( test, 0, 6 );
98                 AssertEquals( 3, charsRead );
99                 AssertEquals(  "Foo\0\0\0", new String( test ) );
100
101                 /* Check that a new invocation on the empty reader will return 0 */
102                 charsRead = reader.Read (test, 0, 6);
103                 AssertEquals (0, charsRead);
104                 
105         }
106
107         public void TestReadEOL() {
108                 StringReader reader = new StringReader( "Line1\rLine2\r\nLine3\nLine4" );
109
110                 string test = reader.ReadLine();
111
112                 AssertEquals( "Line1", test );
113
114                 test = reader.ReadLine();
115
116                 AssertEquals( "Line2", test );
117
118                 test = reader.ReadLine();
119
120                 AssertEquals( "Line3", test );
121
122                 test = reader.ReadLine();
123
124                 AssertEquals( "Line4", test );
125         }
126
127         public void TestClose() {
128                 
129                 StringReader reader = new StringReader("reader");
130                 reader.Close ();
131                 
132                 try {
133                         reader.Read ();
134                         Fail();
135                 } catch (Exception e) {
136                         AssertEquals ("Close 1", typeof (ObjectDisposedException), e.GetType ());
137                 }
138                 
139                 try {
140                         reader.Peek ();
141                         Fail ();
142                 } catch (Exception e) {
143                         AssertEquals ("Close 2", typeof (ObjectDisposedException), e.GetType ());                                    
144                 }               
145         }
146         
147         public void TestExceptions() {
148                 
149                 StringReader reader;
150                 
151                 try {
152                         reader = new StringReader(null);
153                         Fail ();
154                 } catch (Exception e) {
155                         AssertEquals ("Exception 1", typeof (ArgumentNullException), e.GetType ());
156                 }
157                 
158                 reader = new StringReader ("this is a test\nAnd nothing else");
159                 
160                 try {
161                         reader.Read (null, 0, 12);
162                         Fail ();
163                 } catch (Exception e) {
164                         AssertEquals ("Exception 2", typeof (ArgumentNullException), e.GetType ());
165                 }                       
166         }
167
168         [Test]
169         public void MoreEOL ()
170         {
171                 TextReader tr = new StringReader ("There she was just a walking\n" +
172                                                   "Down the street singin'\r" +
173                                                   "Do wah diddy diddy dum diddy do");
174
175                 int i = 0;
176                 while (tr.ReadLine () != null)
177                         i++;
178
179                 AssertEquals ("#01", 3, i);
180         }
181
182         [Test]
183         [ExpectedException (typeof (ArgumentException))]
184         public void Read_IndexOverflow () 
185         {
186                 StringReader sr = new StringReader ("Mono");
187                 sr.Read (new char [4], Int32.MaxValue, 1);
188         }
189
190         [Test]
191         [ExpectedException (typeof (ArgumentException))]
192         public void Read_CountOverflow () 
193         {
194                 StringReader sr = new StringReader ("Mono");
195                 sr.Read (new char [4], 1, Int32.MaxValue);
196         }
197
198         [Test]
199         public void Read_DoesntStopAtLineEndings ()
200         {
201                 StringReader reader = new StringReader ("Line1\rLine2\r\nLine3\nLine4");
202                 AssertEquals (24, reader.Read (new char[24], 0, 24));
203         }       
204
205         [Test]
206         public void MixedLineEnding ()
207         {
208                 string foobar = "Foo\n\r\n\rBar";
209                 StringReader reader = new StringReader (foobar);
210                 int count = 0;
211                 while (reader.ReadLine () != null)
212                         count++;
213                 AssertEquals (4, count);
214
215         }
216 }
217
218 }