KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > lexer > LAStateTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.lib.lexer;
21
22 import java.util.ArrayList JavaDoc;
23 import junit.framework.*;
24 import java.util.List JavaDoc;
25
26 /**
27  *
28  * @author mmetelka
29  */

30 public class LAStateTest extends TestCase {
31
32     public LAStateTest(String JavaDoc testName) {
33         super(testName);
34     }
35
36     protected void setUp() throws Exception JavaDoc {
37     }
38
39     protected void tearDown() throws Exception JavaDoc {
40     }
41
42     public void testLAStateClassTypes() throws Exception JavaDoc {
43         LAState laState = LAState.empty();
44         laState = laState.add(1, null); // should remain NoState
45
assertEquals(laState.getClass(), LAState.NoState.class);
46
47         laState = LAState.empty();
48         laState = laState.add(127, null);
49         assertEquals(laState.getClass(), LAState.NoState.class);
50
51         laState = LAState.empty();
52         laState = laState.add(127, new Integer JavaDoc(127));
53         assertEquals(laState.getClass(), LAState.ByteState.class);
54
55         laState = LAState.empty();
56         laState = laState.add(128, null);
57         assertEquals(laState.getClass(), LAState.LargeState.class);
58
59         laState = LAState.empty();
60         laState = laState.add(0, new Object JavaDoc());
61         assertEquals(laState.getClass(), LAState.LargeState.class);
62     }
63
64     public void testLAState() {
65         List JavaDoc<Object JavaDoc> expected = new ArrayList JavaDoc<Object JavaDoc>();
66         LAState laState = LAState.empty();
67         laState = add(expected, laState, 0, null);
68         laState = add(expected, laState, 1, null);
69         laState = add(expected, laState, 0, new Object JavaDoc());
70         laState = add(expected, laState, 127, null);
71         laState = add(expected, laState, 127, new Integer JavaDoc(-1));
72         remove(expected, laState, 1, 3);
73
74         List JavaDoc<Object JavaDoc> expectedInner = expected;
75         LAState laStateInner = laState;
76
77         expected = new ArrayList JavaDoc<Object JavaDoc>();
78         laState = laState.empty();
79         laState = add(expected, laState, 1, null);
80         laState = add(expected, laState, 7, null);
81         laState = add(expected, laState, 5, null);
82         laState = addAll(expected, laState, 1, expectedInner, laStateInner);
83         laState = addAll(expected, laState, laState.size(), expectedInner, laStateInner);
84         remove(expected, laState, 4, 3);
85         laState = addAll(expected, laState, 0, expectedInner, laStateInner);
86     }
87
88     private static LAState add(List JavaDoc<Object JavaDoc> expectedLAState, LAState laState, int lookahead, Object JavaDoc state) {
89         expectedLAState.add(Integer.valueOf(lookahead));
90         expectedLAState.add(state);
91         laState = laState.add(lookahead, state);
92         consistencyCheck(expectedLAState, laState);
93         return laState;
94     }
95
96     private static LAState addAll(List JavaDoc<Object JavaDoc> expectedLAState, LAState laState, int index,
97     List JavaDoc<Object JavaDoc> expectedLAStateToAdd, LAState laStateToAdd) {
98         expectedLAState.addAll(index << 1, expectedLAStateToAdd);
99         laState = laState.addAll(index, laStateToAdd);
100         consistencyCheck(expectedLAState, laState);
101         return laState;
102     }
103
104     private static void remove(List JavaDoc<Object JavaDoc> expectedLAState, LAState laState, int index, int count) {
105         for (int i = count << 1; i > 0; i--) {
106             expectedLAState.remove(index << 1);
107         }
108         laState.remove(index, count);
109         consistencyCheck(expectedLAState, laState);
110     }
111
112     private static void consistencyCheck(List JavaDoc<Object JavaDoc> expectedLAState, LAState laState) {
113         // Ensure the test laState class is equal to expected one
114
assertEquals(expectedLAState.size() & 1, 0);
115         assertEquals("Invalid size", expectedLAState.size() >> 1, laState.size());
116         for (int i = 0; i < expectedLAState.size(); i++) {
117             assertEquals("Invalid lookahead", ((Integer JavaDoc)expectedLAState.get(i)).intValue(), laState.lookahead(i >> 1));
118             i++;
119             assertEquals("Invalid state", expectedLAState.get(i), laState.state(i >> 1));
120         }
121     }
122
123 }
124
Popular Tags