Source Code Cross Referenced for TestCommonTreeNodeStream.java in  » Parser » antlr-3.0.1 » org » antlr » test » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Parser » antlr 3.0.1 » org.antlr.test 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         [The "BSD licence"]
003:         Copyright (c) 2005-2006 Terence Parr
004:         All rights reserved.
005:
006:         Redistribution and use in source and binary forms, with or without
007:         modification, are permitted provided that the following conditions
008:         are met:
009:         1. Redistributions of source code must retain the above copyright
010:            notice, this list of conditions and the following disclaimer.
011:         2. Redistributions in binary form must reproduce the above copyright
012:            notice, this list of conditions and the following disclaimer in the
013:            documentation and/or other materials provided with the distribution.
014:         3. The name of the author may not be used to endorse or promote products
015:            derived from this software without specific prior written permission.
016:
017:         THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
018:         IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
019:         OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
020:         IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
021:         INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
022:         NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
023:         DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
024:         THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025:         (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
026:         THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027:         */
028:        package org.antlr.test;
029:
030:        import org.antlr.runtime.CommonToken;
031:        import org.antlr.runtime.Token;
032:        import org.antlr.runtime.tree.CommonTree;
033:        import org.antlr.runtime.tree.CommonTreeNodeStream;
034:        import org.antlr.runtime.tree.Tree;
035:
036:        /** Tests specific to CommonTreeNodeStream */
037:        public class TestCommonTreeNodeStream extends TestTreeNodeStream {
038:            public void testPushPop() throws Exception {
039:                // ^(101 ^(102 103) ^(104 105) ^(106 107) 108 109)
040:                // stream has 9 real + 8 nav nodes
041:                // Sequence of types: 101 DN 102 DN 103 UP 104 DN 105 UP 106 DN 107 UP 108 109 UP
042:                Tree r0 = new CommonTree(new CommonToken(101));
043:                Tree r1 = new CommonTree(new CommonToken(102));
044:                r1.addChild(new CommonTree(new CommonToken(103)));
045:                r0.addChild(r1);
046:                Tree r2 = new CommonTree(new CommonToken(104));
047:                r2.addChild(new CommonTree(new CommonToken(105)));
048:                r0.addChild(r2);
049:                Tree r3 = new CommonTree(new CommonToken(106));
050:                r3.addChild(new CommonTree(new CommonToken(107)));
051:                r0.addChild(r3);
052:                r0.addChild(new CommonTree(new CommonToken(108)));
053:                r0.addChild(new CommonTree(new CommonToken(109)));
054:
055:                CommonTreeNodeStream stream = new CommonTreeNodeStream(r0);
056:                String expecting = " 101 2 102 2 103 3 104 2 105 3 106 2 107 3 108 109 3";
057:                String found = stream.toString();
058:                assertEquals(expecting, found);
059:
060:                // Assume we want to hit node 107 and then "call 102" then return
061:
062:                int indexOf102 = 2;
063:                int indexOf107 = 12;
064:                for (int k = 1; k <= indexOf107; k++) { // consume til 107 node
065:                    stream.consume();
066:                }
067:                // CALL 102
068:                assertEquals(107, ((Tree) stream.LT(1)).getType());
069:                stream.push(indexOf102);
070:                assertEquals(102, ((Tree) stream.LT(1)).getType());
071:                stream.consume(); // consume 102
072:                assertEquals(Token.DOWN, ((Tree) stream.LT(1)).getType());
073:                stream.consume(); // consume DN
074:                assertEquals(103, ((Tree) stream.LT(1)).getType());
075:                stream.consume(); // consume 103
076:                assertEquals(Token.UP, ((Tree) stream.LT(1)).getType());
077:                // RETURN
078:                stream.pop();
079:                assertEquals(107, ((Tree) stream.LT(1)).getType());
080:            }
081:
082:            public void testNestedPushPop() throws Exception {
083:                // ^(101 ^(102 103) ^(104 105) ^(106 107) 108 109)
084:                // stream has 9 real + 8 nav nodes
085:                // Sequence of types: 101 DN 102 DN 103 UP 104 DN 105 UP 106 DN 107 UP 108 109 UP
086:                Tree r0 = new CommonTree(new CommonToken(101));
087:                Tree r1 = new CommonTree(new CommonToken(102));
088:                r1.addChild(new CommonTree(new CommonToken(103)));
089:                r0.addChild(r1);
090:                Tree r2 = new CommonTree(new CommonToken(104));
091:                r2.addChild(new CommonTree(new CommonToken(105)));
092:                r0.addChild(r2);
093:                Tree r3 = new CommonTree(new CommonToken(106));
094:                r3.addChild(new CommonTree(new CommonToken(107)));
095:                r0.addChild(r3);
096:                r0.addChild(new CommonTree(new CommonToken(108)));
097:                r0.addChild(new CommonTree(new CommonToken(109)));
098:
099:                CommonTreeNodeStream stream = new CommonTreeNodeStream(r0);
100:
101:                // Assume we want to hit node 107 and then "call 102", which
102:                // calls 104, then return
103:
104:                int indexOf102 = 2;
105:                int indexOf107 = 12;
106:                for (int k = 1; k <= indexOf107; k++) { // consume til 107 node
107:                    stream.consume();
108:                }
109:                assertEquals(107, ((Tree) stream.LT(1)).getType());
110:                // CALL 102
111:                stream.push(indexOf102);
112:                assertEquals(102, ((Tree) stream.LT(1)).getType());
113:                stream.consume(); // consume 102
114:                assertEquals(Token.DOWN, ((Tree) stream.LT(1)).getType());
115:                stream.consume(); // consume DN
116:                assertEquals(103, ((Tree) stream.LT(1)).getType());
117:                stream.consume(); // consume 103
118:
119:                // CALL 104
120:                int indexOf104 = 6;
121:                stream.push(indexOf104);
122:                assertEquals(104, ((Tree) stream.LT(1)).getType());
123:                stream.consume(); // consume 102
124:                assertEquals(Token.DOWN, ((Tree) stream.LT(1)).getType());
125:                stream.consume(); // consume DN
126:                assertEquals(105, ((Tree) stream.LT(1)).getType());
127:                stream.consume(); // consume 103
128:                assertEquals(Token.UP, ((Tree) stream.LT(1)).getType());
129:                // RETURN (to UP node in 102 subtree)
130:                stream.pop();
131:
132:                assertEquals(Token.UP, ((Tree) stream.LT(1)).getType());
133:                // RETURN (to empty stack)
134:                stream.pop();
135:                assertEquals(107, ((Tree) stream.LT(1)).getType());
136:            }
137:
138:            public void testPushPopFromEOF() throws Exception {
139:                // ^(101 ^(102 103) ^(104 105) ^(106 107) 108 109)
140:                // stream has 9 real + 8 nav nodes
141:                // Sequence of types: 101 DN 102 DN 103 UP 104 DN 105 UP 106 DN 107 UP 108 109 UP
142:                Tree r0 = new CommonTree(new CommonToken(101));
143:                Tree r1 = new CommonTree(new CommonToken(102));
144:                r1.addChild(new CommonTree(new CommonToken(103)));
145:                r0.addChild(r1);
146:                Tree r2 = new CommonTree(new CommonToken(104));
147:                r2.addChild(new CommonTree(new CommonToken(105)));
148:                r0.addChild(r2);
149:                Tree r3 = new CommonTree(new CommonToken(106));
150:                r3.addChild(new CommonTree(new CommonToken(107)));
151:                r0.addChild(r3);
152:                r0.addChild(new CommonTree(new CommonToken(108)));
153:                r0.addChild(new CommonTree(new CommonToken(109)));
154:
155:                CommonTreeNodeStream stream = new CommonTreeNodeStream(r0);
156:
157:                while (stream.LA(1) != Token.EOF) {
158:                    stream.consume();
159:                }
160:                int indexOf102 = 2;
161:                int indexOf104 = 6;
162:                assertEquals(Token.EOF, ((Tree) stream.LT(1)).getType());
163:
164:                // CALL 102
165:                stream.push(indexOf102);
166:                assertEquals(102, ((Tree) stream.LT(1)).getType());
167:                stream.consume(); // consume 102
168:                assertEquals(Token.DOWN, ((Tree) stream.LT(1)).getType());
169:                stream.consume(); // consume DN
170:                assertEquals(103, ((Tree) stream.LT(1)).getType());
171:                stream.consume(); // consume 103
172:                assertEquals(Token.UP, ((Tree) stream.LT(1)).getType());
173:                // RETURN (to empty stack)
174:                stream.pop();
175:                assertEquals(Token.EOF, ((Tree) stream.LT(1)).getType());
176:
177:                // CALL 104
178:                stream.push(indexOf104);
179:                assertEquals(104, ((Tree) stream.LT(1)).getType());
180:                stream.consume(); // consume 102
181:                assertEquals(Token.DOWN, ((Tree) stream.LT(1)).getType());
182:                stream.consume(); // consume DN
183:                assertEquals(105, ((Tree) stream.LT(1)).getType());
184:                stream.consume(); // consume 103
185:                assertEquals(Token.UP, ((Tree) stream.LT(1)).getType());
186:                // RETURN (to empty stack)
187:                stream.pop();
188:                assertEquals(Token.EOF, ((Tree) stream.LT(1)).getType());
189:            }
190:
191:            public void testStackStretch() throws Exception {
192:                // make more than INITIAL_CALL_STACK_SIZE pushes
193:                Tree r0 = new CommonTree(new CommonToken(101));
194:                CommonTreeNodeStream stream = new CommonTreeNodeStream(r0);
195:                // go 1 over initial size
196:                for (int i = 1; i <= CommonTreeNodeStream.INITIAL_CALL_STACK_SIZE + 1; i++) {
197:                    stream.push(i);
198:                }
199:                assertEquals(10, stream.pop());
200:                assertEquals(9, stream.pop());
201:            }
202:
203:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.