/* * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /** * @test * @bug 8072727 */ package org.openjdk.tests.java.util.stream; import java.util.Objects; import java.util.function.Supplier; import java.util.stream.BaseStream; import java.util.stream.DoubleStream; import java.util.stream.IntStream; import java.util.stream.LongStream; import java.util.stream.OpTestCase; import java.util.stream.SpliteratorTestHelper; import java.util.stream.Stream; import static java.util.stream.ThowableHelper.checkNPE; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; @Test public class IterateTest extends OpTestCase { private static Object[] row(String name, Supplier> supplier) { return new Object[] {name, supplier}; } @DataProvider(name = "IterateStreamsData.OfRef") public static Object[][] makeIterateStreamsTestData() { Object[][] data = { row("ref.empty", () -> Stream.iterate(1, x -> x < 0, x -> x * 2)), row("ref.one", () -> Stream.iterate(1, x -> x < 2, x -> x * 2)), row("ref.ten", () -> Stream.iterate(1, x -> x < 1000, x -> x * 2)), row("ref.nullCheck", () -> Stream.iterate(10, Objects::nonNull, x -> x > 0 ? x - 1 : null)) }; return data; } @DataProvider(name = "IterateStreamsData.OfInt") public static Object[][] makeIterateIntStreamsTestData() { Object[][] data = { row("int.empty", () -> IntStream.iterate(1, x -> x < 0, x -> x + 1)), row("int.one", () -> IntStream.iterate(1, x -> x < 2, x -> x + 1)), row("int.ten", () -> IntStream.iterate(1, x -> x <= 10, x -> x + 1)), }; return data; } @DataProvider(name = "IterateStreamsData.OfLong") public static Object[][] makeIterateLongStreamsTestData() { Object[][] data = { row("long.empty", () -> LongStream.iterate(1L, x -> x < 0, x -> x + 1)), row("long.one", () -> LongStream.iterate(1L, x -> x < 2, x -> x + 1)), row("long.ten", () -> LongStream.iterate(1L, x -> x <= 10, x -> x + 1)), }; return data; } @DataProvider(name = "IterateStreamsData.OfDouble") public static Object[][] makeIterateDoubleStreamsTestData() { Object[][] data = { row("double.empty", () -> DoubleStream.iterate(1.0, x -> x < 0, x -> x + 1)), row("double.one", () -> DoubleStream.iterate(1.0, x -> x < 2, x -> x + 1)), row("double.ten", () -> DoubleStream.iterate(1.0, x -> x <= 10, x -> x + 1)), }; return data; } @Test(dataProvider = "IterateStreamsData.OfRef") public void testRefIterate(String name, Supplier> data) { SpliteratorTestHelper.testSpliterator(() -> data.get().spliterator()); } @Test(dataProvider = "IterateStreamsData.OfInt") public void testIntIterate(String name, Supplier data) { SpliteratorTestHelper.testIntSpliterator(() -> data.get().spliterator()); } @Test(dataProvider = "IterateStreamsData.OfLong") public void testLongIterate(String name, Supplier data) { SpliteratorTestHelper.testLongSpliterator(() -> data.get().spliterator()); } @Test(dataProvider = "IterateStreamsData.OfDouble") public void testDoubleIterate(String name, Supplier data) { SpliteratorTestHelper.testDoubleSpliterator(() -> data.get().spliterator()); } @Test public void testNPE() { checkNPE(() -> Stream.iterate("", null, x -> x + "a")); checkNPE(() -> Stream.iterate("", String::isEmpty, null)); checkNPE(() -> IntStream.iterate(0, null, x -> x + 1)); checkNPE(() -> IntStream.iterate(0, x -> x < 10, null)); checkNPE(() -> LongStream.iterate(0, null, x -> x + 1)); checkNPE(() -> LongStream.iterate(0, x -> x < 10, null)); checkNPE(() -> DoubleStream.iterate(0, null, x -> x + 1)); checkNPE(() -> DoubleStream.iterate(0, x -> x < 10, null)); } }