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 android.platform.test.annotations.LargeTest;
26 
27 import org.openjdk.testlib.java.util.stream.DoubleStreamTestDataProvider;
28 import org.openjdk.testlib.java.util.stream.IntStreamTestDataProvider;
29 import org.openjdk.testlib.java.util.stream.LambdaTestHelpers;
30 import org.openjdk.testlib.java.util.stream.LongStreamTestDataProvider;
31 import org.openjdk.testlib.java.util.stream.OpTestCase;
32 import org.openjdk.testlib.java.util.stream.StreamTestDataProvider;
33 import org.openjdk.testlib.java.util.stream.TestData;
34 
35 import java.util.stream.BaseStream;
36 import java.util.stream.Collectors;
37 import java.util.stream.Stream;
38 import java.util.stream.IntStream;
39 import java.util.stream.LongStream;
40 import java.util.stream.DoubleStream;
41 
42 import org.testng.annotations.Test;
43 
44 import java.util.Arrays;
45 import java.util.List;
46 import java.util.function.Function;
47 
48 @LargeTest
49 @Test
50 public class StreamLinkTest extends OpTestCase {
51 
apply(int n, Function<S, S> f)52     private <S> Function<S, S> apply(int n, Function<S, S> f) {
53         return s -> {
54             for (int i = 0; i < n; i++) {
55                 s = f.apply(s);
56             }
57             return s;
58         };
59     }
60 
61     private List<Integer> sizes = Arrays.asList(0, 1, 2, 3, 4, 5, 255, 1000);
62 
63     @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
64     public void testManyStreams(String name, TestData.OfRef<Integer> data) {
65         for (int n : sizes) {
66             setContext("n", n);
67             List<Integer> expected = data.stream().map(e -> (Integer) (e + n)).collect(Collectors.toList());
68 
69             withData(data).
70                     stream(apply(n, (Stream<Integer> s) -> s.map(e -> (Integer) (e + 1)))).
71                     expectedResult(expected).
72                     exercise();
73         }
74     }
75 
76     @Test(dataProvider = "IntStreamTestData", dataProviderClass = IntStreamTestDataProvider.class)
testIntManyStreams(String name, TestData.OfInt data)77     public void testIntManyStreams(String name, TestData.OfInt data) {
78         for (int n : sizes) {
79             setContext("n", n);
80             int[] expected = data.stream().map(e -> e + n).toArray();
81 
82             withData(data).
83                     stream(apply(n, (IntStream s) -> s.map(e -> e + 1))).
84                     expectedResult(expected).
85                     exercise();
86         }
87     }
88 
89     @Test(dataProvider = "LongStreamTestData", dataProviderClass = LongStreamTestDataProvider.class)
testLongManyStreams(String name, TestData.OfLong data)90     public void testLongManyStreams(String name, TestData.OfLong data) {
91         for (int n : sizes) {
92             setContext("n", n);
93             long[] expected = data.stream().map(e -> e + n).toArray();
94 
95             withData(data).
96                     stream(apply(n, (LongStream s) -> s.map(e -> e + 1L))).
97                     expectedResult(expected).
98                     exercise();
99         }
100     }
101 
102     @Test(dataProvider = "DoubleStreamTestData", dataProviderClass = DoubleStreamTestDataProvider.class)
testDoubleManyStreams(String name, TestData.OfDouble data)103     public void testDoubleManyStreams(String name, TestData.OfDouble data) {
104         for (int n : sizes) {
105             setContext("n", n);
106             double[] expected = data.stream().map(e -> accumulate(e, n)).toArray();
107 
108             withData(data).
109                     stream(apply(n, (DoubleStream s) -> s.map(e -> e + 1.0))).
110                     expectedResult(expected).
111                     exercise();
112         }
113     }
accumulate(double e, int n)114     private double accumulate(double e, int n) {
115         while (n-- > 0) {
116             e = e + 1.0;
117         }
118         return e;
119     }
120 }
121