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.Arrays;
36 import java.util.Collection;
37 import java.util.Collections;
38 import java.util.function.Function;
39 import java.util.stream.BaseStream;
40 import java.util.stream.Stream;
41 import java.util.stream.IntStream;
42 import java.util.stream.LongStream;
43 import java.util.stream.DoubleStream;
44 
45 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.*;
46 
47 /**
48  * ExplodeOpTest
49  *
50  * @author Brian Goetz
51  */
52 @Test
53 public class ExplodeOpTest extends OpTestCase {
54 
55     static final Function<Integer, Stream<Integer>> integerRangeMapper
56             = e -> IntStream.range(0, e).boxed();
57 
testFlatMap()58     public void testFlatMap() {
59         String[] stringsArray = {"hello", "there", "", "yada"};
60         Stream<String> strings = Arrays.asList(stringsArray).stream();
61         assertConcat(strings.flatMap(flattenChars).iterator(), "hellothereyada");
62 
63         assertCountSum(countTo(10).stream().flatMap(mfId), 10, 55);
64         assertCountSum(countTo(10).stream().flatMap(mfNull), 0, 0);
65         assertCountSum(countTo(3).stream().flatMap(mfLt), 6, 4);
66 
67         exerciseOps(TestData.Factory.ofArray("stringsArray", stringsArray), s -> s.flatMap(flattenChars));
68         exerciseOps(TestData.Factory.ofArray("LONG_STRING", new String[] {LONG_STRING}), s -> s.flatMap(flattenChars));
69     }
70 
71     @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
testOps(String name, TestData.OfRef<Integer> data)72     public void testOps(String name, TestData.OfRef<Integer> data) {
73         Collection<Integer> result = exerciseOps(data, s -> s.flatMap(mfId));
74         assertEquals(data.size(), result.size());
75 
76         result = exerciseOps(data, s -> s.flatMap(mfNull));
77         assertEquals(0, result.size());
78 
79         result = exerciseOps(data, s-> s.flatMap(e -> Stream.empty()));
80         assertEquals(0, result.size());
81 
82         exerciseOps(data, s -> s.flatMap(mfLt));
83         exerciseOps(data, s -> s.flatMap(integerRangeMapper));
84         exerciseOps(data, s -> s.flatMap((Integer e) -> IntStream.range(0, e).boxed().limit(10)));
85     }
86 
87     //
88 
89     @Test(dataProvider = "IntStreamTestData", dataProviderClass = IntStreamTestDataProvider.class)
testIntOps(String name, TestData.OfInt data)90     public void testIntOps(String name, TestData.OfInt data) {
91         Collection<Integer> result = exerciseOps(data, s -> s.flatMap(i -> Collections.singleton(i).stream().mapToInt(j -> j)));
92         assertEquals(data.size(), result.size());
93         assertContents(data, result);
94 
95         result = exerciseOps(data, s -> s.flatMap(i -> IntStream.empty()));
96         assertEquals(0, result.size());
97 
98         exerciseOps(data, s -> s.flatMap(e -> IntStream.range(0, e)));
99         exerciseOps(data, s -> s.flatMap(e -> IntStream.range(0, e).limit(10)));
100     }
101 
102     //
103 
104     @Test(dataProvider = "LongStreamTestData", dataProviderClass = LongStreamTestDataProvider.class)
testLongOps(String name, TestData.OfLong data)105     public void testLongOps(String name, TestData.OfLong data) {
106         Collection<Long> result = exerciseOps(data, s -> s.flatMap(i -> Collections.singleton(i).stream().mapToLong(j -> j)));
107         assertEquals(data.size(), result.size());
108         assertContents(data, result);
109 
110         result = exerciseOps(data, s -> LongStream.empty());
111         assertEquals(0, result.size());
112 
113         exerciseOps(data, s -> s.flatMap(e -> LongStream.range(0, e)));
114         exerciseOps(data, s -> s.flatMap(e -> LongStream.range(0, e).limit(10)));
115     }
116 
117     //
118 
119     @Test(dataProvider = "DoubleStreamTestData", dataProviderClass = DoubleStreamTestDataProvider.class)
testDoubleOps(String name, TestData.OfDouble data)120     public void testDoubleOps(String name, TestData.OfDouble data) {
121         Collection<Double> result = exerciseOps(data, s -> s.flatMap(i -> Collections.singleton(i).stream().mapToDouble(j -> j)));
122         assertEquals(data.size(), result.size());
123         assertContents(data, result);
124 
125         result = exerciseOps(data, s -> DoubleStream.empty());
126         assertEquals(0, result.size());
127 
128         exerciseOps(data, s -> s.flatMap(e -> IntStream.range(0, (int) e).asDoubleStream()));
129         exerciseOps(data, s -> s.flatMap(e -> IntStream.range(0, (int) e).limit(10).asDoubleStream()));
130     }
131 }
132