1 |
|
/* |
2 |
< |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
< |
* public domain. Use, modify, and redistribute this code in any way |
4 |
< |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
< |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
2 |
> |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
> |
* Expert Group and released to the public domain, as explained at |
4 |
> |
* http://creativecommons.org/licenses/publicdomain |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
153 |
|
} |
154 |
|
|
155 |
|
/** |
156 |
< |
* remainingCapacity does not change when elementa added or removed, |
156 |
> |
* remainingCapacity does not change when elements added or removed, |
157 |
|
* but size does |
158 |
|
*/ |
159 |
|
public void testRemainingCapacity() { |
182 |
|
} |
183 |
|
|
184 |
|
/** |
185 |
+ |
* add(null) throws NPE |
186 |
+ |
*/ |
187 |
+ |
public void testAddNull() { |
188 |
+ |
try { |
189 |
+ |
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
190 |
+ |
q.add(null); |
191 |
+ |
shouldThrow(); |
192 |
+ |
} catch (NullPointerException success) { } |
193 |
+ |
} |
194 |
+ |
|
195 |
+ |
/** |
196 |
|
* Offer of comparable element succeeds |
197 |
|
*/ |
198 |
|
public void testOffer() { |
237 |
|
} |
238 |
|
catch (NullPointerException success) {} |
239 |
|
} |
240 |
+ |
|
241 |
+ |
/** |
242 |
+ |
* addAll(this) throws IAE |
243 |
+ |
*/ |
244 |
+ |
public void testAddAllSelf() { |
245 |
+ |
try { |
246 |
+ |
PriorityBlockingQueue q = populatedQueue(SIZE); |
247 |
+ |
q.addAll(q); |
248 |
+ |
shouldThrow(); |
249 |
+ |
} |
250 |
+ |
catch (IllegalArgumentException success) {} |
251 |
+ |
} |
252 |
+ |
|
253 |
|
/** |
254 |
|
* addAll of a collection with null elements throws NPE |
255 |
|
*/ |
626 |
|
q.clear(); |
627 |
|
assertTrue(q.isEmpty()); |
628 |
|
assertEquals(0, q.size()); |
629 |
< |
assertEquals(NOCAP, q.remainingCapacity()); |
605 |
< |
q.add(new Integer(1)); |
629 |
> |
q.add(one); |
630 |
|
assertFalse(q.isEmpty()); |
631 |
+ |
assertTrue(q.contains(one)); |
632 |
|
q.clear(); |
633 |
|
assertTrue(q.isEmpty()); |
634 |
|
} |
712 |
|
unexpectedException(); |
713 |
|
} |
714 |
|
} |
715 |
+ |
|
716 |
+ |
/** |
717 |
+ |
* toArray(null) throws NPE |
718 |
+ |
*/ |
719 |
+ |
public void testToArray_BadArg() { |
720 |
+ |
try { |
721 |
+ |
PriorityBlockingQueue q = populatedQueue(SIZE); |
722 |
+ |
Object o[] = q.toArray(null); |
723 |
+ |
shouldThrow(); |
724 |
+ |
} catch(NullPointerException success){} |
725 |
+ |
} |
726 |
+ |
|
727 |
+ |
/** |
728 |
+ |
* toArray with incompatible array type throws CCE |
729 |
+ |
*/ |
730 |
+ |
public void testToArray1_BadArg() { |
731 |
+ |
try { |
732 |
+ |
PriorityBlockingQueue q = populatedQueue(SIZE); |
733 |
+ |
Object o[] = q.toArray(new String[10] ); |
734 |
+ |
shouldThrow(); |
735 |
+ |
} catch(ArrayStoreException success){} |
736 |
+ |
} |
737 |
|
|
738 |
|
/** |
739 |
|
* iterator iterates through all elements |
836 |
|
} |
837 |
|
} |
838 |
|
|
839 |
+ |
/** |
840 |
+ |
* drainTo(null) throws NPE |
841 |
+ |
*/ |
842 |
+ |
public void testDrainToNull() { |
843 |
+ |
PriorityBlockingQueue q = populatedQueue(SIZE); |
844 |
+ |
try { |
845 |
+ |
q.drainTo(null); |
846 |
+ |
shouldThrow(); |
847 |
+ |
} catch(NullPointerException success) { |
848 |
+ |
} |
849 |
+ |
} |
850 |
+ |
|
851 |
+ |
/** |
852 |
+ |
* drainTo(this) throws IAE |
853 |
+ |
*/ |
854 |
+ |
public void testDrainToSelf() { |
855 |
+ |
PriorityBlockingQueue q = populatedQueue(SIZE); |
856 |
+ |
try { |
857 |
+ |
q.drainTo(q); |
858 |
+ |
shouldThrow(); |
859 |
+ |
} catch(IllegalArgumentException success) { |
860 |
+ |
} |
861 |
+ |
} |
862 |
+ |
|
863 |
+ |
/** |
864 |
+ |
* drainTo(c) empties queue into another collection c |
865 |
+ |
*/ |
866 |
+ |
public void testDrainTo() { |
867 |
+ |
PriorityBlockingQueue q = populatedQueue(SIZE); |
868 |
+ |
ArrayList l = new ArrayList(); |
869 |
+ |
q.drainTo(l); |
870 |
+ |
assertEquals(q.size(), 0); |
871 |
+ |
assertEquals(l.size(), SIZE); |
872 |
+ |
for (int i = 0; i < SIZE; ++i) |
873 |
+ |
assertEquals(l.get(i), new Integer(i)); |
874 |
+ |
q.add(zero); |
875 |
+ |
q.add(one); |
876 |
+ |
assertFalse(q.isEmpty()); |
877 |
+ |
assertTrue(q.contains(zero)); |
878 |
+ |
assertTrue(q.contains(one)); |
879 |
+ |
l.clear(); |
880 |
+ |
q.drainTo(l); |
881 |
+ |
assertEquals(q.size(), 0); |
882 |
+ |
assertEquals(l.size(), 2); |
883 |
+ |
for (int i = 0; i < 2; ++i) |
884 |
+ |
assertEquals(l.get(i), new Integer(i)); |
885 |
+ |
} |
886 |
+ |
|
887 |
+ |
/** |
888 |
+ |
* drainTo empties queue |
889 |
+ |
*/ |
890 |
+ |
public void testDrainToWithActivePut() { |
891 |
+ |
final PriorityBlockingQueue q = populatedQueue(SIZE); |
892 |
+ |
Thread t = new Thread(new Runnable() { |
893 |
+ |
public void run() { |
894 |
+ |
q.put(new Integer(SIZE+1)); |
895 |
+ |
} |
896 |
+ |
}); |
897 |
+ |
try { |
898 |
+ |
t.start(); |
899 |
+ |
ArrayList l = new ArrayList(); |
900 |
+ |
q.drainTo(l); |
901 |
+ |
assertTrue(l.size() >= SIZE); |
902 |
+ |
for (int i = 0; i < SIZE; ++i) |
903 |
+ |
assertEquals(l.get(i), new Integer(i)); |
904 |
+ |
t.join(); |
905 |
+ |
assertTrue(q.size() + l.size() >= SIZE); |
906 |
+ |
} catch(Exception e){ |
907 |
+ |
unexpectedException(); |
908 |
+ |
} |
909 |
+ |
} |
910 |
+ |
|
911 |
+ |
/** |
912 |
+ |
* drainTo(null, n) throws NPE |
913 |
+ |
*/ |
914 |
+ |
public void testDrainToNullN() { |
915 |
+ |
PriorityBlockingQueue q = populatedQueue(SIZE); |
916 |
+ |
try { |
917 |
+ |
q.drainTo(null, 0); |
918 |
+ |
shouldThrow(); |
919 |
+ |
} catch(NullPointerException success) { |
920 |
+ |
} |
921 |
+ |
} |
922 |
+ |
|
923 |
+ |
/** |
924 |
+ |
* drainTo(this, n) throws IAE |
925 |
+ |
*/ |
926 |
+ |
public void testDrainToSelfN() { |
927 |
+ |
PriorityBlockingQueue q = populatedQueue(SIZE); |
928 |
+ |
try { |
929 |
+ |
q.drainTo(q, 0); |
930 |
+ |
shouldThrow(); |
931 |
+ |
} catch(IllegalArgumentException success) { |
932 |
+ |
} |
933 |
+ |
} |
934 |
+ |
|
935 |
+ |
/** |
936 |
+ |
* drainTo(c, n) empties first max {n, size} elements of queue into c |
937 |
+ |
*/ |
938 |
+ |
public void testDrainToN() { |
939 |
+ |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE*2); |
940 |
+ |
for (int i = 0; i < SIZE + 2; ++i) { |
941 |
+ |
for(int j = 0; j < SIZE; j++) |
942 |
+ |
assertTrue(q.offer(new Integer(j))); |
943 |
+ |
ArrayList l = new ArrayList(); |
944 |
+ |
q.drainTo(l, i); |
945 |
+ |
int k = (i < SIZE)? i : SIZE; |
946 |
+ |
assertEquals(l.size(), k); |
947 |
+ |
assertEquals(q.size(), SIZE-k); |
948 |
+ |
for (int j = 0; j < k; ++j) |
949 |
+ |
assertEquals(l.get(j), new Integer(j)); |
950 |
+ |
while (q.poll() != null) ; |
951 |
+ |
} |
952 |
+ |
} |
953 |
+ |
|
954 |
+ |
|
955 |
|
} |