File: StringReaderTest.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (226 lines) | stat: -rw-r--r-- 5,247 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//
// System.IO.StringWriter
//
// Author: Marcin Szczepanski (marcins@zipworld.com.au)
//
// TODO: Add some testing for exceptions
//
// TODO: Some of the tests could be a bit more thorough
//

using NUnit.Framework;
using System.IO;
using System;

namespace MonoTests.System.IO {

[TestFixture]
public class StringReaderTest {
	[Test]
	public  void TestReadLine() {
		string testme = "a\nb\nc\n";
		StringReader sr = new StringReader (testme);
		string inputLine;
		int lines = 0;
		while ((inputLine = sr.ReadLine ()) != null)
			lines++;
		
		Assert.AreEqual (3, lines, "Incorrect number of lines");
	}

	[Test]
	public void TestPeekRead() {
		StringReader reader = new StringReader( "Test String" );

		char c = (char)reader.Peek();
		Assert.AreEqual ('T', c, "A1");

		char read = (char)reader.Read();

		Assert.AreEqual ('T', read, "A2");

		c = (char)reader.Peek();

		Assert.AreEqual ('e', c, "A3");
	}

	[Test]
	public void TestPeekAndReadAtEndOfString() {
		StringReader reader = new StringReader("x");

		char c = (char)reader.Peek();
		Assert.AreEqual ('x', c, "A1");

		c = (char)reader.Read();
		Assert.AreEqual ('x', c, "A2");

		int i = reader.Peek();
		Assert.AreEqual (-1, i, "A3");

		i = reader.Read();
		Assert.AreEqual (-1, i, "A4");

		i = reader.Peek();
		Assert.AreEqual (-1, i, "A5");
	}

	[Test]
	public void TestPeekAndReadEmptyString() {
		StringReader reader = new StringReader("");

		int i = reader.Peek();
		Assert.AreEqual (-1, i, "A1");

		i = reader.Read();
		Assert.AreEqual (-1, i, "A2");
	}

	[Test]
	public void TestRead() {
		StringReader reader = new StringReader( "Test String" );

		/* Read from start of string */
		char[] test = new char[5];

		int charsRead = reader.Read( test, 0, 5 );

		Assert.AreEqual (5, charsRead);
		Assert.AreEqual ("Test ", new String(test));

		/* Read to end of string */
		//reader = new StringReader( "Test String" );

		test = new char[6];
		charsRead = reader.Read( test, 0, 6 );
		Assert.AreEqual (6, charsRead);
		Assert.AreEqual ("String", new String( test ));

		/* Read past end of string */

		test = new char[6];
		reader = new StringReader( "Foo" );
		charsRead = reader.Read( test, 0, 6 );
		Assert.AreEqual (3, charsRead);
		Assert.AreEqual ("Foo\0\0\0", new String( test ));

		/* Check that a new invocation on the empty reader will return 0 */
		charsRead = reader.Read (test, 0, 6);
		Assert.AreEqual (0, charsRead);
		
	}

	[Test]
        public void TestReadEOL() {
                StringReader reader = new StringReader( "Line1\rLine2\r\nLine3\nLine4" );

                string test = reader.ReadLine();

                Assert.AreEqual ("Line1", test);

                test = reader.ReadLine();

                Assert.AreEqual ("Line2", test);

                test = reader.ReadLine();

                Assert.AreEqual ("Line3", test);

                test = reader.ReadLine();

                Assert.AreEqual ("Line4", test);
        }

	[Test]
        public void TestClose() {
        	
        	StringReader reader = new StringReader("reader");
        	reader.Close ();
        	
        	try {
        		reader.Read ();
        		Assert.Fail ();
        	} catch (Exception e) {
        		Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "Close 1");
        	}
        	
        	try {
        		reader.Peek ();
        		Assert.Fail ();
        	} catch (Exception e) {
        		Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "Close 2");        		             
        	}        	
        }
        
	[Test]
        public void TestExceptions() {
        	
        	StringReader reader;
        	
        	try {
	        	reader = new StringReader(null);
        		Assert.Fail ();
        	} catch (Exception e) {
        		Assert.AreEqual (typeof (ArgumentNullException), e.GetType (), "Exception 1");
        	}
        	
        	reader = new StringReader ("this is a test\nAnd nothing else");
		
		try {
			reader.Read (null, 0, 12);
			Assert.Fail ();
		} catch (Exception e) {
			Assert.AreEqual (typeof (ArgumentNullException), e.GetType (), "Exception 2");
		}        		
        }

	[Test]
	public void MoreEOL ()
	{
                TextReader tr = new StringReader ("There she was just a walking\n" +
						  "Down the street singin'\r" +
						  "Do wah diddy diddy dum diddy do");

		int i = 0;
		while (tr.ReadLine () != null)
			i++;

		Assert.AreEqual (3, i, "#01");
	}

	[Test]
	[ExpectedException (typeof (ArgumentException))]
	public void Read_IndexOverflow () 
	{
		StringReader sr = new StringReader ("Mono");
		sr.Read (new char [4], Int32.MaxValue, 1);
	}

	[Test]
	[ExpectedException (typeof (ArgumentException))]
	public void Read_CountOverflow () 
	{
		StringReader sr = new StringReader ("Mono");
		sr.Read (new char [4], 1, Int32.MaxValue);
	}

	[Test]
	public void Read_DoesntStopAtLineEndings ()
	{
		StringReader reader = new StringReader ("Line1\rLine2\r\nLine3\nLine4");
		Assert.AreEqual (reader.Read (new char[24], 0, 24), 24);
	}	

	[Test]
	public void MixedLineEnding ()
	{
		string foobar = "Foo\n\r\n\rBar";
		StringReader reader = new StringReader (foobar);
		int count = 0;
		while (reader.ReadLine () != null)
			count++;
		Assert.AreEqual (4, count);

	}
}

}