• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 package org.openjdk.tests.java.util.stream;
24 
25 import org.openjdk.testlib.java.util.stream.DoubleStreamTestDataProvider;
26 import org.openjdk.testlib.java.util.stream.IntStreamTestDataProvider;
27 import org.openjdk.testlib.java.util.stream.LambdaTestHelpers;
28 import org.openjdk.testlib.java.util.stream.LongStreamTestDataProvider;
29 import org.openjdk.testlib.java.util.stream.OpTestCase;
30 import org.openjdk.testlib.java.util.stream.StreamTestDataProvider;
31 import org.openjdk.testlib.java.util.stream.TestData;
32 
33 import java.util.stream.BaseStream;
34 import java.util.stream.Collectors;
35 import java.util.stream.Stream;
36 import java.util.stream.IntStream;
37 import java.util.stream.LongStream;
38 import java.util.stream.DoubleStream;
39 
40 import org.testng.annotations.Test;
41 
42 import java.util.Arrays;
43 import java.util.List;
44 import java.util.function.Function;
45 
46 @Test
47 public class StreamLinkTest extends OpTestCase {
48 
apply(int n, Function<S, S> f)49     private <S> Function<S, S> apply(int n, Function<S, S> f) {
50         return s -> {
51             for (int i = 0; i < n; i++) {
52                 s = f.apply(s);
53             }
54             return s;
55         };
56     }
57 
58     private List<Integer> sizes = Arrays.asList(0, 1, 2, 3, 4, 5, 255, 1000);
59 
60     @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
61     public void testManyStreams(String name, TestData.OfRef<Integer> data) {
62         for (int n : sizes) {
63             setContext("n", n);
64             List<Integer> expected = data.stream().map(e -> (Integer) (e + n)).collect(Collectors.toList());
65 
66             withData(data).
67                     stream(apply(n, (Stream<Integer> s) -> s.map(e -> (Integer) (e + 1)))).
68                     expectedResult(expected).
69                     exercise();
70         }
71     }
72 
73     @Test(dataProvider = "IntStreamTestData", dataProviderClass = IntStreamTestDataProvider.class)
testIntManyStreams(String name, TestData.OfInt data)74     public void testIntManyStreams(String name, TestData.OfInt data) {
75         for (int n : sizes) {
76             setContext("n", n);
77             int[] expected = data.stream().map(e -> e + n).toArray();
78 
79             withData(data).
80                     stream(apply(n, (IntStream s) -> s.map(e -> e + 1))).
81                     expectedResult(expected).
82                     exercise();
83         }
84     }
85 
86     @Test(dataProvider = "LongStreamTestData", dataProviderClass = LongStreamTestDataProvider.class)
testLongManyStreams(String name, TestData.OfLong data)87     public void testLongManyStreams(String name, TestData.OfLong data) {
88         for (int n : sizes) {
89             setContext("n", n);
90             long[] expected = data.stream().map(e -> e + n).toArray();
91 
92             withData(data).
93                     stream(apply(n, (LongStream s) -> s.map(e -> e + 1L))).
94                     expectedResult(expected).
95                     exercise();
96         }
97     }
98 
99     @Test(dataProvider = "DoubleStreamTestData", dataProviderClass = DoubleStreamTestDataProvider.class)
testDoubleManyStreams(String name, TestData.OfDouble data)100     public void testDoubleManyStreams(String name, TestData.OfDouble data) {
101         for (int n : sizes) {
102             setContext("n", n);
103             double[] expected = data.stream().map(e -> accumulate(e, n)).toArray();
104 
105             withData(data).
106                     stream(apply(n, (DoubleStream s) -> s.map(e -> e + 1.0))).
107                     expectedResult(expected).
108                     exercise();
109         }
110     }
accumulate(double e, int n)111     private double accumulate(double e, int n) {
112         while (n-- > 0) {
113             e = e + 1.0;
114         }
115         return e;
116     }
117 }
118