test/java/util/Collection/MOAT.java

Print this page
rev 8056 : 8023339: Refined Collection.removeIf UOE conditions
Reviewed-by: mduigou
Contributed-by: paul.sandoz@oracle.com


 190         equal(c.size(), 0);
 191         equal(c.toString(),"[]");
 192         equal(c.toArray().length, 0);
 193         equal(c.toArray(new Object[0]).length, 0);
 194         check(c.toArray(new Object[]{42})[0] == null);
 195 
 196         Object[] a = new Object[1]; a[0] = Boolean.TRUE;
 197         equal(c.toArray(a), a);
 198         equal(a[0], null);
 199         testEmptyIterator(c.iterator());
 200     }
 201 
 202     static <T> void testEmptyIterator(final Iterator<T> it) {
 203         if (rnd.nextBoolean())
 204             check(! it.hasNext());
 205 
 206         THROWS(NoSuchElementException.class,
 207                new Fun(){void f(){ it.next(); }});
 208 
 209         try { it.remove(); }
 210         catch (IllegalStateException _) { pass(); }
 211         catch (UnsupportedOperationException _) { pass(); }
 212         catch (Throwable t) { unexpected(t); }
 213 
 214         if (rnd.nextBoolean())
 215             check(! it.hasNext());
 216     }
 217 
 218     private static void testEmptyList(List<?> c) {
 219         testEmptyCollection(c);
 220         equal(c.hashCode(), 1);
 221         equal2(c, Collections.<Integer>emptyList());
 222     }
 223 
 224     private static <T> void testEmptySet(Set<T> c) {
 225         testEmptyCollection(c);
 226         equal(c.hashCode(), 0);
 227         equal2(c, Collections.<Integer>emptySet());
 228         if (c instanceof NavigableSet<?>)
 229             testEmptyIterator(((NavigableSet<T>)c).descendingIterator());
 230     }
 231 


 263                    new Fun(){void f(){
 264                            ListIterator<Integer> it = c.listIterator();
 265                            it.next(); it.remove();}});
 266     }
 267 
 268     private static void clear(Collection<Integer> c) {
 269         try { c.clear(); }
 270         catch (Throwable t) { unexpected(t); }
 271         testEmptyCollection(c);
 272     }
 273 
 274     private static <K,V> void testEmptyMap(final Map<K,V> m) {
 275         check(m.isEmpty());
 276         equal(m.size(), 0);
 277         equal(m.toString(),"{}");
 278         testEmptySet(m.keySet());
 279         testEmptySet(m.entrySet());
 280         testEmptyCollection(m.values());
 281 
 282         try { check(! m.containsValue(null)); }
 283         catch (NullPointerException _) { /* OK */ }
 284         try { check(! m.containsKey(null)); }
 285         catch (NullPointerException _) { /* OK */ }
 286         check(! m.containsValue(1));
 287         check(! m.containsKey(1));
 288     }
 289 
 290     private static void testImmutableMap(final Map<Integer,Integer> m) {
 291         THROWS(UnsupportedOperationException.class,
 292                new Fun(){void f(){ m.put(1,1); }},
 293                new Fun(){void f(){ m.putAll(singletonMap(1,1)); }});
 294         if (! m.isEmpty()) {
 295             final Integer first = m.keySet().iterator().next();
 296             THROWS(UnsupportedOperationException.class,
 297                    new Fun(){void f(){ m.remove(first); }},
 298                    new Fun(){void f(){ m.clear(); }});
 299             final Map.Entry<Integer,Integer> me
 300                 = m.entrySet().iterator().next();
 301             Integer key = me.getKey();
 302             Integer val = me.getValue();
 303             THROWS(UnsupportedOperationException.class,
 304                    new Fun(){void f(){ me.setValue(3); }});
 305             equal(key, me.getKey());


 693         for (int i = 0; i < 5; i++)
 694             q.add(i);
 695         it = q.iterator();
 696         equal(it.next(), 0);
 697         check(it.hasNext());
 698         for (int i = 1; i < 4; i++)
 699             q.remove(i);
 700         equal(it.next(), 1);
 701         equal(it.next(), 4);
 702     }
 703 
 704     private static void testList(final List<Integer> l) {
 705         //----------------------------------------------------------------
 706         // 4802633: (coll) AbstractList.addAll(-1,emptyCollection)
 707         // doesn't throw IndexOutOfBoundsException
 708         //----------------------------------------------------------------
 709         try {
 710             l.addAll(-1, Collections.<Integer>emptyList());
 711             fail("Expected IndexOutOfBoundsException not thrown");
 712         }
 713         catch (UnsupportedOperationException _) {/* OK */}
 714         catch (IndexOutOfBoundsException _) {/* OK */}
 715         catch (Throwable t) { unexpected(t); }
 716 
 717 //      equal(l instanceof Serializable,
 718 //            l.subList(0,0) instanceof Serializable);
 719         if (l.subList(0,0) instanceof Serializable)
 720             check(l instanceof Serializable);
 721 
 722         equal(l instanceof RandomAccess,
 723               l.subList(0,0) instanceof RandomAccess);
 724     }
 725 
 726     private static void testCollection(Collection<Integer> c) {
 727         try { testCollection1(c); }
 728         catch (Throwable t) { unexpected(t); }
 729     }
 730 
 731     private static void testCollection1(Collection<Integer> c) {
 732 
 733         System.out.println("\n==> " + c.getClass().getName());
 734 




 190         equal(c.size(), 0);
 191         equal(c.toString(),"[]");
 192         equal(c.toArray().length, 0);
 193         equal(c.toArray(new Object[0]).length, 0);
 194         check(c.toArray(new Object[]{42})[0] == null);
 195 
 196         Object[] a = new Object[1]; a[0] = Boolean.TRUE;
 197         equal(c.toArray(a), a);
 198         equal(a[0], null);
 199         testEmptyIterator(c.iterator());
 200     }
 201 
 202     static <T> void testEmptyIterator(final Iterator<T> it) {
 203         if (rnd.nextBoolean())
 204             check(! it.hasNext());
 205 
 206         THROWS(NoSuchElementException.class,
 207                new Fun(){void f(){ it.next(); }});
 208 
 209         try { it.remove(); }
 210         catch (IllegalStateException ignored) { pass(); }
 211         catch (UnsupportedOperationException ignored) { pass(); }
 212         catch (Throwable t) { unexpected(t); }
 213 
 214         if (rnd.nextBoolean())
 215             check(! it.hasNext());
 216     }
 217 
 218     private static void testEmptyList(List<?> c) {
 219         testEmptyCollection(c);
 220         equal(c.hashCode(), 1);
 221         equal2(c, Collections.<Integer>emptyList());
 222     }
 223 
 224     private static <T> void testEmptySet(Set<T> c) {
 225         testEmptyCollection(c);
 226         equal(c.hashCode(), 0);
 227         equal2(c, Collections.<Integer>emptySet());
 228         if (c instanceof NavigableSet<?>)
 229             testEmptyIterator(((NavigableSet<T>)c).descendingIterator());
 230     }
 231 


 263                    new Fun(){void f(){
 264                            ListIterator<Integer> it = c.listIterator();
 265                            it.next(); it.remove();}});
 266     }
 267 
 268     private static void clear(Collection<Integer> c) {
 269         try { c.clear(); }
 270         catch (Throwable t) { unexpected(t); }
 271         testEmptyCollection(c);
 272     }
 273 
 274     private static <K,V> void testEmptyMap(final Map<K,V> m) {
 275         check(m.isEmpty());
 276         equal(m.size(), 0);
 277         equal(m.toString(),"{}");
 278         testEmptySet(m.keySet());
 279         testEmptySet(m.entrySet());
 280         testEmptyCollection(m.values());
 281 
 282         try { check(! m.containsValue(null)); }
 283         catch (NullPointerException ignored) { /* OK */ }
 284         try { check(! m.containsKey(null)); }
 285         catch (NullPointerException ignored) { /* OK */ }
 286         check(! m.containsValue(1));
 287         check(! m.containsKey(1));
 288     }
 289 
 290     private static void testImmutableMap(final Map<Integer,Integer> m) {
 291         THROWS(UnsupportedOperationException.class,
 292                new Fun(){void f(){ m.put(1,1); }},
 293                new Fun(){void f(){ m.putAll(singletonMap(1,1)); }});
 294         if (! m.isEmpty()) {
 295             final Integer first = m.keySet().iterator().next();
 296             THROWS(UnsupportedOperationException.class,
 297                    new Fun(){void f(){ m.remove(first); }},
 298                    new Fun(){void f(){ m.clear(); }});
 299             final Map.Entry<Integer,Integer> me
 300                 = m.entrySet().iterator().next();
 301             Integer key = me.getKey();
 302             Integer val = me.getValue();
 303             THROWS(UnsupportedOperationException.class,
 304                    new Fun(){void f(){ me.setValue(3); }});
 305             equal(key, me.getKey());


 693         for (int i = 0; i < 5; i++)
 694             q.add(i);
 695         it = q.iterator();
 696         equal(it.next(), 0);
 697         check(it.hasNext());
 698         for (int i = 1; i < 4; i++)
 699             q.remove(i);
 700         equal(it.next(), 1);
 701         equal(it.next(), 4);
 702     }
 703 
 704     private static void testList(final List<Integer> l) {
 705         //----------------------------------------------------------------
 706         // 4802633: (coll) AbstractList.addAll(-1,emptyCollection)
 707         // doesn't throw IndexOutOfBoundsException
 708         //----------------------------------------------------------------
 709         try {
 710             l.addAll(-1, Collections.<Integer>emptyList());
 711             fail("Expected IndexOutOfBoundsException not thrown");
 712         }
 713         catch (UnsupportedOperationException ignored) {/* OK */}
 714         catch (IndexOutOfBoundsException ignored) {/* OK */}
 715         catch (Throwable t) { unexpected(t); }
 716 
 717 //      equal(l instanceof Serializable,
 718 //            l.subList(0,0) instanceof Serializable);
 719         if (l.subList(0,0) instanceof Serializable)
 720             check(l instanceof Serializable);
 721 
 722         equal(l instanceof RandomAccess,
 723               l.subList(0,0) instanceof RandomAccess);
 724     }
 725 
 726     private static void testCollection(Collection<Integer> c) {
 727         try { testCollection1(c); }
 728         catch (Throwable t) { unexpected(t); }
 729     }
 730 
 731     private static void testCollection1(Collection<Integer> c) {
 732 
 733         System.out.println("\n==> " + c.getClass().getName());
 734