1 4 package com.tctest; 5 6 import org.apache.commons.lang.ArrayUtils; 7 8 import EDU.oswego.cs.dl.util.concurrent.Latch; 9 10 import com.tc.object.config.ConfigVisitor; 11 import com.tc.object.config.DSOClientConfigHelper; 12 import com.tc.object.config.TransparencyClassSpec; 13 import com.tc.simulator.app.ApplicationConfig; 14 import com.tc.simulator.listener.ListenerProvider; 15 import com.tc.util.Assert; 16 import com.tctest.runner.AbstractTransparentApp; 17 18 import java.util.ArrayList ; 19 import java.util.Arrays ; 20 import java.util.List ; 21 22 25 public class JavaUtilArraysTestApp extends AbstractTransparentApp { 26 27 private final List data = new ArrayList (); 28 private final Latch latch; 29 30 public JavaUtilArraysTestApp(String appId, ApplicationConfig cfg, ListenerProvider listenerProvider) { 31 super(appId, cfg, listenerProvider); 32 if (getParticipantCount() != 2) { throw new RuntimeException ("invalid participant count " + getParticipantCount()); } 33 this.latch = new Latch(); 34 } 35 36 public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) { 37 String testClass = JavaUtilArraysTestApp.class.getName(); 38 TransparencyClassSpec spec = config.getOrCreateSpec(testClass); 39 String methodExpression = "* " + testClass + "*.*(..)"; 40 config.addWriteAutolock(methodExpression); 41 spec.addRoot("latch", "latch"); 42 spec.addRoot("data", "data"); 43 config.addIncludePattern(Data.class.getName()); 44 String latchClassName = Latch.class.getName(); 45 config.addIncludePattern(latchClassName); 46 } 48 49 public void run() { 50 final boolean firstNode; 51 52 synchronized (data) { 53 firstNode = data.size() == 0; 54 if (firstNode) { 55 data.add("temp"); 56 } 57 } 58 59 if (!firstNode) { 60 try { 61 synchronized (latch) { 62 latch.acquire(); 63 } 64 } catch (InterruptedException e) { 65 throw new RuntimeException (e); 66 } 67 validateData(); 68 } else { 69 setupData(); 70 synchronized (latch) { 71 latch.release(); 72 } 73 } 74 } 75 76 private String expected(Object theWayItShouldBe, Object theWayItIs) { 77 return "expected " + ArrayUtils.toString(theWayItShouldBe) + ", but found: " + ArrayUtils.toString(theWayItIs); 78 } 79 80 private void validateData() { 81 Data d; 82 83 synchronized (data) { 84 d = (Data) data.remove(0); 85 } 86 87 synchronized (d) { 88 byte[] byteFillCompare = new byte[10]; 89 Arrays.fill(byteFillCompare, (byte) 1); 90 if (!Arrays.equals(byteFillCompare, d.filledByte)) { 91 throw new RuntimeException ("filled bytes " + expected(byteFillCompare, d.filledByte)); 93 } 94 byte[] sortedByte = new byte[] { Byte.MIN_VALUE, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, Byte.MAX_VALUE }; 95 if (!Arrays.equals(sortedByte, d.sortedByte)) { 96 throw new RuntimeException ("sorted bytes: " + expected(sortedByte, d.sortedByte)); 98 } 99 100 char[] charFillCompare = new char[10]; 101 Arrays.fill(charFillCompare, 'z'); 102 if (!Arrays.equals(charFillCompare, d.filledChar)) { 103 throw new RuntimeException ("filled chars: " + expected(charFillCompare, d.filledChar)); 105 } 106 char[] sortedChar = new char[] { Character.MIN_VALUE, 'c', 'e', 'f', 'k', 'm', 'u', Character.MAX_VALUE }; 107 if (!Arrays.equals(sortedChar, d.sortedChar)) { 108 throw new RuntimeException ("sorted chars: " + expected(sortedChar, d.sortedChar)); 110 } 111 112 double[] doubleFillCompare = new double[10]; 113 Arrays.fill(doubleFillCompare, Math.PI); 114 if (!Arrays.equals(doubleFillCompare, d.filledDouble)) { 115 throw new RuntimeException ("filled doubles: " + expected(doubleFillCompare, d.filledDouble)); 117 } 118 double[] sortedDouble = new double[] { -Double.MAX_VALUE, -1.43D, -Double.MIN_VALUE, 0D, Double.MIN_VALUE, 119 Math.E, Math.PI, Double.MAX_VALUE }; 120 if (!Arrays.equals(sortedDouble, d.sortedDouble)) { 121 throw new RuntimeException ("sorted doubles: " + expected(sortedDouble, d.sortedDouble)); 123 } 124 125 float[] floatFillCompare = new float[10]; 126 Arrays.fill(floatFillCompare, 8.1F); 127 if (!Arrays.equals(floatFillCompare, d.filledFloat)) { 128 throw new RuntimeException ("filled floats: " + expected(floatFillCompare, d.filledFloat)); 130 } 131 float[] sortedFloat = new float[] { -123F, 0F, Float.MIN_VALUE, 3.14F, Float.MAX_VALUE }; 132 if (!Arrays.equals(sortedFloat, d.sortedFloat)) { 133 throw new RuntimeException ("sorted floats: " + expected(sortedFloat, d.sortedFloat)); 135 } 136 137 int[] intFillCompare = new int[10]; 138 Arrays.fill(intFillCompare, 1); 139 if (!Arrays.equals(intFillCompare, d.filledInt)) { 140 throw new RuntimeException ("filled ints: " + expected(intFillCompare, d.filledInt)); 142 } 143 int[] sortedInt = new int[] { Integer.MIN_VALUE, 1, 2, 7, 8, Integer.MAX_VALUE }; 144 if (!Arrays.equals(sortedInt, d.sortedInt)) { 145 throw new RuntimeException ("sorted ints: " + expected(sortedInt, d.sortedInt)); 147 } 148 149 long[] longFillCompare = new long[10]; 150 Arrays.fill(longFillCompare, 42L); 151 if (!Arrays.equals(longFillCompare, d.filledLong)) { 152 throw new RuntimeException ("filled longs: " + expected(longFillCompare, d.filledLong)); 154 } 155 long[] sortedLong = new long[] { Long.MIN_VALUE, 1L, 5L, 1000L, Long.MAX_VALUE }; 156 if (!Arrays.equals(sortedLong, d.sortedLong)) { 157 throw new RuntimeException ("sorted longs: " + expected(sortedLong, d.sortedLong)); 159 } 160 161 short[] shortFillCompare = new short[10]; 162 Arrays.fill(shortFillCompare, (short) 69); 163 if (!Arrays.equals(shortFillCompare, d.filledShort)) { 164 throw new RuntimeException ("filled shorts: " + expected(shortFillCompare, d.filledShort)); 166 } 167 short[] sortedShort = new short[] { Short.MIN_VALUE, 0, 2, Short.MAX_VALUE }; 168 if (!Arrays.equals(sortedShort, d.sortedShort)) { 169 throw new RuntimeException ("sorted shorts: " + expected(sortedShort, d.sortedShort)); 171 } 172 173 Object [] objectFillCompare = new Object [10]; 174 Arrays.fill(objectFillCompare, new Integer (1)); 175 if (!Arrays.equals(objectFillCompare, d.filledObject)) { 176 throw new RuntimeException ("filled objects: " + expected(objectFillCompare, d.filledObject)); 178 } 179 Object [] sortedObject = new Object [] { new Integer (-4), new Integer (3) }; 180 if (!Arrays.equals(sortedObject, d.sortedObject)) { 181 throw new RuntimeException ("sorted objects: " + expected(sortedObject, d.sortedObject)); 183 } 184 185 boolean[] booleanFillCompare = new boolean[10]; 186 Arrays.fill(booleanFillCompare, false); 187 if (!Arrays.equals(booleanFillCompare, d.filledBoolean)) { 188 throw new RuntimeException ("filled boolean: " + expected(booleanFillCompare, d.filledBoolean)); 190 } 191 192 List list = d.asList; 193 if (list.size() != 2) { throw new RuntimeException ("list wrong size: " + list.size()); } 194 if (!list.get(0).equals(new Integer (-4))) { throw new RuntimeException ("wrong data"); } 195 if (!list.get(1).equals(new Integer (3))) { throw new RuntimeException ("wrong data"); } 196 197 Object [] array = d.dataBehindAsList2; 198 if (array.length != 4) { throw new RuntimeException ("array size wrong: " + array.length); } 199 Assert.assertEquals("put", array[0]); 200 Assert.assertEquals("the", array[1]); 201 Assert.assertEquals("rap", array[2]); 202 Assert.assertEquals("down", array[3]); 203 } 204 } 205 206 private void setupData() { 207 Data d = new Data(); 208 209 synchronized (data) { 210 data.clear(); 211 data.add(d); 212 } 213 214 d.sort(); 215 d.fill(); 216 d.modifyAsList2(); 217 } 218 219 private static final byte[] unsortedByte = new byte[] { 0, 6, 3, 7, Byte.MAX_VALUE, 4, 2, 9, 8, 1, 5, 221 Byte.MIN_VALUE }; 222 private static final char[] unsortedChar = new char[] { 'f', Character.MAX_VALUE, 'e', 'u', 'k', 'c', 'm', 223 Character.MIN_VALUE }; 224 private static final double[] unsortedDouble = new double[] { Math.PI, Double.MIN_VALUE, -Double.MAX_VALUE, Math.E, 225 Double.MAX_VALUE, 0D, -Double.MIN_VALUE, -1.43D }; 226 private static final float[] unsortedFloat = new float[] { Float.MAX_VALUE, 0F, Float.MIN_VALUE, -123F, 3.14F }; 227 private static final int[] unsortedInt = new int[] { 2, Integer.MAX_VALUE, 7, Integer.MIN_VALUE, 8, 1 }; 228 private static final long[] unsortedLong = new long[] { 1000L, 5L, 1L, Long.MIN_VALUE, Long.MAX_VALUE }; 229 private static final short[] unsortedShort = new short[] { Short.MAX_VALUE, 2, Short.MIN_VALUE, 0 }; 230 private static final Object [] unsortedObject = new Object [] { new Integer (3), new Integer (-4) }; 231 232 private static class Data { 233 234 private boolean[] filledBoolean; 235 237 private final byte[] filledByte; 238 private final byte[] sortedByte; 239 240 private final char[] filledChar; 241 private final char[] sortedChar; 242 243 private final double[] filledDouble; 244 private final double[] sortedDouble; 245 246 private final float[] filledFloat; 247 private final float[] sortedFloat; 248 249 private final int[] filledInt; 250 private final int[] sortedInt; 251 252 private final long[] filledLong; 253 private final long[] sortedLong; 254 255 private final short[] filledShort; 256 private final short[] sortedShort; 257 258 private final Object [] filledObject; 259 private final Object [] sortedObject; 260 261 private final List asList; 262 private final List asList2; 263 264 private final Object [] dataBehindAsList2; 265 266 Data() { 267 this.filledBoolean = new boolean[10]; 268 Arrays.fill(filledBoolean, true); 269 270 this.filledByte = new byte[10]; 271 this.sortedByte = (byte[]) unsortedByte.clone(); 272 273 this.filledChar = new char[10]; 274 this.sortedChar = (char[]) unsortedChar.clone(); 275 276 this.filledDouble = new double[10]; 277 this.sortedDouble = (double[]) unsortedDouble.clone(); 278 279 this.filledInt = new int[10]; 280 this.sortedInt = (int[]) unsortedInt.clone(); 281 282 this.filledFloat = new float[10]; 283 this.sortedFloat = (float[]) unsortedFloat.clone(); 284 285 this.filledLong = new long[10]; 286 this.sortedLong = (long[]) unsortedLong.clone(); 287 288 this.filledShort = new short[10]; 289 this.sortedShort = (short[]) unsortedShort.clone(); 290 291 this.filledObject = new Object [10]; 292 this.sortedObject = (Object []) unsortedObject.clone(); 293 294 this.asList = Arrays.asList(this.sortedObject); 296 297 this.dataBehindAsList2 = new Object [4]; 298 this.dataBehindAsList2[0] = "put"; 299 this.dataBehindAsList2[1] = "the"; 300 this.dataBehindAsList2[2] = "smack"; 301 this.dataBehindAsList2[3] = "down"; 302 303 this.asList2 = Arrays.asList(this.dataBehindAsList2); 304 } 305 306 synchronized void fill() { 307 Arrays.fill(this.filledBoolean, false); 308 Arrays.fill(this.filledByte, (byte) 1); 309 Arrays.fill(this.filledChar, 'z'); 310 Arrays.fill(this.filledDouble, Math.PI); 311 Arrays.fill(this.filledFloat, 8.1F); 312 Arrays.fill(this.filledInt, 1); 313 Arrays.fill(this.filledLong, 42L); 314 Arrays.fill(this.filledShort, (short) 69); 315 Arrays.fill(this.filledObject, new Integer (1)); 316 } 317 318 synchronized void sort() { 319 Arrays.sort(this.sortedByte); 320 Arrays.sort(this.sortedChar); 322 Arrays.sort(this.sortedDouble); 323 Arrays.sort(this.sortedFloat); 324 Arrays.sort(this.sortedInt); 325 Arrays.sort(this.sortedLong); 326 Arrays.sort(this.sortedShort); 327 Arrays.sort(this.sortedObject); 328 } 329 330 synchronized void modifyAsList2() { 331 this.asList2.set(2, "rap"); 333 } 334 335 } 336 337 356 } 357 | Popular Tags |