private <T> ResultAsserter<Iterable<T>> unorderedAsserter() { return (act, exp, ord, par) -> { if (par & !ord) { // Can only assert that all elements of the actual result // are distinct and that the count is the limit size // any element within the range [0, Long.MAX_VALUE) may be // present assertUnique(act); long count = 0; for (T l : act) { count++; } assertEquals(count, SKIP_LIMIT_SIZE, "size not equal"); } else { LambdaTestHelpers.assertContents(act, exp); } }; }
@Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class) public void testTwoLevelPartition(String name, TestData.OfRef<Integer> data) throws ReflectiveOperationException { Predicate<Integer> classifier = i -> i % 3 == 0; Predicate<Integer> classifier2 = i -> i % 7 == 0; // Two level partition exerciseMapTabulation(data, partitioningBy(classifier, partitioningBy(classifier2)), new PartitionAssertion<>(classifier, new PartitionAssertion(classifier2, new ListAssertion<>()))); // Two level partition with reduce exerciseMapTabulation(data, partitioningBy(classifier, reducing(0, Integer::sum)), new PartitionAssertion<>(classifier, new ReduceAssertion<>(0, LambdaTestHelpers.identity(), Integer::sum))); }
List<MapperData<Integer, ?>> getMapperData(TestData.OfRef<Integer> data) { int uniqueSize = data.into(new HashSet<>()).size(); return Arrays.asList( new MapperData<>(mId, uniqueSize), new MapperData<>(mZero, Math.min(1, data.size())), new MapperData<>(mDoubler, uniqueSize), new MapperData<>(LambdaTestHelpers.compose(mId, mDoubler), uniqueSize), new MapperData<>(LambdaTestHelpers.compose(mDoubler, mDoubler), uniqueSize), new MapperData<>(LambdaTestHelpers.forPredicate(pFalse, true, false), Math.min(1, uniqueSize)), new MapperData<>(LambdaTestHelpers.forPredicate(pTrue, true, false), Math.min(1, uniqueSize)), new MapperData<>(LambdaTestHelpers.forPredicate(pEven, true, false), Math.min(2, uniqueSize)), new MapperData<>(LambdaTestHelpers.forPredicate(pOdd, true, false), Math.min(2, uniqueSize)) ); }
private ResultAsserter<Iterable<Integer>> sliceResultAsserter(Iterable<Integer> data, int expectedSize) { return (act, exp, ord, par) -> { if (par & !ord) { List<Integer> expected = new ArrayList<>(); data.forEach(expected::add); List<Integer> actual = new ArrayList<>(); act.forEach(actual::add); assertEquals(actual.size(), expectedSize); assertTrue(expected.containsAll(actual)); } else { LambdaTestHelpers.assertContents(act, exp); } }; }
@Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class) public void testTwoLevelPartitioningBy(String name, TestData.OfRef<Integer> data) throws ReflectiveOperationException { Predicate<Integer> classifier = i -> i % 3 == 0; Predicate<Integer> classifier2 = i -> i % 7 == 0; // Two level partition exerciseMapCollection(data, partitioningBy(classifier, partitioningBy(classifier2)), new PartitioningByAssertion<>(classifier, new PartitioningByAssertion(classifier2, new ToListAssertion<>()))); // Two level partition with reduce exerciseMapCollection(data, partitioningBy(classifier, reducing(0, Integer::sum)), new PartitioningByAssertion<>(classifier, new ReducingAssertion<>(0, LambdaTestHelpers.identity(), Integer::sum))); }
public void findAllFileTest() { // derive expected result by using conventional loop Pattern pat = Pattern.compile("[A-Z]{7,}"); List<String> expected = new ArrayList<>(); try (Scanner sc = makeFileScanner(inputFile)) { String match; while ((match = sc.findWithinHorizon(pat, 0)) != null) { expected.add(match); } } Supplier<Stream<String>> ss = () -> makeFileScanner(inputFile).findAll(pat).map(MatchResult::group); withData(TestData.Factory.ofSupplier("findAllFileTest", ss)) .stream(LambdaTestHelpers.identity()) .expectedResult(expected) .exercise(); }
@Test(dataProvider = "FindAllZero") public void findAllZeroTest(String input, String patternString) { Pattern pattern = Pattern.compile(patternString); // generate expected result using Matcher.find() Matcher m = pattern.matcher(input); List<String> expected = new ArrayList<>(); while (m.find()) { expected.add(m.group()); } Supplier<Stream<String>> ss = () -> new Scanner(input).findAll(pattern) .limit(100) .map(MatchResult::group); withData(TestData.Factory.ofSupplier("findAllZeroTest", ss)) .stream(LambdaTestHelpers.identity()) .expectedResult(expected) .exercise(); }
public void findAllTest() { // derive expected result by using conventional loop Pattern pat = Pattern.compile("[A-Z]{7,}"); List<String> expected = new ArrayList<>(); try (Scanner sc = makeFileScanner(inputFile)) { String match; while ((match = sc.findWithinHorizon(pat, 0)) != null) { expected.add(match); } } Supplier<Stream<String>> ss = () -> makeFileScanner(inputFile).findAll(pat).map(MatchResult::group); withData(TestData.Factory.ofSupplier("findAllTest", ss)) .stream(LambdaTestHelpers.identity()) .expectedResult(expected) .exercise(); }
@SuppressWarnings({"rawtypes", "unchecked"}) @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class) public void testMixedSeqPar(String name, TestData.OfRef<Integer> data) { Function<Integer, Integer> id = LambdaTestHelpers.identity(); UnaryOperator<Stream<Integer>>[] changers = new UnaryOperator[] { (UnaryOperator<Stream<Integer>>) s -> s, (UnaryOperator<Stream<Integer>>) s -> s.sequential(), (UnaryOperator<Stream<Integer>>) s -> s.parallel(), (UnaryOperator<Stream<Integer>>) s -> s.unordered() }; UnaryOperator<Stream<Integer>>[] stuff = new UnaryOperator[] { (UnaryOperator<Stream<Integer>>) s -> s, (UnaryOperator<Stream<Integer>>) s -> s.map(id), (UnaryOperator<Stream<Integer>>) s -> s.sorted(Comparator.naturalOrder()), (UnaryOperator<Stream<Integer>>) s -> s.map(id).sorted(Comparator.naturalOrder()).map(id), (UnaryOperator<Stream<Integer>>) s -> s.filter(LambdaTestHelpers.pEven).sorted(Comparator.naturalOrder()).map(id), }; for (int c1Index = 0; c1Index < changers.length; c1Index++) { setContext("c1Index", c1Index); UnaryOperator<Stream<Integer>> c1 = changers[c1Index]; for (int s1Index = 0; s1Index < stuff.length; s1Index++) { setContext("s1Index", s1Index); UnaryOperator<Stream<Integer>> s1 = stuff[s1Index]; for (int c2Index = 0; c2Index < changers.length; c2Index++) { setContext("c2Index", c2Index); UnaryOperator<Stream<Integer>> c2 = changers[c2Index]; for (int s2Index = 0; s2Index < stuff.length; s2Index++) { setContext("s2Index", s2Index); UnaryOperator<Stream<Integer>> s2 = stuff[s2Index]; UnaryOperator<Stream<Integer>> composed = s -> s2.apply(c2.apply(s1.apply(c1.apply(s)))); exerciseOps(data, composed); } } } } }
private <T> ResultAsserter<T> mapTabulationAsserter(boolean ordered) { return (act, exp, ord, par) -> { if (par && (!ordered || !ord)) { TabulatorsTest.nestedMapEqualityAssertion(act, exp); } else { LambdaTestHelpers.assertContentsEqual(act, exp); } }; }
@Test public void testSingleton() { TestData.OfRef<Integer> data = TestData.Factory.ofSupplier("[0, 1)", () -> Stream.of(1)); withData(data). stream(s -> s). expectedResult(Collections.singletonList(1)). exercise(); withData(data). stream(s -> s.map(LambdaTestHelpers.identity())). expectedResult(Collections.singletonList(1)). exercise(); }
private void testStreamBuilder(int size, Function<Integer, Stream<Integer>> supplier) { TestData.OfRef<Integer> data = TestData.Factory.ofSupplier(String.format("[0, %d)", size), () -> supplier.apply(size)); withData(data). stream(s -> s). expectedResult(IntStream.range(0, size).boxed().collect(toList())). exercise(); withData(data). stream(s -> s.map(LambdaTestHelpers.identity())). expectedResult(IntStream.range(0, size).boxed().collect(toList())). exercise(); }
@Test(dataProvider = "Stream<String>") public void testStrings(String description, String input, Pattern pattern, List<String> expected) { Supplier<Stream<String>> ss = () -> pattern.splitAsStream(input); withData(TestData.Factory.ofSupplier(description, ss)) .stream(LambdaTestHelpers.identity()) .expectedResult(expected) .exercise(); }