TestBufferedTreeNodeStream.java revision 324c4644fee44b9898524c09511bd33c3f12e2df
1/*
2 * [The "BSD license"]
3 *  Copyright (c) 2010 Terence Parr
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 the
13 *      documentation and/or other materials provided with the distribution.
14 *  3. The name of the author may not be used to endorse or promote products
15 *      derived from this software without specific prior written permission.
16 *
17 *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28package org.antlr.test;
29
30import org.antlr.runtime.CommonToken;
31import org.antlr.runtime.tree.BufferedTreeNodeStream;
32import org.antlr.runtime.tree.CommonTree;
33import org.antlr.runtime.tree.Tree;
34import org.antlr.runtime.tree.TreeNodeStream;
35import org.junit.Test;
36
37public class TestBufferedTreeNodeStream extends TestTreeNodeStream {
38    // inherits tests; these methods make it use a new buffer
39
40	public TreeNodeStream newStream(Object t) {
41		return new BufferedTreeNodeStream(t);
42	}
43
44    public String toTokenTypeString(TreeNodeStream stream) {
45        return ((BufferedTreeNodeStream)stream).toTokenTypeString();
46    }
47
48    @Test public void testSeek() throws Exception {
49        // ^(101 ^(102 103 ^(106 107) ) 104 105)
50        // stream has 7 real + 6 nav nodes
51        // Sequence of types: 101 DN 102 DN 103 106 DN 107 UP UP 104 105 UP EOF
52        Tree r0 = new CommonTree(new CommonToken(101));
53        Tree r1 = new CommonTree(new CommonToken(102));
54        r0.addChild(r1);
55        r1.addChild(new CommonTree(new CommonToken(103)));
56        Tree r2 = new CommonTree(new CommonToken(106));
57        r2.addChild(new CommonTree(new CommonToken(107)));
58        r1.addChild(r2);
59        r0.addChild(new CommonTree(new CommonToken(104)));
60        r0.addChild(new CommonTree(new CommonToken(105)));
61
62        TreeNodeStream stream = newStream(r0);
63        stream.consume(); // consume 101
64        stream.consume(); // consume DN
65        stream.consume(); // consume 102
66        stream.seek(7);   // seek to 107
67        assertEquals(107, ((Tree)stream.LT(1)).getType());
68        stream.consume(); // consume 107
69        stream.consume(); // consume UP
70        stream.consume(); // consume UP
71        assertEquals(104, ((Tree)stream.LT(1)).getType());
72    }
73}
74