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.OpTestCase;
26 import org.openjdk.testlib.java.util.stream.StreamTestDataProvider;
27 import org.openjdk.testlib.java.util.stream.TestData;
28 
29 import org.testng.annotations.Test;
30 
31 import java.util.Spliterator;
32 import java.util.stream.BaseStream;
33 import java.util.stream.Stream;
34 import java.util.stream.IntStream;
35 import java.util.stream.LongStream;
36 import java.util.stream.DoubleStream;
37 
38 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.*;
39 import static org.testng.Assert.assertEquals;
40 
41 /**
42  * @test
43  * @bug 8021863
44  */
45 public class ConcatOpTest extends OpTestCase {
46 
47     // Sanity to make sure all type of stream source works
48     @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
testOps(String name, TestData.OfRef<Integer> data)49     public void testOps(String name, TestData.OfRef<Integer> data) {
50         exerciseOpsInt(data,
51                        s -> Stream.concat(s, data.stream()),
52                        s -> IntStream.concat(s, data.stream().mapToInt(Integer::intValue)),
53                        s -> LongStream.concat(s, data.stream().mapToLong(Integer::longValue)),
54                        s -> DoubleStream.concat(s, data.stream().mapToDouble(Integer::doubleValue)));
55     }
56 
testSize()57     public void testSize() {
58         assertSized(Stream.concat(
59                 LongStream.range(0, Long.MAX_VALUE / 2).boxed(),
60                 LongStream.range(0, Long.MAX_VALUE / 2).boxed()));
61 
62         assertUnsized(Stream.concat(
63                 LongStream.range(0, Long.MAX_VALUE).boxed(),
64                 LongStream.range(0, Long.MAX_VALUE).boxed()));
65 
66         assertUnsized(Stream.concat(
67                 LongStream.range(0, Long.MAX_VALUE).boxed(),
68                 Stream.iterate(0, i -> i + 1)));
69 
70         assertUnsized(Stream.concat(
71                 Stream.iterate(0, i -> i + 1),
72                 LongStream.range(0, Long.MAX_VALUE).boxed()));
73     }
74 
testLongSize()75     public void testLongSize() {
76         assertSized(LongStream.concat(
77                 LongStream.range(0, Long.MAX_VALUE / 2),
78                 LongStream.range(0, Long.MAX_VALUE / 2)));
79 
80         assertUnsized(LongStream.concat(
81                 LongStream.range(0, Long.MAX_VALUE),
82                 LongStream.range(0, Long.MAX_VALUE)));
83 
84         assertUnsized(LongStream.concat(
85                 LongStream.range(0, Long.MAX_VALUE),
86                 LongStream.iterate(0, i -> i + 1)));
87 
88         assertUnsized(LongStream.concat(
89                 LongStream.iterate(0, i -> i + 1),
90                 LongStream.range(0, Long.MAX_VALUE)));
91     }
92 
testIntSize()93     public void testIntSize() {
94         assertSized(IntStream.concat(
95                 IntStream.range(0, Integer.MAX_VALUE),
96                 IntStream.range(0, Integer.MAX_VALUE)));
97 
98         assertUnsized(IntStream.concat(
99                 LongStream.range(0, Long.MAX_VALUE).mapToInt(i -> (int) i),
100                 LongStream.range(0, Long.MAX_VALUE).mapToInt(i -> (int) i)));
101 
102         assertUnsized(IntStream.concat(
103                 LongStream.range(0, Long.MAX_VALUE).mapToInt(i -> (int) i),
104                 IntStream.iterate(0, i -> i + 1)));
105 
106         assertUnsized(IntStream.concat(
107                 IntStream.iterate(0, i -> i + 1),
108                 LongStream.range(0, Long.MAX_VALUE).mapToInt(i -> (int) i)));
109     }
110 
testDoubleSize()111     public void testDoubleSize() {
112         assertSized(DoubleStream.concat(
113                 IntStream.range(0, Integer.MAX_VALUE).mapToDouble(i -> i),
114                 IntStream.range(0, Integer.MAX_VALUE).mapToDouble(i -> i)));
115 
116         assertUnsized(DoubleStream.concat(
117                 LongStream.range(0, Long.MAX_VALUE).mapToDouble(i -> i),
118                 LongStream.range(0, Long.MAX_VALUE).mapToDouble(i -> i)));
119 
120         assertUnsized(DoubleStream.concat(
121                 LongStream.range(0, Long.MAX_VALUE).mapToDouble(i -> i),
122                 DoubleStream.iterate(0, i -> i + 1)));
123 
124         assertUnsized(DoubleStream.concat(
125                 DoubleStream.iterate(0, i -> i + 1),
126                 LongStream.range(0, Long.MAX_VALUE).mapToDouble(i -> i)));
127     }
128 
assertUnsized(BaseStream<?, ?> s)129     void assertUnsized(BaseStream<?, ?> s) {
130         Spliterator<?> sp = s.spliterator();
131 
132         assertFalse(sp.hasCharacteristics(Spliterator.SIZED | Spliterator.SUBSIZED));
133         assertEquals(sp.estimateSize(), Long.MAX_VALUE);
134     }
135 
assertSized(BaseStream<?, ?> s)136     void assertSized(BaseStream<?, ?> s) {
137         Spliterator<?> sp = s.spliterator();
138 
139         assertTrue(sp.hasCharacteristics(Spliterator.SIZED | Spliterator.SUBSIZED));
140         assertTrue(sp.estimateSize() < Long.MAX_VALUE);
141     }
142 }
143