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())
585
586 private static boolean supportsAdd(Collection<Integer> c) {
587 try { check(c.add(ABSENT_VALUE)); }
588 catch (UnsupportedOperationException t) { return false; }
589 catch (Throwable t) { unexpected(t); }
590
591 try {
592 check(c.contains(ABSENT_VALUE));
593 check(c.remove(ABSENT_VALUE));
594 } catch (Throwable t) { unexpected(t); }
595 return true;
596 }
597
598 private static boolean supportsRemove(Collection<Integer> c) {
599 try { check(! c.remove(ABSENT_VALUE)); }
600 catch (UnsupportedOperationException t) { return false; }
601 catch (Throwable t) { unexpected(t); }
602 return true;
603 }
604
605 // 6260652: (coll) Arrays.asList(x).toArray().getClass()
606 // should be Object[].class
607 // Fixed in jdk9, but not jdk8 ...
608 static final boolean needToWorkAround6260652 =
609 Arrays.asList("").toArray().getClass() != Object[].class;
610
611 private static void checkFunctionalInvariants(Collection<Integer> c) {
612 try {
613 checkContainsSelf(c);
614 checkContainsEmpty(c);
615 check(c.size() != 0 ^ c.isEmpty());
616 check(! c.contains(ABSENT_VALUE));
617
618 {
619 int size = 0;
620 for (Integer i : c) size++;
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())
586
587 private static boolean supportsAdd(Collection<Integer> c) {
588 try { check(c.add(ABSENT_VALUE)); }
589 catch (UnsupportedOperationException t) { return false; }
590 catch (Throwable t) { unexpected(t); }
591
592 try {
593 check(c.contains(ABSENT_VALUE));
594 check(c.remove(ABSENT_VALUE));
595 } catch (Throwable t) { unexpected(t); }
596 return true;
597 }
598
599 private static boolean supportsRemove(Collection<Integer> c) {
600 try { check(! c.remove(ABSENT_VALUE)); }
601 catch (UnsupportedOperationException t) { return false; }
602 catch (Throwable t) { unexpected(t); }
603 return true;
604 }
605
606 private static void checkFunctionalInvariants(Collection<Integer> c) {
607 try {
608 checkContainsSelf(c);
609 checkContainsEmpty(c);
610 check(c.size() != 0 ^ c.isEmpty());
611 check(! c.contains(ABSENT_VALUE));
612
613 {
614 int size = 0;
615 for (Integer i : c) size++;
616 check(c.size() == size);
617 }
618
619 if (c instanceof Set) {
620 checkUnique((Set<Integer>)c);
621 }
622
623 check(c.toArray().length == c.size());
624 check(c.toArray().getClass() == Object[].class);
625 for (int size : new int[]{0,1,c.size(), c.size()+1}) {
626 Integer[] a = c.toArray(new Integer[size]);
627 check((size > c.size()) || a.length == c.size());
628 int i = 0; for (Integer j : c) check(a[i++] == j);
629 check((size <= c.size()) || (a[c.size()] == null));
630 check(a.getClass() == Integer[].class);
631 }
632
633 {
634 Integer[] a = c.toArray(Integer[]::new);
635 equal(c.size(), a.length);
636 check(a.getClass() == Integer[].class);
637 check(Arrays.equals(c.toArray(new Integer[0]), a));
638 }
639
640 check(c.equals(c));
641 if (c instanceof Serializable) {
642 //System.out.printf("Serializing %s%n", c.getClass().getName());
643 try {
644 Object clone = serialClone(c);
645 equal(c instanceof Serializable,
646 clone instanceof Serializable);
647 equal(c instanceof RandomAccess,
648 clone instanceof RandomAccess);
649 if ((c instanceof List) || (c instanceof Set))
650 equal(c, clone);
651 else
652 equal(new HashSet<Integer>(c),
653 new HashSet<Integer>(serialClone(c)));
654 } catch (Error xxx) {
655 if (! (xxx.getCause() instanceof NotSerializableException))
656 throw xxx;
657 }
658 }
659 }
|