Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 1 | /* |
| 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/publicdomain/zero/1.0/ |
| 5 | * Other contributors include Andrew Wright, Jeffrey Hayes, |
| 6 | * Pat Fisher, Mike Judd. |
| 7 | */ |
| 8 | |
| 9 | package jsr166; |
| 10 | |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 11 | import static java.util.concurrent.TimeUnit.MILLISECONDS; |
| 12 | |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 13 | import java.util.ArrayList; |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 14 | import java.util.Arrays; |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 15 | import java.util.Collection; |
| 16 | import java.util.Comparator; |
| 17 | import java.util.Iterator; |
| 18 | import java.util.NoSuchElementException; |
| 19 | import java.util.Queue; |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 20 | import java.util.concurrent.BlockingQueue; |
| 21 | import java.util.concurrent.CountDownLatch; |
| 22 | import java.util.concurrent.Executors; |
| 23 | import java.util.concurrent.ExecutorService; |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 24 | import java.util.concurrent.PriorityBlockingQueue; |
| 25 | |
| 26 | import junit.framework.Test; |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 27 | |
| 28 | public class PriorityBlockingQueueTest extends JSR166TestCase { |
| 29 | |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 30 | // android-note: These tests have been moved into their own separate |
| 31 | // classes to work around CTS issues. |
| 32 | // |
| 33 | // public static class Generic extends BlockingQueueTest { |
| 34 | // protected BlockingQueue emptyCollection() { |
| 35 | // return new PriorityBlockingQueue(); |
| 36 | // } |
| 37 | // } |
| 38 | // |
| 39 | // public static class InitialCapacity extends BlockingQueueTest { |
| 40 | // protected BlockingQueue emptyCollection() { |
| 41 | // return new PriorityBlockingQueue(SIZE); |
| 42 | // } |
| 43 | // } |
| 44 | // |
| 45 | // public static void main(String[] args) { |
| 46 | // main(suite(), args); |
| 47 | // } |
| 48 | // |
| 49 | // public static Test suite() { |
| 50 | // return newTestSuite(PriorityBlockingQueueTest.class, |
| 51 | // new Generic().testSuite(), |
| 52 | // new InitialCapacity().testSuite()); |
| 53 | // } |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 54 | |
| 55 | /** Sample Comparator */ |
| 56 | static class MyReverseComparator implements Comparator { |
| 57 | public int compare(Object x, Object y) { |
| 58 | return ((Comparable)y).compareTo(x); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Returns a new queue of given size containing consecutive |
| 64 | * Integers 0 ... n. |
| 65 | */ |
| 66 | private PriorityBlockingQueue<Integer> populatedQueue(int n) { |
| 67 | PriorityBlockingQueue<Integer> q = |
| 68 | new PriorityBlockingQueue<Integer>(n); |
| 69 | assertTrue(q.isEmpty()); |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 70 | for (int i = n-1; i >= 0; i -= 2) |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 71 | assertTrue(q.offer(new Integer(i))); |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 72 | for (int i = (n & 1); i < n; i += 2) |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 73 | assertTrue(q.offer(new Integer(i))); |
| 74 | assertFalse(q.isEmpty()); |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 75 | assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 76 | assertEquals(n, q.size()); |
| 77 | return q; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * A new queue has unbounded capacity |
| 82 | */ |
| 83 | public void testConstructor1() { |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 84 | assertEquals(Integer.MAX_VALUE, |
| 85 | new PriorityBlockingQueue(SIZE).remainingCapacity()); |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Constructor throws IAE if capacity argument nonpositive |
| 90 | */ |
| 91 | public void testConstructor2() { |
| 92 | try { |
| 93 | new PriorityBlockingQueue(0); |
| 94 | shouldThrow(); |
| 95 | } catch (IllegalArgumentException success) {} |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Initializing from null Collection throws NPE |
| 100 | */ |
| 101 | public void testConstructor3() { |
| 102 | try { |
| 103 | new PriorityBlockingQueue(null); |
| 104 | shouldThrow(); |
| 105 | } catch (NullPointerException success) {} |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Initializing from Collection of null elements throws NPE |
| 110 | */ |
| 111 | public void testConstructor4() { |
| 112 | Collection<Integer> elements = Arrays.asList(new Integer[SIZE]); |
| 113 | try { |
| 114 | new PriorityBlockingQueue(elements); |
| 115 | shouldThrow(); |
| 116 | } catch (NullPointerException success) {} |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Initializing from Collection with some null elements throws NPE |
| 121 | */ |
| 122 | public void testConstructor5() { |
| 123 | Integer[] ints = new Integer[SIZE]; |
| 124 | for (int i = 0; i < SIZE-1; ++i) |
| 125 | ints[i] = i; |
| 126 | Collection<Integer> elements = Arrays.asList(ints); |
| 127 | try { |
| 128 | new PriorityBlockingQueue(elements); |
| 129 | shouldThrow(); |
| 130 | } catch (NullPointerException success) {} |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Queue contains all elements of collection used to initialize |
| 135 | */ |
| 136 | public void testConstructor6() { |
| 137 | Integer[] ints = new Integer[SIZE]; |
| 138 | for (int i = 0; i < SIZE; ++i) |
| 139 | ints[i] = i; |
| 140 | PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints)); |
| 141 | for (int i = 0; i < SIZE; ++i) |
| 142 | assertEquals(ints[i], q.poll()); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * The comparator used in constructor is used |
| 147 | */ |
| 148 | public void testConstructor7() { |
| 149 | MyReverseComparator cmp = new MyReverseComparator(); |
| 150 | PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE, cmp); |
| 151 | assertEquals(cmp, q.comparator()); |
| 152 | Integer[] ints = new Integer[SIZE]; |
| 153 | for (int i = 0; i < SIZE; ++i) |
| 154 | ints[i] = new Integer(i); |
| 155 | q.addAll(Arrays.asList(ints)); |
| 156 | for (int i = SIZE-1; i >= 0; --i) |
| 157 | assertEquals(ints[i], q.poll()); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * isEmpty is true before add, false after |
| 162 | */ |
| 163 | public void testEmpty() { |
| 164 | PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
| 165 | assertTrue(q.isEmpty()); |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 166 | assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 167 | q.add(one); |
| 168 | assertFalse(q.isEmpty()); |
| 169 | q.add(two); |
| 170 | q.remove(); |
| 171 | q.remove(); |
| 172 | assertTrue(q.isEmpty()); |
| 173 | } |
| 174 | |
| 175 | /** |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 176 | * remainingCapacity() always returns Integer.MAX_VALUE |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 177 | */ |
| 178 | public void testRemainingCapacity() { |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 179 | BlockingQueue q = populatedQueue(SIZE); |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 180 | for (int i = 0; i < SIZE; ++i) { |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 181 | assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
| 182 | assertEquals(SIZE - i, q.size()); |
| 183 | assertEquals(i, q.remove()); |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 184 | } |
| 185 | for (int i = 0; i < SIZE; ++i) { |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 186 | assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 187 | assertEquals(i, q.size()); |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 188 | assertTrue(q.add(i)); |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Offer of comparable element succeeds |
| 194 | */ |
| 195 | public void testOffer() { |
| 196 | PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
| 197 | assertTrue(q.offer(zero)); |
| 198 | assertTrue(q.offer(one)); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Offer of non-Comparable throws CCE |
| 203 | */ |
| 204 | public void testOfferNonComparable() { |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 205 | PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 206 | try { |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 207 | q.offer(new Object()); |
| 208 | q.offer(new Object()); |
| 209 | shouldThrow(); |
| 210 | } catch (ClassCastException success) {} |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * add of comparable succeeds |
| 215 | */ |
| 216 | public void testAdd() { |
| 217 | PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
| 218 | for (int i = 0; i < SIZE; ++i) { |
| 219 | assertEquals(i, q.size()); |
| 220 | assertTrue(q.add(new Integer(i))); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * addAll(this) throws IAE |
| 226 | */ |
| 227 | public void testAddAllSelf() { |
| 228 | try { |
| 229 | PriorityBlockingQueue q = populatedQueue(SIZE); |
| 230 | q.addAll(q); |
| 231 | shouldThrow(); |
| 232 | } catch (IllegalArgumentException success) {} |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * addAll of a collection with any null elements throws NPE after |
| 237 | * possibly adding some elements |
| 238 | */ |
| 239 | public void testAddAll3() { |
| 240 | try { |
| 241 | PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
| 242 | Integer[] ints = new Integer[SIZE]; |
| 243 | for (int i = 0; i < SIZE-1; ++i) |
| 244 | ints[i] = new Integer(i); |
| 245 | q.addAll(Arrays.asList(ints)); |
| 246 | shouldThrow(); |
| 247 | } catch (NullPointerException success) {} |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Queue contains all elements of successful addAll |
| 252 | */ |
| 253 | public void testAddAll5() { |
| 254 | Integer[] empty = new Integer[0]; |
| 255 | Integer[] ints = new Integer[SIZE]; |
| 256 | for (int i = SIZE-1; i >= 0; --i) |
| 257 | ints[i] = new Integer(i); |
| 258 | PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
| 259 | assertFalse(q.addAll(Arrays.asList(empty))); |
| 260 | assertTrue(q.addAll(Arrays.asList(ints))); |
| 261 | for (int i = 0; i < SIZE; ++i) |
| 262 | assertEquals(ints[i], q.poll()); |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * all elements successfully put are contained |
| 267 | */ |
| 268 | public void testPut() { |
| 269 | PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
| 270 | for (int i = 0; i < SIZE; ++i) { |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 271 | Integer x = new Integer(i); |
| 272 | q.put(x); |
| 273 | assertTrue(q.contains(x)); |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 274 | } |
| 275 | assertEquals(SIZE, q.size()); |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * put doesn't block waiting for take |
| 280 | */ |
| 281 | public void testPutWithTake() throws InterruptedException { |
| 282 | final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
| 283 | final int size = 4; |
| 284 | Thread t = newStartedThread(new CheckedRunnable() { |
| 285 | public void realRun() { |
| 286 | for (int i = 0; i < size; i++) |
| 287 | q.put(new Integer(0)); |
| 288 | }}); |
| 289 | |
| 290 | awaitTermination(t); |
| 291 | assertEquals(size, q.size()); |
| 292 | q.take(); |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * timed offer does not time out |
| 297 | */ |
| 298 | public void testTimedOffer() throws InterruptedException { |
| 299 | final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
| 300 | Thread t = newStartedThread(new CheckedRunnable() { |
| 301 | public void realRun() { |
| 302 | q.put(new Integer(0)); |
| 303 | q.put(new Integer(0)); |
| 304 | assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, MILLISECONDS)); |
| 305 | assertTrue(q.offer(new Integer(0), LONG_DELAY_MS, MILLISECONDS)); |
| 306 | }}); |
| 307 | |
| 308 | awaitTermination(t); |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * take retrieves elements in priority order |
| 313 | */ |
| 314 | public void testTake() throws InterruptedException { |
| 315 | PriorityBlockingQueue q = populatedQueue(SIZE); |
| 316 | for (int i = 0; i < SIZE; ++i) { |
| 317 | assertEquals(i, q.take()); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Take removes existing elements until empty, then blocks interruptibly |
| 323 | */ |
| 324 | public void testBlockingTake() throws InterruptedException { |
| 325 | final PriorityBlockingQueue q = populatedQueue(SIZE); |
| 326 | final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
| 327 | Thread t = newStartedThread(new CheckedRunnable() { |
| 328 | public void realRun() throws InterruptedException { |
| 329 | for (int i = 0; i < SIZE; ++i) { |
| 330 | assertEquals(i, q.take()); |
| 331 | } |
| 332 | |
| 333 | Thread.currentThread().interrupt(); |
| 334 | try { |
| 335 | q.take(); |
| 336 | shouldThrow(); |
| 337 | } catch (InterruptedException success) {} |
| 338 | assertFalse(Thread.interrupted()); |
| 339 | |
| 340 | pleaseInterrupt.countDown(); |
| 341 | try { |
| 342 | q.take(); |
| 343 | shouldThrow(); |
| 344 | } catch (InterruptedException success) {} |
| 345 | assertFalse(Thread.interrupted()); |
| 346 | }}); |
| 347 | |
| 348 | await(pleaseInterrupt); |
| 349 | assertThreadStaysAlive(t); |
| 350 | t.interrupt(); |
| 351 | awaitTermination(t); |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * poll succeeds unless empty |
| 356 | */ |
| 357 | public void testPoll() { |
| 358 | PriorityBlockingQueue q = populatedQueue(SIZE); |
| 359 | for (int i = 0; i < SIZE; ++i) { |
| 360 | assertEquals(i, q.poll()); |
| 361 | } |
| 362 | assertNull(q.poll()); |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * timed poll with zero timeout succeeds when non-empty, else times out |
| 367 | */ |
| 368 | public void testTimedPoll0() throws InterruptedException { |
| 369 | PriorityBlockingQueue q = populatedQueue(SIZE); |
| 370 | for (int i = 0; i < SIZE; ++i) { |
| 371 | assertEquals(i, q.poll(0, MILLISECONDS)); |
| 372 | } |
| 373 | assertNull(q.poll(0, MILLISECONDS)); |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * timed poll with nonzero timeout succeeds when non-empty, else times out |
| 378 | */ |
| 379 | public void testTimedPoll() throws InterruptedException { |
| 380 | PriorityBlockingQueue<Integer> q = populatedQueue(SIZE); |
| 381 | for (int i = 0; i < SIZE; ++i) { |
| 382 | long startTime = System.nanoTime(); |
| 383 | assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
| 384 | assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
| 385 | } |
| 386 | long startTime = System.nanoTime(); |
| 387 | assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
| 388 | assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
| 389 | checkEmpty(q); |
| 390 | } |
| 391 | |
| 392 | /** |
| 393 | * Interrupted timed poll throws InterruptedException instead of |
| 394 | * returning timeout status |
| 395 | */ |
| 396 | public void testInterruptedTimedPoll() throws InterruptedException { |
| 397 | final BlockingQueue<Integer> q = populatedQueue(SIZE); |
| 398 | final CountDownLatch aboutToWait = new CountDownLatch(1); |
| 399 | Thread t = newStartedThread(new CheckedRunnable() { |
| 400 | public void realRun() throws InterruptedException { |
| 401 | for (int i = 0; i < SIZE; ++i) { |
| 402 | long t0 = System.nanoTime(); |
| 403 | assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
| 404 | assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
| 405 | } |
| 406 | long t0 = System.nanoTime(); |
| 407 | aboutToWait.countDown(); |
| 408 | try { |
| 409 | q.poll(LONG_DELAY_MS, MILLISECONDS); |
| 410 | shouldThrow(); |
| 411 | } catch (InterruptedException success) { |
| 412 | assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
| 413 | } |
| 414 | }}); |
| 415 | |
| 416 | aboutToWait.await(); |
| 417 | waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
| 418 | t.interrupt(); |
| 419 | awaitTermination(t, MEDIUM_DELAY_MS); |
| 420 | } |
| 421 | |
| 422 | /** |
| 423 | * peek returns next element, or null if empty |
| 424 | */ |
| 425 | public void testPeek() { |
| 426 | PriorityBlockingQueue q = populatedQueue(SIZE); |
| 427 | for (int i = 0; i < SIZE; ++i) { |
| 428 | assertEquals(i, q.peek()); |
| 429 | assertEquals(i, q.poll()); |
| 430 | assertTrue(q.peek() == null || |
| 431 | !q.peek().equals(i)); |
| 432 | } |
| 433 | assertNull(q.peek()); |
| 434 | } |
| 435 | |
| 436 | /** |
| 437 | * element returns next element, or throws NSEE if empty |
| 438 | */ |
| 439 | public void testElement() { |
| 440 | PriorityBlockingQueue q = populatedQueue(SIZE); |
| 441 | for (int i = 0; i < SIZE; ++i) { |
| 442 | assertEquals(i, q.element()); |
| 443 | assertEquals(i, q.poll()); |
| 444 | } |
| 445 | try { |
| 446 | q.element(); |
| 447 | shouldThrow(); |
| 448 | } catch (NoSuchElementException success) {} |
| 449 | } |
| 450 | |
| 451 | /** |
| 452 | * remove removes next element, or throws NSEE if empty |
| 453 | */ |
| 454 | public void testRemove() { |
| 455 | PriorityBlockingQueue q = populatedQueue(SIZE); |
| 456 | for (int i = 0; i < SIZE; ++i) { |
| 457 | assertEquals(i, q.remove()); |
| 458 | } |
| 459 | try { |
| 460 | q.remove(); |
| 461 | shouldThrow(); |
| 462 | } catch (NoSuchElementException success) {} |
| 463 | } |
| 464 | |
| 465 | /** |
| 466 | * contains(x) reports true when elements added but not yet removed |
| 467 | */ |
| 468 | public void testContains() { |
| 469 | PriorityBlockingQueue q = populatedQueue(SIZE); |
| 470 | for (int i = 0; i < SIZE; ++i) { |
| 471 | assertTrue(q.contains(new Integer(i))); |
| 472 | q.poll(); |
| 473 | assertFalse(q.contains(new Integer(i))); |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | /** |
| 478 | * clear removes all elements |
| 479 | */ |
| 480 | public void testClear() { |
| 481 | PriorityBlockingQueue q = populatedQueue(SIZE); |
| 482 | q.clear(); |
| 483 | assertTrue(q.isEmpty()); |
| 484 | assertEquals(0, q.size()); |
| 485 | q.add(one); |
| 486 | assertFalse(q.isEmpty()); |
| 487 | assertTrue(q.contains(one)); |
| 488 | q.clear(); |
| 489 | assertTrue(q.isEmpty()); |
| 490 | } |
| 491 | |
| 492 | /** |
| 493 | * containsAll(c) is true when c contains a subset of elements |
| 494 | */ |
| 495 | public void testContainsAll() { |
| 496 | PriorityBlockingQueue q = populatedQueue(SIZE); |
| 497 | PriorityBlockingQueue p = new PriorityBlockingQueue(SIZE); |
| 498 | for (int i = 0; i < SIZE; ++i) { |
| 499 | assertTrue(q.containsAll(p)); |
| 500 | assertFalse(p.containsAll(q)); |
| 501 | p.add(new Integer(i)); |
| 502 | } |
| 503 | assertTrue(p.containsAll(q)); |
| 504 | } |
| 505 | |
| 506 | /** |
| 507 | * retainAll(c) retains only those elements of c and reports true if changed |
| 508 | */ |
| 509 | public void testRetainAll() { |
| 510 | PriorityBlockingQueue q = populatedQueue(SIZE); |
| 511 | PriorityBlockingQueue p = populatedQueue(SIZE); |
| 512 | for (int i = 0; i < SIZE; ++i) { |
| 513 | boolean changed = q.retainAll(p); |
| 514 | if (i == 0) |
| 515 | assertFalse(changed); |
| 516 | else |
| 517 | assertTrue(changed); |
| 518 | |
| 519 | assertTrue(q.containsAll(p)); |
| 520 | assertEquals(SIZE-i, q.size()); |
| 521 | p.remove(); |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | /** |
| 526 | * removeAll(c) removes only those elements of c and reports true if changed |
| 527 | */ |
| 528 | public void testRemoveAll() { |
| 529 | for (int i = 1; i < SIZE; ++i) { |
| 530 | PriorityBlockingQueue q = populatedQueue(SIZE); |
| 531 | PriorityBlockingQueue p = populatedQueue(i); |
| 532 | assertTrue(q.removeAll(p)); |
| 533 | assertEquals(SIZE-i, q.size()); |
| 534 | for (int j = 0; j < i; ++j) { |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 535 | Integer x = (Integer)(p.remove()); |
| 536 | assertFalse(q.contains(x)); |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 537 | } |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | /** |
| 542 | * toArray contains all elements |
| 543 | */ |
| 544 | public void testToArray() throws InterruptedException { |
| 545 | PriorityBlockingQueue q = populatedQueue(SIZE); |
| 546 | Object[] o = q.toArray(); |
| 547 | Arrays.sort(o); |
| 548 | for (int i = 0; i < o.length; i++) |
| 549 | assertSame(o[i], q.take()); |
| 550 | } |
| 551 | |
| 552 | /** |
| 553 | * toArray(a) contains all elements |
| 554 | */ |
| 555 | public void testToArray2() throws InterruptedException { |
| 556 | PriorityBlockingQueue<Integer> q = populatedQueue(SIZE); |
| 557 | Integer[] ints = new Integer[SIZE]; |
| 558 | Integer[] array = q.toArray(ints); |
| 559 | assertSame(ints, array); |
| 560 | Arrays.sort(ints); |
| 561 | for (int i = 0; i < ints.length; i++) |
| 562 | assertSame(ints[i], q.take()); |
| 563 | } |
| 564 | |
| 565 | /** |
| 566 | * toArray(incompatible array type) throws ArrayStoreException |
| 567 | */ |
| 568 | public void testToArray1_BadArg() { |
| 569 | PriorityBlockingQueue q = populatedQueue(SIZE); |
| 570 | try { |
| 571 | q.toArray(new String[10]); |
| 572 | shouldThrow(); |
| 573 | } catch (ArrayStoreException success) {} |
| 574 | } |
| 575 | |
| 576 | /** |
| 577 | * iterator iterates through all elements |
| 578 | */ |
| 579 | public void testIterator() { |
| 580 | PriorityBlockingQueue q = populatedQueue(SIZE); |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 581 | Iterator it = q.iterator(); |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 582 | int i; |
| 583 | for (i = 0; it.hasNext(); i++) |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 584 | assertTrue(q.contains(it.next())); |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 585 | assertEquals(i, SIZE); |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 586 | assertIteratorExhausted(it); |
| 587 | } |
| 588 | |
| 589 | /** |
| 590 | * iterator of empty collection has no elements |
| 591 | */ |
| 592 | public void testEmptyIterator() { |
| 593 | assertIteratorExhausted(new PriorityBlockingQueue().iterator()); |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | /** |
| 597 | * iterator.remove removes current element |
| 598 | */ |
| 599 | public void testIteratorRemove() { |
| 600 | final PriorityBlockingQueue q = new PriorityBlockingQueue(3); |
| 601 | q.add(new Integer(2)); |
| 602 | q.add(new Integer(1)); |
| 603 | q.add(new Integer(3)); |
| 604 | |
| 605 | Iterator it = q.iterator(); |
| 606 | it.next(); |
| 607 | it.remove(); |
| 608 | |
| 609 | it = q.iterator(); |
| 610 | assertEquals(it.next(), new Integer(2)); |
| 611 | assertEquals(it.next(), new Integer(3)); |
| 612 | assertFalse(it.hasNext()); |
| 613 | } |
| 614 | |
| 615 | /** |
| 616 | * toString contains toStrings of elements |
| 617 | */ |
| 618 | public void testToString() { |
| 619 | PriorityBlockingQueue q = populatedQueue(SIZE); |
| 620 | String s = q.toString(); |
| 621 | for (int i = 0; i < SIZE; ++i) { |
| 622 | assertTrue(s.contains(String.valueOf(i))); |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | /** |
| 627 | * timed poll transfers elements across Executor tasks |
| 628 | */ |
| 629 | public void testPollInExecutor() { |
| 630 | final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
| 631 | final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
| 632 | ExecutorService executor = Executors.newFixedThreadPool(2); |
| 633 | executor.execute(new CheckedRunnable() { |
| 634 | public void realRun() throws InterruptedException { |
| 635 | assertNull(q.poll()); |
| 636 | threadsStarted.await(); |
| 637 | assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
| 638 | checkEmpty(q); |
| 639 | }}); |
| 640 | |
| 641 | executor.execute(new CheckedRunnable() { |
| 642 | public void realRun() throws InterruptedException { |
| 643 | threadsStarted.await(); |
| 644 | q.put(one); |
| 645 | }}); |
| 646 | |
| 647 | joinPool(executor); |
| 648 | } |
| 649 | |
| 650 | /** |
| 651 | * A deserialized serialized queue has same elements |
| 652 | */ |
| 653 | public void testSerialization() throws Exception { |
| 654 | Queue x = populatedQueue(SIZE); |
| 655 | Queue y = serialClone(x); |
| 656 | |
| 657 | assertNotSame(x, y); |
| 658 | assertEquals(x.size(), y.size()); |
| 659 | while (!x.isEmpty()) { |
| 660 | assertFalse(y.isEmpty()); |
| 661 | assertEquals(x.remove(), y.remove()); |
| 662 | } |
| 663 | assertTrue(y.isEmpty()); |
| 664 | } |
| 665 | |
| 666 | /** |
| 667 | * drainTo(c) empties queue into another collection c |
| 668 | */ |
| 669 | public void testDrainTo() { |
| 670 | PriorityBlockingQueue q = populatedQueue(SIZE); |
| 671 | ArrayList l = new ArrayList(); |
| 672 | q.drainTo(l); |
| 673 | assertEquals(0, q.size()); |
| 674 | assertEquals(SIZE, l.size()); |
| 675 | for (int i = 0; i < SIZE; ++i) |
| 676 | assertEquals(l.get(i), new Integer(i)); |
| 677 | q.add(zero); |
| 678 | q.add(one); |
| 679 | assertFalse(q.isEmpty()); |
| 680 | assertTrue(q.contains(zero)); |
| 681 | assertTrue(q.contains(one)); |
| 682 | l.clear(); |
| 683 | q.drainTo(l); |
| 684 | assertEquals(0, q.size()); |
| 685 | assertEquals(2, l.size()); |
| 686 | for (int i = 0; i < 2; ++i) |
| 687 | assertEquals(l.get(i), new Integer(i)); |
| 688 | } |
| 689 | |
| 690 | /** |
| 691 | * drainTo empties queue |
| 692 | */ |
| 693 | public void testDrainToWithActivePut() throws InterruptedException { |
| 694 | final PriorityBlockingQueue q = populatedQueue(SIZE); |
| 695 | Thread t = new Thread(new CheckedRunnable() { |
| 696 | public void realRun() { |
| 697 | q.put(new Integer(SIZE+1)); |
| 698 | }}); |
| 699 | |
| 700 | t.start(); |
| 701 | ArrayList l = new ArrayList(); |
| 702 | q.drainTo(l); |
| 703 | assertTrue(l.size() >= SIZE); |
| 704 | for (int i = 0; i < SIZE; ++i) |
| 705 | assertEquals(l.get(i), new Integer(i)); |
| 706 | t.join(); |
| 707 | assertTrue(q.size() + l.size() >= SIZE); |
| 708 | } |
| 709 | |
| 710 | /** |
| 711 | * drainTo(c, n) empties first min(n, size) elements of queue into c |
| 712 | */ |
| 713 | public void testDrainToN() { |
| 714 | PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE*2); |
| 715 | for (int i = 0; i < SIZE + 2; ++i) { |
| 716 | for (int j = 0; j < SIZE; j++) |
| 717 | assertTrue(q.offer(new Integer(j))); |
| 718 | ArrayList l = new ArrayList(); |
| 719 | q.drainTo(l, i); |
| 720 | int k = (i < SIZE) ? i : SIZE; |
| 721 | assertEquals(k, l.size()); |
| 722 | assertEquals(SIZE-k, q.size()); |
| 723 | for (int j = 0; j < k; ++j) |
| 724 | assertEquals(l.get(j), new Integer(j)); |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 725 | do {} while (q.poll() != null); |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | /** |
| 730 | * remove(null), contains(null) always return false |
| 731 | */ |
| 732 | public void testNeverContainsNull() { |
| 733 | Collection<?>[] qs = { |
| 734 | new PriorityBlockingQueue<Object>(), |
| 735 | populatedQueue(2), |
| 736 | }; |
| 737 | |
| 738 | for (Collection<?> q : qs) { |
| 739 | assertFalse(q.contains(null)); |
| 740 | assertFalse(q.remove(null)); |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 741 | } |
| 742 | } |
| 743 | |
| 744 | } |