java.util.stream.TestData Java Examples

The following examples show how to use java.util.stream.TestData. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TabulatorsTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@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)));
}
 
Example #2
Source File: TabulatorsTest.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
@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)));
}
 
Example #3
Source File: TabulatorsTest.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private<T, M extends Map>
void exerciseMapTabulation(TestData<T, Stream<T>> data,
                           Collector<T, ?, ? extends M> collector,
                           TabulationAssertion<T, M> assertion)
        throws ReflectiveOperationException {
    boolean ordered = !collector.characteristics().contains(Collector.Characteristics.UNORDERED);

    M m = withData(data)
            .terminal(s -> s.collect(collector))
            .resultAsserter(mapTabulationAsserter(ordered))
            .exercise();
    assertion.assertValue(m, () -> data.stream(), ordered);

    m = withData(data)
            .terminal(s -> s.unordered().collect(collector))
            .resultAsserter(mapTabulationAsserter(ordered))
            .exercise();
    assertion.assertValue(m, () -> data.stream(), false);
}
 
Example #4
Source File: SliceOpTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class,
      groups = { "serialization-hostile" })
public void testSkipOps(String name, TestData.OfRef<Integer> data) {
    List<Integer> skips = sizes(data.size());

    for (int s : skips) {
        setContext("skip", s);
        testSliceMulti(data,
                       sliceSize(data.size(), s),
                       st -> st.skip(s),
                       st -> st.skip(s),
                       st -> st.skip(s),
                       st -> st.skip(s));

        testSliceMulti(data,
                       sliceSize(sliceSize(data.size(), s), s/2),
                       st -> st.skip(s).skip(s / 2),
                       st -> st.skip(s).skip(s / 2),
                       st -> st.skip(s).skip(s / 2),
                       st -> st.skip(s).skip(s / 2));
    }
}
 
Example #5
Source File: SliceOpTest.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class,
      groups = { "serialization-hostile" })
public void testSkipLimitOps(String name, TestData.OfRef<Integer> data) {
    List<Integer> skips = sizes(data.size());
    List<Integer> limits = skips;

    for (int s : skips) {
        setContext("skip", s);
        for (int l : limits) {
            setContext("limit", l);
            testSliceMulti(data,
                           sliceSize(sliceSize(data.size(), s), 0, l),
                           st -> st.skip(s).limit(l),
                           st -> st.skip(s).limit(l),
                           st -> st.skip(s).limit(l),
                           st -> st.skip(s).limit(l));
        }
    }
}
 
Example #6
Source File: SliceOpTest.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class,
      groups = { "serialization-hostile" })
public void testSkipOps(String name, TestData.OfRef<Integer> data) {
    List<Integer> skips = sizes(data.size());

    for (int s : skips) {
        setContext("skip", s);
        testSliceMulti(data,
                       sliceSize(data.size(), s),
                       st -> st.skip(s),
                       st -> st.skip(s),
                       st -> st.skip(s),
                       st -> st.skip(s));

        testSliceMulti(data,
                       sliceSize(sliceSize(data.size(), s), s/2),
                       st -> st.skip(s).skip(s / 2),
                       st -> st.skip(s).skip(s / 2),
                       st -> st.skip(s).skip(s / 2),
                       st -> st.skip(s).skip(s / 2));
    }
}
 
Example #7
Source File: GroupByOpTest.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
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))
    );
}
 
Example #8
Source File: SliceOpTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class,
      groups = { "serialization-hostile" })
public void testSkipLimitOps(String name, TestData.OfRef<Integer> data) {
    List<Integer> skips = sizes(data.size());
    List<Integer> limits = skips;

    for (int s : skips) {
        setContext("skip", s);
        for (int l : limits) {
            setContext("limit", l);
            testSliceMulti(data,
                           sliceSize(sliceSize(data.size(), s), 0, l),
                           st -> st.skip(s).limit(l),
                           st -> st.skip(s).limit(l),
                           st -> st.skip(s).limit(l),
                           st -> st.skip(s).limit(l));
        }
    }
}
 
Example #9
Source File: SplittableRandomTest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "ints")
public void testInts(TestData.OfInt data, ResultAsserter<Iterable<Integer>> ra) {
    withData(data).
            stream(s -> s).
            without(IntStreamTestScenario.CLEAR_SIZED_SCENARIOS).
            resultAsserter(ra).
            exercise();
}
 
