< prev index next >

test/jdk/java/util/Collection/MOAT.java

Print this page
rev 48215 : 8060192: Add default method <A> A[] Collection.toArray(IntFunction<A[]> generator)
Reviewed-by: martin, forax, psandoz


 346         check(c.containsAll(new ArrayList<Integer>()));
 347     }
 348 
 349     private static void checkUnique(Set<Integer> s) {
 350         for (Integer i : s) {
 351             int count = 0;
 352             for (Integer j : s) {
 353                 if (Objects.equals(i,j))
 354                     ++count;
 355             }
 356             check(count == 1);
 357         }
 358     }
 359 
 360     private static <T> void testEmptyCollection(Collection<T> c) {
 361         check(c.isEmpty());
 362         equal(c.size(), 0);
 363         equal(c.toString(),"[]");
 364         equal(c.toArray().length, 0);
 365         equal(c.toArray(new Object[0]).length, 0);

 366         check(c.toArray(new Object[]{42})[0] == null);
 367 
 368         Object[] a = new Object[1]; a[0] = Boolean.TRUE;
 369         equal(c.toArray(a), a);
 370         equal(a[0], null);
 371         testEmptyIterator(c.iterator());
 372     }
 373 
 374     static <T> void testEmptyIterator(final Iterator<T> it) {
 375         if (rnd.nextBoolean())
 376             check(! it.hasNext());
 377 
 378         THROWS(NoSuchElementException.class, () -> it.next());
 379 
 380         try { it.remove(); }
 381         catch (IllegalStateException ignored) { pass(); }
 382         catch (UnsupportedOperationException ignored) { pass(); }
 383         catch (Throwable t) { unexpected(t); }
 384 
 385         if (rnd.nextBoolean())


 621                 check(c.size() == size);
 622             }
 623 
 624             if (c instanceof Set) {
 625                 checkUnique((Set<Integer>)c);
 626             }
 627 
 628             check(c.toArray().length == c.size());
 629             check(c.toArray().getClass() == Object[].class
 630                   ||
 631                   (needToWorkAround6260652 &&
 632                    c.getClass().getName().equals("java.util.Arrays$ArrayList")));
 633             for (int size : new int[]{0,1,c.size(), c.size()+1}) {
 634                 Integer[] a = c.toArray(new Integer[size]);
 635                 check((size > c.size()) || a.length == c.size());
 636                 int i = 0; for (Integer j : c) check(a[i++] == j);
 637                 check((size <= c.size()) || (a[c.size()] == null));
 638                 check(a.getClass() == Integer[].class);
 639             }
 640 







 641             check(c.equals(c));
 642             if (c instanceof Serializable) {
 643                 //System.out.printf("Serializing %s%n", c.getClass().getName());
 644                 try {
 645                     Object clone = serialClone(c);
 646                     equal(c instanceof Serializable,
 647                           clone instanceof Serializable);
 648                     equal(c instanceof RandomAccess,
 649                           clone instanceof RandomAccess);
 650                     if ((c instanceof List) || (c instanceof Set))
 651                         equal(c, clone);
 652                     else
 653                         equal(new HashSet<Integer>(c),
 654                               new HashSet<Integer>(serialClone(c)));
 655                 } catch (Error xxx) {
 656                     if (! (xxx.getCause() instanceof NotSerializableException))
 657                         throw xxx;
 658                 }
 659             }
 660         }




 346         check(c.containsAll(new ArrayList<Integer>()));
 347     }
 348 
 349     private static void checkUnique(Set<Integer> s) {
 350         for (Integer i : s) {
 351             int count = 0;
 352             for (Integer j : s) {
 353                 if (Objects.equals(i,j))
 354                     ++count;
 355             }
 356             check(count == 1);
 357         }
 358     }
 359 
 360     private static <T> void testEmptyCollection(Collection<T> c) {
 361         check(c.isEmpty());
 362         equal(c.size(), 0);
 363         equal(c.toString(),"[]");
 364         equal(c.toArray().length, 0);
 365         equal(c.toArray(new Object[0]).length, 0);
 366         equal(c.toArray(Object[]::new).length, 0);
 367         check(c.toArray(new Object[]{42})[0] == null);
 368 
 369         Object[] a = new Object[1]; a[0] = Boolean.TRUE;
 370         equal(c.toArray(a), a);
 371         equal(a[0], null);
 372         testEmptyIterator(c.iterator());
 373     }
 374 
 375     static <T> void testEmptyIterator(final Iterator<T> it) {
 376         if (rnd.nextBoolean())
 377             check(! it.hasNext());
 378 
 379         THROWS(NoSuchElementException.class, () -> it.next());
 380 
 381         try { it.remove(); }
 382         catch (IllegalStateException ignored) { pass(); }
 383         catch (UnsupportedOperationException ignored) { pass(); }
 384         catch (Throwable t) { unexpected(t); }
 385 
 386         if (rnd.nextBoolean())


 622                 check(c.size() == size);
 623             }
 624 
 625             if (c instanceof Set) {
 626                 checkUnique((Set<Integer>)c);
 627             }
 628 
 629             check(c.toArray().length == c.size());
 630             check(c.toArray().getClass() == Object[].class
 631                   ||
 632                   (needToWorkAround6260652 &&
 633                    c.getClass().getName().equals("java.util.Arrays$ArrayList")));
 634             for (int size : new int[]{0,1,c.size(), c.size()+1}) {
 635                 Integer[] a = c.toArray(new Integer[size]);
 636                 check((size > c.size()) || a.length == c.size());
 637                 int i = 0; for (Integer j : c) check(a[i++] == j);
 638                 check((size <= c.size()) || (a[c.size()] == null));
 639                 check(a.getClass() == Integer[].class);
 640             }
 641 
 642             {
 643                 Integer[] a = c.toArray(Integer[]::new);
 644                 equal(c.size(), a.length);
 645                 check(a.getClass() == Integer[].class);
 646                 check(Arrays.equals(c.toArray(new Integer[0]), a));
 647             }
 648 
 649             check(c.equals(c));
 650             if (c instanceof Serializable) {
 651                 //System.out.printf("Serializing %s%n", c.getClass().getName());
 652                 try {
 653                     Object clone = serialClone(c);
 654                     equal(c instanceof Serializable,
 655                           clone instanceof Serializable);
 656                     equal(c instanceof RandomAccess,
 657                           clone instanceof RandomAccess);
 658                     if ((c instanceof List) || (c instanceof Set))
 659                         equal(c, clone);
 660                     else
 661                         equal(new HashSet<Integer>(c),
 662                               new HashSet<Integer>(serialClone(c)));
 663                 } catch (Error xxx) {
 664                     if (! (xxx.getCause() instanceof NotSerializableException))
 665                         throw xxx;
 666                 }
 667             }
 668         }


< prev index next >