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