1 /* 2 * Copyright (c) 2016, 2022, 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 24 /** 25 * @test 26 * @bug 8072727 27 */ 28 29 package org.openjdk.tests.java.util.stream; 30 31 import org.openjdk.testlib.java.util.stream.OpTestCase; 32 import org.openjdk.testlib.java.util.stream.TestData; 33 import org.openjdk.testlib.java.util.stream.TestData.Factory; 34 35 import java.util.List; 36 import java.util.Objects; 37 import java.util.stream.Collector; 38 import java.util.stream.DoubleStream; 39 import java.util.stream.IntStream; 40 import java.util.stream.LongStream; 41 import java.util.stream.Stream; 42 43 import static org.openjdk.testlib.java.util.stream.ThrowableHelper.checkNPE; 44 45 import org.testng.annotations.DataProvider; 46 import org.testng.annotations.Test; 47 48 @Test 49 public class IterateTest extends OpTestCase { 50 51 @DataProvider(name = "IterateStreamsData") makeIterateStreamsTestData()52 public static Object[][] makeIterateStreamsTestData() { 53 Object[][] data = { 54 {List.of(), 55 Factory.ofSupplier("ref.empty", () -> Stream.iterate(1, x -> x < 0, x -> x * 2))}, 56 {List.of(1), 57 Factory.ofSupplier("ref.one", () -> Stream.iterate(1, x -> x < 2, x -> x * 2))}, 58 {List.of(1, 2, 4, 8, 16, 32, 64, 128, 256, 512), 59 Factory.ofSupplier("ref.ten", () -> Stream.iterate(1, x -> x < 1000, x -> x * 2))}, 60 {List.of(10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0), 61 Factory.ofSupplier("ref.nullCheck", () -> Stream.iterate(10, Objects::nonNull, x -> x > 0 ? x - 1 : null))}, 62 {List.of(), 63 Factory.ofIntSupplier("int.empty", () -> IntStream.iterate(1, x -> x < 0, x -> x + 1))}, 64 {List.of(1), 65 Factory.ofIntSupplier("int.one", () -> IntStream.iterate(1, x -> x < 2, x -> x + 1))}, 66 {List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), 67 Factory.ofIntSupplier("int.ten", () -> IntStream.iterate(1, x -> x <= 10, x -> x + 1))}, 68 {List.of(5, 4, 3, 2, 1), 69 Factory.ofIntSupplier("int.divZero", () -> IntStream.iterate(5, x -> x != 0, x -> x - 1/x/2 - 1))}, 70 {List.of(), 71 Factory.ofLongSupplier("long.empty", () -> LongStream.iterate(1L, x -> x < 0, x -> x + 1))}, 72 {List.of(1L), 73 Factory.ofLongSupplier("long.one", () -> LongStream.iterate(1L, x -> x < 2, x -> x + 1))}, 74 {List.of(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L), 75 Factory.ofLongSupplier("long.ten", () -> LongStream.iterate(1L, x -> x <= 10, x -> x + 1))}, 76 {List.of(), 77 Factory.ofDoubleSupplier("double.empty", () -> DoubleStream.iterate(1.0, x -> x < 0, x -> x + 1))}, 78 {List.of(1.0), 79 Factory.ofDoubleSupplier("double.one", () -> DoubleStream.iterate(1.0, x -> x < 2, x -> x + 1))}, 80 {List.of(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0), 81 Factory.ofDoubleSupplier("double.ten", () -> DoubleStream.iterate(1.0, x -> x <= 10, x -> x + 1))} 82 }; 83 return data; 84 } 85 86 @Test(dataProvider = "IterateStreamsData") testIterate(List<T> expected, TestData<T, ?> data)87 public <T> void testIterate(List<T> expected, TestData<T, ?> data) { 88 withData(data).stream(s -> s).expectedResult(expected).exercise(); 89 } 90 91 @Test testNPE()92 public void testNPE() { 93 checkNPE(() -> Stream.iterate("", null, x -> x + "a")); 94 checkNPE(() -> Stream.iterate("", String::isEmpty, null)); 95 checkNPE(() -> IntStream.iterate(0, null, x -> x + 1)); 96 checkNPE(() -> IntStream.iterate(0, x -> x < 10, null)); 97 checkNPE(() -> LongStream.iterate(0, null, x -> x + 1)); 98 checkNPE(() -> LongStream.iterate(0, x -> x < 10, null)); 99 checkNPE(() -> DoubleStream.iterate(0, null, x -> x + 1)); 100 checkNPE(() -> DoubleStream.iterate(0, x -> x < 10, null)); 101 } 102 103 @Test testParallelize()104 public void testParallelize() { 105 checkHasSplit(Stream.iterate(0, x -> x < 10, x -> x + 1)); 106 checkHasSplit(IntStream.iterate(0, x -> x < 10, x -> x + 1).boxed()); 107 checkHasSplit(LongStream.iterate(0, x -> x < 10, x -> x + 1).boxed()); 108 checkHasSplit(DoubleStream.iterate(0, x -> x < 10, x -> x + 1).boxed()); 109 } 110 checkHasSplit(Stream<?> stream)111 private void checkHasSplit(Stream<?> stream) { 112 int[] numberOfNonEmptyParts = stream.parallel().collect( 113 Collector.of(() -> new int[1], (acc, e) -> acc[0] = 1, (acc1, acc2) -> { 114 acc1[0] += acc2[0]; 115 return acc1; 116 })); 117 assertTrue(numberOfNonEmptyParts[0] >= 2); 118 } 119 } 120