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.2 by dl, Wed Sep 14 23:50:31 2005 UTC vs.
Revision 1.6 by jsr166, Mon Nov 16 04:57:10 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 24 | Line 24 | public class ArrayDequeTest extends JSR1
24      private ArrayDeque populatedDeque(int n) {
25          ArrayDeque q = new ArrayDeque();
26          assertTrue(q.isEmpty());
27 <        for(int i = 0; i < n; ++i)
27 >        for (int i = 0; i < n; ++i)
28              assertTrue(q.offerLast(new Integer(i)));
29          assertFalse(q.isEmpty());
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 452 | Line 452 | public class ArrayDequeTest extends JSR1
452          ArrayDeque q = populatedDeque(SIZE);
453          Object[] o = q.toArray();
454          Arrays.sort(o);
455 <        for(int i = 0; i < o.length; i++)
455 >        for (int i = 0; i < o.length; i++)
456              assertEquals(o[i], q.pollFirst());
457      }
458  
# Line 464 | Line 464 | public class ArrayDequeTest extends JSR1
464          Integer[] ints = new Integer[SIZE];
465          ints = (Integer[])q.toArray(ints);
466          Arrays.sort(ints);
467 <        for(int i = 0; i < ints.length; i++)
467 >        for (int i = 0; i < ints.length; i++)
468              assertEquals(ints[i], q.pollFirst());
469      }
470  
# Line 477 | Line 477 | public class ArrayDequeTest extends JSR1
477              l.add(new Object());
478              Object o[] = l.toArray(null);
479              shouldThrow();
480 <        } catch(NullPointerException success){}
480 >        } catch (NullPointerException success){}
481      }
482  
483      /**
# Line 489 | Line 489 | public class ArrayDequeTest extends JSR1
489              l.add(new Integer(5));
490              Object o[] = l.toArray(new String[10] );
491              shouldThrow();
492 <        } catch(ArrayStoreException  success){}
492 >        } catch (ArrayStoreException  success){}
493      }
494 <    
494 >
495      /**
496       *  iterator iterates through all elements
497       */
# Line 499 | Line 499 | public class ArrayDequeTest extends JSR1
499          ArrayDeque q = populatedDeque(SIZE);
500          int i = 0;
501          Iterator it = q.iterator();
502 <        while(it.hasNext()) {
502 >        while (it.hasNext()) {
503              assertTrue(q.contains(it.next()));
504              ++i;
505          }
# 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));
540 <        assertFalse(it.hasNext());
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      /**
# Line 547 | Line 558 | public class ArrayDequeTest extends JSR1
558          ArrayDeque q = populatedDeque(SIZE);
559          int i = 0;
560          Iterator it = q.descendingIterator();
561 <        while(it.hasNext()) {
561 >        while (it.hasNext()) {
562              assertTrue(q.contains(it.next()));
563              ++i;
564          }
# Line 555 | Line 566 | public class ArrayDequeTest extends JSR1
566          assertFalse(it.hasNext());
567          try {
568              it.next();
569 <        } catch(NoSuchElementException success) {
569 >        } catch (NoSuchElementException success) {
570          }
571      }
572  
# Line 564 | Line 575 | public class ArrayDequeTest extends JSR1
575       */
576      public void testDescendingIteratorOrdering() {
577          final ArrayDeque q = new ArrayDeque();
578 <        q.add(new Integer(3));
579 <        q.add(new Integer(2));
580 <        q.add(new Integer(1));
581 <        int k = 0;
582 <        for (Iterator it = q.descendingIterator(); it.hasNext();) {
583 <            int i = ((Integer)(it.next())).intValue();
584 <            assertEquals(++k, i);
585 <        }
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);
588 >            assertEquals(3, k);
589 >            q.remove();
590 >            q.remove();
591 >            q.remove();
592 >        }
593      }
594  
595      /**
# Line 581 | Line 597 | public class ArrayDequeTest extends JSR1
597       */
598      public void testDescendingIteratorRemove () {
599          final ArrayDeque q = new ArrayDeque();
600 <        q.add(new Integer(3));
601 <        q.add(new Integer(2));
602 <        q.add(new Integer(1));
603 <        Iterator it = q.descendingIterator();
604 <        it.next();
605 <        it.remove();
606 <        it = q.descendingIterator();
607 <        assertEquals(it.next(), new Integer(2));
608 <        assertEquals(it.next(), new Integer(3));
609 <        assertFalse(it.hasNext());
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 603 | 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 612 | 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 621 | 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