PageRenderTime 23ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/netbeans-7.3/lexer/test/unit/src/org/netbeans/lib/lexer/LAStateTest.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 148 lines | 81 code | 19 blank | 48 comment | 2 complexity | aadca267072dd4ffc6ae6ebd6f5d7d6b MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7. * Other names may be trademarks of their respective owners.
  8. *
  9. * The contents of this file are subject to the terms of either the GNU
  10. * General Public License Version 2 only ("GPL") or the Common
  11. * Development and Distribution License("CDDL") (collectively, the
  12. * "License"). You may not use this file except in compliance with the
  13. * License. You can obtain a copy of the License at
  14. * http://www.netbeans.org/cddl-gplv2.html
  15. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  16. * specific language governing permissions and limitations under the
  17. * License. When distributing the software, include this License Header
  18. * Notice in each file and include the License file at
  19. * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
  20. * particular file as subject to the "Classpath" exception as provided
  21. * by Oracle in the GPL Version 2 section of the License file that
  22. * accompanied this code. If applicable, add the following below the
  23. * License Header, with the fields enclosed by brackets [] replaced by
  24. * your own identifying information:
  25. * "Portions Copyrighted [year] [name of copyright owner]"
  26. *
  27. * Contributor(s):
  28. *
  29. * The Original Software is NetBeans. The Initial Developer of the Original
  30. * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
  31. * Microsystems, Inc. All Rights Reserved.
  32. *
  33. * If you wish your version of this file to be governed by only the CDDL
  34. * or only the GPL Version 2, indicate your decision by adding
  35. * "[Contributor] elects to include this software in this distribution
  36. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  37. * single choice of license, a recipient has the option to distribute
  38. * your version of this file under either the CDDL, the GPL Version 2 or
  39. * to extend the choice of license to its licensees as provided above.
  40. * However, if you add GPL Version 2 code and therefore, elected the GPL
  41. * Version 2 license, then the option applies only if the new code is
  42. * made subject to such option by the copyright holder.
  43. */
  44. package org.netbeans.lib.lexer;
  45. import java.util.ArrayList;
  46. import junit.framework.*;
  47. import java.util.List;
  48. /**
  49. *
  50. * @author mmetelka
  51. */
  52. public class LAStateTest extends TestCase {
  53. public LAStateTest(String testName) {
  54. super(testName);
  55. }
  56. protected void setUp() throws Exception {
  57. }
  58. protected void tearDown() throws Exception {
  59. }
  60. public void testLAStateClassTypes() throws Exception {
  61. LAState laState = LAState.empty();
  62. laState = laState.add(1, null); // should remain NoState
  63. assertEquals(laState.getClass(), LAState.NoState.class);
  64. laState = LAState.empty();
  65. laState = laState.add(127, null);
  66. assertEquals(laState.getClass(), LAState.NoState.class);
  67. laState = LAState.empty();
  68. laState = laState.add(127, new Integer(127));
  69. assertEquals(laState.getClass(), LAState.ByteState.class);
  70. laState = LAState.empty();
  71. laState = laState.add(128, null);
  72. assertEquals(laState.getClass(), LAState.LargeState.class);
  73. laState = LAState.empty();
  74. laState = laState.add(0, new Object());
  75. assertEquals(laState.getClass(), LAState.LargeState.class);
  76. }
  77. public void testLAState() {
  78. List<Object> expected = new ArrayList<Object>();
  79. LAState laState = LAState.empty();
  80. laState = add(expected, laState, 0, null);
  81. laState = add(expected, laState, 1, null);
  82. laState = add(expected, laState, 0, new Object());
  83. laState = add(expected, laState, 127, null);
  84. laState = add(expected, laState, 127, new Integer(-1));
  85. remove(expected, laState, 1, 3);
  86. List<Object> expectedInner = expected;
  87. LAState laStateInner = laState;
  88. expected = new ArrayList<Object>();
  89. laState = laState.empty();
  90. laState = add(expected, laState, 1, null);
  91. laState = add(expected, laState, 7, null);
  92. laState = add(expected, laState, 5, null);
  93. laState = addAll(expected, laState, 1, expectedInner, laStateInner);
  94. laState = addAll(expected, laState, laState.size(), expectedInner, laStateInner);
  95. remove(expected, laState, 4, 3);
  96. laState = addAll(expected, laState, 0, expectedInner, laStateInner);
  97. }
  98. private static LAState add(List<Object> expectedLAState, LAState laState, int lookahead, Object state) {
  99. expectedLAState.add(Integer.valueOf(lookahead));
  100. expectedLAState.add(state);
  101. laState = laState.add(lookahead, state);
  102. consistencyCheck(expectedLAState, laState);
  103. return laState;
  104. }
  105. private static LAState addAll(List<Object> expectedLAState, LAState laState, int index,
  106. List<Object> expectedLAStateToAdd, LAState laStateToAdd) {
  107. expectedLAState.addAll(index << 1, expectedLAStateToAdd);
  108. laState = laState.addAll(index, laStateToAdd);
  109. consistencyCheck(expectedLAState, laState);
  110. return laState;
  111. }
  112. private static void remove(List<Object> expectedLAState, LAState laState, int index, int count) {
  113. for (int i = count << 1; i > 0; i--) {
  114. expectedLAState.remove(index << 1);
  115. }
  116. laState.remove(index, count);
  117. consistencyCheck(expectedLAState, laState);
  118. }
  119. private static void consistencyCheck(List<Object> expectedLAState, LAState laState) {
  120. // Ensure the test laState class is equal to expected one
  121. assertEquals(expectedLAState.size() & 1, 0);
  122. assertEquals("Invalid size", expectedLAState.size() >> 1, laState.size());
  123. for (int i = 0; i < expectedLAState.size(); i++) {
  124. assertEquals("Invalid lookahead", ((Integer)expectedLAState.get(i)).intValue(), laState.lookahead(i >> 1));
  125. i++;
  126. assertEquals("Invalid state", expectedLAState.get(i), laState.state(i >> 1));
  127. }
  128. }
  129. }