30 import java.util.function.DoubleBinaryOperator;
31 import java.util.function.DoubleConsumer;
32 import java.util.function.DoublePredicate;
33 import java.util.function.Function;
34 import java.util.function.IntBinaryOperator;
35 import java.util.function.IntConsumer;
36 import java.util.function.IntFunction;
37 import java.util.function.IntPredicate;
38 import java.util.function.IntUnaryOperator;
39 import java.util.function.LongBinaryOperator;
40 import java.util.function.LongConsumer;
41 import java.util.function.LongPredicate;
42 import java.util.function.Predicate;
43 import java.util.function.Supplier;
44 import java.util.function.ToDoubleFunction;
45 import java.util.function.ToIntFunction;
46 import java.util.function.ToLongFunction;
47
48 import static org.testng.Assert.assertEquals;
49 import static org.testng.Assert.assertTrue;
50
51 /**
52 * LambdaTestHelpers -- assertion methods and useful objects for lambda test cases
53 */
54 public class LambdaTestHelpers {
55 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.";
56
57 @SuppressWarnings("rawtypes")
58 public static final Consumer bEmpty = x -> { };
59 @SuppressWarnings("rawtypes")
60 public static final IntConsumer bIntEmpty = x -> { };
61 @SuppressWarnings("rawtypes")
62 public static final BiConsumer bBiEmpty = (x,y) -> { };
63 @SuppressWarnings("rawtypes")
64 public static final Consumer bHashCode = x -> { Objects.hashCode(x); };
65 @SuppressWarnings("rawtypes")
66 public static final BiConsumer bBiHashCode = (x,y) -> { Objects.hash(x, y); };
67 public static final Function<Integer, Integer> mZero = x -> 0;
68 public static final Function<Integer, Integer> mId = x -> x;
69 public static final Function<Integer, Integer> mDoubler = x -> x * 2;
381 result.put(o, 1);
382 }));
383
384 return (Map<T, Integer>) result;
385 }
386
387 @SuppressWarnings("unchecked")
388 public static void assertContentsEqual(Object a, Object b) {
389 if (a instanceof Iterable && b instanceof Iterable)
390 assertContents((Iterable) a, (Iterable) b);
391 else
392 assertEquals(a, b);
393 }
394
395 public static<T> void assertContentsUnordered(Iterable<T> actual, Iterable<T> expected) {
396 assertContentsUnordered(actual.iterator(), expected.iterator());
397 }
398
399 public static<T> void assertContentsUnordered(Iterator<T> actual, Iterator<T> expected) {
400 assertEquals(toBoxedMultiset(actual), toBoxedMultiset(expected));
401 }
402
403 public static void launderAssertion(Runnable r, Supplier<String> additionalInfo) {
404 try {
405 r.run();
406 }
407 catch (AssertionError ae) {
408 AssertionError cloned = new AssertionError(ae.getMessage() + String.format("%n%s", additionalInfo.get()));
409 cloned.setStackTrace(ae.getStackTrace());
410 if (ae.getCause() != null)
411 cloned.initCause(ae.getCause());
412 throw cloned;
413 }
414 }
415
416 public static <T, S extends BaseStream<T, S>>
417 List<Function<S, S>> permuteStreamFunctions(List<Function<S, S>> opFunctions) {
418 List<List<Function<S, S>>> opFunctionPermutations = perm(opFunctions);
419
420 List<Function<S, S>> appliedFunctions = new ArrayList<>();
|
30 import java.util.function.DoubleBinaryOperator;
31 import java.util.function.DoubleConsumer;
32 import java.util.function.DoublePredicate;
33 import java.util.function.Function;
34 import java.util.function.IntBinaryOperator;
35 import java.util.function.IntConsumer;
36 import java.util.function.IntFunction;
37 import java.util.function.IntPredicate;
38 import java.util.function.IntUnaryOperator;
39 import java.util.function.LongBinaryOperator;
40 import java.util.function.LongConsumer;
41 import java.util.function.LongPredicate;
42 import java.util.function.Predicate;
43 import java.util.function.Supplier;
44 import java.util.function.ToDoubleFunction;
45 import java.util.function.ToIntFunction;
46 import java.util.function.ToLongFunction;
47
48 import static org.testng.Assert.assertEquals;
49 import static org.testng.Assert.assertTrue;
50 import static org.testng.Assert.assertFalse;
51
52 /**
53 * LambdaTestHelpers -- assertion methods and useful objects for lambda test cases
54 */
55 public class LambdaTestHelpers {
56 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.";
57
58 @SuppressWarnings("rawtypes")
59 public static final Consumer bEmpty = x -> { };
60 @SuppressWarnings("rawtypes")
61 public static final IntConsumer bIntEmpty = x -> { };
62 @SuppressWarnings("rawtypes")
63 public static final BiConsumer bBiEmpty = (x,y) -> { };
64 @SuppressWarnings("rawtypes")
65 public static final Consumer bHashCode = x -> { Objects.hashCode(x); };
66 @SuppressWarnings("rawtypes")
67 public static final BiConsumer bBiHashCode = (x,y) -> { Objects.hash(x, y); };
68 public static final Function<Integer, Integer> mZero = x -> 0;
69 public static final Function<Integer, Integer> mId = x -> x;
70 public static final Function<Integer, Integer> mDoubler = x -> x * 2;
382 result.put(o, 1);
383 }));
384
385 return (Map<T, Integer>) result;
386 }
387
388 @SuppressWarnings("unchecked")
389 public static void assertContentsEqual(Object a, Object b) {
390 if (a instanceof Iterable && b instanceof Iterable)
391 assertContents((Iterable) a, (Iterable) b);
392 else
393 assertEquals(a, b);
394 }
395
396 public static<T> void assertContentsUnordered(Iterable<T> actual, Iterable<T> expected) {
397 assertContentsUnordered(actual.iterator(), expected.iterator());
398 }
399
400 public static<T> void assertContentsUnordered(Iterator<T> actual, Iterator<T> expected) {
401 assertEquals(toBoxedMultiset(actual), toBoxedMultiset(expected));
402 }
403
404 public static<T> void assertContains(Optional<T> actual, Iterator<T> it) {
405 if (actual.isPresent()) {
406 T r = actual.get();
407 boolean contained = false;
408 while (!contained && it.hasNext()) {
409 contained = Objects.equals(r, it.next());
410 }
411 assertTrue(contained);
412 }
413 else {
414 assertFalse(it.hasNext());
415 }
416 }
417
418 public static void launderAssertion(Runnable r, Supplier<String> additionalInfo) {
419 try {
420 r.run();
421 }
422 catch (AssertionError ae) {
423 AssertionError cloned = new AssertionError(ae.getMessage() + String.format("%n%s", additionalInfo.get()));
424 cloned.setStackTrace(ae.getStackTrace());
425 if (ae.getCause() != null)
426 cloned.initCause(ae.getCause());
427 throw cloned;
428 }
429 }
430
431 public static <T, S extends BaseStream<T, S>>
432 List<Function<S, S>> permuteStreamFunctions(List<Function<S, S>> opFunctions) {
433 List<List<Function<S, S>>> opFunctionPermutations = perm(opFunctions);
434
435 List<Function<S, S>> appliedFunctions = new ArrayList<>();
|