KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > lexer > test > simple > SimpleLexerRandomTest


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.test.simple;
21
22 import junit.framework.TestCase;
23 import org.netbeans.lib.lexer.test.FixedTextDescriptor;
24 import org.netbeans.lib.lexer.test.LexerTestUtilities;
25 import org.netbeans.lib.lexer.test.RandomCharDescriptor;
26 import org.netbeans.lib.lexer.test.RandomModifyDescriptor;
27 import org.netbeans.lib.lexer.test.RandomTextProvider;
28 import org.netbeans.lib.lexer.test.TestRandomModify;
29
30 /**
31  * Test several simple lexer impls.
32  *
33  * @author mmetelka
34  */

35 public class SimpleLexerRandomTest extends TestCase {
36
37     public SimpleLexerRandomTest(String JavaDoc testName) {
38         super(testName);
39     }
40     
41     protected void setUp() throws java.lang.Exception JavaDoc {
42         // Set-up testing environment
43
LexerTestUtilities.setTesting(true);
44     }
45
46     protected void tearDown() throws java.lang.Exception JavaDoc {
47     }
48
49     public void testRandom() throws Exception JavaDoc {
50         test(0);
51     }
52     
53     public void testWithSeed_1140557399761L() throws Exception JavaDoc {
54         test(1140557399761L);
55     }
56     
57     private void test(long seed) throws Exception JavaDoc {
58         TestRandomModify randomModify = new TestRandomModify(seed);
59         randomModify.setLanguage(SimpleTokenId.language());
60         
61         //randomModify.setDebugOperation(true);
62
//randomModify.setDebugDocumentText(true);
63
//randomModify.setDebugHierarchy(true);
64

65         // Check for incorrect lookahead counting problem
66
// after one of the larger updates of the LexerInputOperation
67
randomModify.insertText(0, "+--+");
68         randomModify.removeText(2, 1);
69         
70         randomModify.clearDocument();
71
72         // Check that token list updater respects that the lookaheads
73
// of subsequent tokens (already present in the token list)
74
// correspond to the lookahead of the relexed token.
75
// In the following example a "+-+" token must be created.
76
randomModify.insertText(0, "---+");
77         randomModify.insertText(1, "+");
78         randomModify.removeText(3, 1);
79         
80         randomModify.clearDocument();
81
82         // Check that the token list updater checks respects the rule
83
// that the lookahead of the incrementally created tokens
84
// is the same like in a regular batch lexing.
85
randomModify.insertText(0, "+--+--");
86         randomModify.removeText(2, 1);
87         
88         randomModify.clearDocument();
89    
90         // Check for the previous case but this time the relexing would normally
91
// be skipped but this would lead to lookahead 1 for the "-" token
92
// after the removed "+" (while the batch lexing would produce lookahead 0)
93
randomModify.insertText(0, "-+--");
94         randomModify.removeText(1, 1);
95         
96         randomModify.clearDocument();
97         
98         // Similar case to the previous one but with more tokens
99
randomModify.insertText(0, "-+-++--");
100         randomModify.removeText(1, 4);
101         
102         randomModify.clearDocument();
103
104         // Check for the case when token validation cannot be performed
105
// because although the lenghth of the removal is less than
106
// the "+-+" token's length the removal spans token boundaries
107
randomModify.insertText(0, "-+-+ --");
108         randomModify.removeText(3, 2);
109         
110         randomModify.clearDocument();
111
112
113         // Begin really randomized testing
114
FixedTextDescriptor[] fixedTexts = new FixedTextDescriptor[] {
115             FixedTextDescriptor.create("-+--+-+", 0.2),
116             FixedTextDescriptor.create("+-", 0.2),
117             FixedTextDescriptor.create("-+", 0.2),
118         };
119         
120         RandomCharDescriptor[] regularChars = new RandomCharDescriptor[] {
121             RandomCharDescriptor.letter(0.3),
122             RandomCharDescriptor.space(0.3),
123             RandomCharDescriptor.lf(0.3),
124             RandomCharDescriptor.chars(new char[] { '+', '-', '*', '/'}, 0.3),
125         };
126
127         RandomCharDescriptor[] plusMinusChars = new RandomCharDescriptor[] {
128             RandomCharDescriptor.chars(new char[] { '+', '-' }, 0.3),
129 // RandomCharDescriptor.chars(new char[] { '*', '/' }, 0.1),
130
RandomCharDescriptor.space(0.1),
131         };
132
133         RandomTextProvider regularTextProvider = new RandomTextProvider(regularChars, fixedTexts);
134         RandomTextProvider plusMinusTextProvider = new RandomTextProvider(plusMinusChars, fixedTexts);
135         
136         RandomTextProvider textProvider = plusMinusTextProvider;
137
138         randomModify.test(
139             new RandomModifyDescriptor[] {
140                 new RandomModifyDescriptor(200, plusMinusTextProvider,
141                         0.2, 0.2, 0.1,
142                         0.2, 0.2,
143                         0.0, 0.0), // snapshots create/destroy
144
}
145         );
146
147         randomModify.test(
148             new RandomModifyDescriptor[] {
149                 new RandomModifyDescriptor(200, regularTextProvider,
150                         0.4, 0.2, 0.2,
151                         0.1, 0.1,
152                         0.0, 0.0), // snapshots create/destroy
153
new RandomModifyDescriptor(200, regularTextProvider,
154                         0.2, 0.2, 0.1,
155                         0.4, 0.3,
156                         0.0, 0.0), // snapshots create/destroy
157
}
158         );
159     }
160     
161 }
162
Popular Tags