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 org.testng.annotations.Test;
34 
35 import java.util.Spliterator;
36 import java.util.stream.BaseStream;
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 static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.*;
43 
44 import android.platform.test.annotations.LargeTest;
45 
46 /**
47  * MapOpTest
48  *
49  * @author Brian Goetz
50  */
51 @Test
52 public class MapOpTest extends OpTestCase {
53 
testMap()54     public void testMap() {
55         assertCountSum(countTo(0).stream().map(mId), 0, 0);
56         assertCountSum(countTo(10).stream().map(mId), 10, 55);
57         assertCountSum(countTo(10).stream().map(mZero), 10, 0);
58         assertCountSum(countTo(0).stream().map(mDoubler), 0, 0);
59         assertCountSum(countTo(10).stream().map(mDoubler), 10, 110);
60         assertCountSum(countTo(10).stream().map(mDoubler).map(mDoubler), 10, 220);
61 
62         exerciseOps(countTo(0), s -> s.map(LambdaTestHelpers.identity()), countTo(0));
63         exerciseOps(countTo(1000), s -> s.map(LambdaTestHelpers.identity()), countTo(1000));
64         // @@@ Force cast to integer so output is Stream<Integer> rather an IntStream
65         //     this just ensures that no warnings are logged about boxing
66         //     when the result is compared with the output
67         exerciseOps(countTo(1000), s -> s.map(e -> (Integer) (1000 + e)), range(1001, 2000));
68     }
69 
testEveryMapShape()70     public void testEveryMapShape() {
71         assertCountSum(countTo(1000).stream()
72                                .mapToInt(i -> i - 1)
73                                .mapToObj(i -> i + 1)
74                                .mapToLong(i -> i - 1)
75                                .mapToObj(i -> i + 1)
76                                .mapToDouble(i -> i - 1)
77                                .mapToObj(i -> i + 1)
78                                .mapToInt(i -> (int) (double) i)
79                                .mapToLong(i -> i)
80                                .mapToDouble(i -> i)
81                                .mapToLong(i -> (long) i)
82                                .mapToInt(i -> (int) i)
83                                .mapToObj(i -> i),
84                        1000, countTo(1000).stream().mapToInt(i -> i).sum());
85     }
86 
87     @LargeTest
88     @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
testOps(String name, TestData.OfRef<Integer> data)89     public void testOps(String name, TestData.OfRef<Integer> data) {
90         exerciseOpsInt(data, s -> s.map(mId), s -> s.map(e -> e), s -> s.map(e -> e), s -> s.map(e -> e));
91         exerciseOpsInt(data, s -> s.map(mZero), s -> s.map(e -> 0), s -> s.map(e -> 0), s -> s.map(e -> 0));
92         exerciseOpsInt(data, s -> s.map(mDoubler), s -> s.map(e -> 2*e), s -> s.map(e -> 2*e), s -> s.map(e -> 2*e));
93         exerciseOpsInt(data, s -> s.map(LambdaTestHelpers.compose(mId, mDoubler)), s -> s.map(e -> 2*e), s -> s.map(e -> 2*e), s -> s.map(e -> 2*e));
94         exerciseOpsInt(data, s -> s.map(LambdaTestHelpers.compose(mDoubler, mDoubler)), s -> s.map(e -> 4*e), s -> s.map(e -> 4*e), s -> s.map(e -> 4*e));
95         exerciseOps(data, s -> s.mapToInt(i -> i));
96         exerciseOps(data, s -> s.mapToLong(i -> i));
97         exerciseOps(data, s -> s.mapToDouble(i -> i));
98     }
99 
100     //
101 
102     @LargeTest
103     @Test(dataProvider = "IntStreamTestData", dataProviderClass = IntStreamTestDataProvider.class)
testIntOps(String name, TestData.OfInt data)104     public void testIntOps(String name, TestData.OfInt data) {
105         exerciseOps(data, s -> s.mapToObj(i -> i));
106         exerciseOps(data, s -> s.map(i -> 0));
107         exerciseOps(data, s -> s.map(i -> i * 2));
108         exerciseOps(data, s -> s.asLongStream());
109         exerciseOps(data, s -> s.asDoubleStream());
110         exerciseOps(data, s -> s.boxed());
111         exerciseOps(data, s -> s.mapToObj(Integer::toString));
112         exerciseOps(data, s -> s.mapToLong(i -> i));
113         exerciseOps(data, s -> s.mapToDouble(i -> i));
114     }
115 
116     //
117 
118     @LargeTest
119     @Test(dataProvider = "LongStreamTestData", dataProviderClass = LongStreamTestDataProvider.class)
testLongOps(String name, TestData.OfLong data)120     public void testLongOps(String name, TestData.OfLong data) {
121         exerciseOps(data, s -> s.mapToObj(i -> i));
122         exerciseOps(data, s -> s.map(i -> 0L));
123         exerciseOps(data, s -> s.map(i -> i * 2L));
124         exerciseOps(data, s -> s.asDoubleStream());
125         exerciseOps(data, s -> s.boxed());
126         exerciseOps(data, s -> s.mapToObj(e -> Long.toString(e)));
127         exerciseOps(data, s -> s.mapToInt(i -> (int) i));
128         exerciseOps(data, s -> s.mapToDouble(i -> i));
129     }
130 
131     //
132 
133     @LargeTest
134     @Test(dataProvider = "DoubleStreamTestData", dataProviderClass = DoubleStreamTestDataProvider.class)
testDoubleOps(String name, TestData.OfDouble data)135     public void testDoubleOps(String name, TestData.OfDouble data) {
136         exerciseOps(data, s -> s.mapToObj(i -> i));
137         exerciseOps(data, s -> s.map(i -> 0.0));
138         exerciseOps(data, s -> s.map(i -> i * 2.0));
139         exerciseOps(data, s -> s.boxed());
140         exerciseOps(data, s -> s.mapToObj(e -> Double.toString(e)));
141         exerciseOps(data, s -> s.mapToLong(i -> (long) i));
142         exerciseOps(data, s -> s.mapToInt(i -> (int) i));
143     }
144 }
145