下面列出了java.util.stream.TestData#OfRef ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
public void testSimpleGroupBy(String name, TestData.OfRef<Integer> data) throws ReflectiveOperationException {
Function<Integer, Integer> classifier = i -> i % 3;
// Single-level groupBy
exerciseMapTabulation(data, groupingBy(classifier),
new GroupedMapAssertion<>(classifier, HashMap.class,
new ListAssertion<>()));
exerciseMapTabulation(data, groupingByConcurrent(classifier),
new GroupedMapAssertion<>(classifier, ConcurrentHashMap.class,
new ListAssertion<>()));
// With explicit constructors
exerciseMapTabulation(data,
groupingBy(classifier, TreeMap::new, toCollection(HashSet::new)),
new GroupedMapAssertion<>(classifier, TreeMap.class,
new CollectionAssertion<Integer>(HashSet.class, false)));
exerciseMapTabulation(data,
groupingByConcurrent(classifier, ConcurrentSkipListMap::new,
toCollection(HashSet::new)),
new GroupedMapAssertion<>(classifier, ConcurrentSkipListMap.class,
new CollectionAssertion<Integer>(HashSet.class, false)));
}
@Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
public void testSimpleGroupBy(String name, TestData.OfRef<Integer> data) throws ReflectiveOperationException {
Function<Integer, Integer> classifier = i -> i % 3;
// Single-level groupBy
exerciseMapTabulation(data, groupingBy(classifier),
new GroupedMapAssertion<>(classifier, HashMap.class,
new ListAssertion<>()));
exerciseMapTabulation(data, groupingByConcurrent(classifier),
new GroupedMapAssertion<>(classifier, ConcurrentHashMap.class,
new ListAssertion<>()));
// With explicit constructors
exerciseMapTabulation(data,
groupingBy(classifier, TreeMap::new, toCollection(HashSet::new)),
new GroupedMapAssertion<>(classifier, TreeMap.class,
new CollectionAssertion<Integer>(HashSet.class, false)));
exerciseMapTabulation(data,
groupingByConcurrent(classifier, ConcurrentSkipListMap::new,
toCollection(HashSet::new)),
new GroupedMapAssertion<>(classifier, ConcurrentSkipListMap.class,
new CollectionAssertion<Integer>(HashSet.class, false)));
}
@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));
}
}
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)
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));
}
}
}
@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);
}
}
}
}
}
@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
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();
}
@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);
}
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();
}
@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 = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
public void testOp(String name, TestData.OfRef<Integer> data) {
Collection<Integer> result = exerciseOpsInt(
data,
Stream::distinct,
IntStream::distinct,
LongStream::distinct,
DoubleStream::distinct);
assertUnique(result);
assertTrue((data.size() > 0) ? result.size() > 0 : result.size() == 0);
assertTrue(result.size() <= data.size());
}
@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);
}
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);
}
private TestData.OfRef<Long> refLongs() {
return refLongRange(0, Long.MAX_VALUE);
}
@Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
public void testSpliterators(String name, TestData.OfRef<Integer> data) {
for (Function<Stream<Integer>, Stream<Integer>> f : streamFunctions()) {
SpliteratorTestHelper.testSpliterator(() -> f.apply(data.stream()).spliterator());
}
}
private TestData.OfRef<Long> refLongs() {
return refLongRange(0, Long.MAX_VALUE);
}
private<T, R> void assertCollect(TestData.OfRef<T> data,
Collector<T, ?, R> collector,
Function<Stream<T>, R> streamReduction) {
R check = streamReduction.apply(data.stream());
withData(data).terminal(s -> s.collect(collector)).expectedResult(check).exercise();
}
@Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
public void testJoin(String name, TestData.OfRef<Integer> data) throws ReflectiveOperationException {
withData(data)
.terminal(s -> s.map(Object::toString).collect(Collectors.joining()))
.expectedResult(join(data, ""))
.exercise();
Collector<String, StringBuilder, String> likeJoining = Collector.of(StringBuilder::new, StringBuilder::append, (sb1, sb2) -> sb1.append(sb2.toString()), StringBuilder::toString);
withData(data)
.terminal(s -> s.map(Object::toString).collect(likeJoining))
.expectedResult(join(data, ""))
.exercise();
withData(data)
.terminal(s -> s.map(Object::toString).collect(Collectors.joining(",")))
.expectedResult(join(data, ","))
.exercise();
withData(data)
.terminal(s -> s.map(Object::toString).collect(Collectors.joining(",", "[", "]")))
.expectedResult("[" + join(data, ",") + "]")
.exercise();
withData(data)
.terminal(s -> s.map(Object::toString)
.collect(StringBuilder::new, StringBuilder::append, StringBuilder::append)
.toString())
.expectedResult(join(data, ""))
.exercise();
withData(data)
.terminal(s -> s.map(Object::toString)
.collect(() -> new StringJoiner(","),
(sj, cs) -> sj.add(cs),
(j1, j2) -> j1.merge(j2))
.toString())
.expectedResult(join(data, ","))
.exercise();
withData(data)
.terminal(s -> s.map(Object::toString)
.collect(() -> new StringJoiner(",", "[", "]"),
(sj, cs) -> sj.add(cs),
(j1, j2) -> j1.merge(j2))
.toString())
.expectedResult("[" + join(data, ",") + "]")
.exercise();
}