1 19 20 package org.netbeans.lib.editor.util; 21 22 import java.util.ArrayList ; 23 import java.util.Random ; 24 import junit.framework.TestCase; 25 26 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 <Object > al; 54 55 private GapList<Object > gl; 56 57 public GapListRandomTest(String testName) { 58 super(testName); 59 } 60 61 public void test() { 62 testFresh(0); 63 } 64 65 public void testFresh(long seed) { 66 Random random = new Random (); 67 if (seed != 0) { 68 System.err.println("TESTING with SEED=" + seed); 69 random.setSeed(seed); 70 } 71 72 gl = new GapList<Object >(); 73 al = new ArrayList <Object >(); 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 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 o = new Object (); 94 int index = (int)(al.size() * random.nextDouble()); 95 al.add(index, o); 96 if (debug) { 97 debugOp(op, "add() at index=" + index); } 99 gl.add(index, o); 100 101 } else if ((r -= addAllRatio) < 0) { 102 int count = (int)(random.nextDouble() * addAllMaxCount); 103 ArrayList <Object > l = new ArrayList <Object >(); 104 for (int i = count; i > 0; i--) { 105 l.add(new Object ()); 106 } 107 108 Object o = new Object (); 109 int index = (int)(al.size() * random.nextDouble()); 110 al.addAll(index, l); 111 if (debug) { 112 debugOp(op, "addAll() at index=" + index); } 114 gl.addAll(index, l); 115 116 } else if ((r -= removeRatio) < 0) { 117 if (al.size() > 0) { int index = (int)(al.size() * random.nextDouble()); 119 al.remove(index); 120 if (debug) { 121 debugOp(op, "remove() at index=" + index); } 123 gl.remove(index); 124 } 125 126 } else if ((r -= removeRangeRatio) < 0) { 127 if (al.size() > 0) { 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); } 136 gl.remove(index, length); 137 } 138 139 } else if ((r -= clearRatio) < 0) { 140 al.clear(); 141 if (debug) { 142 debugOp(op, "clear()"); } 144 gl.clear(); 145 146 } else if ((r -= setRatio) < 0) { 147 if (al.size() > 0) { int index = (int)(al.size() * random.nextDouble()); 149 Object o = new Object (); 150 al.set(index, o); 151 if (debug) { 152 debugOp(op, "set() at index=" + index); } 154 gl.set(index, o); 155 } 156 } 157 158 checkConsistency(); 159 } 160 161 } 162 163 private void debugOp(int op, String 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) + ", al:" + al.get(i), (gl.get(i) == al.get(i))); 177 } 178 } 179 180 181 } 182 | Popular Tags |