1 19 20 package org.netbeans.lib.lexer; 21 22 import java.util.ArrayList ; 23 import junit.framework.*; 24 import java.util.List ; 25 26 30 public class LAStateTest extends TestCase { 31 32 public LAStateTest(String testName) { 33 super(testName); 34 } 35 36 protected void setUp() throws Exception { 37 } 38 39 protected void tearDown() throws Exception { 40 } 41 42 public void testLAStateClassTypes() throws Exception { 43 LAState laState = LAState.empty(); 44 laState = laState.add(1, null); 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 (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 ()); 61 assertEquals(laState.getClass(), LAState.LargeState.class); 62 } 63 64 public void testLAState() { 65 List <Object > expected = new ArrayList <Object >(); 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 ()); 70 laState = add(expected, laState, 127, null); 71 laState = add(expected, laState, 127, new Integer (-1)); 72 remove(expected, laState, 1, 3); 73 74 List <Object > expectedInner = expected; 75 LAState laStateInner = laState; 76 77 expected = new ArrayList <Object >(); 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 <Object > expectedLAState, LAState laState, int lookahead, Object 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 <Object > expectedLAState, LAState laState, int index, 97 List <Object > 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 <Object > 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 <Object > expectedLAState, LAState laState) { 113 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 )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 |