KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > editor > util > GapListRandomTest


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.lib.editor.util;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Random JavaDoc;
24 import junit.framework.TestCase;
25
26 /**
27  * Random test of GapList correctness.
28  *
29  * @author mmetelka
30  */

31 public class GapListRandomTest extends TestCase {
32
33     private static final boolean debug = false;
34
35     private static final int OP_COUNT_1 = 10000;
36     private static final int ADD_RATIO_1 = 100;
37     private static final int ADD_ALL_RATIO_1 = 10;
38     private static final int ADD_ALL_MAX_COUNT_1 = 10;
39     private static final int REMOVE_RATIO_1 = 100;
40     private static final int REMOVE_RANGE_RATIO_1 = 10;
41     private static final int CLEAR_RATIO_1 = 5;
42     private static final int SET_RATIO_1 = 50;
43     
44     private static final int OP_COUNT_2 = 10000;
45     private static final int ADD_RATIO_2 = 50;
46     private static final int ADD_ALL_RATIO_2 = 20;
47     private static final int ADD_ALL_MAX_COUNT_2 = 5;
48     private static final int REMOVE_RATIO_2 = 100;
49     private static final int REMOVE_RANGE_RATIO_2 = 10;
50     private static final int CLEAR_RATIO_2 = 3;
51     private static final int SET_RATIO_2 = 50;
52     
53     private ArrayList JavaDoc<Object JavaDoc> al;
54     
55     private GapList<Object JavaDoc> gl;
56     
57     public GapListRandomTest(String JavaDoc testName) {
58         super(testName);
59     }
60     
61     public void test() {
62         testFresh(0);
63     }
64
65     public void testFresh(long seed) {
66         Random JavaDoc random = new Random JavaDoc();
67         if (seed != 0) {
68             System.err.println("TESTING with SEED=" + seed);
69             random.setSeed(seed);
70         }
71         
72         gl = new GapList<Object JavaDoc>();
73         al = new ArrayList JavaDoc<Object JavaDoc>();
74         
75         
76         testRound(random, OP_COUNT_1, ADD_RATIO_1, ADD_ALL_RATIO_1, ADD_ALL_MAX_COUNT_1,
77             REMOVE_RATIO_1, REMOVE_RANGE_RATIO_1, CLEAR_RATIO_1, SET_RATIO_1);
78         testRound(random, OP_COUNT_2, ADD_RATIO_2, ADD_ALL_RATIO_2, ADD_ALL_MAX_COUNT_2,
79             REMOVE_RATIO_2, REMOVE_RANGE_RATIO_2, CLEAR_RATIO_2, SET_RATIO_2);
80     }
81     
82     private void testRound(Random JavaDoc random, int opCount,
83     int addRatio, int addAllRatio, int addAllMaxCount,
84     int removeRatio, int removeRangeRatio, int clearRatio, int setRatio) {
85         
86         int ratioSum = addRatio + addAllRatio + removeRatio + removeRangeRatio
87             + clearRatio + setRatio;
88         
89         for (int op = 0; op < opCount; op++) {
90             double r = random.nextDouble() * ratioSum;
91
92             if ((r -= addRatio) < 0) {
93                 Object JavaDoc o = new Object JavaDoc();
94                 int index = (int)(al.size() * random.nextDouble());
95                 al.add(index, o);
96                 if (debug) {
97                     debugOp(op, "add() at index=" + index); // NOI18N
98
}
99                 gl.add(index, o);
100
101             } else if ((r -= addAllRatio) < 0) {
102                 int count = (int)(random.nextDouble() * addAllMaxCount);
103                 ArrayList JavaDoc<Object JavaDoc> l = new ArrayList JavaDoc<Object JavaDoc>();
104                 for (int i = count; i > 0; i--) {
105                     l.add(new Object JavaDoc());
106                 }
107
108                 Object JavaDoc o = new Object JavaDoc();
109                 int index = (int)(al.size() * random.nextDouble());
110                 al.addAll(index, l);
111                 if (debug) {
112                     debugOp(op, "addAll() at index=" + index); // NOI18N
113
}
114                 gl.addAll(index, l);
115
116             } else if ((r -= removeRatio) < 0) {
117                 if (al.size() > 0) { // is anything to remove
118
int index = (int)(al.size() * random.nextDouble());
119                     al.remove(index);
120                     if (debug) {
121                         debugOp(op, "remove() at index=" + index); // NOI18N
122
}
123                     gl.remove(index);
124                 }
125
126             } else if ((r -= removeRangeRatio) < 0) {
127                 if (al.size() > 0) { // is anything to remove
128
int index = (int)(al.size() * random.nextDouble());
129                     int length = (int)((al.size() - index + 1) * random.nextDouble());
130                     for (int count = length; count > 0; count--) {
131                         al.remove(index);
132                     }
133                     if (debug) {
134                         debugOp(op, "remove() at index=" + index + ", length=" + length); // NOI18N
135
}
136                     gl.remove(index, length);
137                 }
138                 
139             } else if ((r -= clearRatio) < 0) {
140                 al.clear();
141                 if (debug) {
142                     debugOp(op, "clear()"); // NOI18N
143
}
144                 gl.clear();
145                 
146             } else if ((r -= setRatio) < 0) {
147                 if (al.size() > 0) { // is anything to remove
148
int index = (int)(al.size() * random.nextDouble());
149                     Object JavaDoc o = new Object JavaDoc();
150                     al.set(index, o);
151                     if (debug) {
152                         debugOp(op, "set() at index=" + index); // NOI18N
153
}
154                     gl.set(index, o);
155                 }
156             }
157
158             checkConsistency();
159         }
160         
161     }
162         
163     private void debugOp(int op, String JavaDoc s) {
164         System.err.println("op: " + op + ", " + s + ", " + gl.dumpInternals());
165     }
166     
167     private void checkConsistency() {
168         gl.consistencyCheck();
169
170         assertEquals(gl.size(), al.size());
171         
172         int size = al.size();
173         for (int i = 0; i < size; i++) {
174             assertTrue("Contents differ at index " + i + ", gl: " + gl.get(i) // NOI18N
175
+ ", al:" + al.get(i), // NOI18N
176
(gl.get(i) == al.get(i)));
177         }
178     }
179     
180     
181 }
182
Popular Tags