001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.lib.lexer;
043:
044: import java.util.ArrayList;
045: import junit.framework.*;
046: import java.util.List;
047:
048: /**
049: *
050: * @author mmetelka
051: */
052: public class LAStateTest extends TestCase {
053:
054: public LAStateTest(String testName) {
055: super (testName);
056: }
057:
058: protected void setUp() throws Exception {
059: }
060:
061: protected void tearDown() throws Exception {
062: }
063:
064: public void testLAStateClassTypes() throws Exception {
065: LAState laState = LAState.empty();
066: laState = laState.add(1, null); // should remain NoState
067: assertEquals(laState.getClass(), LAState.NoState.class);
068:
069: laState = LAState.empty();
070: laState = laState.add(127, null);
071: assertEquals(laState.getClass(), LAState.NoState.class);
072:
073: laState = LAState.empty();
074: laState = laState.add(127, new Integer(127));
075: assertEquals(laState.getClass(), LAState.ByteState.class);
076:
077: laState = LAState.empty();
078: laState = laState.add(128, null);
079: assertEquals(laState.getClass(), LAState.LargeState.class);
080:
081: laState = LAState.empty();
082: laState = laState.add(0, new Object());
083: assertEquals(laState.getClass(), LAState.LargeState.class);
084: }
085:
086: public void testLAState() {
087: List<Object> expected = new ArrayList<Object>();
088: LAState laState = LAState.empty();
089: laState = add(expected, laState, 0, null);
090: laState = add(expected, laState, 1, null);
091: laState = add(expected, laState, 0, new Object());
092: laState = add(expected, laState, 127, null);
093: laState = add(expected, laState, 127, new Integer(-1));
094: remove(expected, laState, 1, 3);
095:
096: List<Object> expectedInner = expected;
097: LAState laStateInner = laState;
098:
099: expected = new ArrayList<Object>();
100: laState = laState.empty();
101: laState = add(expected, laState, 1, null);
102: laState = add(expected, laState, 7, null);
103: laState = add(expected, laState, 5, null);
104: laState = addAll(expected, laState, 1, expectedInner,
105: laStateInner);
106: laState = addAll(expected, laState, laState.size(),
107: expectedInner, laStateInner);
108: remove(expected, laState, 4, 3);
109: laState = addAll(expected, laState, 0, expectedInner,
110: laStateInner);
111: }
112:
113: private static LAState add(List<Object> expectedLAState,
114: LAState laState, int lookahead, Object state) {
115: expectedLAState.add(Integer.valueOf(lookahead));
116: expectedLAState.add(state);
117: laState = laState.add(lookahead, state);
118: consistencyCheck(expectedLAState, laState);
119: return laState;
120: }
121:
122: private static LAState addAll(List<Object> expectedLAState,
123: LAState laState, int index,
124: List<Object> expectedLAStateToAdd, LAState laStateToAdd) {
125: expectedLAState.addAll(index << 1, expectedLAStateToAdd);
126: laState = laState.addAll(index, laStateToAdd);
127: consistencyCheck(expectedLAState, laState);
128: return laState;
129: }
130:
131: private static void remove(List<Object> expectedLAState,
132: LAState laState, int index, int count) {
133: for (int i = count << 1; i > 0; i--) {
134: expectedLAState.remove(index << 1);
135: }
136: laState.remove(index, count);
137: consistencyCheck(expectedLAState, laState);
138: }
139:
140: private static void consistencyCheck(List<Object> expectedLAState,
141: LAState laState) {
142: // Ensure the test laState class is equal to expected one
143: assertEquals(expectedLAState.size() & 1, 0);
144: assertEquals("Invalid size", expectedLAState.size() >> 1,
145: laState.size());
146: for (int i = 0; i < expectedLAState.size(); i++) {
147: assertEquals("Invalid lookahead",
148: ((Integer) expectedLAState.get(i)).intValue(),
149: laState.lookahead(i >> 1));
150: i++;
151: assertEquals("Invalid state", expectedLAState.get(i),
152: laState.state(i >> 1));
153: }
154: }
155:
156: }
|