1 /*
2 [The "BSD licence"]
3 Copyright (c) 2005-2007 Kunle Odutola
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
9 1. Redistributions of source code MUST RETAIN the above copyright
10    notice, this list of conditions and the following disclaimer.
11 2. Redistributions in binary form MUST REPRODUCE the above copyright
12    notice, this list of conditions and the following disclaimer in
13    the documentation and/or other materials provided with the
14    distribution.
15 3. The name of the author may not be used to endorse or promote products
16    derived from this software without specific prior WRITTEN permission.
17 4. Unless explicitly state otherwise, any contribution intentionally
18    submitted for inclusion in this work to the copyright owner or licensor
19    shall be under the terms and conditions of this license, without any
20    additional terms or conditions.
21 
22 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 
34 
35 #pragma warning disable 219 // No unused variable warnings
36 
37 namespace Antlr.Runtime.Tests
38 {
39 	using System;
40 	using Stream = System.IO.Stream;
41 	using FileStream = System.IO.FileStream;
42 	using MemoryStream = System.IO.MemoryStream;
43 	using FileMode = System.IO.FileMode;
44 	using Encoding = System.Text.Encoding;
45 	using Encoder = System.Text.Encoder;
46 
47 	using ANTLRInputStream = Antlr.Runtime.ANTLRInputStream;
48 
49 	using MbUnit.Framework;
50 
51 	[TestFixture]
52 	public class ANTLRxxxxStreamFixture : TestFixtureBase
53 	{
54 		private static readonly string grammarStr = ""
55 				+ "parser grammar p;" + NL
56 				+ "prog : WHILE ID LCURLY (assign)* RCURLY EOF;" + NL
57 				+ "assign : ID ASSIGN expr SEMI ;" + NL
58 				+ "expr : INT | FLOAT | ID ;" + NL;
59 
60 
61 		#region ANTLRInputStream Tests
62 
63 		[Test]
TestANTLRInputStreamConstructorDoesNotHang()64 		public void TestANTLRInputStreamConstructorDoesNotHang()
65 		{
66 			Encoding encoding = Encoding.Unicode;
67 			byte[] grammarStrBuffer = encoding.GetBytes(grammarStr);
68 			MemoryStream grammarStream = new MemoryStream(grammarStrBuffer);
69 
70 			ANTLRInputStream input = new ANTLRInputStream(grammarStream, encoding);
71 		}
72 
73 		[Test]
TestSizeOnEmptyANTLRInputStream()74 		public void TestSizeOnEmptyANTLRInputStream()
75 		{
76 			MemoryStream grammarStream = new MemoryStream(new byte[] { });
77 
78 			ANTLRInputStream inputStream = new ANTLRInputStream(grammarStream, Encoding.Unicode);
79 			Assert.AreEqual(0, inputStream.Count);
80 		}
81 
82 		[Test]
TestSizeOnANTLRInputStream()83 		public void TestSizeOnANTLRInputStream()
84 		{
85 			Encoding encoding = Encoding.Unicode;
86 			byte[] grammarStrBuffer = encoding.GetBytes(grammarStr);
87 			MemoryStream grammarStream = new MemoryStream(grammarStrBuffer);
88 
89 			ANTLRInputStream inputStream = new ANTLRInputStream(grammarStream, Encoding.Unicode);
90 			Assert.AreEqual(grammarStr.Length, inputStream.Count);
91 		}
92 
93 		[Test]
TestConsumeAndIndexOnANTLRInputStream()94 		public void TestConsumeAndIndexOnANTLRInputStream()
95 		{
96 			Encoding encoding = Encoding.Unicode;
97 			byte[] grammarStrBuffer = encoding.GetBytes(grammarStr);
98 			MemoryStream grammarStream = new MemoryStream(grammarStrBuffer);
99 
100 			ANTLRInputStream inputStream = new ANTLRInputStream(grammarStream, Encoding.Unicode);
101 			Assert.AreEqual(0, inputStream.Index);
102 
103 			inputStream.Consume();
104 			Assert.AreEqual(1, inputStream.Index);
105 
106 			inputStream.Consume();
107 			Assert.AreEqual(2, inputStream.Index);
108 
109 			while (inputStream.Index < inputStream.Count)
110 			{
111 				inputStream.Consume();
112 			}
113 			Assert.AreEqual(inputStream.Index, inputStream.Count);
114 		}
115 
116 		[Test]
TestConsumeAllCharactersInAnANTLRInputStream()117 		public void TestConsumeAllCharactersInAnANTLRInputStream()
118 		{
119 			Encoding encoding = Encoding.Unicode;
120 			byte[] grammarStrBuffer = encoding.GetBytes(grammarStr);
121 			MemoryStream grammarStream = new MemoryStream(grammarStrBuffer);
122 
123 			ANTLRInputStream inputStream = new ANTLRInputStream(grammarStream, Encoding.Unicode);
124 			while (inputStream.Index < inputStream.Count)
125 			{
126 				Console.Out.Write((char)inputStream.LA(1));
127 				inputStream.Consume();
128 			}
129 			Assert.AreEqual(inputStream.Index, inputStream.Count);
130 		}
131 
132 		[Test]
TestConsumeOnANTLRInputStream()133 		public void TestConsumeOnANTLRInputStream()
134 		{
135 			Encoding encoding = Encoding.Unicode;
136 			byte[] buffer = encoding.GetBytes("One\r\nTwo");
137 			MemoryStream grammarStream = new MemoryStream(buffer);
138 
139 			ANTLRInputStream inputStream = new ANTLRInputStream(grammarStream, Encoding.Unicode);
140 			Assert.AreEqual(0, inputStream.Index);
141 			Assert.AreEqual(0, inputStream.CharPositionInLine);
142 			Assert.AreEqual(1, inputStream.Line);
143 
144 			inputStream.Consume();		// O
145 			Assert.AreEqual(1, inputStream.Index);
146 			Assert.AreEqual(1, inputStream.CharPositionInLine);
147 			Assert.AreEqual(1, inputStream.Line);
148 
149 			inputStream.Consume();		// n
150 			Assert.AreEqual(2, inputStream.Index);
151 			Assert.AreEqual(2, inputStream.CharPositionInLine);
152 			Assert.AreEqual(1, inputStream.Line);
153 
154 			inputStream.Consume();		// e
155 			Assert.AreEqual(3, inputStream.Index);
156 			Assert.AreEqual(3, inputStream.CharPositionInLine);
157 			Assert.AreEqual(1, inputStream.Line);
158 
159 			inputStream.Consume();		// \r
160 			Assert.AreEqual(4, inputStream.Index);
161 			Assert.AreEqual(4, inputStream.CharPositionInLine);
162 			Assert.AreEqual(1, inputStream.Line);
163 
164 			inputStream.Consume();		// \n
165 			Assert.AreEqual(5, inputStream.Index);
166 			Assert.AreEqual(0, inputStream.CharPositionInLine);
167 			Assert.AreEqual(2, inputStream.Line);
168 
169 			inputStream.Consume();		// T
170 			Assert.AreEqual(6, inputStream.Index);
171 			Assert.AreEqual(1, inputStream.CharPositionInLine);
172 			Assert.AreEqual(2, inputStream.Line);
173 
174 			inputStream.Consume();		// w
175 			Assert.AreEqual(7, inputStream.Index);
176 			Assert.AreEqual(2, inputStream.CharPositionInLine);
177 			Assert.AreEqual(2, inputStream.Line);
178 
179 			inputStream.Consume();		// o
180 			Assert.AreEqual(8, inputStream.Index);
181 			Assert.AreEqual(3, inputStream.CharPositionInLine);
182 			Assert.AreEqual(2, inputStream.Line);
183 
184 			inputStream.Consume();		// EOF
185 			Assert.AreEqual(8, inputStream.Index);
186 			Assert.AreEqual(3, inputStream.CharPositionInLine);
187 			Assert.AreEqual(2, inputStream.Line);
188 
189 			inputStream.Consume();		// EOF
190 			Assert.AreEqual(8, inputStream.Index);
191 			Assert.AreEqual(3, inputStream.CharPositionInLine);
192 			Assert.AreEqual(2, inputStream.Line);
193 		}
194 
195 		[Test]
TestResetOnANTLRInputStream()196 		public void TestResetOnANTLRInputStream()
197 		{
198 			Encoding encoding = Encoding.Unicode;
199 			byte[] buffer = encoding.GetBytes("One\r\nTwo");
200 			MemoryStream grammarStream = new MemoryStream(buffer);
201 
202 			ANTLRInputStream inputStream = new ANTLRInputStream(grammarStream, encoding);
203 			Assert.AreEqual(0, inputStream.Index);
204 			Assert.AreEqual(0, inputStream.CharPositionInLine);
205 			Assert.AreEqual(1, inputStream.Line);
206 
207 			inputStream.Consume();		// O
208 			inputStream.Consume();		// n
209 
210 			Assert.AreEqual('e', inputStream.LA(1));
211 			Assert.AreEqual(2, inputStream.Index);
212 
213 			inputStream.Reset();
214 			Assert.AreEqual('O', inputStream.LA(1));
215 			Assert.AreEqual(0, inputStream.Index);
216 			Assert.AreEqual(0, inputStream.CharPositionInLine);
217 			Assert.AreEqual(1, inputStream.Line);
218 			inputStream.Consume();		// O
219 
220 			Assert.AreEqual('n', inputStream.LA(1));
221 			Assert.AreEqual(1, inputStream.Index);
222 			Assert.AreEqual(1, inputStream.CharPositionInLine);
223 			Assert.AreEqual(1, inputStream.Line);
224 			inputStream.Consume();		// n
225 
226 			Assert.AreEqual('e', inputStream.LA(1));
227 			Assert.AreEqual(2, inputStream.Index);
228 			Assert.AreEqual(2, inputStream.CharPositionInLine);
229 			Assert.AreEqual(1, inputStream.Line);
230 			inputStream.Consume();		// e
231 		}
232 
233 		[Test]
TestSubstringOnANTLRInputStream()234 		public void TestSubstringOnANTLRInputStream()
235 		{
236 			Encoding encoding = Encoding.Unicode;
237 			byte[] buffer = encoding.GetBytes("One\r\nTwo\r\nThree");
238 			MemoryStream grammarStream = new MemoryStream(buffer);
239 
240 			ANTLRInputStream stream = new ANTLRInputStream(grammarStream, encoding);
241 
242 			Assert.AreEqual("Two", stream.Substring(5, 7));
243 			Assert.AreEqual("One", stream.Substring(0, 2));
244 			Assert.AreEqual("Three", stream.Substring(10, 14));
245 
246 			stream.Consume();
247 
248 			Assert.AreEqual("Two", stream.Substring(5, 7));
249 			Assert.AreEqual("One", stream.Substring(0, 2));
250 			Assert.AreEqual("Three", stream.Substring(10, 14));
251 		}
252 
253 		[Test]
TestSeekOnANTLRInputStream()254 		public void TestSeekOnANTLRInputStream()
255 		{
256 			Encoding encoding = Encoding.Unicode;
257 			byte[] buffer = encoding.GetBytes("One\r\nTwo\r\nThree");
258 			MemoryStream grammarStream = new MemoryStream(buffer);
259 
260 			ANTLRInputStream stream = new ANTLRInputStream(grammarStream, encoding);
261 			Assert.AreEqual('O', stream.LA(1));
262 			Assert.AreEqual(0, stream.Index);
263 			Assert.AreEqual(0, stream.CharPositionInLine);
264 			Assert.AreEqual(1, stream.Line);
265 
266 			stream.Seek(6);
267 			Assert.AreEqual('w', stream.LA(1));
268 			Assert.AreEqual(6, stream.Index);
269 			Assert.AreEqual(1, stream.CharPositionInLine);
270 			Assert.AreEqual(2, stream.Line);
271 
272 			stream.Seek(11);
273 			Assert.AreEqual('h', stream.LA(1));
274 			Assert.AreEqual(11, stream.Index);
275 			Assert.AreEqual(1, stream.CharPositionInLine);
276 			Assert.AreEqual(3, stream.Line);
277 
278 			// seeking backwards leaves state info (other than index in stream) unchanged
279 			stream.Seek(1);
280 			Assert.AreEqual('n', stream.LA(1));
281 			Assert.AreEqual(1, stream.Index);
282 			Assert.AreEqual(1, stream.CharPositionInLine);
283 			Assert.AreEqual(3, stream.Line);
284 		}
285 
286 		#endregion
287 
288 
289 		#region ANTLRStringStream Tests
290 
291 		[Test]
TestSizeOnEmptyANTLRStringStream()292 		public void TestSizeOnEmptyANTLRStringStream()
293 		{
294 			ANTLRStringStream s1 = new ANTLRStringStream("");
295 			Assert.AreEqual(0, s1.Count);
296 			Assert.AreEqual(0, s1.Index);
297 		}
298 
299 		[Test]
TestSizeOnANTLRStringStream()300 		public void TestSizeOnANTLRStringStream()
301 		{
302 			ANTLRStringStream s1 = new ANTLRStringStream("lexer\r\n");
303 			Assert.AreEqual(7, s1.Count);
304 
305 			ANTLRStringStream s2 = new ANTLRStringStream(grammarStr);
306 			Assert.AreEqual(grammarStr.Length, s2.Count);
307 
308 			ANTLRStringStream s3 = new ANTLRStringStream("grammar P;");
309 			Assert.AreEqual(10, s3.Count);
310 		}
311 
312 		[Test]
TestConsumeOnANTLRStringStream()313 		public void TestConsumeOnANTLRStringStream()
314 		{
315 			ANTLRStringStream stream = new ANTLRStringStream("One\r\nTwo");
316 			Assert.AreEqual(0, stream.Index);
317 			Assert.AreEqual(0, stream.CharPositionInLine);
318 			Assert.AreEqual(1, stream.Line);
319 
320 			stream.Consume();		// O
321 			Assert.AreEqual(1, stream.Index);
322 			Assert.AreEqual(1, stream.CharPositionInLine);
323 			Assert.AreEqual(1, stream.Line);
324 
325 			stream.Consume();		// n
326 			Assert.AreEqual(2, stream.Index);
327 			Assert.AreEqual(2, stream.CharPositionInLine);
328 			Assert.AreEqual(1, stream.Line);
329 
330 			stream.Consume();		// e
331 			Assert.AreEqual(3, stream.Index);
332 			Assert.AreEqual(3, stream.CharPositionInLine);
333 			Assert.AreEqual(1, stream.Line);
334 
335 			stream.Consume();		// \r
336 			Assert.AreEqual(4, stream.Index);
337 			Assert.AreEqual(4, stream.CharPositionInLine);
338 			Assert.AreEqual(1, stream.Line);
339 
340 			stream.Consume();		// \n
341 			Assert.AreEqual(5, stream.Index);
342 			Assert.AreEqual(0, stream.CharPositionInLine);
343 			Assert.AreEqual(2, stream.Line);
344 
345 			stream.Consume();		// T
346 			Assert.AreEqual(6, stream.Index);
347 			Assert.AreEqual(1, stream.CharPositionInLine);
348 			Assert.AreEqual(2, stream.Line);
349 
350 			stream.Consume();		// w
351 			Assert.AreEqual(7, stream.Index);
352 			Assert.AreEqual(2, stream.CharPositionInLine);
353 			Assert.AreEqual(2, stream.Line);
354 
355 			stream.Consume();		// o
356 			Assert.AreEqual(8, stream.Index);
357 			Assert.AreEqual(3, stream.CharPositionInLine);
358 			Assert.AreEqual(2, stream.Line);
359 
360 			stream.Consume();		// EOF
361 			Assert.AreEqual(8, stream.Index);
362 			Assert.AreEqual(3, stream.CharPositionInLine);
363 			Assert.AreEqual(2, stream.Line);
364 
365 			stream.Consume();		// EOF
366 			Assert.AreEqual(8, stream.Index);
367 			Assert.AreEqual(3, stream.CharPositionInLine);
368 			Assert.AreEqual(2, stream.Line);
369 		}
370 
371 		[Test]
TestResetOnANTLRStringStream()372 		public void TestResetOnANTLRStringStream()
373 		{
374 			ANTLRStringStream stream = new ANTLRStringStream("One\r\nTwo");
375 			Assert.AreEqual(0, stream.Index);
376 			Assert.AreEqual(0, stream.CharPositionInLine);
377 			Assert.AreEqual(1, stream.Line);
378 
379 			stream.Consume();		// O
380 			stream.Consume();		// n
381 
382 			Assert.AreEqual('e', stream.LA(1));
383 			Assert.AreEqual(2, stream.Index);
384 
385 			stream.Reset();
386 			Assert.AreEqual('O', stream.LA(1));
387 			Assert.AreEqual(0, stream.Index);
388 			Assert.AreEqual(0, stream.CharPositionInLine);
389 			Assert.AreEqual(1, stream.Line);
390 			stream.Consume();		// O
391 
392 			Assert.AreEqual('n', stream.LA(1));
393 			Assert.AreEqual(1, stream.Index);
394 			Assert.AreEqual(1, stream.CharPositionInLine);
395 			Assert.AreEqual(1, stream.Line);
396 			stream.Consume();		// n
397 
398 			Assert.AreEqual('e', stream.LA(1));
399 			Assert.AreEqual(2, stream.Index);
400 			Assert.AreEqual(2, stream.CharPositionInLine);
401 			Assert.AreEqual(1, stream.Line);
402 			stream.Consume();		// e
403 		}
404 
405 		[Test]
TestSubstringOnANTLRStringStream()406 		public void TestSubstringOnANTLRStringStream()
407 		{
408 			ANTLRStringStream stream = new ANTLRStringStream("One\r\nTwo\r\nThree");
409 
410 			Assert.AreEqual("Two", stream.Substring(5, 7));
411 			Assert.AreEqual("One", stream.Substring(0, 2));
412 			Assert.AreEqual("Three", stream.Substring(10, 14));
413 
414 			stream.Consume();
415 
416 			Assert.AreEqual("Two", stream.Substring(5, 7));
417 			Assert.AreEqual("One", stream.Substring(0, 2));
418 			Assert.AreEqual("Three", stream.Substring(10, 14));
419 		}
420 
421 		#endregion
422 	}
423 }