Home | History | Annotate | Download | only in test
      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  */
     28 package org.antlr.test;
     29 
     30 import org.antlr.runtime.tree.*;
     31 import org.junit.Test;
     32 
     33 import static org.junit.Assert.assertEquals;
     34 
     35 public class TestTreeIterator {
     36     static final String[] tokens = new String[] {
     37         "<invalid>", "<EOR>", "<DOWN>", "<UP>", "A", "B", "C", "D", "E", "F", "G"
     38     };
     39 
     40     @Test public void testNode() {
     41         TreeAdaptor adaptor = new CommonTreeAdaptor();
     42         TreeWizard wiz = new TreeWizard(adaptor, tokens);
     43         CommonTree t = (CommonTree)wiz.create("A");
     44         TreeIterator it = new TreeIterator(t);
     45         StringBuffer buf = toString(it);
     46         String expecting = "A EOF";
     47         String found = buf.toString();
     48         assertEquals(expecting, found);
     49     }
     50 
     51     @Test public void testFlatAB() {
     52         TreeAdaptor adaptor = new CommonTreeAdaptor();
     53         TreeWizard wiz = new TreeWizard(adaptor, tokens);
     54         CommonTree t = (CommonTree)wiz.create("(nil A B)");
     55         TreeIterator it = new TreeIterator(t);
     56         StringBuffer buf = toString(it);
     57         String expecting = "nil DOWN A B UP EOF";
     58         String found = buf.toString();
     59         assertEquals(expecting, found);
     60     }
     61 
     62     @Test public void testAB() {
     63         TreeAdaptor adaptor = new CommonTreeAdaptor();
     64         TreeWizard wiz = new TreeWizard(adaptor, tokens);
     65         CommonTree t = (CommonTree)wiz.create("(A B)");
     66         TreeIterator it = new TreeIterator(t);
     67         StringBuffer buf = toString(it);
     68         String expecting = "A DOWN B UP EOF";
     69         String found = buf.toString();
     70         assertEquals(expecting, found);
     71     }
     72 
     73     @Test public void testABC() {
     74         TreeAdaptor adaptor = new CommonTreeAdaptor();
     75         TreeWizard wiz = new TreeWizard(adaptor, tokens);
     76         CommonTree t = (CommonTree)wiz.create("(A B C)");
     77         TreeIterator it = new TreeIterator(t);
     78         StringBuffer buf = toString(it);
     79         String expecting = "A DOWN B C UP EOF";
     80         String found = buf.toString();
     81         assertEquals(expecting, found);
     82     }
     83 
     84     @Test public void testVerticalList() {
     85         TreeAdaptor adaptor = new CommonTreeAdaptor();
     86         TreeWizard wiz = new TreeWizard(adaptor, tokens);
     87         CommonTree t = (CommonTree)wiz.create("(A (B C))");
     88         TreeIterator it = new TreeIterator(t);
     89         StringBuffer buf = toString(it);
     90         String expecting = "A DOWN B DOWN C UP UP EOF";
     91         String found = buf.toString();
     92         assertEquals(expecting, found);
     93     }
     94 
     95     @Test public void testComplex() {
     96         TreeAdaptor adaptor = new CommonTreeAdaptor();
     97         TreeWizard wiz = new TreeWizard(adaptor, tokens);
     98         CommonTree t = (CommonTree)wiz.create("(A (B (C D E) F) G)");
     99         TreeIterator it = new TreeIterator(t);
    100         StringBuffer buf = toString(it);
    101         String expecting = "A DOWN B DOWN C DOWN D E UP F UP G UP EOF";
    102         String found = buf.toString();
    103         assertEquals(expecting, found);
    104     }
    105 
    106     @Test public void testReset() {
    107         TreeAdaptor adaptor = new CommonTreeAdaptor();
    108         TreeWizard wiz = new TreeWizard(adaptor, tokens);
    109         CommonTree t = (CommonTree)wiz.create("(A (B (C D E) F) G)");
    110         TreeIterator it = new TreeIterator(t);
    111         StringBuffer buf = toString(it);
    112         String expecting = "A DOWN B DOWN C DOWN D E UP F UP G UP EOF";
    113         String found = buf.toString();
    114         assertEquals(expecting, found);
    115 
    116         it.reset();
    117         buf = toString(it);
    118         expecting = "A DOWN B DOWN C DOWN D E UP F UP G UP EOF";
    119         found = buf.toString();
    120         assertEquals(expecting, found);
    121     }
    122 
    123     protected static StringBuffer toString(TreeIterator it) {
    124         StringBuffer buf = new StringBuffer();
    125         while ( it.hasNext() ) {
    126             CommonTree n = (CommonTree)it.next();
    127             buf.append(n);
    128             if ( it.hasNext() ) buf.append(" ");
    129         }
    130         return buf;
    131     }
    132 }
    133