1//
2//  ANTLRStringStreamTest.m
3//  ANTLR
4//
5//  Created by Ian Michell on 12/05/2010.
6//  Copyright 2010 Ian Michell. All rights reserved.
7//
8
9#import "ANTLRStringStreamTest.h"
10#import "ANTLRCharStream.h"
11#import "ANTLRStringStream.h"
12#import "ANTLRError.h"
13
14@implementation ANTLRStringStreamTest
15
16-(void) testInitWithInput
17{
18	NSString *input = @"This is a string used for ANTLRStringStream input ;)";
19	ANTLRStringStream *stream = [ANTLRStringStream newANTLRStringStream:input];
20	NSString *subString = [stream substring:0 To:10];
21	NSLog(@"The first ten chars are '%@'", subString);
22	STAssertTrue([@"This is a " isEqualToString:subString], @"The strings do not match");
23	[stream release];
24}
25
26-(void) testConsumeAndReset
27{
28	ANTLRStringStream *stream = [ANTLRStringStream newANTLRStringStream:@"This is a string used for input"];
29	[stream consume];
30	STAssertTrue(stream.index > 0, @"Index should be greater than 0 after consume");
31	[stream reset];
32	STAssertTrue(stream.index == 0, @"Index should be 0 after reset");
33	[stream release];
34}
35
36-(void) testConsumeWithNewLine
37{
38	ANTLRStringStream *stream = [ANTLRStringStream newANTLRStringStream:@"This is a string\nused for input"];
39	while (stream.index < [stream size] && stream.line == 1)
40	{
41		[stream consume];
42	}
43	STAssertTrue(stream.line == 2, @"Line number is incorrect, should be 2, was %d!", stream.line);
44	STAssertTrue(stream.charPositionInLine == 0, @"Char position in line should be 0, it was: %d!", stream.charPositionInLine);
45	[stream release];
46}
47
48-(void) testLAEOF
49{
50    NSInteger i;
51	ANTLRStringStream *stream = [ANTLRStringStream newANTLRStringStream:@"This is a string\nused for input"];
52	BOOL eofFound = NO;
53	for (i = 1; i <= [stream size]+1; i++) {
54		NSInteger r = [stream LA:i];
55		if (r == (NSInteger)ANTLRCharStreamEOF) {
56			eofFound = YES;
57            break;
58		}
59	}
60	STAssertTrue(eofFound, @"EOF Was not found in stream, Length =%d, index = %d, i = %d", [stream size], stream.index, i);
61	[stream release];
62}
63
64-(void) testLTEOF
65{
66    NSInteger i;
67	ANTLRStringStream *stream = [ANTLRStringStream newANTLRStringStream:@"This is a string\nused for input"];
68	BOOL eofFound = NO;
69	for ( i = 1; i <= [stream size]+1; i++) {
70		NSInteger r = [stream LT:i];
71		if (r == (NSInteger)ANTLRCharStreamEOF) {
72			eofFound = YES;
73            break;
74		}
75	}
76	STAssertTrue(eofFound, @"EOF Was not found in stream, Length =%d, index = %d, i = %d", [stream size], stream.index, i);
77	[stream release];
78}
79
80-(void) testSeek
81{
82	ANTLRStringStream *stream =[ANTLRStringStream newANTLRStringStream:@"This is a string used for input"];
83	[stream seek:10];
84	STAssertTrue(stream.index == 10, @"Index should be 10");
85	// Get char 10 which is s (with 0 being T)
86	STAssertTrue([stream LA:1] > -1 && (char)[stream LA:1] == 's', @"Char returned should be s");
87	[stream release];
88}
89
90-(void) testSeekMarkAndRewind
91{
92	ANTLRStringStream *stream =[ANTLRStringStream newANTLRStringStream:@"This is a string used for input"];
93	[stream mark];
94	[stream seek:10];
95	STAssertTrue(stream.index == 10, @"Index should be 10");
96	[stream rewind];
97	STAssertTrue(stream.index == 0, @"Index should be 0");
98	[stream seek:5];
99	STAssertTrue(stream.index == 5, @"Index should be 5");
100	[stream mark]; // make a new marker to test a branch.
101	[stream seek:10];
102	STAssertTrue(stream.index == 10, @"Index should be 10");
103	[stream rewind]; // should be marked to 5.
104	STAssertTrue(stream.index == 5, @"Index should be 5");
105	[stream release];
106}
107
108@end
109