Example #10
Source File: DistinctOpTest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "withNull:StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
public void testOpWithNullSorted(String name, TestData.OfRef<Integer> data) {
    List<Integer> l = new ArrayList<>();
    data.into(l).sort(cNullInteger);
    // Need to inject SORTED into the sorted list source since
    // sorted() with a comparator ironically clears SORTED
    Collection<Integer> node = exerciseOps(new SortedTestData<>(l), Stream::distinct);
    assertUnique(node);
    assertSorted(node, cNullInteger);
}
 
Example #11
Source File: IntReduceTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "IntStreamTestData", dataProviderClass = IntStreamTestDataProvider.class)
public void testOps(String name, TestData.OfInt data) {
    assertEquals(0, (int) exerciseTerminalOps(data, s -> s.filter(ipFalse), s -> s.reduce(0, irPlus)));

    OptionalInt seedless = exerciseTerminalOps(data, s -> s.reduce(irPlus));
    int folded = exerciseTerminalOps(data, s -> s.reduce(0, irPlus));
    assertEquals(folded, seedless.orElse(0));

    seedless = exerciseTerminalOps(data, s -> s.reduce(irMin));
    folded = exerciseTerminalOps(data, s -> s.reduce(Integer.MAX_VALUE, irMin));
    assertEquals(folded, seedless.orElse(Integer.MAX_VALUE));

    seedless = exerciseTerminalOps(data, s -> s.reduce(irMax));
    folded = exerciseTerminalOps(data, s -> s.reduce(Integer.MIN_VALUE, irMax));
    assertEquals(folded, seedless.orElse(Integer.MIN_VALUE));

    seedless = exerciseTerminalOps(data, s -> s.map(irDoubler), s -> s.reduce(irPlus));
    folded = exerciseTerminalOps(data, s -> s.map(irDoubler), s -> s.reduce(0, irPlus));
    assertEquals(folded, seedless.orElse(0));

    seedless = exerciseTerminalOps(data, s -> s.map(irDoubler), s -> s.reduce(irMin));
    folded = exerciseTerminalOps(data, s -> s.map(irDoubler), s -> s.reduce(Integer.MAX_VALUE, irMin));
    assertEquals(folded, seedless.orElse(Integer.MAX_VALUE));

    seedless = exerciseTerminalOps(data, s -> s.map(irDoubler), s -> s.reduce(irMax));
    folded = exerciseTerminalOps(data, s -> s.map(irDoubler), s -> s.reduce(Integer.MIN_VALUE, irMax));
    assertEquals(folded, seedless.orElse(Integer.MIN_VALUE));
}
 
Example #12
Source File: DistinctOpTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
public void testSortedDistinct(String name, TestData.OfRef<Integer> data) {
    Collection<Integer> result = withData(data)
            .stream(s -> s.sorted().distinct(),
                    new CollectorOps.TestParallelSizedOp<>())
            .exercise();
    assertUnique(result);
    assertSorted(result);
}
 
Example #13
Source File: DistinctOpTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
public void testDistinctSorted(String name, TestData.OfRef<Integer> data) {
    Collection<Integer> result = withData(data)
            .stream(s -> s.distinct().sorted(),
                    new CollectorOps.TestParallelSizedOp<>())
            .exercise();
    assertUnique(result);
    assertSorted(result);
}
 
Example #14
Source File: SliceOpTest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@SafeVarargs
private final void testSliceMulti(TestData.OfRef<Integer> data,
                                  int expectedSize,
                                  Function<Stream<Integer>, Stream<Integer>>... ms) {
    for (int i = 0; i < ms.length; i++) {
        setContext("mIndex", i);
        Function<Stream<Integer>, Stream<Integer>> m = ms[i];
        Collection<Integer> sr = withData(data)
                .stream(m)
                .resultAsserter(sliceResultAsserter(data, expectedSize))
                .exercise();
        assertEquals(sr.size(), expectedSize);
    }
}
 
Example #15
Source File: InfiniteStreamWithLimitOpTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "IntStream.limit")
public void testIntUnorderedGenerator(String description, UnaryOperator<IntStream> fs) {
    // Source is spliterator of infinite size
    TestData.OfInt generator = TestData.Factory.ofIntSupplier(
            "[1, 1, ...]", () -> IntStream.generate(() -> 1));

    withData(generator).
            stream(s -> fs.apply(s.filter(i -> true).unordered())).
            exercise();
}
 
