1 5 package com.tctest; 6 7 import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier; 8 9 import com.tc.object.config.ConfigVisitor; 10 import com.tc.object.config.DSOClientConfigHelper; 11 import com.tc.object.config.TransparencyClassSpec; 12 import com.tc.simulator.app.ApplicationConfig; 13 import com.tc.simulator.listener.ListenerProvider; 14 import com.tc.util.Assert; 15 16 import java.util.Arrays ; 17 import java.util.HashMap ; 18 19 public class ArrayCopyTestApp extends GenericTestApp { 20 private final static int ARRAY_LENGTH = 20; 21 private final static int BIG_ARRAY_LENGTH = 10000; 22 23 public ArrayCopyTestApp(String appId, ApplicationConfig cfg, ListenerProvider listenerProvider) { 24 super(appId, cfg, listenerProvider, Root.class); 25 } 26 27 protected Object getTestObject(String testName) { 28 return this.sharedMap.get("root"); 29 } 30 31 protected void setupTestObject(String testName) { 32 sharedMap.put("root", new Root(ARRAY_LENGTH, BIG_ARRAY_LENGTH)); 33 } 34 35 private static int[] makeUnsharedIntArray() { 36 return new int[ARRAY_LENGTH]; 37 } 38 39 private static int[] makeBigUnsharedIntArray() { 40 return new int[BIG_ARRAY_LENGTH]; 41 } 42 43 private static TestObject[] makeUnsharedRefArray() { 44 return new TestObject[ARRAY_LENGTH]; 45 } 46 47 public void testBasicCopy(Root r, boolean validate) { 48 int[] unshared = makeUnsharedIntArray(); 49 initialize(unshared); 50 51 DataRoot root = r.getRoot(); 52 53 if (validate) { 54 assertEqualIntArray(unshared, root.getDestIntArray(), 0, 5, 10); 55 } else { 56 synchronized (r) { 57 System.arraycopy(unshared, 0, root.getDestIntArray(), 5, 10); 58 } 59 } 60 } 61 62 public void testOverlapWithBiggerArray(Root r, boolean validate) throws Exception { 63 int[] bigUnsharedIntArray = makeBigUnsharedIntArray(); 64 initialize(bigUnsharedIntArray); 65 66 final int destPos = 5; 67 final int destSize = 100; 68 69 DataRoot bigRoot = r.getBigRoot(); 70 71 if (validate) { 72 int[] destIntArray = bigRoot.destIntArray; 73 assertEqualIntArray(bigUnsharedIntArray, destIntArray, 0, destPos, destSize); 74 } else { 75 synchronized (bigRoot) { 76 int[] destIntArray = bigRoot.destIntArray; 77 System.arraycopy(bigUnsharedIntArray, 10, destIntArray, destPos, 10); 79 System.arraycopy(bigUnsharedIntArray, 0, destIntArray, destPos, destSize); 81 } 82 } 83 84 } 85 86 public void testCopyThenChange(Root r, boolean validate) throws Exception { 87 int[] bigUnsharedIntArray = makeBigUnsharedIntArray(); 88 initialize(bigUnsharedIntArray); 89 90 final int destPos = 5; 91 final int destSize = 100; 92 93 DataRoot bigRoot = r.getBigRoot(); 94 95 if (validate) { 96 int[] destIntArray = bigRoot.destIntArray; 97 bigUnsharedIntArray[1] = -5; 98 bigUnsharedIntArray[2] = -6; 99 assertEqualIntArray(bigUnsharedIntArray, destIntArray, 0, destPos, destSize); 100 } else { 101 synchronized (bigRoot) { 102 int[] destIntArray = bigRoot.destIntArray; 103 System.arraycopy(bigUnsharedIntArray, 0, destIntArray, destPos, destSize); 105 destIntArray[destPos + 1] = -5; 106 destIntArray[destPos + 2] = -6; 107 } 108 } 109 } 110 111 public void testBigBasicCopy(Root r, boolean validate) throws Exception { 112 int[] bigUnsharedIntArray = makeBigUnsharedIntArray(); 113 initialize(bigUnsharedIntArray); 114 115 DataRoot bigRoot = r.getBigRoot(); 116 117 if (validate) { 118 assertEqualIntArray(bigUnsharedIntArray, bigRoot.getDestIntArray(), 0, 5, (BIG_ARRAY_LENGTH - 5)); 119 } else { 120 synchronized (bigRoot) { 121 System.arraycopy(bigUnsharedIntArray, 0, bigRoot.getDestIntArray(), 5, (BIG_ARRAY_LENGTH - 5)); 122 } 123 } 124 125 } 126 127 public void testBigCopyToSameArray(Root r, boolean validate) throws Exception { 128 int[] bigUnsharedIntArray = makeBigUnsharedIntArray(); 129 initialize(bigUnsharedIntArray); 130 131 DataRoot bigRoot = r.getBigRoot(); 132 133 if (validate) { 134 assertEqualIntArray(bigUnsharedIntArray, bigRoot.getSrcIntArray(), 0, 5, (BIG_ARRAY_LENGTH - 5)); 135 } else { 136 synchronized (bigRoot) { 137 initialize(bigRoot.getSrcIntArray()); 138 System.arraycopy(bigRoot.getSrcIntArray(), 0, bigRoot.getSrcIntArray(), 5, (BIG_ARRAY_LENGTH - 5)); 139 } 140 } 141 } 142 143 public void testMultipleBasicCopyTest(Root r, boolean validate) throws Exception { 144 int[] unsharedIntArray = makeUnsharedIntArray(); 145 initialize(unsharedIntArray); 146 147 DataRoot root = r.getRoot(); 148 149 if (validate) { 150 assertEqualIntArray(unsharedIntArray, root.getDestIntArray(), 0, 5, 10); 151 } else { 152 for (int i = 0; i < 100; i++) { 153 synchronized (root) { 154 System.arraycopy(unsharedIntArray, 0, root.getDestIntArray(), 5, 10); 155 } 156 } 157 } 158 } 159 160 public void testMultipleCopyToSameArray(Root r, boolean validate) throws Exception { 161 int[] unsharedIntArray = makeUnsharedIntArray(); 162 initialize(unsharedIntArray); 163 164 DataRoot root = r.getRoot(); 165 166 if (validate) { 167 for (int i = 0; i < 2; i++) { 168 System.arraycopy(unsharedIntArray, 0, unsharedIntArray, 5, 10); 169 } 170 171 assertEqualIntArray(unsharedIntArray, root.getSrcIntArray(), 0, 5, 10); 172 } else { 173 synchronized (root) { 174 initialize(root.getSrcIntArray()); 175 } 176 177 for (int i = 0; i < 100; i++) { 178 synchronized (root) { 179 System.arraycopy(root.getSrcIntArray(), 0, root.getSrcIntArray(), 5, 10); 180 } 181 } 182 } 183 } 184 185 public void testCopyRefToUnshared(Root r, boolean validate) throws Exception { 186 189 Object [] sharedArray = r.getSharedArray(); 190 191 int len = sharedArray.length; 192 Object [] unsharedDest = new Object [len]; 193 System.arraycopy(sharedArray, 0, unsharedDest, 0, len); 194 195 if (validate) { 196 Assert.assertNoNullElements(unsharedDest); 197 Assert.assertTrue(Arrays.equals(unsharedDest, makeArrayData())); 198 } 199 } 200 201 public void testBasicRefCopyTest(Root r, boolean validate) throws Exception { 202 TestObject[] unsharedRefArray = makeUnsharedRefArray(); 203 initialize(unsharedRefArray); 204 205 DataRoot root = r.getRoot(); 206 207 if (validate) { 208 assertEqualRefArray(unsharedRefArray, root.getDestRefArray(), 0, 5, 10); 209 } else { 210 synchronized (root) { 211 System.arraycopy(unsharedRefArray, 0, root.getDestRefArray(), 5, 10); 212 } 213 } 214 } 215 216 public void testCopyToSameArray(Root r, boolean validate) throws Exception { 217 int[] unsharedIntArray = makeUnsharedIntArray(); 218 initialize(unsharedIntArray); 219 220 DataRoot root = r.getRoot(); 221 222 if (validate) { 223 assertEqualIntArray(unsharedIntArray, root.getSrcIntArray(), 0, 5, 10); 224 } else { 225 synchronized (root) { 226 initialize(root.getSrcIntArray()); 227 } 228 synchronized (root) { 229 System.arraycopy(root.getSrcIntArray(), 0, root.getSrcIntArray(), 5, 10); 230 } 231 } 232 } 233 234 public void testCopyRefToSameArray(Root r, boolean validate) throws Exception { 235 TestObject[] unsharedRefArray = makeUnsharedRefArray(); 236 initialize(unsharedRefArray); 237 238 DataRoot root = r.getRoot(); 239 240 if (validate) { 241 assertEqualRefArray(unsharedRefArray, root.getSrcRefArray(), 0, 5, 10); 242 } else { 243 synchronized (root) { 244 root.initializeSrcRefArray(root.getSrcRefArray()); 245 } 246 247 synchronized (root) { 248 System.arraycopy(root.getSrcRefArray(), 0, root.getSrcRefArray(), 5, 10); 249 } 250 } 251 } 252 253 public void testCopyToDifferentPrimitiveTypeArray(Root r, boolean validate) throws Exception { 254 DataRoot root = r.getRoot(); 255 256 if (validate) { 257 long[] actual = root.getDestLongArray(); 258 long[] expected = new long[actual.length]; 259 Arrays.fill(expected, 42); 260 Assert.assertTrue(Arrays.equals(expected, actual)); 261 } else { 262 synchronized (root) { 263 long[] l = root.getDestLongArray(); 264 Arrays.fill(l, 42); 265 } 266 267 synchronized (root) { 268 try { 269 System.arraycopy(makeUnsharedIntArray(), 0, root.getDestLongArray(), 5, 10); 270 throw new AssertionError ("Should have thrown an ArrayStoreException."); 271 } catch (ArrayStoreException e) { 272 } 274 } 275 } 276 } 277 278 public void testNullSrc(Root r, boolean validate) throws Exception { 279 int[] unsharedIntArray = makeUnsharedIntArray(); 280 initialize(unsharedIntArray); 281 282 DataRoot root = r.getRoot(); 283 284 if (validate) { 285 assertEqualIntArray(unsharedIntArray, root.getDestIntArray(), 0, 0, ARRAY_LENGTH); 286 } else { 287 synchronized (root) { 288 initialize(root.getDestIntArray()); 289 } 290 291 synchronized (root) { 292 try { 293 System.arraycopy(null, 0, root.getDestIntArray(), 5, 10); 294 throw new AssertionError ("Should have thrown an NullPointerException."); 295 } catch (NullPointerException e) { 296 } 298 } 299 } 300 } 301 302 public void testNullDest(Root r, boolean validate) throws Exception { 303 int[] unsharedIntArray = makeUnsharedIntArray(); 304 initialize(unsharedIntArray); 305 306 DataRoot root = r.getRoot(); 307 308 synchronized (root) { 309 try { 310 System.arraycopy(unsharedIntArray, 0, null, 5, 10); 311 throw new AssertionError ("Should have thrown an NullPointerException."); 312 } catch (NullPointerException e) { 313 } 315 } 316 } 317 318 public void testSrcNotArray(Root r, boolean validate) throws Exception { 319 int[] unsharedIntArray = makeUnsharedIntArray(); 320 initialize(unsharedIntArray); 321 322 DataRoot root = r.getRoot(); 323 324 if (validate) { 325 assertEqualIntArray(unsharedIntArray, root.getDestIntArray(), 0, 0, ARRAY_LENGTH); 326 } else { 327 synchronized (root) { 328 initialize(root.getDestIntArray()); 329 } 330 331 synchronized (root) { 332 try { 333 System.arraycopy(new Object (), 0, root.getDestIntArray(), 5, 10); 334 throw new AssertionError ("Should have thrown an ArrayStoreException."); 335 } catch (ArrayStoreException e) { 336 } 338 } 339 } 340 } 341 342 public void testDestNotArray(Root r, boolean validate) throws Exception { 343 try { 344 System.arraycopy(makeUnsharedIntArray(), 0, new Object (), 5, 10); 345 throw new AssertionError ("Should have thrown an ArrayStoreException."); 346 } catch (ArrayStoreException e) { 347 } 349 } 350 351 public void testSrcAndDestNotCompatibleTest(Root r, boolean validate) throws Exception { 352 TestObject[] unsharedRefArray = makeUnsharedRefArray(); 353 initialize(unsharedRefArray); 354 355 DataRoot root = r.getRoot(); 356 357 if (validate) { 358 assertEqualRefArray(unsharedRefArray, root.getDestRefArray(), 0, 0, ARRAY_LENGTH); 359 } else { 360 root.initializeDestRefArray(); 361 362 synchronized (root) { 363 try { 364 System.arraycopy(makeUnsharedIntArray(), 0, root.getDestRefArray(), 5, 10); 365 throw new AssertionError ("Should have thrown an ArrayStoreException."); 366 } catch (ArrayStoreException e) { 367 } 369 } 370 } 371 372 } 373 374 public void testNegativeLengthCopyTest(Root r, boolean validate) throws Exception { 375 int[] unsharedIntArray = makeUnsharedIntArray(); 376 initialize(unsharedIntArray); 377 378 DataRoot root = r.getRoot(); 379 380 if (validate) { 381 assertEqualIntArray(unsharedIntArray, root.getDestIntArray(), 0, 0, ARRAY_LENGTH); 382 } else { 383 synchronized (root) { 384 System.arraycopy(unsharedIntArray, 0, root.getDestIntArray(), 0, ARRAY_LENGTH); 385 } 386 387 mutate(unsharedIntArray); 388 try { 389 System.arraycopy(unsharedIntArray, -1, root.getDestIntArray(), 0, 10); 390 throw new AssertionError ("Should have thrown an IndexOutOfBoundsException."); 391 } catch (IndexOutOfBoundsException e) { 392 } 394 395 mutate(unsharedIntArray); 396 try { 397 System.arraycopy(unsharedIntArray, 0, root.getDestIntArray(), -1, 10); 398 throw new AssertionError ("Should have thrown an IndexOutOfBoundsException."); 399 } catch (IndexOutOfBoundsException e) { 400 } 402 403 mutate(unsharedIntArray); 404 try { 405 System.arraycopy(unsharedIntArray, 0, root.getDestIntArray(), 0, -1); 406 throw new AssertionError ("Should have thrown an IndexOutOfBoundsException."); 407 } catch (IndexOutOfBoundsException e) { 408 } 410 } 411 412 } 413 414 public void testIndexOutOfBoundsCopy(Root r, boolean validate) throws Exception { 415 int[] unsharedIntArray = makeUnsharedIntArray(); 416 initialize(unsharedIntArray); 417 418 DataRoot root = r.getRoot(); 419 420 if (validate) { 421 assertEqualIntArray(unsharedIntArray, root.getDestIntArray(), 0, 0, ARRAY_LENGTH); 422 } else { 423 synchronized (root) { 424 System.arraycopy(unsharedIntArray, 0, root.getDestIntArray(), 0, ARRAY_LENGTH); 425 } 426 427 mutate(unsharedIntArray); 428 try { 429 System.arraycopy(unsharedIntArray, 15, root.getDestIntArray(), 0, 10); 430 throw new AssertionError ("Should have thrown an IndexOutOfBoundsException."); 431 } catch (IndexOutOfBoundsException e) { 432 } 434 435 mutate(unsharedIntArray); 436 try { 437 System.arraycopy(unsharedIntArray, 0, root.getDestIntArray(), 15, 10); 438 throw new AssertionError ("Should have thrown an IndexOutOfBoundsException."); 439 } catch (IndexOutOfBoundsException e) { 440 } 442 } 443 } 444 445 public void testCopyNonCompatibleRefObject(Root r, boolean validate) throws Exception { 446 TestObject[] unsharedRefArray = makeUnsharedRefArray(); 447 initialize(unsharedRefArray); 448 449 DataRoot root = r.getRoot(); 450 451 if (validate) { 452 assertEqualRefArray(unsharedRefArray, root.getDestRefArray(), 0, 0, 10); 453 } else { 454 synchronized (root) { 455 System.arraycopy(unsharedRefArray, 0, root.getDestRefArray(), 0, 10); 456 } 457 458 Double d[] = new Double [ARRAY_LENGTH]; 459 for (int i = 0; i < d.length; i++) { 460 d[i] = new Double (i); 461 } 462 463 synchronized (root) { 464 try { 465 System.arraycopy(d, 0, root.getDestRefArray(), 0, 10); 466 throw new AssertionError ("Should have thrown an ArrayStoreException."); 467 } catch (ArrayStoreException e) { 468 } 470 } 471 } 472 } 473 474 public void testPartialCopy(Root r, boolean validate) throws Exception { 475 TestObject[] unsharedRefArray = makeUnsharedRefArray(); 476 initialize(unsharedRefArray); 477 478 DataRoot root = r.getRoot(); 479 480 TestObject[] dest = root.getDestRefArray(); 481 482 if (validate) { 483 unsharedRefArray[0] = new TestObject(42); 484 unsharedRefArray[1] = null; 485 assertEqualRefArray(unsharedRefArray, dest, 0, 0, dest.length); 486 } else { 487 synchronized (root) { 488 System.arraycopy(unsharedRefArray, 0, dest, 0, dest.length); 489 } 490 491 Object [] array = new Object [dest.length]; 492 array[0] = new TestObject(42); 493 array[2] = new HashMap (); 495 496 synchronized (root) { 497 try { 498 System.arraycopy(array, 0, dest, 0, dest.length); 499 throw new AssertionError (); 500 } catch (ArrayStoreException ase) { 501 } 503 } 504 } 505 } 506 507 private static void mutate(int[] unsharedIntArray) { 508 for (int i = 0; i < unsharedIntArray.length; i++) { 509 unsharedIntArray[i]++; 510 } 511 } 512 513 private static void assertEqualIntArray(int[] expected, int[] actual, int startExpectedPos, int startActualPos, 514 int length) { 515 for (int i = startExpectedPos, j = startActualPos; i < length; i++, j++) { 516 Assert.assertEquals(expected[i], actual[j]); 517 } 518 } 519 520 private static void assertEqualRefArray(Object [] expected, Object [] actual, int startExpectedPos, int startActualPos, 521 int length) { 522 for (int i = startExpectedPos, j = startActualPos; i < length; i++, j++) { 523 Assert.assertEquals(expected[i], actual[j]); 524 } 525 } 526 527 private static void initialize(int[] array) { 528 for (int i = 0; i < array.length; i++) { 529 array[i] = i; 530 } 531 } 532 533 private static void initialize(TestObject[] array) { 534 for (int i = 0; i < array.length; i++) { 535 array[i] = new TestObject(i); 536 } 537 } 538 539 private static Object [] makeArrayData() { 540 return new Object [] { "dig", "this", "yo", new Thingy("!") }; 541 } 542 543 public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) { 544 TransparencyClassSpec spec = config.getOrCreateSpec(CyclicBarrier.class.getName()); 545 config.addWriteAutolock("* " + CyclicBarrier.class.getName() + "*.*(..)"); 546 547 String testClass = ArrayCopyTestApp.class.getName(); 548 spec = config.getOrCreateSpec(testClass); 549 550 config.addIncludePattern(testClass + "$*"); 551 552 String methodExpression = "* " + testClass + "*.*(..)"; 553 config.addWriteAutolock(methodExpression); 554 555 spec.addRoot("root", "root"); 556 spec.addRoot("sharedArray", "sharedArray"); 557 spec.addRoot("bigRoot", "bigRoot"); 558 spec.addRoot("barrier", "barrier"); 559 } 560 561 private static class TestObject { 562 private final int i; 563 564 public TestObject(int i) { 565 this.i = i; 566 } 567 568 public int hashCode() { 569 return i; 570 } 571 572 public boolean equals(Object o) { 573 if (!(o instanceof TestObject)) { return false; } 574 return this.i == ((TestObject) o).i; 575 } 576 577 public String toString() { 578 return getClass().getName() + "(" + i + ")"; 579 } 580 } 581 582 private static class DataRoot { 583 private final int[] srcIntArray; 584 private final TestObject[] srcRefArray; 585 private final int[] destIntArray; 586 private final long[] destLongArray; 587 private final TestObject[] destRefArray; 588 private final int size; 589 590 public DataRoot(int size) { 591 this.size = size; 592 srcIntArray = new int[size]; 593 srcRefArray = new TestObject[size]; 594 destIntArray = new int[size]; 595 destLongArray = new long[size]; 596 destRefArray = new TestObject[size]; 597 } 598 599 public int[] getSrcIntArray() { 600 return srcIntArray; 601 } 602 603 public TestObject[] getSrcRefArray() { 604 return srcRefArray; 605 } 606 607 public int[] getDestIntArray() { 608 return destIntArray; 609 } 610 611 public long[] getDestLongArray() { 612 return destLongArray; 613 } 614 615 public TestObject[] getDestRefArray() { 616 return destRefArray; 617 } 618 619 public synchronized void initializeDestRefArray() { 620 initialize(destRefArray); 621 } 622 623 public void initializeSrcRefArray(Object [] array) { 624 for (int i = 0; i < size; i++) { 625 array[i] = new TestObject(i); 626 } 627 } 628 } 629 630 private static class Thingy { 631 private final String val; 632 633 Thingy(String val) { 634 this.val = val; 635 } 636 637 public int hashCode() { 638 return val.hashCode(); 639 } 640 641 public boolean equals(Object obj) { 642 if (!(obj instanceof Thingy)) { return false; } 643 return val.equals(((Thingy) obj).val); 644 } 645 646 public String toString() { 647 return getClass().getName() + "(" + val + ")"; 648 } 649 } 650 651 private static class Root { 652 653 private final DataRoot root; 654 private final DataRoot bigRoot; 655 private final Object [] sharedArray = makeArrayData(); 656 657 Root(int rootSize, int bigRootSize) { 658 this.root = new DataRoot(rootSize); 659 this.bigRoot = new DataRoot(bigRootSize); 660 } 661 662 DataRoot getBigRoot() { 663 return bigRoot; 664 } 665 666 DataRoot getRoot() { 667 return root; 668 } 669 670 Object [] getSharedArray() { 671 return sharedArray; 672 } 673 } 674 675 } 676 | Popular Tags |