--- old/test/java/util/stream/bootlib/java/util/stream/LambdaTestHelpers.java 2015-11-13 15:38:21.998844627 -0800 +++ /dev/null 2015-05-15 15:37:46.536788447 -0700 @@ -1,462 +0,0 @@ -/* - * Copyright (c) 1997, 2013, 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. - */ -package java.util.stream; - -import java.util.*; -import java.util.function.BiConsumer; -import java.util.function.BiPredicate; -import java.util.function.BinaryOperator; -import java.util.function.Consumer; -import java.util.function.DoubleBinaryOperator; -import java.util.function.DoubleConsumer; -import java.util.function.DoublePredicate; -import java.util.function.Function; -import java.util.function.IntBinaryOperator; -import java.util.function.IntConsumer; -import java.util.function.IntFunction; -import java.util.function.IntPredicate; -import java.util.function.IntUnaryOperator; -import java.util.function.LongBinaryOperator; -import java.util.function.LongConsumer; -import java.util.function.LongPredicate; -import java.util.function.Predicate; -import java.util.function.Supplier; -import java.util.function.ToDoubleFunction; -import java.util.function.ToIntFunction; -import java.util.function.ToLongFunction; - -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertTrue; - -/** - * LambdaTestHelpers -- assertion methods and useful objects for lambda test cases - */ -public class LambdaTestHelpers { - public static final String LONG_STRING = "When in the Course of human events it becomes necessary for one people to dissolve the political bands which have connected them with another and to assume among the powers of the earth, the separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation."; - - @SuppressWarnings("rawtypes") - public static final Consumer bEmpty = x -> { }; - @SuppressWarnings("rawtypes") - public static final IntConsumer bIntEmpty = x -> { }; - @SuppressWarnings("rawtypes") - public static final BiConsumer bBiEmpty = (x,y) -> { }; - @SuppressWarnings("rawtypes") - public static final Consumer bHashCode = x -> { Objects.hashCode(x); }; - @SuppressWarnings("rawtypes") - public static final BiConsumer bBiHashCode = (x,y) -> { Objects.hash(x, y); }; - public static final Function mZero = x -> 0; - public static final Function mId = x -> x; - public static final Function mDoubler = x -> x * 2; - public static final Function> mfId = e -> Collections.singletonList(e).stream(); - public static final Function> mfNull = e -> Collections.emptyList().stream(); - public static final Function> mfLt = e -> { - List l = new ArrayList<>(); - for (int i=0; i imDoubler = x -> x * 2; - public static final ToLongFunction lmDoubler = x -> x * 2; - public static final ToDoubleFunction dmDoubler = x -> x * 2; - public static final Predicate pFalse = x -> false; - public static final Predicate pTrue = x -> true; - public static final Predicate pEven = x -> 0 == x % 2; - public static final Predicate pOdd = x -> 1 == x % 2; - public static final IntPredicate ipFalse = x -> false; - public static final IntPredicate ipTrue = x -> true; - public static final IntPredicate ipEven = x -> 0 == x % 2; - public static final IntPredicate ipOdd = x -> 1 == x % 2; - public static final LongPredicate lpFalse = x -> false; - public static final LongPredicate lpTrue = x -> true; - public static final LongPredicate lpEven = x -> 0 == x % 2; - public static final LongPredicate lpOdd = x -> 1 == x % 2; - public static final DoublePredicate dpFalse = x -> false; - public static final DoublePredicate dpTrue = x -> true; - public static final DoublePredicate dpEven = x -> 0 == ((long) x) % 2; - public static final DoublePredicate dpOdd = x -> 1 == ((long) x) % 2; - public static final BinaryOperator rPlus = (x, y) -> x+y; - public static final BinaryOperator rMax = (x, y) -> Math.max(x, y); - public static final BinaryOperator rMin = (x, y) -> Math.min(x,y); - public static final IntBinaryOperator irPlus = (x, y) -> x+y; - public static final IntBinaryOperator irMax = (x, y) -> Math.max(x, y); - public static final IntBinaryOperator irMin = (x, y) -> Math.min(x,y); - public static final IntUnaryOperator irDoubler = x -> x * 2; - public static final LongBinaryOperator lrPlus = (x, y) -> x+y; - public static final DoubleBinaryOperator drPlus = (x, y) -> x+y; - public static final Comparator cInteger = (a, b) -> Integer.compare(a, b); - public static final BiPredicate bipFalse = (x, y) -> false; - public static final BiPredicate bipTrue = (x, y) -> true; - public static final BiPredicate bipBothEven = (x, y) -> 0 == (x % 2 + y % 2); - public static final BiPredicate bipBothOdd = (x, y) -> 2 == (x % 2 + y % 2); - public static final BiPredicate bipSameString = (x, y) -> String.valueOf(x).equals(String.valueOf(y)); - - public static final IntFunction integerArrayGenerator = s -> new Integer[s]; - - public static final IntFunction objectArrayGenerator = s -> new Object[s]; - - public static final Function> flattenChars = string -> { - List l = new ArrayList<>(); - for (int i=0; i flattenInt - = string -> IntStream.range(0, string.length()).map(string::charAt); - - public static Function forPredicate(Predicate predicate, R forTrue, R forFalse) { - Objects.requireNonNull(predicate); - - return t -> predicate.test(t) ? forTrue : forFalse; - } - - public static Function identity() { - return t -> t; - } - - public static Function compose(Function after, Function before) { - Objects.requireNonNull(before); - return (V v) -> after.apply(before.apply(v)); - } - - public static List empty() { - ArrayList list = new ArrayList<>(); - list.add(null); - return list; - } - - public static List countTo(int n) { - return range(1, n); - } - - public static List range(int l, int u) { - ArrayList list = new ArrayList<>(u - l + 1); - for (int i=l; i<=u; i++) { - list.add(i); - } - return list; - } - - public static List repeat(int value, int n) { - ArrayList list = new ArrayList<>(n); - for (int i=1; i<=n; i++) { - list.add(value); - } - return list; - } - - public static List asDoubles(List integers) { - ArrayList list = new ArrayList<>(); - for (Integer i : integers) { - list.add((double) i); - } - return list; - } - - public static List asLongs(List integers) { - ArrayList list = new ArrayList<>(); - for (Integer i : integers) { - list.add((long) i); - } - return list; - } - - public static void assertCountSum(Stream it, int count, int sum) { - assertCountSum(it.iterator(), count, sum); - } - - 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) { - i = toBoxedList(i).iterator(); - - 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 instanceof PrimitiveIterator.OfInt - || i instanceof PrimitiveIterator.OfDouble - || i instanceof PrimitiveIterator.OfLong) { - i = toBoxedList(i).iterator(); - } - - 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; - } - - if (iter instanceof PrimitiveIterator.OfInt - || iter instanceof PrimitiveIterator.OfDouble - || iter instanceof PrimitiveIterator.OfLong) { - iter = toBoxedList(iter).iterator(); - } - - Set uniq = new HashSet<>(); - while(iter.hasNext()) { - T each = iter.next(); - assertTrue(!uniq.contains(each), "Not unique"); - uniq.add(each); - } - } - - public static void assertContents(Iterable actual, Iterable expected) { - if (actual instanceof Collection && expected instanceof Collection) { - assertEquals(actual, expected); - } else { - assertContents(actual.iterator(), expected.iterator()); - } - } - - public static void assertContents(Iterator actual, Iterator expected) { - assertEquals(toBoxedList(actual), toBoxedList(expected)); - } - - @SafeVarargs - @SuppressWarnings("varargs") - public static void assertContents(Iterator actual, T... expected) { - assertContents(actual, Arrays.asList(expected).iterator()); - } - - /** - * The all consuming consumer (rampant capitalist) that can accepting a reference or any primitive value. - */ - private static interface OmnivorousConsumer - extends Consumer, IntConsumer, LongConsumer, DoubleConsumer { } - - @SuppressWarnings({"rawtypes", "unchecked"}) - public static Consumer toBoxingConsumer(Consumer c) { - return (Consumer) new OmnivorousConsumer() { - @Override - public void accept(Object t) { - c.accept((T) t); - } - - @Override - public void accept(int t) { - accept((Object) t); - } - - @Override - public void accept(long t) { - accept((Object) t); - } - - @Override - public void accept(double t) { - accept((Object) t); - } - }; - } - - /** - * Convert an iterator to a list using forEach with an implementation of - * {@link java.util.stream.LambdaTestHelpers.OmnivorousConsumer}. - * - * This ensures equality comparisons for test results do not trip - * the boxing trip-wires. - */ - private static List toBoxedList(Iterator it) { - List l = new ArrayList<>(); - it.forEachRemaining(toBoxingConsumer(l::add)); - return l; - } - - /** - * Convert a spliterator to a list using forEach with an implementation of - * {@link java.util.stream.LambdaTestHelpers.OmnivorousConsumer}. - * - * This ensures equality comparisons for test results do not trip - * the boxing trip-wires. - */ - public static List toBoxedList(Spliterator sp) { - List l = new ArrayList<>(); - sp.forEachRemaining(toBoxingConsumer(l::add)); - return l; - } - - /** - * Convert an iterator to a multi-set, represented as a Map, using forEach with an implementation of - * {@link java.util.stream.LambdaTestHelpers.OmnivorousConsumer}. - * - * This ensures equality comparisons for test results do not trip - * the boxing trip-wires. - */ - @SuppressWarnings("unchecked") - private static Map toBoxedMultiset(Iterator it) { - Map result = new HashMap<>(); - - it.forEachRemaining(toBoxingConsumer(o -> { - if (result.containsKey(o)) - result.put(o, result.get(o) + 1); - else - result.put(o, 1); - })); - - return (Map) result; - } - - @SuppressWarnings("unchecked") - public static Map toBoxedMultiset(Spliterator it) { - Map result = new HashMap<>(); - - it.forEachRemaining(toBoxingConsumer(o -> { - if (result.containsKey(o)) - result.put(o, result.get(o) + 1); - else - result.put(o, 1); - })); - - return (Map) result; - } - - @SuppressWarnings("unchecked") - public static void assertContentsEqual(Object a, Object b) { - if (a instanceof Iterable && b instanceof Iterable) - assertContents((Iterable) a, (Iterable) b); - else - assertEquals(a, b); - } - - public static void assertContentsUnordered(Iterable actual, Iterable expected) { - assertContentsUnordered(actual.iterator(), expected.iterator()); - } - - public static void assertContentsUnordered(Iterator actual, Iterator expected) { - assertEquals(toBoxedMultiset(actual), toBoxedMultiset(expected)); - } - - public static void launderAssertion(Runnable r, Supplier additionalInfo) { - try { - r.run(); - } - catch (AssertionError ae) { - AssertionError cloned = new AssertionError(ae.getMessage() + String.format("%n%s", additionalInfo.get())); - cloned.setStackTrace(ae.getStackTrace()); - if (ae.getCause() != null) - cloned.initCause(ae.getCause()); - throw cloned; - } - } - - public static > - List> permuteStreamFunctions(List> opFunctions) { - List>> opFunctionPermutations = perm(opFunctions); - - List> appliedFunctions = new ArrayList<>(); - for (List> fs : opFunctionPermutations) { - Function applied = s -> { - for (Function f : fs) { - s = f.apply(s); - } - return s; - }; - appliedFunctions.add(applied); - } - - return appliedFunctions; - } - - private static List sub(List l, int index) { - List subL = new ArrayList<>(l); - subL.remove(index); - return subL; - } - - public static List> perm(List l) { - List> result = new ArrayList<>(); - for (int i = 0; i < l.size(); i++) { - for (List perm : perm(sub(l, i))) { - perm.add(0, l.get(i)); - result.add(perm); - } - } - result.add(new ArrayList()); - - return result; - } - - public static String flagsToString(int flags) { - StringJoiner sj = new StringJoiner(", ", "StreamOpFlag[", "]"); - if (StreamOpFlag.DISTINCT.isKnown(flags)) sj.add("IS_DISTINCT"); - if (StreamOpFlag.ORDERED.isKnown(flags)) sj.add("IS_ORDERED"); - if (StreamOpFlag.SIZED.isKnown(flags)) sj.add("IS_SIZED"); - if (StreamOpFlag.SORTED.isKnown(flags)) sj.add("IS_SORTED"); - if (StreamOpFlag.SHORT_CIRCUIT.isKnown(flags)) sj.add("IS_SHORT_CIRCUIT"); - return sj.toString(); - } -} --- /dev/null 2015-05-15 15:37:46.536788447 -0700 +++ new/test/java/util/stream/bootlib/java.base/java/util/stream/LambdaTestHelpers.java 2015-11-13 15:38:21.802836291 -0800 @@ -0,0 +1,462 @@ +/* + * Copyright (c) 1997, 2013, 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. + */ +package java.util.stream; + +import java.util.*; +import java.util.function.BiConsumer; +import java.util.function.BiPredicate; +import java.util.function.BinaryOperator; +import java.util.function.Consumer; +import java.util.function.DoubleBinaryOperator; +import java.util.function.DoubleConsumer; +import java.util.function.DoublePredicate; +import java.util.function.Function; +import java.util.function.IntBinaryOperator; +import java.util.function.IntConsumer; +import java.util.function.IntFunction; +import java.util.function.IntPredicate; +import java.util.function.IntUnaryOperator; +import java.util.function.LongBinaryOperator; +import java.util.function.LongConsumer; +import java.util.function.LongPredicate; +import java.util.function.Predicate; +import java.util.function.Supplier; +import java.util.function.ToDoubleFunction; +import java.util.function.ToIntFunction; +import java.util.function.ToLongFunction; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; + +/** + * LambdaTestHelpers -- assertion methods and useful objects for lambda test cases + */ +public class LambdaTestHelpers { + public static final String LONG_STRING = "When in the Course of human events it becomes necessary for one people to dissolve the political bands which have connected them with another and to assume among the powers of the earth, the separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation."; + + @SuppressWarnings("rawtypes") + public static final Consumer bEmpty = x -> { }; + @SuppressWarnings("rawtypes") + public static final IntConsumer bIntEmpty = x -> { }; + @SuppressWarnings("rawtypes") + public static final BiConsumer bBiEmpty = (x,y) -> { }; + @SuppressWarnings("rawtypes") + public static final Consumer bHashCode = x -> { Objects.hashCode(x); }; + @SuppressWarnings("rawtypes") + public static final BiConsumer bBiHashCode = (x,y) -> { Objects.hash(x, y); }; + public static final Function mZero = x -> 0; + public static final Function mId = x -> x; + public static final Function mDoubler = x -> x * 2; + public static final Function> mfId = e -> Collections.singletonList(e).stream(); + public static final Function> mfNull = e -> Collections.emptyList().stream(); + public static final Function> mfLt = e -> { + List l = new ArrayList<>(); + for (int i=0; i imDoubler = x -> x * 2; + public static final ToLongFunction lmDoubler = x -> x * 2; + public static final ToDoubleFunction dmDoubler = x -> x * 2; + public static final Predicate pFalse = x -> false; + public static final Predicate pTrue = x -> true; + public static final Predicate pEven = x -> 0 == x % 2; + public static final Predicate pOdd = x -> 1 == x % 2; + public static final IntPredicate ipFalse = x -> false; + public static final IntPredicate ipTrue = x -> true; + public static final IntPredicate ipEven = x -> 0 == x % 2; + public static final IntPredicate ipOdd = x -> 1 == x % 2; + public static final LongPredicate lpFalse = x -> false; + public static final LongPredicate lpTrue = x -> true; + public static final LongPredicate lpEven = x -> 0 == x % 2; + public static final LongPredicate lpOdd = x -> 1 == x % 2; + public static final DoublePredicate dpFalse = x -> false; + public static final DoublePredicate dpTrue = x -> true; + public static final DoublePredicate dpEven = x -> 0 == ((long) x) % 2; + public static final DoublePredicate dpOdd = x -> 1 == ((long) x) % 2; + public static final BinaryOperator rPlus = (x, y) -> x+y; + public static final BinaryOperator rMax = (x, y) -> Math.max(x, y); + public static final BinaryOperator rMin = (x, y) -> Math.min(x,y); + public static final IntBinaryOperator irPlus = (x, y) -> x+y; + public static final IntBinaryOperator irMax = (x, y) -> Math.max(x, y); + public static final IntBinaryOperator irMin = (x, y) -> Math.min(x,y); + public static final IntUnaryOperator irDoubler = x -> x * 2; + public static final LongBinaryOperator lrPlus = (x, y) -> x+y; + public static final DoubleBinaryOperator drPlus = (x, y) -> x+y; + public static final Comparator cInteger = (a, b) -> Integer.compare(a, b); + public static final BiPredicate bipFalse = (x, y) -> false; + public static final BiPredicate bipTrue = (x, y) -> true; + public static final BiPredicate bipBothEven = (x, y) -> 0 == (x % 2 + y % 2); + public static final BiPredicate bipBothOdd = (x, y) -> 2 == (x % 2 + y % 2); + public static final BiPredicate bipSameString = (x, y) -> String.valueOf(x).equals(String.valueOf(y)); + + public static final IntFunction integerArrayGenerator = s -> new Integer[s]; + + public static final IntFunction objectArrayGenerator = s -> new Object[s]; + + public static final Function> flattenChars = string -> { + List l = new ArrayList<>(); + for (int i=0; i flattenInt + = string -> IntStream.range(0, string.length()).map(string::charAt); + + public static Function forPredicate(Predicate predicate, R forTrue, R forFalse) { + Objects.requireNonNull(predicate); + + return t -> predicate.test(t) ? forTrue : forFalse; + } + + public static Function identity() { + return t -> t; + } + + public static Function compose(Function after, Function before) { + Objects.requireNonNull(before); + return (V v) -> after.apply(before.apply(v)); + } + + public static List empty() { + ArrayList list = new ArrayList<>(); + list.add(null); + return list; + } + + public static List countTo(int n) { + return range(1, n); + } + + public static List range(int l, int u) { + ArrayList list = new ArrayList<>(u - l + 1); + for (int i=l; i<=u; i++) { + list.add(i); + } + return list; + } + + public static List repeat(int value, int n) { + ArrayList list = new ArrayList<>(n); + for (int i=1; i<=n; i++) { + list.add(value); + } + return list; + } + + public static List asDoubles(List integers) { + ArrayList list = new ArrayList<>(); + for (Integer i : integers) { + list.add((double) i); + } + return list; + } + + public static List asLongs(List integers) { + ArrayList list = new ArrayList<>(); + for (Integer i : integers) { + list.add((long) i); + } + return list; + } + + public static void assertCountSum(Stream it, int count, int sum) { + assertCountSum(it.iterator(), count, sum); + } + + 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) { + i = toBoxedList(i).iterator(); + + 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 instanceof PrimitiveIterator.OfInt + || i instanceof PrimitiveIterator.OfDouble + || i instanceof PrimitiveIterator.OfLong) { + i = toBoxedList(i).iterator(); + } + + 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; + } + + if (iter instanceof PrimitiveIterator.OfInt + || iter instanceof PrimitiveIterator.OfDouble + || iter instanceof PrimitiveIterator.OfLong) { + iter = toBoxedList(iter).iterator(); + } + + Set uniq = new HashSet<>(); + while(iter.hasNext()) { + T each = iter.next(); + assertTrue(!uniq.contains(each), "Not unique"); + uniq.add(each); + } + } + + public static void assertContents(Iterable actual, Iterable expected) { + if (actual instanceof Collection && expected instanceof Collection) { + assertEquals(actual, expected); + } else { + assertContents(actual.iterator(), expected.iterator()); + } + } + + public static void assertContents(Iterator actual, Iterator expected) { + assertEquals(toBoxedList(actual), toBoxedList(expected)); + } + + @SafeVarargs + @SuppressWarnings("varargs") + public static void assertContents(Iterator actual, T... expected) { + assertContents(actual, Arrays.asList(expected).iterator()); + } + + /** + * The all consuming consumer (rampant capitalist) that can accepting a reference or any primitive value. + */ + private static interface OmnivorousConsumer + extends Consumer, IntConsumer, LongConsumer, DoubleConsumer { } + + @SuppressWarnings({"rawtypes", "unchecked"}) + public static Consumer toBoxingConsumer(Consumer c) { + return (Consumer) new OmnivorousConsumer() { + @Override + public void accept(Object t) { + c.accept((T) t); + } + + @Override + public void accept(int t) { + accept((Object) t); + } + + @Override + public void accept(long t) { + accept((Object) t); + } + + @Override + public void accept(double t) { + accept((Object) t); + } + }; + } + + /** + * Convert an iterator to a list using forEach with an implementation of + * {@link java.util.stream.LambdaTestHelpers.OmnivorousConsumer}. + * + * This ensures equality comparisons for test results do not trip + * the boxing trip-wires. + */ + private static List toBoxedList(Iterator it) { + List l = new ArrayList<>(); + it.forEachRemaining(toBoxingConsumer(l::add)); + return l; + } + + /** + * Convert a spliterator to a list using forEach with an implementation of + * {@link java.util.stream.LambdaTestHelpers.OmnivorousConsumer}. + * + * This ensures equality comparisons for test results do not trip + * the boxing trip-wires. + */ + public static List toBoxedList(Spliterator sp) { + List l = new ArrayList<>(); + sp.forEachRemaining(toBoxingConsumer(l::add)); + return l; + } + + /** + * Convert an iterator to a multi-set, represented as a Map, using forEach with an implementation of + * {@link java.util.stream.LambdaTestHelpers.OmnivorousConsumer}. + * + * This ensures equality comparisons for test results do not trip + * the boxing trip-wires. + */ + @SuppressWarnings("unchecked") + private static Map toBoxedMultiset(Iterator it) { + Map result = new HashMap<>(); + + it.forEachRemaining(toBoxingConsumer(o -> { + if (result.containsKey(o)) + result.put(o, result.get(o) + 1); + else + result.put(o, 1); + })); + + return (Map) result; + } + + @SuppressWarnings("unchecked") + public static Map toBoxedMultiset(Spliterator it) { + Map result = new HashMap<>(); + + it.forEachRemaining(toBoxingConsumer(o -> { + if (result.containsKey(o)) + result.put(o, result.get(o) + 1); + else + result.put(o, 1); + })); + + return (Map) result; + } + + @SuppressWarnings("unchecked") + public static void assertContentsEqual(Object a, Object b) { + if (a instanceof Iterable && b instanceof Iterable) + assertContents((Iterable) a, (Iterable) b); + else + assertEquals(a, b); + } + + public static void assertContentsUnordered(Iterable actual, Iterable expected) { + assertContentsUnordered(actual.iterator(), expected.iterator()); + } + + public static void assertContentsUnordered(Iterator actual, Iterator expected) { + assertEquals(toBoxedMultiset(actual), toBoxedMultiset(expected)); + } + + public static void launderAssertion(Runnable r, Supplier additionalInfo) { + try { + r.run(); + } + catch (AssertionError ae) { + AssertionError cloned = new AssertionError(ae.getMessage() + String.format("%n%s", additionalInfo.get())); + cloned.setStackTrace(ae.getStackTrace()); + if (ae.getCause() != null) + cloned.initCause(ae.getCause()); + throw cloned; + } + } + + public static > + List> permuteStreamFunctions(List> opFunctions) { + List>> opFunctionPermutations = perm(opFunctions); + + List> appliedFunctions = new ArrayList<>(); + for (List> fs : opFunctionPermutations) { + Function applied = s -> { + for (Function f : fs) { + s = f.apply(s); + } + return s; + }; + appliedFunctions.add(applied); + } + + return appliedFunctions; + } + + private static List sub(List l, int index) { + List subL = new ArrayList<>(l); + subL.remove(index); + return subL; + } + + public static List> perm(List l) { + List> result = new ArrayList<>(); + for (int i = 0; i < l.size(); i++) { + for (List perm : perm(sub(l, i))) { + perm.add(0, l.get(i)); + result.add(perm); + } + } + result.add(new ArrayList()); + + return result; + } + + public static String flagsToString(int flags) { + StringJoiner sj = new StringJoiner(", ", "StreamOpFlag[", "]"); + if (StreamOpFlag.DISTINCT.isKnown(flags)) sj.add("IS_DISTINCT"); + if (StreamOpFlag.ORDERED.isKnown(flags)) sj.add("IS_ORDERED"); + if (StreamOpFlag.SIZED.isKnown(flags)) sj.add("IS_SIZED"); + if (StreamOpFlag.SORTED.isKnown(flags)) sj.add("IS_SORTED"); + if (StreamOpFlag.SHORT_CIRCUIT.isKnown(flags)) sj.add("IS_SHORT_CIRCUIT"); + return sj.toString(); + } +}