Path: blob/master/test/jdk/java/util/Collection/testlibrary/CollectionAsserts.java
41153 views
/*1* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.util.ArrayList;24import java.util.Arrays;25import java.util.Comparator;26import java.util.HashSet;27import java.util.Iterator;28import java.util.List;29import java.util.Objects;30import java.util.Set;3132import static org.testng.Assert.assertEquals;33import static org.testng.Assert.assertTrue;34import static org.testng.Assert.fail;3536/**37* @library38* CollectionAssert -- assertion methods for lambda test cases39*/40public class CollectionAsserts {4142private CollectionAsserts() {43// no instances44}4546public static void assertCountSum(Iterable<? super Integer> it, int count, int sum) {47assertCountSum(it.iterator(), count, sum);48}4950public static void assertCountSum(Iterator<? super Integer> it, int count, int sum) {51int c = 0;52int s = 0;53while (it.hasNext()) {54int i = (Integer) it.next();55c++;56s += i;57}5859assertEquals(c, count);60assertEquals(s, sum);61}6263public static void assertConcat(Iterator<Character> it, String result) {64StringBuilder sb = new StringBuilder();65while (it.hasNext()) {66sb.append(it.next());67}6869assertEquals(result, sb.toString());70}7172public static<T extends Comparable<? super T>> void assertSorted(Iterator<T> i) {73if (!i.hasNext())74return;75T last = i.next();76while (i.hasNext()) {77T t = i.next();78assertTrue(last.compareTo(t) <= 0);79assertTrue(t.compareTo(last) >= 0);80last = t;81}82}8384public static<T> void assertSorted(Iterator<T> i, Comparator<? super T> comp) {85if (!i.hasNext())86return;87T last = i.next();88while (i.hasNext()) {89T t = i.next();90assertTrue(comp.compare(last, t) <= 0);91assertTrue(comp.compare(t, last) >= 0);92last = t;93}94}9596public static<T extends Comparable<? super T>> void assertSorted(Iterable<T> iter) {97assertSorted(iter.iterator());98}99100public static<T> void assertSorted(Iterable<T> iter, Comparator<? super T> comp) {101assertSorted(iter.iterator(), comp);102}103104public static <T> void assertUnique(Iterable<T> iter) {105assertUnique(iter.iterator());106}107108public static<T> void assertUnique(Iterator<T> iter) {109if (!iter.hasNext()) {110return;111}112113Set<T> uniq = new HashSet<>();114while (iter.hasNext()) {115T each = iter.next();116assertTrue(!uniq.contains(each));117uniq.add(each);118}119}120121public static<T> void assertContents(Iterable<T> actual, Iterable<T> expected) {122assertContents(actual, expected, null);123}124125public static<T> void assertContents(Iterable<T> actual, Iterable<T> expected, String msg) {126assertContents(actual.iterator(), expected.iterator(), msg);127}128129public static<T> void assertContents(Iterator<T> actual, Iterator<T> expected) {130assertContents(actual, expected, null);131}132133public static<T> void assertContents(Iterator<T> actual, Iterator<T> expected, String msg) {134List<T> history = new ArrayList<>();135136while (expected.hasNext()) {137if (!actual.hasNext()) {138List<T> expectedData = new ArrayList<>(history);139while (expected.hasNext())140expectedData.add(expected.next());141fail(String.format("%s Premature end of data; expected=%s, found=%s",142(msg == null ? "" : msg), expectedData, history));143}144T a = actual.next();145T e = expected.next();146history.add(a);147148if (!Objects.equals(a, e))149fail(String.format("%s Data mismatch; preceding=%s, nextExpected=%s, nextFound=%s",150(msg == null ? "" : msg), history, e, a));151}152if (actual.hasNext()) {153List<T> rest = new ArrayList<>();154while (actual.hasNext())155rest.add(actual.next());156fail(String.format("%s Unexpected data %s after %s",157(msg == null ? "" : msg), rest, history));158}159}160161@SafeVarargs162@SuppressWarnings("varargs")163public static<T> void assertContents(Iterator<T> actual, T... expected) {164assertContents(actual, Arrays.asList(expected).iterator());165}166167public static<T extends Comparable<? super T>> void assertContentsUnordered(Iterable<T> actual, Iterable<T> expected) {168assertContentsUnordered(actual, expected, null);169}170171public static<T extends Comparable<? super T>> void assertContentsUnordered(Iterable<T> actual, Iterable<T> expected, String msg) {172List<T> allExpected = new ArrayList<>();173for (T t : expected) {174allExpected.add(t);175}176177for (T t : actual) {178assertTrue(allExpected.remove(t), msg + " element '" + String.valueOf(t) + "' not found");179}180181assertTrue(allExpected.isEmpty(), msg + "expected contained additional elements");182}183184static <T> void assertSplitContents(Iterable<Iterable<T>> splits, Iterable<T> list) {185Iterator<Iterable<T>> mI = splits.iterator();186Iterator<T> pI = null;187Iterator<T> lI = list.iterator();188189while (lI.hasNext()) {190if (pI == null)191pI = mI.next().iterator();192while (!pI.hasNext()) {193if (!mI.hasNext()) {194break;195}196else {197pI = mI.next().iterator();198}199}200assertTrue(pI.hasNext());201T pT = pI.next();202T lT = lI.next();203assertEquals(pT, lT);204}205206if (pI != null) {207assertTrue(!pI.hasNext());208}209210while (mI.hasNext()) {211pI = mI.next().iterator();212assertTrue(!pI.hasNext());213}214}215}216217218