4fa1f1f4fc
Co-authored-by: Arne Siegel <v.a.ammodytes@googlemail.com> Co-authored-by: Brian Goetz <brian.goetz@oracle.com> Reviewed-by: alanb, dholmes, mduigou, psandoz, smarks
212 lines
6.5 KiB
Java
212 lines
6.5 KiB
Java
/*
|
|
* 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 {
|
|
|
|
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) {
|
|
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<Character> it, String result) {
|
|
StringBuilder sb = new StringBuilder();
|
|
while (it.hasNext()) {
|
|
sb.append(it.next());
|
|
}
|
|
|
|
assertEquals(result, sb.toString());
|
|
}
|
|
|
|
public static<T extends Comparable<? super T>> void assertSorted(Iterator<T> 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<T> void assertSorted(Iterator<T> i, Comparator<? super T> 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<T extends Comparable<? super T>> void assertSorted(Iterable<T> iter) {
|
|
assertSorted(iter.iterator());
|
|
}
|
|
|
|
public static<T> void assertSorted(Iterable<T> iter, Comparator<? super T> comp) {
|
|
assertSorted(iter.iterator(), comp);
|
|
}
|
|
|
|
public static <T> void assertUnique(Iterable<T> iter) {
|
|
assertUnique(iter.iterator());
|
|
}
|
|
|
|
public static<T> void assertUnique(Iterator<T> iter) {
|
|
if (!iter.hasNext()) {
|
|
return;
|
|
}
|
|
|
|
Set<T> uniq = new HashSet<>();
|
|
while(iter.hasNext()) {
|
|
T each = iter.next();
|
|
assertTrue(!uniq.contains(each));
|
|
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;
|
|
Iterator<T> 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());
|
|
}
|
|
}
|
|
}
|