Example #16
Source File: SplittableRandomTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "doubles")
public void testDoubles(TestData.OfDouble data, ResultAsserter<Iterable<Double>> ra) {
    withData(data).
            stream(s -> s).
            without(DoubleStreamTestScenario.CLEAR_SIZED_SCENARIOS).
            resultAsserter(ra).
            exercise();
}
 
Example #17
Source File: SliceOpTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private void testSliceMulti(TestData.OfRef<Integer> data,
                            int expectedSize,
                            Function<Stream<Integer>, Stream<Integer>> mRef,
                            Function<IntStream, IntStream> mInt,
                            Function<LongStream, LongStream> mLong,
                            Function<DoubleStream, DoubleStream> mDouble) {

    @SuppressWarnings({ "rawtypes", "unchecked" })
    Function<Stream<Integer>, Stream<Integer>>[] ms = new Function[4];
    ms[0] = mRef;
    ms[1] = s -> mInt.apply(s.mapToInt(e -> e)).mapToObj(e -> e);
    ms[2] = s -> mLong.apply(s.mapToLong(e -> e)).mapToObj(e -> (int) e);
    ms[3] = s -> mDouble.apply(s.mapToDouble(e -> e)).mapToObj(e -> (int) e);
    testSliceMulti(data, expectedSize, ms);
}
 
Example #18
Source File: ReduceTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
public void testOps(String name, TestData.OfRef<Integer> data) {
    assertEquals(0, (int) exerciseTerminalOps(data, s -> s.filter(pFalse), s -> s.reduce(0, rPlus, rPlus)));

    Optional<Integer> seedless = exerciseTerminalOps(data, s -> s.reduce(rPlus));
    Integer folded = exerciseTerminalOps(data, s -> s.reduce(0, rPlus, rPlus));
    assertEquals(folded, seedless.orElse(0));

    seedless = exerciseTerminalOps(data, s -> s.reduce(rMin));
    folded = exerciseTerminalOps(data, s -> s.reduce(Integer.MAX_VALUE, rMin, rMin));
    assertEquals(folded, seedless.orElse(Integer.MAX_VALUE));

    seedless = exerciseTerminalOps(data, s -> s.reduce(rMax));
    folded = exerciseTerminalOps(data, s -> s.reduce(Integer.MIN_VALUE, rMax, rMax));
    assertEquals(folded, seedless.orElse(Integer.MIN_VALUE));

    seedless = exerciseTerminalOps(data, s -> s.map(mDoubler), s -> s.reduce(rPlus));
    folded = exerciseTerminalOps(data, s -> s.map(mDoubler), s -> s.reduce(0, rPlus, rPlus));
    assertEquals(folded, seedless.orElse(0));

    seedless = exerciseTerminalOps(data, s -> s.map(mDoubler), s -> s.reduce(rMin));
    folded = exerciseTerminalOps(data, s -> s.map(mDoubler), s -> s.reduce(Integer.MAX_VALUE, rMin, rMin));
    assertEquals(folded, seedless.orElse(Integer.MAX_VALUE));

    seedless = exerciseTerminalOps(data, s -> s.map(mDoubler), s -> s.reduce(rMax));
    folded = exerciseTerminalOps(data, s -> s.map(mDoubler), s -> s.reduce(Integer.MIN_VALUE, rMax, rMax));
    assertEquals(folded, seedless.orElse(Integer.MIN_VALUE));
}
 
Example #19
Source File: InfiniteStreamWithLimitOpTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "IntStream.limit")
public void testIntUnorderedIteration(String description, UnaryOperator<IntStream> fs) {
    // Source is a right-balanced tree of infinite size
    TestData.OfInt iterator = TestData.Factory.ofIntSupplier(
            "[1, 2, 3, ...]", () -> IntStream.iterate(1, i -> i + 1));

    // Ref
    withData(iterator).
            stream(s -> fs.apply(s.unordered())).
            resultAsserter(unorderedAsserter()).
            exercise();
}
 
Example #20
Source File: TabulatorsTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private<T> String join(TestData.OfRef<T> data, String delim) {
    StringBuilder sb = new StringBuilder();
    boolean first = true;
    for (T i : data) {
        if (!first)
            sb.append(delim);
        sb.append(i.toString());
        first = false;
    }
    return sb.toString();
}
 
