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