ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ArrayDequeTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ArrayDequeTest.java (file contents):
Revision 1.1 by dl, Tue Dec 28 16:15:59 2004 UTC vs.
Revision 1.5 by jsr166, Mon Nov 2 20:28:31 2009 UTC

# Line 10 | Line 10 | import java.util.concurrent.*;
10  
11   public class ArrayDequeTest extends JSR166TestCase {
12      public static void main(String[] args) {
13 <        junit.textui.TestRunner.run (suite());  
13 >        junit.textui.TestRunner.run (suite());
14      }
15  
16      public static Test suite() {
# Line 30 | Line 30 | public class ArrayDequeTest extends JSR1
30          assertEquals(n, q.size());
31          return q;
32      }
33 <
33 >
34      /**
35       * new queue is empty
36       */
# Line 102 | Line 102 | public class ArrayDequeTest extends JSR1
102              ArrayDeque q = new ArrayDeque(1);
103              q.push(null);
104              shouldThrow();
105 <        } catch (NullPointerException success) { }  
105 >        } catch (NullPointerException success) { }
106      }
107  
108      /**
# Line 113 | Line 113 | public class ArrayDequeTest extends JSR1
113          q.pollLast();
114          q.push(four);
115          assertEquals(four,q.peekFirst());
116 <    }  
116 >    }
117  
118      /**
119       *  pop removes next element, or throws NSEE if empty
# Line 127 | Line 127 | public class ArrayDequeTest extends JSR1
127              q.pop();
128              shouldThrow();
129          } catch (NoSuchElementException success){
130 <        }  
130 >        }
131      }
132  
133      /**
# Line 138 | Line 138 | public class ArrayDequeTest extends JSR1
138              ArrayDeque q = new ArrayDeque();
139              q.offerFirst(null);
140              shouldThrow();
141 <        } catch (NullPointerException success) {
142 <        }  
141 >        } catch (NullPointerException success) {
142 >        }
143      }
144  
145      /**
146 <     * OfferFirst succeeds
146 >     * OfferFirst succeeds
147       */
148      public void testOfferFirst() {
149          ArrayDeque q = new ArrayDeque();
# Line 152 | Line 152 | public class ArrayDequeTest extends JSR1
152      }
153  
154      /**
155 <     * OfferLast succeeds
155 >     * OfferLast succeeds
156       */
157      public void testOfferLast() {
158          ArrayDeque q = new ArrayDeque();
# Line 246 | Line 246 | public class ArrayDequeTest extends JSR1
246              q.remove();
247              shouldThrow();
248          } catch (NoSuchElementException success){
249 <        }  
249 >        }
250      }
251  
252      /**
# Line 337 | Line 337 | public class ArrayDequeTest extends JSR1
337              q.removeFirst();
338              shouldThrow();
339          } catch (NoSuchElementException success){
340 <        }  
340 >        }
341      }
342  
343      /**
# Line 491 | Line 491 | public class ArrayDequeTest extends JSR1
491              shouldThrow();
492          } catch(ArrayStoreException  success){}
493      }
494 <    
494 >
495      /**
496       *  iterator iterates through all elements
497       */
# Line 528 | Line 528 | public class ArrayDequeTest extends JSR1
528       */
529      public void testIteratorRemove () {
530          final ArrayDeque q = new ArrayDeque();
531 <        q.add(new Integer(1));
532 <        q.add(new Integer(2));
533 <        q.add(new Integer(3));
534 <        Iterator it = q.iterator();
535 <        it.next();
536 <        it.remove();
537 <        it = q.iterator();
538 <        assertEquals(it.next(), new Integer(2));
539 <        assertEquals(it.next(), new Integer(3));
531 >        final Random rng = new Random();
532 >        for (int iters = 0; iters < 100; ++iters) {
533 >            int max = rng.nextInt(5) + 2;
534 >            int split = rng.nextInt(max-1) + 1;
535 >            for (int j = 1; j <= max; ++j)
536 >                q.add(new Integer(j));
537 >            Iterator it = q.iterator();
538 >            for (int j = 1; j <= split; ++j)
539 >                assertEquals(it.next(), new Integer(j));
540 >            it.remove();
541 >            assertEquals(it.next(), new Integer(split+1));
542 >            for (int j = 1; j <= split; ++j)
543 >                q.remove(new Integer(j));
544 >            it = q.iterator();
545 >            for (int j = split+1; j <= max; ++j) {
546 >                assertEquals(it.next(), new Integer(j));
547 >                it.remove();
548 >            }
549 >            assertFalse(it.hasNext());
550 >            assertTrue(q.isEmpty());
551 >        }
552 >    }
553 >
554 >    /**
555 >     *  Descending iterator iterates through all elements
556 >     */
557 >    public void testDescendingIterator() {
558 >        ArrayDeque q = populatedDeque(SIZE);
559 >        int i = 0;
560 >        Iterator it = q.descendingIterator();
561 >        while(it.hasNext()) {
562 >            assertTrue(q.contains(it.next()));
563 >            ++i;
564 >        }
565 >        assertEquals(i, SIZE);
566          assertFalse(it.hasNext());
567 +        try {
568 +            it.next();
569 +        } catch(NoSuchElementException success) {
570 +        }
571 +    }
572 +
573 +    /**
574 +     *  Descending iterator ordering is reverse FIFO
575 +     */
576 +    public void testDescendingIteratorOrdering() {
577 +        final ArrayDeque q = new ArrayDeque();
578 +        for (int iters = 0; iters < 100; ++iters) {
579 +            q.add(new Integer(3));
580 +            q.add(new Integer(2));
581 +            q.add(new Integer(1));
582 +            int k = 0;
583 +            for (Iterator it = q.descendingIterator(); it.hasNext();) {
584 +                int i = ((Integer)(it.next())).intValue();
585 +                assertEquals(++k, i);
586 +            }
587 +
588 +            assertEquals(3, k);
589 +            q.remove();
590 +            q.remove();
591 +            q.remove();
592 +        }
593 +    }
594 +
595 +    /**
596 +     * descendingIterator.remove removes current element
597 +     */
598 +    public void testDescendingIteratorRemove () {
599 +        final ArrayDeque q = new ArrayDeque();
600 +        final Random rng = new Random();
601 +        for (int iters = 0; iters < 100; ++iters) {
602 +            int max = rng.nextInt(5) + 2;
603 +            int split = rng.nextInt(max-1) + 1;
604 +            for (int j = max; j >= 1; --j)
605 +                q.add(new Integer(j));
606 +            Iterator it = q.descendingIterator();
607 +            for (int j = 1; j <= split; ++j)
608 +                assertEquals(it.next(), new Integer(j));
609 +            it.remove();
610 +            assertEquals(it.next(), new Integer(split+1));
611 +            for (int j = 1; j <= split; ++j)
612 +                q.remove(new Integer(j));
613 +            it = q.descendingIterator();
614 +            for (int j = split+1; j <= max; ++j) {
615 +                assertEquals(it.next(), new Integer(j));
616 +                it.remove();
617 +            }
618 +            assertFalse(it.hasNext());
619 +            assertTrue(q.isEmpty());
620 +        }
621      }
622  
623  
# Line 550 | Line 630 | public class ArrayDequeTest extends JSR1
630          for (int i = 0; i < SIZE; ++i) {
631              assertTrue(s.indexOf(String.valueOf(i)) >= 0);
632          }
633 <    }        
633 >    }
634  
635      /**
636       * peekFirst returns element inserted with addFirst
# Line 559 | Line 639 | public class ArrayDequeTest extends JSR1
639          ArrayDeque q = populatedDeque(3);
640          q.addFirst(four);
641          assertEquals(four,q.peekFirst());
642 <    }  
642 >    }
643  
644      /**
645       * peekLast returns element inserted with addLast
# Line 568 | Line 648 | public class ArrayDequeTest extends JSR1
648          ArrayDeque q = populatedDeque(3);
649          q.addLast(four);
650          assertEquals(four,q.peekLast());
651 <    }  
651 >    }
652  
653   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines