1 /* 2 * Copyright (c) 2013, 2014, 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.testng.annotations.DataProvider; 26 import org.testng.annotations.Test; 27 28 import java.util.Arrays; 29 import java.util.Collections; 30 import java.util.List; 31 import java.util.function.Function; 32 import java.util.stream.DoubleStream; 33 import java.util.stream.IntStream; 34 import java.util.stream.LambdaTestHelpers; 35 import java.util.stream.LongStream; 36 import java.util.stream.OpTestCase; 37 import java.util.stream.Stream; 38 import java.util.stream.TestData; 39 40 import static java.util.stream.Collectors.toList; 41 import static java.util.stream.ThowableHelper.checkISE; 42 43 @Test 44 public class StreamBuilderTest extends OpTestCase { 45 46 List<Integer> sizes = Arrays.asList(0, 1, 4, 16, 256, 47 1023, 1024, 1025, 48 2047, 2048, 2049, 49 1024 * 32 - 1, 1024 * 32, 1024 * 32 + 1); 50 51 @DataProvider(name = "sizes") createStreamBuilders()52 public Object[][] createStreamBuilders() { 53 return sizes.stream().map(i -> new Object[] { i }).toArray(Object[][]::new); 54 } 55 56 57 @Test testSingleton()58 public void testSingleton() { 59 TestData.OfRef<Integer> data = TestData.Factory.ofSupplier("[0, 1)", 60 () -> Stream.of(1)); 61 62 withData(data). 63 stream(s -> s). 64 expectedResult(Collections.singletonList(1)). 65 exercise(); 66 67 withData(data). 68 stream(s -> s.map(LambdaTestHelpers.identity())). 69 expectedResult(Collections.singletonList(1)). 70 exercise(); 71 } 72 73 @Test(dataProvider = "sizes") testAfterBuilding(int size)74 public void testAfterBuilding(int size) { 75 Stream.Builder<Integer> sb = Stream.builder(); 76 IntStream.range(0, size).boxed().forEach(sb); 77 sb.build(); 78 79 checkISE(() -> sb.accept(1)); 80 checkISE(() -> sb.add(1)); 81 checkISE(() -> sb.build()); 82 } 83 84 @Test(dataProvider = "sizes", groups = { "serialization-hostile" }) testStreamBuilder(int size)85 public void testStreamBuilder(int size) { 86 testStreamBuilder(size, (s) -> { 87 Stream.Builder<Integer> sb = Stream.builder(); 88 IntStream.range(0, s).boxed().forEach(sb); 89 return sb.build(); 90 }); 91 92 testStreamBuilder(size, (s) -> { 93 Stream.Builder<Integer> sb = Stream.builder(); 94 IntStream.range(0, s).boxed().forEach(i -> { 95 Stream.Builder<Integer> _sb = sb.add(i); 96 assertTrue(sb == _sb); 97 }); 98 return sb.build(); 99 }); 100 } 101 testStreamBuilder(int size, Function<Integer, Stream<Integer>> supplier)102 private void testStreamBuilder(int size, Function<Integer, Stream<Integer>> supplier) { 103 TestData.OfRef<Integer> data = TestData.Factory.ofSupplier(String.format("[0, %d)", size), 104 () -> supplier.apply(size)); 105 106 withData(data). 107 stream(s -> s). 108 expectedResult(IntStream.range(0, size).boxed().collect(toList())). 109 exercise(); 110 111 withData(data). 112 stream(s -> s.map(LambdaTestHelpers.identity())). 113 expectedResult(IntStream.range(0, size).boxed().collect(toList())). 114 exercise(); 115 } 116 117 // 118 119 @Test testIntSingleton()120 public void testIntSingleton() { 121 TestData.OfInt data = TestData.Factory.ofIntSupplier("[0, 1)", 122 () -> IntStream.of(1)); 123 124 withData(data). 125 stream(s -> s). 126 expectedResult(Collections.singletonList(1)). 127 exercise(); 128 129 withData(data). 130 stream(s -> s.map(i -> i)). 131 expectedResult(Collections.singletonList(1)). 132 exercise(); 133 } 134 135 @Test(dataProvider = "sizes") testIntAfterBuilding(int size)136 public void testIntAfterBuilding(int size) { 137 IntStream.Builder sb = IntStream.builder(); 138 IntStream.range(0, size).forEach(sb); 139 sb.build(); 140 141 checkISE(() -> sb.accept(1)); 142 checkISE(() -> sb.add(1)); 143 checkISE(() -> sb.build()); 144 } 145 146 @Test(dataProvider = "sizes", groups = { "serialization-hostile" }) testIntStreamBuilder(int size)147 public void testIntStreamBuilder(int size) { 148 testIntStreamBuilder(size, (s) -> { 149 IntStream.Builder sb = IntStream.builder(); 150 IntStream.range(0, s).forEach(sb); 151 return sb.build(); 152 }); 153 154 testIntStreamBuilder(size, (s) -> { 155 IntStream.Builder sb = IntStream.builder(); 156 IntStream.range(0, s).forEach(i -> { 157 IntStream.Builder _sb = sb.add(i); 158 assertTrue(sb == _sb); 159 }); 160 return sb.build(); 161 }); 162 } 163 testIntStreamBuilder(int size, Function<Integer, IntStream> supplier)164 private void testIntStreamBuilder(int size, Function<Integer, IntStream> supplier) { 165 TestData.OfInt data = TestData.Factory.ofIntSupplier(String.format("[0, %d)", size), 166 () -> supplier.apply(size)); 167 168 withData(data). 169 stream(s -> s). 170 expectedResult(IntStream.range(0, size).toArray()). 171 exercise(); 172 173 withData(data). 174 stream(s -> s.map(i -> i)). 175 expectedResult(IntStream.range(0, size).toArray()). 176 exercise(); 177 } 178 179 // 180 181 @Test testLongSingleton()182 public void testLongSingleton() { 183 TestData.OfLong data = TestData.Factory.ofLongSupplier("[0, 1)", 184 () -> LongStream.of(1)); 185 186 withData(data). 187 stream(s -> s). 188 expectedResult(Collections.singletonList(1L)). 189 exercise(); 190 191 withData(data). 192 stream(s -> s.map(i -> i)). 193 expectedResult(Collections.singletonList(1L)). 194 exercise(); 195 } 196 197 @Test(dataProvider = "sizes") testLongAfterBuilding(int size)198 public void testLongAfterBuilding(int size) { 199 LongStream.Builder sb = LongStream.builder(); 200 LongStream.range(0, size).forEach(sb); 201 sb.build(); 202 203 checkISE(() -> sb.accept(1)); 204 checkISE(() -> sb.add(1)); 205 checkISE(() -> sb.build()); 206 } 207 208 @Test(dataProvider = "sizes", groups = { "serialization-hostile" }) testLongStreamBuilder(int size)209 public void testLongStreamBuilder(int size) { 210 testLongStreamBuilder(size, (s) -> { 211 LongStream.Builder sb = LongStream.builder(); 212 LongStream.range(0, s).forEach(sb); 213 return sb.build(); 214 }); 215 216 testLongStreamBuilder(size, (s) -> { 217 LongStream.Builder sb = LongStream.builder(); 218 LongStream.range(0, s).forEach(i -> { 219 LongStream.Builder _sb = sb.add(i); 220 assertTrue(sb == _sb); 221 }); 222 return sb.build(); 223 }); 224 } 225 testLongStreamBuilder(int size, Function<Integer, LongStream> supplier)226 private void testLongStreamBuilder(int size, Function<Integer, LongStream> supplier) { 227 TestData.OfLong data = TestData.Factory.ofLongSupplier(String.format("[0, %d)", size), 228 () -> supplier.apply(size)); 229 230 withData(data). 231 stream(s -> s). 232 expectedResult(LongStream.range(0, size).toArray()). 233 exercise(); 234 235 withData(data). 236 stream(s -> s.map(i -> i)). 237 expectedResult(LongStream.range(0, size).toArray()). 238 exercise(); 239 } 240 241 // 242 243 @Test testDoubleSingleton()244 public void testDoubleSingleton() { 245 TestData.OfDouble data = TestData.Factory.ofDoubleSupplier("[0, 1)", () -> DoubleStream.of(1)); 246 247 withData(data). 248 stream(s -> s). 249 expectedResult(Collections.singletonList(1.0)). 250 exercise(); 251 252 withData(data). 253 stream(s -> s.map(i -> i)). 254 expectedResult(Collections.singletonList(1.0)). 255 exercise(); 256 } 257 258 @Test(dataProvider = "sizes") testDoubleAfterBuilding(int size)259 public void testDoubleAfterBuilding(int size) { 260 DoubleStream.Builder sb = DoubleStream.builder(); 261 IntStream.range(0, size).asDoubleStream().forEach(sb); 262 sb.build(); 263 264 checkISE(() -> sb.accept(1)); 265 checkISE(() -> sb.add(1)); 266 checkISE(() -> sb.build()); 267 } 268 269 @Test(dataProvider = "sizes", groups = { "serialization-hostile" }) testDoubleStreamBuilder(int size)270 public void testDoubleStreamBuilder(int size) { 271 testDoubleStreamBuilder(size, (s) -> { 272 DoubleStream.Builder sb = DoubleStream.builder(); 273 IntStream.range(0, s).asDoubleStream().forEach(sb); 274 return sb.build(); 275 }); 276 277 testDoubleStreamBuilder(size, (s) -> { 278 DoubleStream.Builder sb = DoubleStream.builder(); 279 IntStream.range(0, s).asDoubleStream().forEach(i -> { 280 DoubleStream.Builder _sb = sb.add(i); 281 assertTrue(sb == _sb); 282 }); 283 return sb.build(); 284 }); 285 } 286 testDoubleStreamBuilder(int size, Function<Integer, DoubleStream> supplier)287 private void testDoubleStreamBuilder(int size, Function<Integer, DoubleStream> supplier) { 288 TestData.OfDouble data = TestData.Factory.ofDoubleSupplier(String.format("[0, %d)", size), 289 () -> supplier.apply(size)); 290 291 withData(data). 292 stream(s -> s). 293 expectedResult(IntStream.range(0, size).asDoubleStream().toArray()). 294 exercise(); 295 296 withData(data). 297 stream(s -> s.map(i -> i)). 298 expectedResult(IntStream.range(0, size).asDoubleStream().toArray()). 299 exercise(); 300 } 301 302 } 303