1 /* 2 * Copyright (c) 2016, 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.DoubleStream; 38 import java.util.stream.IntStream; 39 import java.util.stream.LongStream; 40 import java.util.stream.Stream; 41 42 import static org.openjdk.testlib.java.util.stream.ThrowableHelper.checkNPE; 43 44 import org.testng.annotations.DataProvider; 45 import org.testng.annotations.Test; 46 47 @Test 48 public class IterateTest extends OpTestCase { 49 50 @DataProvider(name = "IterateStreamsData") makeIterateStreamsTestData()51 public static Object[][] makeIterateStreamsTestData() { 52 Object[][] data = { 53 {List.of(), 54 Factory.ofSupplier("ref.empty", () -> Stream.iterate(1, x -> x < 0, x -> x * 2))}, 55 {List.of(1), 56 Factory.ofSupplier("ref.one", () -> Stream.iterate(1, x -> x < 2, x -> x * 2))}, 57 {List.of(1, 2, 4, 8, 16, 32, 64, 128, 256, 512), 58 Factory.ofSupplier("ref.ten", () -> Stream.iterate(1, x -> x < 1000, x -> x * 2))}, 59 {List.of(10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0), 60 Factory.ofSupplier("ref.nullCheck", () -> Stream.iterate(10, Objects::nonNull, x -> x > 0 ? x - 1 : null))}, 61 {List.of(), 62 Factory.ofIntSupplier("int.empty", () -> IntStream.iterate(1, x -> x < 0, x -> x + 1))}, 63 {List.of(1), 64 Factory.ofIntSupplier("int.one", () -> IntStream.iterate(1, x -> x < 2, x -> x + 1))}, 65 {List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), 66 Factory.ofIntSupplier("int.ten", () -> IntStream.iterate(1, x -> x <= 10, x -> x + 1))}, 67 {List.of(5, 4, 3, 2, 1), 68 Factory.ofIntSupplier("int.divZero", () -> IntStream.iterate(5, x -> x != 0, x -> x - 1/x/2 - 1))}, 69 {List.of(), 70 Factory.ofLongSupplier("long.empty", () -> LongStream.iterate(1L, x -> x < 0, x -> x + 1))}, 71 {List.of(1L), 72 Factory.ofLongSupplier("long.one", () -> LongStream.iterate(1L, x -> x < 2, x -> x + 1))}, 73 {List.of(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L), 74 Factory.ofLongSupplier("long.ten", () -> LongStream.iterate(1L, x -> x <= 10, x -> x + 1))}, 75 {List.of(), 76 Factory.ofDoubleSupplier("double.empty", () -> DoubleStream.iterate(1.0, x -> x < 0, x -> x + 1))}, 77 {List.of(1.0), 78 Factory.ofDoubleSupplier("double.one", () -> DoubleStream.iterate(1.0, x -> x < 2, x -> x + 1))}, 79 {List.of(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0), 80 Factory.ofDoubleSupplier("double.ten", () -> DoubleStream.iterate(1.0, x -> x <= 10, x -> x + 1))} 81 }; 82 return data; 83 } 84 85 @Test(dataProvider = "IterateStreamsData") testIterate(List<T> expected, TestData<T, ?> data)86 public <T> void testIterate(List<T> expected, TestData<T, ?> data) { 87 withData(data).stream(s -> s).expectedResult(expected).exercise(); 88 } 89 90 @Test testNPE()91 public void testNPE() { 92 checkNPE(() -> Stream.iterate("", null, x -> x + "a")); 93 checkNPE(() -> Stream.iterate("", String::isEmpty, null)); 94 checkNPE(() -> IntStream.iterate(0, null, x -> x + 1)); 95 checkNPE(() -> IntStream.iterate(0, x -> x < 10, null)); 96 checkNPE(() -> LongStream.iterate(0, null, x -> x + 1)); 97 checkNPE(() -> LongStream.iterate(0, x -> x < 10, null)); 98 checkNPE(() -> DoubleStream.iterate(0, null, x -> x + 1)); 99 checkNPE(() -> DoubleStream.iterate(0, x -> x < 10, null)); 100 } 101 } 102