Example #21
Source File: DistinctOpTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "withNull:StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
public void testOpWithNullSorted(String name, TestData.OfRef<Integer> data) {
    List<Integer> l = new ArrayList<>();
    data.into(l).sort(cNullInteger);
    // Need to inject SORTED into the sorted list source since
    // sorted() with a comparator ironically clears SORTED
    Collection<Integer> node = exerciseOps(new SortedTestData<>(l), Stream::distinct);
    assertUnique(node);
    assertSorted(node, cNullInteger);
}
 
Example #22
Source File: DistinctOpTest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
public void testDistinctDistinct(String name, TestData.OfRef<Integer> data) {
    Collection<Integer> result = exerciseOpsInt(
            data,
            s -> s.distinct().distinct(),
            s -> s.distinct().distinct(),
            s -> s.distinct().distinct(),
            s -> s.distinct().distinct());

     assertUnique(result);
}
 
Example #23
Source File: CountTest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
public void testOps(String name, TestData.OfRef<Integer> data) {
    AtomicLong expectedCount = new AtomicLong();
    data.stream().forEach(e -> expectedCount.incrementAndGet());

    withData(data).
            terminal(Stream::count).
            expectedResult(expectedCount.get()).
            exercise();
}
 
Example #24
Source File: RangeTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public void testInfiniteRangeFindFirst() {
    Integer first = Stream.iterate(0, i -> i + 1).filter(i -> i > 10000).findFirst().get();
    assertEquals(first, Stream.iterate(0, i -> i + 1).parallel().filter(i -> i > 10000).findFirst().get());

    // Limit is required to transform the infinite stream to a finite stream
    // since the exercising requires a finite stream
    withData(TestData.Factory.ofSupplier(
            "", () -> Stream.iterate(0, i -> i + 1).filter(i -> i > 10000).limit(20000))).
            terminal(s->s.findFirst()).expectedResult(Optional.of(10001)).exercise();
}
 
Example #25
Source File: SplittableRandomTest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "doubles")
public void testDoubles(TestData.OfDouble data, ResultAsserter<Iterable<Double>> ra) {
    withData(data).
            stream(s -> s).
            without(DoubleStreamTestScenario.CLEAR_SIZED_SCENARIOS).
            resultAsserter(ra).
            exercise();
}
 
Example #26
Source File: InfiniteStreamWithLimitOpTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "LongStream.limit")
public void testLongUnorderedGenerator(String description, UnaryOperator<LongStream> fs) {
    // Source is spliterator of infinite size
    TestData.OfLong generator = TestData.Factory.ofLongSupplier(
            "[1L, 1L, ...]", () -> LongStream.generate(() -> 1));

    withData(generator).
            stream(s -> fs.apply(s.filter(i -> true).unordered())).
            exercise();
}
 
Example #27
Source File: MatchOpTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
public void testStream(String name, TestData.OfRef<Integer> data) {
    for (Predicate<Integer> p : INTEGER_PREDICATES) {
        setContext("p", p);
        for (Kind kind : Kind.values()) {
            setContext("kind", kind);
            exerciseTerminalOps(data, this.<Integer>kinds().get(kind).apply(p));
            exerciseTerminalOps(data, s -> s.filter(pFalse), this.<Integer>kinds().get(kind).apply(p));
            exerciseTerminalOps(data, s -> s.filter(pEven), this.<Integer>kinds().get(kind).apply(p));
        }
    }
}
 
Example #28
Source File: StreamBuilderTest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testLongSingleton() {
    TestData.OfLong data = TestData.Factory.ofLongSupplier("[0, 1)",
                                                           () -> LongStream.of(1));

    withData(data).
            stream(s -> s).
            expectedResult(Collections.singletonList(1L)).
            exercise();

    withData(data).
            stream(s -> s.map(i -> i)).
            expectedResult(Collections.singletonList(1L)).
            exercise();
}
 
Example #29
Source File: StreamBuilderTest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private void testIntStreamBuilder(int size, Function<Integer, IntStream> supplier) {
    TestData.OfInt data = TestData.Factory.ofIntSupplier(String.format("[0, %d)", size),
                                                         () -> supplier.apply(size));

    withData(data).
            stream(s -> s).
            expectedResult(IntStream.range(0, size).toArray()).
            exercise();

    withData(data).
            stream(s -> s.map(i -> i)).
            expectedResult(IntStream.range(0, size).toArray()).
            exercise();
}
 
Example #30
Source File: StreamBuilderTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
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();
}