test/java/util/Collection/testlibrary/CollectionAsserts.java

Print this page
rev 7932 : 8021591: Additional explicit null checks
Reviewed-by: psandoz, martin, alanb

*** 39,48 **** --- 39,52 ---- * @library * CollectionAssert -- assertion methods for lambda test cases */ public class CollectionAsserts { + private CollectionAsserts() { + // no instances + } + public static void assertCountSum(Iterable<? super Integer> it, int count, int sum) { assertCountSum(it.iterator(), count, sum); } public static void assertCountSum(Iterator<? super Integer> it, int count, int sum) {
*** 115,182 **** uniq.add(each); } } public static<T> void assertContents(Iterable<T> actual, Iterable<T> expected) { ! assertContents(actual.iterator(), expected.iterator()); } public static<T> void assertContents(Iterator<T> actual, Iterator<T> expected) { List<T> history = new ArrayList<>(); while (expected.hasNext()) { if (!actual.hasNext()) { List<T> expectedData = new ArrayList<>(history); while (expected.hasNext()) expectedData.add(expected.next()); ! fail(String.format("Premature end of data; expected=%s, found=%s", expectedData, history)); } T a = actual.next(); T e = expected.next(); history.add(a); if (!Objects.equals(a, e)) ! fail(String.format("Data mismatch; preceding=%s, nextExpected=%s, nextFound=%s", history, e, a)); } if (actual.hasNext()) { List<T> rest = new ArrayList<>(); while (actual.hasNext()) rest.add(actual.next()); ! fail(String.format("Unexpected data %s after %s", rest, history)); } } @SafeVarargs @SuppressWarnings("varargs") public static<T> void assertContents(Iterator<T> actual, T... expected) { assertContents(actual, Arrays.asList(expected).iterator()); } ! public static <T> boolean equalsContentsUnordered(Iterable<T> a, Iterable<T> b) { ! Set<T> sa = new HashSet<>(); ! for (T t : a) { ! sa.add(t); } ! Set<T> sb = new HashSet<>(); ! for (T t : b) { ! sb.add(t); } ! return Objects.equals(sa, sb); } ! public static<T extends Comparable<? super T>> void assertContentsUnordered(Iterable<T> actual, Iterable<T> expected) { ! ArrayList<T> one = new ArrayList<>(); ! for (T t : actual) ! one.add(t); ! ArrayList<T> two = new ArrayList<>(); ! for (T t : expected) ! two.add(t); ! Collections.sort(one); ! Collections.sort(two); ! assertContents(one, two); } static <T> void assertSplitContents(Iterable<Iterable<T>> splits, Iterable<T> list) { Iterator<Iterable<T>> mI = splits.iterator(); Iterator<T> pI = null; --- 119,188 ---- uniq.add(each); } } public static<T> void assertContents(Iterable<T> actual, Iterable<T> expected) { ! assertContents(actual, expected, null); ! } ! ! public static<T> void assertContents(Iterable<T> actual, Iterable<T> expected, String msg) { ! assertContents(actual.iterator(), expected.iterator(), msg); } public static<T> void assertContents(Iterator<T> actual, Iterator<T> expected) { + assertContents(actual, expected, null); + } + + public static<T> void assertContents(Iterator<T> actual, Iterator<T> expected, String msg) { List<T> history = new ArrayList<>(); while (expected.hasNext()) { if (!actual.hasNext()) { List<T> expectedData = new ArrayList<>(history); while (expected.hasNext()) expectedData.add(expected.next()); ! fail(String.format("%s Premature end of data; expected=%s, found=%s", ! (msg == null ? "" : msg), expectedData, history)); } T a = actual.next(); T e = expected.next(); history.add(a); if (!Objects.equals(a, e)) ! fail(String.format("%s Data mismatch; preceding=%s, nextExpected=%s, nextFound=%s", ! (msg == null ? "" : msg), history, e, a)); } if (actual.hasNext()) { List<T> rest = new ArrayList<>(); while (actual.hasNext()) rest.add(actual.next()); ! fail(String.format("%s Unexpected data %s after %s", ! (msg == null ? "" : msg), rest, history)); } } @SafeVarargs @SuppressWarnings("varargs") public static<T> void assertContents(Iterator<T> actual, T... expected) { assertContents(actual, Arrays.asList(expected).iterator()); } ! public static<T extends Comparable<? super T>> void assertContentsUnordered(Iterable<T> actual, Iterable<T> expected) { ! assertContentsUnordered(actual, expected, null); } ! public static<T extends Comparable<? super T>> void assertContentsUnordered(Iterable<T> actual, Iterable<T> expected, String msg) { ! List<T> allExpected = new ArrayList<>(); ! for (T t : expected) { ! allExpected.add(t); } ! for (T t : actual) { ! assertTrue(allExpected.remove(t), msg + " element '" + String.valueOf(t) + "' not found"); } ! assertTrue(allExpected.isEmpty(), msg + "expected contained additional elements"); } static <T> void assertSplitContents(Iterable<Iterable<T>> splits, Iterable<T> list) { Iterator<Iterable<T>> mI = splits.iterator(); Iterator<T> pI = null;