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;
260 if (!iter.hasNext()) {
261 return;
262 }
263
264 if (iter instanceof PrimitiveIterator.OfInt
265 || iter instanceof PrimitiveIterator.OfDouble
266 || iter instanceof PrimitiveIterator.OfLong) {
267 iter = toBoxedList(iter).iterator();
268 }
269
270 Set<T> uniq = new HashSet<>();
271 while(iter.hasNext()) {
272 T each = iter.next();
273 assertTrue(!uniq.contains(each), "Not unique");
274 uniq.add(each);
275 }
276 }
277
278 public static<T> void assertContents(Iterable<T> actual, Iterable<T> expected) {
279 if (actual instanceof Collection && expected instanceof Collection) {
280 assertEquals(actual, expected);
281 } else {
282 assertContents(actual.iterator(), expected.iterator());
283 }
284 }
285
286 public static<T> void assertContents(Iterator<T> actual, Iterator<T> expected) {
287 assertEquals(toBoxedList(actual), toBoxedList(expected));
288 }
289
290 @SafeVarargs
291 @SuppressWarnings("varargs")
292 public static<T> void assertContents(Iterator<T> actual, T... expected) {
293 assertContents(actual, Arrays.asList(expected).iterator());
294 }
295
296 /**
297 * The all consuming consumer (rampant capitalist) that can accepting a reference or any primitive value.
298 */
299 private static interface OmnivorousConsumer<T>
300 extends Consumer<T>, IntConsumer, LongConsumer, DoubleConsumer { }
301
302 @SuppressWarnings({"rawtypes", "unchecked"})
303 public static<T> Consumer<T> toBoxingConsumer(Consumer<? super T> c) {
304 return (Consumer<T>) new OmnivorousConsumer() {
305 @Override
306 public void accept(Object t) {
307 c.accept((T) t);
308 }
309
|
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 import static org.testng.Assert.fail;
52
53 /**
54 * LambdaTestHelpers -- assertion methods and useful objects for lambda test cases
55 */
56 public class LambdaTestHelpers {
57 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.";
58
59 @SuppressWarnings("rawtypes")
60 public static final Consumer bEmpty = x -> { };
61 @SuppressWarnings("rawtypes")
62 public static final IntConsumer bIntEmpty = x -> { };
63 @SuppressWarnings("rawtypes")
64 public static final BiConsumer bBiEmpty = (x,y) -> { };
65 @SuppressWarnings("rawtypes")
66 public static final Consumer bHashCode = x -> { Objects.hashCode(x); };
67 @SuppressWarnings("rawtypes")
68 public static final BiConsumer bBiHashCode = (x,y) -> { Objects.hash(x, y); };
69 public static final Function<Integer, Integer> mZero = x -> 0;
70 public static final Function<Integer, Integer> mId = x -> x;
71 public static final Function<Integer, Integer> mDoubler = x -> x * 2;
261 if (!iter.hasNext()) {
262 return;
263 }
264
265 if (iter instanceof PrimitiveIterator.OfInt
266 || iter instanceof PrimitiveIterator.OfDouble
267 || iter instanceof PrimitiveIterator.OfLong) {
268 iter = toBoxedList(iter).iterator();
269 }
270
271 Set<T> uniq = new HashSet<>();
272 while(iter.hasNext()) {
273 T each = iter.next();
274 assertTrue(!uniq.contains(each), "Not unique");
275 uniq.add(each);
276 }
277 }
278
279 public static<T> void assertContents(Iterable<T> actual, Iterable<T> expected) {
280 if (actual instanceof Collection && expected instanceof Collection) {
281 assertIterableEquals(actual, expected);
282 } else {
283 assertContents(actual.iterator(), expected.iterator());
284 }
285 }
286
287 public static<T> void assertContents(Iterator<T> actual, Iterator<T> expected) {
288 assertEquals(toBoxedList(actual), toBoxedList(expected));
289 }
290
291 // Workaround String creation in inner loop inside org.testng.Assert.assertEquals(Iterable<?>, Iterable<?>)
292 static public void assertIterableEquals(Iterable<?> actual, Iterable<?> expected) {
293 if(actual == expected) {
294 return;
295 }
296
297 if(actual == null || expected == null) {
298 fail("Iterables not equal: expected: " + expected + " and actual: " + actual);
299 }
300
301 assertIteratorsEquals(actual.iterator(), expected.iterator());
302 }
303
304 // Workaround String creation in inner loop inside org.testng.Assert.assertEquals(Iterator<?>, Iterator<?>)
305 static public void assertIteratorsEquals(Iterator<?> actual, Iterator<?> expected) {
306 if (actual == expected) {
307 return;
308 }
309
310 if (actual == null || expected == null) {
311 fail("Iterators not equal: expected: " + expected + " and actual: " + actual);
312 }
313
314 while (actual.hasNext() && expected.hasNext()) {
315 Object e = expected.next();
316 Object a = actual.next();
317 assertEquals(a, e, "Iterator contents differ");
318
319 }
320
321 if(actual.hasNext()) {
322 fail("Actual iterator returned more elements than the expected iterator.");
323 } else if(expected.hasNext()) {
324 fail("Expected iterator returned more elements than the actual iterator.");
325 }
326
327 }
328 @SafeVarargs
329 @SuppressWarnings("varargs")
330 public static<T> void assertContents(Iterator<T> actual, T... expected) {
331 assertContents(actual, Arrays.asList(expected).iterator());
332 }
333
334 /**
335 * The all consuming consumer (rampant capitalist) that can accepting a reference or any primitive value.
336 */
337 private static interface OmnivorousConsumer<T>
338 extends Consumer<T>, IntConsumer, LongConsumer, DoubleConsumer { }
339
340 @SuppressWarnings({"rawtypes", "unchecked"})
341 public static<T> Consumer<T> toBoxingConsumer(Consumer<? super T> c) {
342 return (Consumer<T>) new OmnivorousConsumer() {
343 @Override
344 public void accept(Object t) {
345 c.accept((T) t);
346 }
347
|