--- /dev/null 2012-12-10 07:41:39.297440197 -0800 +++ new/test/java/util/CollectionExtensionMethods/testlibrary/CollectionAsserts.java 2012-12-10 21:19:12.207766278 -0800 @@ -0,0 +1,207 @@ +/* + * Copyright (c) 1997, 2010, 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. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * 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.*; +import java.util.Iterator; +import java.util.function.*; + +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 { + + 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.iterator(), expected.iterator()); + } + + public static void assertContents(Iterator actual, Iterator expected) { + 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("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 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 void assertContents(Iterator actual, T... expected) { + assertContents(actual, Arrays.asList(expected).iterator()); + } + + public static boolean equalsContentsUnordered(Iterable a, Iterable b) { + Set sa = new HashSet<>(); + for (T t : a) { + sa.add(t); + } + + Set sb = new HashSet<>(); + for (T t : b) { + sb.add(t); + } + + return Objects.equals(sa, sb); + } + + public static> void assertContentsUnordered(Iterable actual, Iterable expected) { + ArrayList one = new ArrayList<>(); + for (T t : actual) + one.add(t); + ArrayList two = new ArrayList<>(); + for (T t : expected) + two.add(t); + Collections.sort(one); + Collections.sort(two); + assertContents(one, two); + } + + 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()); + } + } +}