1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
|
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* This file is available under and governed by the GNU General Public
* License version 2 only, as published by the Free Software Foundation.
* However, the following notice accompanied the original version of this
* file:
*
* Written by Doug Lea with assistance from members of JCP JSR-166
* Expert Group and released to the public domain, as explained at
* http://creativecommons.org/publicdomain/zero/1.0/
* Other contributors include Andrew Wright, Jeffrey Hayes,
* Pat Fisher, Mike Judd.
*/
import java.util.Arrays;
import java.util.Collection;
import java.util.Deque;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Queue;
import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.LongAdder;
import junit.framework.Test;
public class ConcurrentLinkedDequeTest extends JSR166TestCase {
public static void main(String[] args) {
main(suite(), args);
}
public static Test suite() {
class Implementation implements CollectionImplementation {
public Class<?> klazz() { return ConcurrentLinkedDeque.class; }
public Collection emptyCollection() { return new ConcurrentLinkedDeque(); }
public Object makeElement(int i) { return i; }
public boolean isConcurrent() { return true; }
public boolean permitsNulls() { return false; }
}
return newTestSuite(ConcurrentLinkedDequeTest.class,
CollectionTest.testSuite(new Implementation()));
}
/**
* Returns a new deque of given size containing consecutive
* Integers 0 ... n - 1.
*/
private static ConcurrentLinkedDeque<Integer> populatedDeque(int n) {
ConcurrentLinkedDeque<Integer> q = new ConcurrentLinkedDeque<>();
assertTrue(q.isEmpty());
for (int i = 0; i < n; ++i)
assertTrue(q.offer(new Integer(i)));
assertFalse(q.isEmpty());
assertEquals(n, q.size());
assertEquals((Integer) 0, q.peekFirst());
assertEquals((Integer) (n - 1), q.peekLast());
return q;
}
/**
* new deque is empty
*/
public void testConstructor1() {
assertTrue(new ConcurrentLinkedDeque().isEmpty());
assertEquals(0, new ConcurrentLinkedDeque().size());
}
/**
* Initializing from null Collection throws NPE
*/
public void testConstructor3() {
try {
new ConcurrentLinkedDeque((Collection)null);
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* Initializing from Collection of null elements throws NPE
*/
public void testConstructor4() {
try {
new ConcurrentLinkedDeque(Arrays.asList(new Integer[SIZE]));
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* Initializing from Collection with some null elements throws NPE
*/
public void testConstructor5() {
Integer[] ints = new Integer[SIZE];
for (int i = 0; i < SIZE - 1; ++i)
ints[i] = new Integer(i);
try {
new ConcurrentLinkedDeque(Arrays.asList(ints));
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* Deque contains all elements of collection used to initialize
*/
public void testConstructor6() {
Integer[] ints = new Integer[SIZE];
for (int i = 0; i < SIZE; ++i)
ints[i] = new Integer(i);
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(Arrays.asList(ints));
for (int i = 0; i < SIZE; ++i)
assertEquals(ints[i], q.poll());
}
/**
* isEmpty is true before add, false after
*/
public void testEmpty() {
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
assertTrue(q.isEmpty());
q.add(one);
assertFalse(q.isEmpty());
q.add(two);
q.remove();
q.remove();
assertTrue(q.isEmpty());
}
/**
* size() changes when elements added and removed
*/
public void testSize() {
ConcurrentLinkedDeque q = populatedDeque(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertEquals(SIZE - i, q.size());
q.remove();
}
for (int i = 0; i < SIZE; ++i) {
assertEquals(i, q.size());
q.add(new Integer(i));
}
}
/**
* push(null) throws NPE
*/
public void testPushNull() {
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
try {
q.push(null);
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* peekFirst() returns element inserted with push
*/
public void testPush() {
ConcurrentLinkedDeque q = populatedDeque(3);
q.pollLast();
q.push(four);
assertSame(four, q.peekFirst());
}
/**
* pop() removes first element, or throws NSEE if empty
*/
public void testPop() {
ConcurrentLinkedDeque q = populatedDeque(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertEquals(i, q.pop());
}
try {
q.pop();
shouldThrow();
} catch (NoSuchElementException success) {}
}
/**
* offer(null) throws NPE
*/
public void testOfferNull() {
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
try {
q.offer(null);
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* offerFirst(null) throws NPE
*/
public void testOfferFirstNull() {
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
try {
q.offerFirst(null);
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* offerLast(null) throws NPE
*/
public void testOfferLastNull() {
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
try {
q.offerLast(null);
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* offer(x) succeeds
*/
public void testOffer() {
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
assertTrue(q.offer(zero));
assertTrue(q.offer(one));
assertSame(zero, q.peekFirst());
assertSame(one, q.peekLast());
}
/**
* offerFirst(x) succeeds
*/
public void testOfferFirst() {
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
assertTrue(q.offerFirst(zero));
assertTrue(q.offerFirst(one));
assertSame(one, q.peekFirst());
assertSame(zero, q.peekLast());
}
/**
* offerLast(x) succeeds
*/
public void testOfferLast() {
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
assertTrue(q.offerLast(zero));
assertTrue(q.offerLast(one));
assertSame(zero, q.peekFirst());
assertSame(one, q.peekLast());
}
/**
* add(null) throws NPE
*/
public void testAddNull() {
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
try {
q.add(null);
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* addFirst(null) throws NPE
*/
public void testAddFirstNull() {
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
try {
q.addFirst(null);
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* addLast(null) throws NPE
*/
public void testAddLastNull() {
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
try {
q.addLast(null);
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* add(x) succeeds
*/
public void testAdd() {
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
assertTrue(q.add(zero));
assertTrue(q.add(one));
assertSame(zero, q.peekFirst());
assertSame(one, q.peekLast());
}
/**
* addFirst(x) succeeds
*/
public void testAddFirst() {
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
q.addFirst(zero);
q.addFirst(one);
assertSame(one, q.peekFirst());
assertSame(zero, q.peekLast());
}
/**
* addLast(x) succeeds
*/
public void testAddLast() {
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
q.addLast(zero);
q.addLast(one);
assertSame(zero, q.peekFirst());
assertSame(one, q.peekLast());
}
/**
* addAll(null) throws NPE
*/
public void testAddAll1() {
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
try {
q.addAll(null);
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* addAll(this) throws IllegalArgumentException
*/
public void testAddAllSelf() {
ConcurrentLinkedDeque q = populatedDeque(SIZE);
try {
q.addAll(q);
shouldThrow();
} catch (IllegalArgumentException success) {}
}
/**
* addAll of a collection with null elements throws NPE
*/
public void testAddAll2() {
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
try {
q.addAll(Arrays.asList(new Integer[SIZE]));
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* addAll of a collection with any null elements throws NPE after
* possibly adding some elements
*/
public void testAddAll3() {
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
Integer[] ints = new Integer[SIZE];
for (int i = 0; i < SIZE - 1; ++i)
ints[i] = new Integer(i);
try {
q.addAll(Arrays.asList(ints));
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* Deque contains all elements, in traversal order, of successful addAll
*/
public void testAddAll5() {
Integer[] empty = new Integer[0];
Integer[] ints = new Integer[SIZE];
for (int i = 0; i < SIZE; ++i)
ints[i] = new Integer(i);
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
assertFalse(q.addAll(Arrays.asList(empty)));
assertTrue(q.addAll(Arrays.asList(ints)));
for (int i = 0; i < SIZE; ++i)
assertEquals(ints[i], q.poll());
}
/**
* pollFirst() succeeds unless empty
*/
public void testPollFirst() {
ConcurrentLinkedDeque q = populatedDeque(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertEquals(i, q.pollFirst());
}
assertNull(q.pollFirst());
}
/**
* pollLast() succeeds unless empty
*/
public void testPollLast() {
ConcurrentLinkedDeque q = populatedDeque(SIZE);
for (int i = SIZE - 1; i >= 0; --i) {
assertEquals(i, q.pollLast());
}
assertNull(q.pollLast());
}
/**
* poll() succeeds unless empty
*/
public void testPoll() {
ConcurrentLinkedDeque q = populatedDeque(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertEquals(i, q.poll());
}
assertNull(q.poll());
}
/**
* peek() returns next element, or null if empty
*/
public void testPeek() {
ConcurrentLinkedDeque q = populatedDeque(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertEquals(i, q.peek());
assertEquals(i, q.poll());
assertTrue(q.peek() == null ||
!q.peek().equals(i));
}
assertNull(q.peek());
}
/**
* element() returns first element, or throws NSEE if empty
*/
public void testElement() {
ConcurrentLinkedDeque q = populatedDeque(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertEquals(i, q.element());
assertEquals(i, q.poll());
}
try {
q.element();
shouldThrow();
} catch (NoSuchElementException success) {}
}
/**
* remove() removes next element, or throws NSEE if empty
*/
public void testRemove() {
ConcurrentLinkedDeque q = populatedDeque(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertEquals(i, q.remove());
}
try {
q.remove();
shouldThrow();
} catch (NoSuchElementException success) {}
}
/**
* remove(x) removes x and returns true if present
*/
public void testRemoveElement() {
ConcurrentLinkedDeque q = populatedDeque(SIZE);
for (int i = 1; i < SIZE; i += 2) {
assertTrue(q.contains(i));
assertTrue(q.remove(i));
assertFalse(q.contains(i));
assertTrue(q.contains(i - 1));
}
for (int i = 0; i < SIZE; i += 2) {
assertTrue(q.contains(i));
assertTrue(q.remove(i));
assertFalse(q.contains(i));
assertFalse(q.remove(i + 1));
assertFalse(q.contains(i + 1));
}
assertTrue(q.isEmpty());
}
/**
* peekFirst() returns next element, or null if empty
*/
public void testPeekFirst() {
ConcurrentLinkedDeque q = populatedDeque(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertEquals(i, q.peekFirst());
assertEquals(i, q.pollFirst());
assertTrue(q.peekFirst() == null ||
!q.peekFirst().equals(i));
}
assertNull(q.peekFirst());
}
/**
* peekLast() returns next element, or null if empty
*/
public void testPeekLast() {
ConcurrentLinkedDeque q = populatedDeque(SIZE);
for (int i = SIZE - 1; i >= 0; --i) {
assertEquals(i, q.peekLast());
assertEquals(i, q.pollLast());
assertTrue(q.peekLast() == null ||
!q.peekLast().equals(i));
}
assertNull(q.peekLast());
}
/**
* getFirst() returns first element, or throws NSEE if empty
*/
public void testFirstElement() {
ConcurrentLinkedDeque q = populatedDeque(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertEquals(i, q.getFirst());
assertEquals(i, q.pollFirst());
}
try {
q.getFirst();
shouldThrow();
} catch (NoSuchElementException success) {}
}
/**
* getLast() returns last element, or throws NSEE if empty
*/
public void testLastElement() {
ConcurrentLinkedDeque q = populatedDeque(SIZE);
for (int i = SIZE - 1; i >= 0; --i) {
assertEquals(i, q.getLast());
assertEquals(i, q.pollLast());
}
try {
q.getLast();
shouldThrow();
} catch (NoSuchElementException success) {}
assertNull(q.peekLast());
}
/**
* removeFirst() removes first element, or throws NSEE if empty
*/
public void testRemoveFirst() {
ConcurrentLinkedDeque q = populatedDeque(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertEquals(i, q.removeFirst());
}
try {
q.removeFirst();
shouldThrow();
} catch (NoSuchElementException success) {}
assertNull(q.peekFirst());
}
/**
* removeLast() removes last element, or throws NSEE if empty
*/
public void testRemoveLast() {
ConcurrentLinkedDeque q = populatedDeque(SIZE);
for (int i = SIZE - 1; i >= 0; --i) {
assertEquals(i, q.removeLast());
}
try {
q.removeLast();
shouldThrow();
} catch (NoSuchElementException success) {}
assertNull(q.peekLast());
}
/**
* removeFirstOccurrence(x) removes x and returns true if present
*/
public void testRemoveFirstOccurrence() {
ConcurrentLinkedDeque q = populatedDeque(SIZE);
for (int i = 1; i < SIZE; i += 2) {
assertTrue(q.removeFirstOccurrence(new Integer(i)));
}
for (int i = 0; i < SIZE; i += 2) {
assertTrue(q.removeFirstOccurrence(new Integer(i)));
assertFalse(q.removeFirstOccurrence(new Integer(i + 1)));
}
assertTrue(q.isEmpty());
}
/**
* removeLastOccurrence(x) removes x and returns true if present
*/
public void testRemoveLastOccurrence() {
ConcurrentLinkedDeque q = populatedDeque(SIZE);
for (int i = 1; i < SIZE; i += 2) {
assertTrue(q.removeLastOccurrence(new Integer(i)));
}
for (int i = 0; i < SIZE; i += 2) {
assertTrue(q.removeLastOccurrence(new Integer(i)));
assertFalse(q.removeLastOccurrence(new Integer(i + 1)));
}
assertTrue(q.isEmpty());
}
/**
* contains(x) reports true when elements added but not yet removed
*/
public void testContains() {
ConcurrentLinkedDeque q = populatedDeque(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertTrue(q.contains(new Integer(i)));
q.poll();
assertFalse(q.contains(new Integer(i)));
}
}
/**
* clear() removes all elements
*/
public void testClear() {
ConcurrentLinkedDeque q = populatedDeque(SIZE);
q.clear();
assertTrue(q.isEmpty());
assertEquals(0, q.size());
q.add(one);
assertFalse(q.isEmpty());
q.clear();
assertTrue(q.isEmpty());
}
/**
* containsAll(c) is true when c contains a subset of elements
*/
public void testContainsAll() {
ConcurrentLinkedDeque q = populatedDeque(SIZE);
ConcurrentLinkedDeque p = new ConcurrentLinkedDeque();
for (int i = 0; i < SIZE; ++i) {
assertTrue(q.containsAll(p));
assertFalse(p.containsAll(q));
p.add(new Integer(i));
}
assertTrue(p.containsAll(q));
}
/**
* retainAll(c) retains only those elements of c and reports true if change
*/
public void testRetainAll() {
ConcurrentLinkedDeque q = populatedDeque(SIZE);
ConcurrentLinkedDeque p = populatedDeque(SIZE);
for (int i = 0; i < SIZE; ++i) {
boolean changed = q.retainAll(p);
if (i == 0)
assertFalse(changed);
else
assertTrue(changed);
assertTrue(q.containsAll(p));
assertEquals(SIZE - i, q.size());
p.remove();
}
}
/**
* removeAll(c) removes only those elements of c and reports true if changed
*/
public void testRemoveAll() {
for (int i = 1; i < SIZE; ++i) {
ConcurrentLinkedDeque q = populatedDeque(SIZE);
ConcurrentLinkedDeque p = populatedDeque(i);
assertTrue(q.removeAll(p));
assertEquals(SIZE - i, q.size());
for (int j = 0; j < i; ++j) {
Integer x = (Integer)(p.remove());
assertFalse(q.contains(x));
}
}
}
/**
* toArray() contains all elements in FIFO order
*/
public void testToArray() {
ConcurrentLinkedDeque q = populatedDeque(SIZE);
Object[] a = q.toArray();
assertSame(Object[].class, a.getClass());
for (Object o : a)
assertSame(o, q.poll());
assertTrue(q.isEmpty());
}
/**
* toArray(a) contains all elements in FIFO order
*/
public void testToArray2() {
ConcurrentLinkedDeque<Integer> q = populatedDeque(SIZE);
Integer[] ints = new Integer[SIZE];
Integer[] array = q.toArray(ints);
assertSame(ints, array);
for (Integer o : ints)
assertSame(o, q.poll());
assertTrue(q.isEmpty());
}
/**
* toArray(null) throws NullPointerException
*/
public void testToArray_NullArg() {
ConcurrentLinkedDeque q = populatedDeque(SIZE);
try {
q.toArray((Object[])null);
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* toArray(incompatible array type) throws ArrayStoreException
*/
public void testToArray1_BadArg() {
ConcurrentLinkedDeque q = populatedDeque(SIZE);
try {
q.toArray(new String[10]);
shouldThrow();
} catch (ArrayStoreException success) {}
}
/**
* Iterator iterates through all elements
*/
public void testIterator() {
ConcurrentLinkedDeque q = populatedDeque(SIZE);
Iterator it = q.iterator();
int i;
for (i = 0; it.hasNext(); i++)
assertTrue(q.contains(it.next()));
assertEquals(i, SIZE);
assertIteratorExhausted(it);
}
/**
* iterator of empty collection has no elements
*/
public void testEmptyIterator() {
Deque c = new ConcurrentLinkedDeque();
assertIteratorExhausted(c.iterator());
assertIteratorExhausted(c.descendingIterator());
}
/**
* Iterator ordering is FIFO
*/
public void testIteratorOrdering() {
final ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
q.add(one);
q.add(two);
q.add(three);
int k = 0;
for (Iterator it = q.iterator(); it.hasNext();) {
assertEquals(++k, it.next());
}
assertEquals(3, k);
}
/**
* Modifications do not cause iterators to fail
*/
public void testWeaklyConsistentIteration() {
final ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
q.add(one);
q.add(two);
q.add(three);
for (Iterator it = q.iterator(); it.hasNext();) {
q.remove();
it.next();
}
assertEquals("deque should be empty again", 0, q.size());
}
/**
* iterator.remove() removes current element
*/
public void testIteratorRemove() {
final ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
final Random rng = new Random();
for (int iters = 0; iters < 100; ++iters) {
int max = rng.nextInt(5) + 2;
int split = rng.nextInt(max - 1) + 1;
for (int j = 1; j <= max; ++j)
q.add(new Integer(j));
Iterator it = q.iterator();
for (int j = 1; j <= split; ++j)
assertEquals(it.next(), new Integer(j));
it.remove();
assertEquals(it.next(), new Integer(split + 1));
for (int j = 1; j <= split; ++j)
q.remove(new Integer(j));
it = q.iterator();
for (int j = split + 1; j <= max; ++j) {
assertEquals(it.next(), new Integer(j));
it.remove();
}
assertFalse(it.hasNext());
assertTrue(q.isEmpty());
}
}
/**
* Descending iterator iterates through all elements
*/
public void testDescendingIterator() {
ConcurrentLinkedDeque q = populatedDeque(SIZE);
int i = 0;
Iterator it = q.descendingIterator();
while (it.hasNext()) {
assertTrue(q.contains(it.next()));
++i;
}
assertEquals(i, SIZE);
assertFalse(it.hasNext());
try {
it.next();
shouldThrow();
} catch (NoSuchElementException success) {}
}
/**
* Descending iterator ordering is reverse FIFO
*/
public void testDescendingIteratorOrdering() {
final ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
for (int iters = 0; iters < 100; ++iters) {
q.add(new Integer(3));
q.add(new Integer(2));
q.add(new Integer(1));
int k = 0;
for (Iterator it = q.descendingIterator(); it.hasNext();) {
assertEquals(++k, it.next());
}
assertEquals(3, k);
q.remove();
q.remove();
q.remove();
}
}
/**
* descendingIterator.remove() removes current element
*/
public void testDescendingIteratorRemove() {
final ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
final Random rng = new Random();
for (int iters = 0; iters < 100; ++iters) {
int max = rng.nextInt(5) + 2;
int split = rng.nextInt(max - 1) + 1;
for (int j = max; j >= 1; --j)
q.add(new Integer(j));
Iterator it = q.descendingIterator();
for (int j = 1; j <= split; ++j)
assertEquals(it.next(), new Integer(j));
it.remove();
assertEquals(it.next(), new Integer(split + 1));
for (int j = 1; j <= split; ++j)
q.remove(new Integer(j));
it = q.descendingIterator();
for (int j = split + 1; j <= max; ++j) {
assertEquals(it.next(), new Integer(j));
it.remove();
}
assertFalse(it.hasNext());
assertTrue(q.isEmpty());
}
}
/**
* toString() contains toStrings of elements
*/
public void testToString() {
ConcurrentLinkedDeque q = populatedDeque(SIZE);
String s = q.toString();
for (int i = 0; i < SIZE; ++i) {
assertTrue(s.contains(String.valueOf(i)));
}
}
/**
* A deserialized/reserialized deque has same elements in same order
*/
public void testSerialization() throws Exception {
Queue x = populatedDeque(SIZE);
Queue y = serialClone(x);
assertNotSame(x, y);
assertEquals(x.size(), y.size());
assertEquals(x.toString(), y.toString());
assertTrue(Arrays.equals(x.toArray(), y.toArray()));
while (!x.isEmpty()) {
assertFalse(y.isEmpty());
assertEquals(x.remove(), y.remove());
}
assertTrue(y.isEmpty());
}
/**
* contains(null) always return false.
* remove(null) always throws NullPointerException.
*/
public void testNeverContainsNull() {
Deque<?>[] qs = {
new ConcurrentLinkedDeque<Object>(),
populatedDeque(2),
};
for (Deque<?> q : qs) {
assertFalse(q.contains(null));
try {
assertFalse(q.remove(null));
shouldThrow();
} catch (NullPointerException success) {}
try {
assertFalse(q.removeFirstOccurrence(null));
shouldThrow();
} catch (NullPointerException success) {}
try {
assertFalse(q.removeLastOccurrence(null));
shouldThrow();
} catch (NullPointerException success) {}
}
}
void runAsync(Runnable r1, Runnable r2) {
boolean b = ThreadLocalRandom.current().nextBoolean();
CompletableFuture<Void> f1 = CompletableFuture.runAsync(b ? r1 : r2);
CompletableFuture<Void> f2 = CompletableFuture.runAsync(b ? r2 : r1);
f1.join();
f2.join();
}
/**
* Non-traversing Deque operations are linearizable.
* https://bugs.openjdk.java.net/browse/JDK-8188900
* ant -Djsr166.expensiveTests=true -Djsr166.tckTestClass=ConcurrentLinkedDequeTest -Djsr166.methodFilter=testBug8188900 tck
*/
public void testBug8188900() {
final ThreadLocalRandom rnd = ThreadLocalRandom.current();
final LongAdder nulls = new LongAdder(), zeros = new LongAdder();
for (int n = expensiveTests ? 100_000 : 10; n--> 0; ) {
ConcurrentLinkedDeque<Integer> d = new ConcurrentLinkedDeque<>();
boolean peek = rnd.nextBoolean();
Runnable getter = () -> {
Integer x = peek ? d.peekFirst() : d.pollFirst();
if (x == null) nulls.increment();
else if (x == 0) zeros.increment();
else
throw new AssertionError(
String.format(
"unexpected value %d after %d nulls and %d zeros",
x, nulls.sum(), zeros.sum()));
};
Runnable adder = () -> { d.addFirst(0); d.addLast(42); };
runAsync(getter, adder);
}
}
/**
* Reverse direction variant of testBug8188900
*/
public void testBug8188900_reverse() {
final ThreadLocalRandom rnd = ThreadLocalRandom.current();
final LongAdder nulls = new LongAdder(), zeros = new LongAdder();
for (int n = expensiveTests ? 100_000 : 10; n--> 0; ) {
ConcurrentLinkedDeque<Integer> d = new ConcurrentLinkedDeque<>();
boolean peek = rnd.nextBoolean();
Runnable getter = () -> {
Integer x = peek ? d.peekLast() : d.pollLast();
if (x == null) nulls.increment();
else if (x == 0) zeros.increment();
else
throw new AssertionError(
String.format(
"unexpected value %d after %d nulls and %d zeros",
x, nulls.sum(), zeros.sum()));
};
Runnable adder = () -> { d.addLast(0); d.addFirst(42); };
runAsync(getter, adder);
}
}
<T> T chooseRandomly(T... choices) {
return choices[ThreadLocalRandom.current().nextInt(choices.length)];
}
/**
* Non-traversing Deque operations (that return null) are linearizable.
* Don't return null when the deque is observably never empty.
* https://bugs.openjdk.java.net/browse/JDK-8189387
* ant -Djsr166.expensiveTests=true -Djsr166.tckTestClass=ConcurrentLinkedDequeTest -Djsr166.methodFilter=testBug8189387 tck
*/
public void testBug8189387() {
final ThreadLocalRandom rnd = ThreadLocalRandom.current();
Object x = new Object();
for (int n = expensiveTests ? 100_000 : 10; n--> 0; ) {
ConcurrentLinkedDeque<Object> d = new ConcurrentLinkedDeque<>();
Runnable add = chooseRandomly(
() -> d.addFirst(x),
() -> d.offerFirst(x),
() -> d.addLast(x),
() -> d.offerLast(x));
Runnable get = chooseRandomly(
() -> assertFalse(d.isEmpty()),
() -> assertSame(x, d.peekFirst()),
() -> assertSame(x, d.peekLast()),
() -> assertSame(x, d.pollFirst()),
() -> assertSame(x, d.pollLast()));
Runnable addRemove = chooseRandomly(
() -> { d.addFirst(x); d.pollLast(); },
() -> { d.offerFirst(x); d.removeFirst(); },
() -> { d.offerLast(x); d.removeLast(); },
() -> { d.addLast(x); d.pollFirst(); });
add.run();
runAsync(get, addRemove);
}
}
}
|