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.tree.*;
031: import org.antlr.runtime.CommonToken;
032: import org.antlr.runtime.Token;
033:
034: /**
035: * Created by IntelliJ IDEA.
036: * User: parrt
037: * Date: Dec 22, 2006
038: * Time: 11:47:55 AM
039: * To change this template use File | Settings | File Templates.
040: */
041: public class TestUnBufferedTreeNodeStream extends TestTreeNodeStream {
042:
043: public TreeNodeStream newStream(Object t) {
044: return new UnBufferedTreeNodeStream(t);
045: }
046:
047: public void testBufferOverflow() throws Exception {
048: StringBuffer buf = new StringBuffer();
049: StringBuffer buf2 = new StringBuffer();
050: // make ^(101 102 ... n)
051: Tree t = new CommonTree(new CommonToken(101));
052: buf.append(" 101");
053: buf2.append(" 101");
054: buf2.append(" ");
055: buf2.append(Token.DOWN);
056: for (int i = 0; i <= UnBufferedTreeNodeStream.INITIAL_LOOKAHEAD_BUFFER_SIZE + 10; i++) {
057: t.addChild(new CommonTree(new CommonToken(102 + i)));
058: buf.append(" ");
059: buf.append(102 + i);
060: buf2.append(" ");
061: buf2.append(102 + i);
062: }
063: buf2.append(" ");
064: buf2.append(Token.UP);
065:
066: TreeNodeStream stream = newStream(t);
067: String expecting = buf.toString();
068: String found = toNodesOnlyString(stream);
069: assertEquals(expecting, found);
070:
071: expecting = buf2.toString();
072: found = stream.toString();
073: assertEquals(expecting, found);
074: }
075:
076: /** Test what happens when tail hits the end of the buffer, but there
077: * is more room left. Specifically that would mean that head is not
078: * at 0 but has advanced somewhere to the middle of the lookahead
079: * buffer.
080: *
081: * Use consume() to advance N nodes into lookahead. Then use LT()
082: * to load at least INITIAL_LOOKAHEAD_BUFFER_SIZE-N nodes so the
083: * buffer has to wrap.
084: */
085: public void testBufferWrap() throws Exception {
086: int N = 10;
087: // make tree with types: 1 2 ... INITIAL_LOOKAHEAD_BUFFER_SIZE+N
088: Tree t = new CommonTree((Token) null);
089: for (int i = 0; i < UnBufferedTreeNodeStream.INITIAL_LOOKAHEAD_BUFFER_SIZE
090: + N; i++) {
091: t.addChild(new CommonTree(new CommonToken(i + 1)));
092: }
093:
094: // move head to index N
095: TreeNodeStream stream = newStream(t);
096: for (int i = 1; i <= N; i++) { // consume N
097: Tree node = (Tree) stream.LT(1);
098: assertEquals(i, node.getType());
099: stream.consume();
100: }
101:
102: // now use LT to lookahead past end of buffer
103: int remaining = UnBufferedTreeNodeStream.INITIAL_LOOKAHEAD_BUFFER_SIZE
104: - N;
105: int wrapBy = 4; // wrap around by 4 nodes
106: assertTrue("bad test code; wrapBy must be less than N",
107: wrapBy < N);
108: for (int i = 1; i <= remaining + wrapBy; i++) { // wrap past end of buffer
109: Tree node = (Tree) stream.LT(i); // look ahead to ith token
110: assertEquals(N + i, node.getType());
111: }
112: }
113:
114: }
|