/* * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Objects; import java.util.Set; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertTrue; import static org.testng.Assert.fail; /** * @library * CollectionAssert -- assertion methods for lambda test cases */ public class CollectionAsserts { private CollectionAsserts() { // no instances } public static void assertCountSum(Iterable it, int count, int sum) { assertCountSum(it.iterator(), count, sum); } public static void assertCountSum(Iterator it, int count, int sum) { int c = 0; int s = 0; while (it.hasNext()) { int i = (Integer) it.next(); c++; s += i; } assertEquals(c, count); assertEquals(s, sum); } public static void assertConcat(Iterator it, String result) { StringBuilder sb = new StringBuilder(); while (it.hasNext()) { sb.append(it.next()); } assertEquals(result, sb.toString()); } public static> void assertSorted(Iterator i) { if (!i.hasNext()) return; T last = i.next(); while (i.hasNext()) { T t = i.next(); assertTrue(last.compareTo(t) <= 0); assertTrue(t.compareTo(last) >= 0); last = t; } } public static void assertSorted(Iterator i, Comparator comp) { if (!i.hasNext()) return; T last = i.next(); while (i.hasNext()) { T t = i.next(); assertTrue(comp.compare(last, t) <= 0); assertTrue(comp.compare(t, last) >= 0); last = t; } } public static> void assertSorted(Iterable iter) { assertSorted(iter.iterator()); } public static void assertSorted(Iterable iter, Comparator comp) { assertSorted(iter.iterator(), comp); } public static void assertUnique(Iterable iter) { assertUnique(iter.iterator()); } public static void assertUnique(Iterator iter) { if (!iter.hasNext()) { return; } Set uniq = new HashSet<>(); while(iter.hasNext()) { T each = iter.next(); assertTrue(!uniq.contains(each)); uniq.add(each); } } public static void assertContents(Iterable actual, Iterable expected) { assertContents(actual, expected, null); } public static void assertContents(Iterable actual, Iterable expected, String msg) { assertContents(actual.iterator(), expected.iterator(), msg); } public static void assertContents(Iterator actual, Iterator expected) { assertContents(actual, expected, null); } public static void assertContents(Iterator actual, Iterator expected, String msg) { List history = new ArrayList<>(); while (expected.hasNext()) { if (!actual.hasNext()) { List 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 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 void assertContents(Iterator actual, T... expected) { assertContents(actual, Arrays.asList(expected).iterator()); } public static> void assertContentsUnordered(Iterable actual, Iterable expected) { assertContentsUnordered(actual, expected, null); } public static> void assertContentsUnordered(Iterable actual, Iterable expected, String msg) { List 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 void assertSplitContents(Iterable> splits, Iterable list) { Iterator> mI = splits.iterator(); Iterator pI = null; Iterator lI = list.iterator(); while (lI.hasNext()) { if (pI == null) pI = mI.next().iterator(); while (!pI.hasNext()) { if (!mI.hasNext()) { break; } else { pI = mI.next().iterator(); } } assertTrue(pI.hasNext()); T pT = pI.next(); T lT = lI.next(); assertEquals(pT, lT); } if (pI != null) { assertTrue(!pI.hasNext()); } while(mI.hasNext()) { pI = mI.next().iterator(); assertTrue(!pI.hasNext()); } } }