下面列出了怎么用java.util.stream.TestData的API类实例代码及写法,或者点击链接到github查看源代码。
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);
}
@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));
}
}
@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))
);
}
@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));
}
}
}
@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));
}
}
@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));
}
}
}
@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)));
}
@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));
}
}
}
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);
}
@Test(dataProvider = "LongStreamTestData", dataProviderClass = LongStreamTestDataProvider.class)
public void testLongStream(String name, TestData.OfLong data) {
for (LongPredicate p : LONG_PREDICATES) {
setContext("p", p);
for (Kind kind : Kind.values()) {
setContext("kind", kind);
exerciseTerminalOps(data, longKinds.get(kind).apply(p));
exerciseTerminalOps(data, s -> s.filter(lpFalse), longKinds.get(kind).apply(p));
exerciseTerminalOps(data, s -> s.filter(lpEven), longKinds.get(kind).apply(p));
}
}
}
@Test(dataProvider = "DoubleStreamTestData", dataProviderClass = DoubleStreamTestDataProvider.class)
public void testDoubleStream(String name, TestData.OfDouble data) {
for (DoublePredicate p : DOUBLE_PREDICATES) {
setContext("p", p);
for (Kind kind : Kind.values()) {
setContext("kind", kind);
exerciseTerminalOps(data, doubleKinds.get(kind).apply(p));
exerciseTerminalOps(data, s -> s.filter(dpFalse), doubleKinds.get(kind).apply(p));
exerciseTerminalOps(data, s -> s.filter(dpEven), doubleKinds.get(kind).apply(p));
}
}
}
@SuppressWarnings({"rawtypes", "unchecked"})
@Test(dataProvider = "StreamTestData<Integer>.mini", 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 void testDoubleStreamBuilder(int size, Function<Integer, DoubleStream> supplier) {
TestData.OfDouble data = TestData.Factory.ofDoubleSupplier(String.format("[0, %d)", size),
() -> supplier.apply(size));
withData(data).
stream(s -> s).
expectedResult(IntStream.range(0, size).asDoubleStream().toArray()).
exercise();
withData(data).
stream(s -> s.map(i -> i)).
expectedResult(IntStream.range(0, size).asDoubleStream().toArray()).
exercise();
}
@Test(dataProvider = "IntStreamTestData", dataProviderClass = IntStreamTestDataProvider.class)
public void testIntStream(String name, TestData.OfInt data) {
for (IntPredicate p : INT_PREDICATES) {
setContext("p", p);
for (Kind kind : Kind.values()) {
setContext("kind", kind);
exerciseTerminalOps(data, intKinds.get(kind).apply(p));
exerciseTerminalOps(data, s -> s.filter(ipFalse), intKinds.get(kind).apply(p));
exerciseTerminalOps(data, s -> s.filter(ipEven), intKinds.get(kind).apply(p));
}
}
}
@Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
public void testSimplePartition(String name, TestData.OfRef<Integer> data) throws ReflectiveOperationException {
Predicate<Integer> classifier = i -> i % 3 == 0;
// Single-level partition to downstream List
exerciseMapTabulation(data,
partitioningBy(classifier),
new PartitionAssertion<>(classifier, new ListAssertion<>()));
exerciseMapTabulation(data,
partitioningBy(classifier, toList()),
new PartitionAssertion<>(classifier, new ListAssertion<>()));
}
@Test(dataProvider = "Stream.limit")
public void testUnorderedIteration(String description, UnaryOperator<Stream<Long>> fs) {
// Source is a right-balanced tree of infinite size
TestData.OfRef<Long> iterator = TestData.Factory.ofSupplier(
"[1L, 2L, 3L, ...]", () -> Stream.iterate(1L, i -> i + 1L));
// Ref
withData(iterator).
stream(s -> fs.apply(s.unordered())).
resultAsserter(unorderedAsserter()).
exercise();
}
@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();
}
@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();
}
@Test(dataProvider = "DoubleStream.limit")
public void testDoubleUnorderedIteration(String description, UnaryOperator<DoubleStream> fs) {
// Source is a right-balanced tree of infinite size
TestData.OfDouble iterator = TestData.Factory.ofDoubleSupplier(
"[1.0, 2.0, 3.0, ...]", () -> DoubleStream.iterate(1, i -> i + 1));
// Ref
withData(iterator).
stream(s -> fs.apply(s.unordered())).
resultAsserter(unorderedAsserter()).
exercise();
}
@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();
}
@SuppressWarnings({"rawtypes", "unchecked"})
@Test(dataProvider = "StreamTestData<Integer>.mini", 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);
}
}
}
}
}
@Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
public void testComposeFinisher(String name, TestData.OfRef<Integer> data) throws ReflectiveOperationException {
List<Integer> asList = exerciseTerminalOps(data, s -> s.collect(toList()));
List<Integer> asImmutableList = exerciseTerminalOps(data, s -> s.collect(collectingAndThen(toList(), Collections::unmodifiableList)));
assertEquals(asList, asImmutableList);
try {
asImmutableList.add(0);
fail("Expecting immutable result");
}
catch (UnsupportedOperationException ignored) { }
}
@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();
}
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();
}
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();
}
@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();
}
@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));
}
}
}
@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();
}