Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Collection/testlibrary/CollectionAsserts.java
38828 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.Collections;26import java.util.Comparator;27import java.util.HashSet;28import java.util.Iterator;29import java.util.List;30import java.util.Objects;31import java.util.Set;3233import static org.testng.Assert.assertEquals;34import static org.testng.Assert.assertTrue;35import static org.testng.Assert.fail;3637/**38* @library39* CollectionAssert -- assertion methods for lambda test cases40*/41public class CollectionAsserts {4243private CollectionAsserts() {44// no instances45}4647public static void assertCountSum(Iterable<? super Integer> it, int count, int sum) {48assertCountSum(it.iterator(), count, sum);49}5051public static void assertCountSum(Iterator<? super Integer> it, int count, int sum) {52int c = 0;53int s = 0;54while (it.hasNext()) {55int i = (Integer) it.next();56c++;57s += i;58}5960assertEquals(c, count);61assertEquals(s, sum);62}6364public static void assertConcat(Iterator<Character> it, String result) {65StringBuilder sb = new StringBuilder();66while (it.hasNext()) {67sb.append(it.next());68}6970assertEquals(result, sb.toString());71}7273public static<T extends Comparable<? super T>> void assertSorted(Iterator<T> i) {74if (!i.hasNext())75return;76T last = i.next();77while (i.hasNext()) {78T t = i.next();79assertTrue(last.compareTo(t) <= 0);80assertTrue(t.compareTo(last) >= 0);81last = t;82}83}8485public static<T> void assertSorted(Iterator<T> i, Comparator<? super T> comp) {86if (!i.hasNext())87return;88T last = i.next();89while (i.hasNext()) {90T t = i.next();91assertTrue(comp.compare(last, t) <= 0);92assertTrue(comp.compare(t, last) >= 0);93last = t;94}95}9697public static<T extends Comparable<? super T>> void assertSorted(Iterable<T> iter) {98assertSorted(iter.iterator());99}100101public static<T> void assertSorted(Iterable<T> iter, Comparator<? super T> comp) {102assertSorted(iter.iterator(), comp);103}104105public static <T> void assertUnique(Iterable<T> iter) {106assertUnique(iter.iterator());107}108109public static<T> void assertUnique(Iterator<T> iter) {110if (!iter.hasNext()) {111return;112}113114Set<T> uniq = new HashSet<>();115while(iter.hasNext()) {116T each = iter.next();117assertTrue(!uniq.contains(each));118uniq.add(each);119}120}121122public static<T> void assertContents(Iterable<T> actual, Iterable<T> expected) {123assertContents(actual, expected, null);124}125126public static<T> void assertContents(Iterable<T> actual, Iterable<T> expected, String msg) {127assertContents(actual.iterator(), expected.iterator(), msg);128}129130public static<T> void assertContents(Iterator<T> actual, Iterator<T> expected) {131assertContents(actual, expected, null);132}133134public static<T> void assertContents(Iterator<T> actual, Iterator<T> expected, String msg) {135List<T> history = new ArrayList<>();136137while (expected.hasNext()) {138if (!actual.hasNext()) {139List<T> expectedData = new ArrayList<>(history);140while (expected.hasNext())141expectedData.add(expected.next());142fail(String.format("%s Premature end of data; expected=%s, found=%s",143(msg == null ? "" : msg), expectedData, history));144}145T a = actual.next();146T e = expected.next();147history.add(a);148149if (!Objects.equals(a, e))150fail(String.format("%s Data mismatch; preceding=%s, nextExpected=%s, nextFound=%s",151(msg == null ? "" : msg), history, e, a));152}153if (actual.hasNext()) {154List<T> rest = new ArrayList<>();155while (actual.hasNext())156rest.add(actual.next());157fail(String.format("%s Unexpected data %s after %s",158(msg == null ? "" : msg), rest, history));159}160}161162@SafeVarargs163@SuppressWarnings("varargs")164public static<T> void assertContents(Iterator<T> actual, T... expected) {165assertContents(actual, Arrays.asList(expected).iterator());166}167168public static<T extends Comparable<? super T>> void assertContentsUnordered(Iterable<T> actual, Iterable<T> expected) {169assertContentsUnordered(actual, expected, null);170}171172public static<T extends Comparable<? super T>> void assertContentsUnordered(Iterable<T> actual, Iterable<T> expected, String msg) {173List<T> allExpected = new ArrayList<>();174for (T t : expected) {175allExpected.add(t);176}177178for (T t : actual) {179assertTrue(allExpected.remove(t), msg + " element '" + String.valueOf(t) + "' not found");180}181182assertTrue(allExpected.isEmpty(), msg + "expected contained additional elements");183}184185static <T> void assertSplitContents(Iterable<Iterable<T>> splits, Iterable<T> list) {186Iterator<Iterable<T>> mI = splits.iterator();187Iterator<T> pI = null;188Iterator<T> lI = list.iterator();189190while (lI.hasNext()) {191if (pI == null)192pI = mI.next().iterator();193while (!pI.hasNext()) {194if (!mI.hasNext()) {195break;196}197else {198pI = mI.next().iterator();199}200}201assertTrue(pI.hasNext());202T pT = pI.next();203T lT = lI.next();204assertEquals(pT, lT);205}206207if (pI != null) {208assertTrue(!pI.hasNext());209}210211while(mI.hasNext()) {212pI = mI.next().iterator();213assertTrue(!pI.hasNext());214}215}216}217218219