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-2006 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.modules.cnd.lexer;
043:
044: import java.io.File;
045: import java.io.FileReader;
046: import java.nio.CharBuffer;
047: import java.util.ArrayList;
048: import java.util.List;
049: import org.netbeans.api.lexer.LanguagePath;
050: import org.netbeans.cnd.api.lexer.CppTokenId;
051: import org.netbeans.api.lexer.TokenHierarchy;
052: import org.netbeans.api.lexer.TokenSequence;
053: import org.netbeans.junit.NbTestCase;
054: import org.netbeans.lib.lexer.test.LexerTestUtilities;
055:
056: /**
057: * Test how many flyweight tokens gets created over a typical system headers
058: * (copy of <istream> and <stdio.h> are used).
059: *
060: * @author Vladimir Voskresensky
061: */
062: public class CppFlyTokensTestCase extends NbTestCase {
063:
064: public CppFlyTokensTestCase(String testName) {
065: super (testName);
066: }
067:
068: @Override
069: protected void setUp() throws java.lang.Exception {
070: // Set-up testing environment
071: LexerTestUtilities.setTesting(true);
072: }
073:
074: @Override
075: protected void tearDown() throws java.lang.Exception {
076: }
077:
078: public void testHCpp() throws Exception {
079: File testJComponentFile = new File(getDataDir()
080: + "/testfiles/istream.txt");
081: FileReader r = new FileReader(testJComponentFile);
082: int fileLen = (int) testJComponentFile.length();
083: CharBuffer cb = CharBuffer.allocate(fileLen);
084: r.read(cb);
085: cb.rewind();
086: String text = cb.toString();
087: TokenHierarchy<?> hi = TokenHierarchy.create(text, CppTokenId
088: .languageCpp());
089: TokenSequence<?> ts = hi.tokenSequence();
090:
091: System.err.println("Flyweight tokens: "
092: + LexerTestUtilities.flyweightTokenCount(ts)
093: + "\nTotal tokens: " + ts.tokenCount()
094: + "\nFlyweight text length: "
095: + LexerTestUtilities.flyweightTextLength(ts)
096: + "\nTotal text length: " + fileLen
097: + "\nDistribution: "
098: + LexerTestUtilities.flyweightDistribution(ts));
099: assertEquals(1606, LexerTestUtilities.flyweightTokenCount(ts));
100: assertEquals(2822, LexerTestUtilities.flyweightTextLength(ts));
101: assertEquals(2485, ts.tokenCount());
102: }
103:
104: public void testHC() throws Exception {
105: File testJComponentFile = new File(getDataDir()
106: + "/testfiles/stdio.h.txt");
107: FileReader r = new FileReader(testJComponentFile);
108: int fileLen = (int) testJComponentFile.length();
109: CharBuffer cb = CharBuffer.allocate(fileLen);
110: r.read(cb);
111: cb.rewind();
112: String text = cb.toString();
113: TokenHierarchy<?> hi = TokenHierarchy.create(text, CppTokenId
114: .languageC());
115: TokenSequence<?> ts = hi.tokenSequence();
116:
117: System.err.println("Flyweight tokens: "
118: + LexerTestUtilities.flyweightTokenCount(ts)
119: + "\nTotal tokens: " + ts.tokenCount()
120: + "\nFlyweight text length: "
121: + LexerTestUtilities.flyweightTextLength(ts)
122: + "\nTotal text length: " + fileLen
123: + "\nDistribution: "
124: + LexerTestUtilities.flyweightDistribution(ts));
125:
126: assertEquals(2402, LexerTestUtilities.flyweightTokenCount(ts));
127: assertEquals(3508, LexerTestUtilities.flyweightTextLength(ts));
128: assertEquals(3577, ts.tokenCount());
129: }
130:
131: public void testC() throws Exception {
132: File testJComponentFile = new File(getDataDir()
133: + "/testfiles/istream.txt");
134: FileReader r = new FileReader(testJComponentFile);
135: int fileLen = (int) testJComponentFile.length();
136: CharBuffer cb = CharBuffer.allocate(fileLen);
137: r.read(cb);
138: cb.rewind();
139: String text = cb.toString();
140: TokenHierarchy<?> hi = TokenHierarchy.create(text, CppTokenId
141: .languageCpp());
142:
143: LanguagePath path = LanguagePath.get(CppTokenId.languageCpp());
144: path = LanguagePath.get(path, CppTokenId.languagePreproc());
145: List<TokenSequence<?>> tsList = hi.tokenSequenceList(path, 0,
146: Integer.MAX_VALUE);
147: int fwTokenCount = 0;
148: int tokenCount = 0;
149: int fwTextLength = 0;
150: List<Integer> distribution = new ArrayList<Integer>();
151: for (TokenSequence<?> ts : tsList) {
152: List<Integer> tsDistribution = LexerTestUtilities
153: .flyweightDistribution(ts);
154: System.err.println("Flyweight tokens: "
155: + LexerTestUtilities.flyweightTokenCount(ts)
156: + "\nTotal tokens: " + ts.tokenCount()
157: + "\nFlyweight text length: "
158: + LexerTestUtilities.flyweightTextLength(ts)
159: + "\nDistribution: " + tsDistribution);
160: fwTokenCount += LexerTestUtilities.flyweightTokenCount(ts);
161: tokenCount += ts.tokenCount();
162: fwTextLength += LexerTestUtilities.flyweightTextLength(ts);
163: for (int i = 0; i < tsDistribution.size(); i++) {
164: while (distribution.size() <= i) {
165: distribution.add(0);
166: }
167: distribution.set(i, distribution.get(i)
168: + tsDistribution.get(i));
169: }
170:
171: }
172:
173: System.err.println("Flyweight tokens: " + fwTokenCount
174: + "\nTotal tokens: " + tokenCount
175: + "\nFlyweight text length: " + fwTextLength
176: + "\nDistribution: " + distribution);
177: assertEquals(52, fwTokenCount);
178: assertEquals(66, tokenCount);
179: assertEquals(114, fwTextLength);
180:
181: }
182: }
|